README revision 1.4
11.4Sapb$NetBSD: README,v 1.4 2015/01/03 13:20:11 apb 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.4Sapbdefined in C89 (ISO 9899-1989); they should avoid using C99 language 331.4Sapbfeatures. There are a few tools, such as compilers, where it is not 341.4Sapbpractical for the C89 restriction to be maintained. There are also a 351.4Sapbfew features, such as the long long data type, that are used by many 361.4Sapbtools despite not being defined in C89. 371.4Sapb 381.4SapbTools may use library features such as functions, macros, and 391.4Sapbtypes, that are defined in C89 and in POSIX (IEEE Std 1003.1) (XXX 401.4Sapbyear?), and features that are provided by the compatibility framework 411.4Sapb(src/tools/compat) described in a separate section below. This is 421.4Sapbusually not an onerous burden, because many C99 library features, and 431.4SapbNetBSD-specific features, are already provided by src/tools/compat, or 441.4Sapbcan be added when the need for them becomes apparent. 451.3Sapb 461.3SapbIf a tool attempts to use a feature that is not available on the host 471.3Sapbplatform, then the tools build will fail. This can be addressed by 481.3Sapbchanging the tool to avoid that feature, or by adding the feature to the 491.3Sapbsrc/tools/compat framework. It is usually easy to add new macros or 501.3Sapbfunctions to src/tools/compat, and that is usually better than adding 511.3Sapbcompatibility definitions to individual tools. 521.2Sapb 531.1Sapb 541.1SapbCompatibility framework 551.1Sapb======================= 561.1Sapb 571.1Sapbsrc/tools/compat provides a compatibility framework for use by tools. 581.1SapbIt installs the following components, and more: 591.1Sapb 601.1Sapb${TOOLDIR}/lib/libnbcompat.a 611.1Sapb 621.1Sapb A library containing functions that are needed by some tools. 631.1Sapb 641.1Sapb${TOOLDIR}/include/nbtool_compat.h 651.1Sapb 661.1Sapb A header file defining macros that are needed by some tools. 671.1Sapb 681.1Sapb${TOOLDIR}/share/compat/defs.mk 691.1Sapb 701.1Sapb A makefile fragment, to be included by other makefiles, 711.1Sapb to define make variables appropriate for building tools. 721.1Sapb 731.1Sapb Among other things, this makefile fragment automatically adds 741.1Sapb the libnbcompat.a library to the LDADD and DPADD variables, 751.1Sapb so that tools will be linked with that library, and adds 761.1Sapb -I${NETBSDSRCDIR}/tools/compat and -DHAVE_NBTOOL_CONFIG_H=1 to the 771.1Sapb HOST_CPPFLAGS variable, so that compiled programs can detect when 781.1Sapb they are being built as tools. 791.1Sapb 801.1Sapb 811.1SapbAdapting Makefiles for use with tools 821.1Sapb===================================== 831.1Sapb 841.3SapbMakefiles under src/tools/*/Makefile should define the HOSTPROG 851.3Sapbvariable. This is typically done by tools/Makefile.hostprog, 861.1Sapbwhich is directly or indirectly included by all Makefiles in 871.1Sapbsrc/tools/*/Makefile. 881.1Sapb 891.3SapbMakefiles in the non-tools part of the src tree can test whether or not 901.3Sapbthe HOSTPROG variable is defined, in order tell the difference between 911.3Sapbbuilding a tool and building part of a NetBSD release, and they may 921.3Sapbalter their behavior accordingly. 931.3Sapb 941.1SapbFor example, the Makefile may conditionally refrain from compiling and 951.1Sapblinking certain files, and the Makefile may conditionally pass macros to 961.1Sapbthe compiler via constructs like this: 971.1Sapb 981.1Sapb .if defined(HOSTPROG) 991.3Sapb CPPFLAGS+= -DWITH_FEATURE_X=0 # exclude feature X from tools build 1001.1Sapb .else 1011.3Sapb CPPFLAGS+= -DWITH_FEATURE_X=1 # include feature X in release build 1021.1Sapb .endif 1031.1Sapb 1041.1SapbAdapting Programs for use with tools 1051.1Sapb==================================== 1061.1Sapb 1071.3SapbWhen a tool is being built, the C compiler should automatically be 1081.3Sapbinvoked with -DHAVE_NBTOOL_CONFIG_H=1. This is done as a result of 1091.3Sapbsettings in ${TOOLDIR}/share/compat/defs.mk, which should be included 1101.3Sapbfrom src/tools/Makefile.host, which should be included directly or 1111.3Sapbindirectly from src/tools/*/Makefile. 1121.3Sapb 1131.3SapbA C source file can test whether the HAVE_NBTOOL_CONFIG_H macro is 1141.3Sapbdefined, in order to tell whether or not it is being compiled as part of 1151.3Sapba tool. 1161.3Sapb 1171.3SapbIn order to obtain the definitions provided by the tools compatibility 1181.3Sapbframework, almost every C source file that is built as part of a tool 1191.3Sapbshould have lines like these as the first non-comment lines: 1201.1Sapb 1211.1Sapb #if HAVE_NBTOOL_CONFIG_H 1221.1Sapb #include "nbtool_config.h" 1231.3Sapb #endif 1241.1Sapb 1251.3SapbTo omit features from the tools version of a program, the program 1261.3Sapbmay test the HAVE_NBTOOL_CONFIG_H macro, like this: 1271.3Sapb 1281.3Sapb #if HAVE_NBTOOL_CONFIG_H 1291.3Sapb ... code to be used when built as a tool 1301.3Sapb #else 1311.3Sapb ... code to be used when built as part of a release 1321.3Sapb #endif 1331.3Sapb 1341.3SapbIt is often preferable to use macros whose names refer to the features 1351.3Sapbthat should be included or omitted. See the section on "Adapting 1361.3SapbMakefiles for use with tools" for an example in which the Makefile 1371.3Sapbpasses -DWITH_FEATURE_X=0 or -DWITH_FEATURE_X=1 to the compiler 1381.3Sapbaccording to whether or not the program is being built as a tool. Then 1391.3Sapbthe program can use code like this: 1401.1Sapb 1411.1Sapb #if WITH_FEATURE_X 1421.3Sapb ... code to be used when FEATURE X is desired, 1431.3Sapb ... e.g. when being built as part of a release. 1441.3Sapb #else 1451.3Sapb ... code to be used when FEATURE X is not desired, 1461.3Sapb ... e.g. when being built as a tool. 1471.3Sapb #endif 148