11.7Smartin$NetBSD: README,v 1.7 2025/05/01 07:40:42 martin Exp $ 21.1Sapb 31.1SapbNotes for NetBSD src/tools 41.1Sapb 51.1Sapb 61.1SapbBackground 71.1Sapb========== 81.1Sapb 91.1SapbSeveral programs that are part of NetBSD are also built as tools. Such 101.3Sapbprograms are typically built twice: once as a tool and once as part of 111.3Sapbthe release build. Tools are relevant only when the make(1) variable 121.3SapbUSETOOLS=yes, which is the default for most NetBSD builds. 131.1Sapb 141.1SapbTools are built on the host platform, using the host compiler, 151.1Sapband will run on the host platform during the cross-build of the 161.1Sapbremainder of NetBSD. They are built near the beginning of a NetBSD 171.1Sapbbuild (e.g. "build.sh tools" or "make tools" from the top level src 181.1Sapbdirectory), and installed in ${TOOLDIR}. 191.1Sapb 201.1SapbTools are executed during the main part of the build, when several 211.1SapbTOOL_* variables defined in src/share/mk/bsd.*.mk will refer to the 221.1Sapbtools installed in ${TOOLDIR}. 231.1Sapb 241.1Sapb 251.1SapbPortability 261.1Sapb=========== 271.1Sapb 281.1SapbPrograms that are built as tools need to be more portable than other 291.1Sapbparts of NetBSD, because they will need to run on the host platform. 301.3Sapb 311.4SapbMost tools should restrict themselves to C language features that are 321.5Srilligdefined in C99 (ISO/IEC 9899-1999); they should avoid using C11 language 331.5Srilligfeatures, such as <threads.h>, _Alignof, <uchar.h>, _Generic, 341.5Srilligstatic_assert, anonymous structures and unions. 351.5Srillig 361.5SrilligTools may use library features such as functions, macros, and types, 371.5Srilligthat are defined in C99 and in POSIX (IEEE Std 1003.1) (XXX year?), and 381.5Srilligfeatures that are provided by the compatibility framework 391.5Srillig(src/tools/compat) described in a separate section below. 401.3Sapb 411.3SapbIf a tool attempts to use a feature that is not available on the host 421.3Sapbplatform, then the tools build will fail. This can be addressed by 431.3Sapbchanging the tool to avoid that feature, or by adding the feature to the 441.3Sapbsrc/tools/compat framework. It is usually easy to add new macros or 451.3Sapbfunctions to src/tools/compat, and that is usually better than adding 461.3Sapbcompatibility definitions to individual tools. 471.2Sapb 481.1Sapb 491.1SapbCompatibility framework 501.1Sapb======================= 511.1Sapb 521.1Sapbsrc/tools/compat provides a compatibility framework for use by tools. 531.1SapbIt installs the following components, and more: 541.1Sapb 551.1Sapb${TOOLDIR}/lib/libnbcompat.a 561.1Sapb 571.1Sapb A library containing functions that are needed by some tools. 581.1Sapb 591.7Smartin${TOOLDIR}/include/compat/nbtool_config.h 601.1Sapb 611.1Sapb A header file defining macros that are needed by some tools. 621.1Sapb 631.1Sapb${TOOLDIR}/share/compat/defs.mk 641.1Sapb 651.1Sapb A makefile fragment, to be included by other makefiles, 661.1Sapb to define make variables appropriate for building tools. 671.1Sapb 681.1Sapb Among other things, this makefile fragment automatically adds 691.1Sapb the libnbcompat.a library to the LDADD and DPADD variables, 701.1Sapb so that tools will be linked with that library, and adds 711.1Sapb -I${NETBSDSRCDIR}/tools/compat and -DHAVE_NBTOOL_CONFIG_H=1 to the 721.1Sapb HOST_CPPFLAGS variable, so that compiled programs can detect when 731.1Sapb they are being built as tools. 741.1Sapb 751.1Sapb 761.1SapbAdapting Makefiles for use with tools 771.1Sapb===================================== 781.1Sapb 791.3SapbMakefiles under src/tools/*/Makefile should define the HOSTPROG 801.3Sapbvariable. This is typically done by tools/Makefile.hostprog, 811.1Sapbwhich is directly or indirectly included by all Makefiles in 821.1Sapbsrc/tools/*/Makefile. 831.1Sapb 841.3SapbMakefiles in the non-tools part of the src tree can test whether or not 851.3Sapbthe HOSTPROG variable is defined, in order tell the difference between 861.3Sapbbuilding a tool and building part of a NetBSD release, and they may 871.3Sapbalter their behavior accordingly. 881.3Sapb 891.1SapbFor example, the Makefile may conditionally refrain from compiling and 901.1Sapblinking certain files, and the Makefile may conditionally pass macros to 911.1Sapbthe compiler via constructs like this: 921.1Sapb 931.1Sapb .if defined(HOSTPROG) 941.3Sapb CPPFLAGS+= -DWITH_FEATURE_X=0 # exclude feature X from tools build 951.1Sapb .else 961.3Sapb CPPFLAGS+= -DWITH_FEATURE_X=1 # include feature X in release build 971.1Sapb .endif 981.1Sapb 991.1SapbAdapting Programs for use with tools 1001.1Sapb==================================== 1011.1Sapb 1021.3SapbWhen a tool is being built, the C compiler should automatically be 1031.3Sapbinvoked with -DHAVE_NBTOOL_CONFIG_H=1. This is done as a result of 1041.3Sapbsettings in ${TOOLDIR}/share/compat/defs.mk, which should be included 1051.3Sapbfrom src/tools/Makefile.host, which should be included directly or 1061.3Sapbindirectly from src/tools/*/Makefile. 1071.3Sapb 1081.3SapbA C source file can test whether the HAVE_NBTOOL_CONFIG_H macro is 1091.3Sapbdefined, in order to tell whether or not it is being compiled as part of 1101.3Sapba tool. 1111.3Sapb 1121.3SapbIn order to obtain the definitions provided by the tools compatibility 1131.3Sapbframework, almost every C source file that is built as part of a tool 1141.3Sapbshould have lines like these as the first non-comment lines: 1151.1Sapb 1161.1Sapb #if HAVE_NBTOOL_CONFIG_H 1171.1Sapb #include "nbtool_config.h" 1181.3Sapb #endif 1191.1Sapb 1201.3SapbTo omit features from the tools version of a program, the program 1211.3Sapbmay test the HAVE_NBTOOL_CONFIG_H macro, like this: 1221.3Sapb 1231.3Sapb #if HAVE_NBTOOL_CONFIG_H 1241.3Sapb ... code to be used when built as a tool 1251.3Sapb #else 1261.3Sapb ... code to be used when built as part of a release 1271.3Sapb #endif 1281.3Sapb 1291.3SapbIt is often preferable to use macros whose names refer to the features 1301.3Sapbthat should be included or omitted. See the section on "Adapting 1311.3SapbMakefiles for use with tools" for an example in which the Makefile 1321.3Sapbpasses -DWITH_FEATURE_X=0 or -DWITH_FEATURE_X=1 to the compiler 1331.3Sapbaccording to whether or not the program is being built as a tool. Then 1341.3Sapbthe program can use code like this: 1351.1Sapb 1361.1Sapb #if WITH_FEATURE_X 1371.3Sapb ... code to be used when FEATURE X is desired, 1381.3Sapb ... e.g. when being built as part of a release. 1391.3Sapb #else 1401.3Sapb ... code to be used when FEATURE X is not desired, 1411.3Sapb ... e.g. when being built as a tool. 1421.3Sapb #endif 143