1*d24699dcSchristos$NetBSD: README,v 1.6 2024/09/25 16:24:58 christos Exp $ 2893aa1d4Sapb 3893aa1d4SapbNotes for NetBSD src/tools 4893aa1d4Sapb 5893aa1d4Sapb 6893aa1d4SapbBackground 7893aa1d4Sapb========== 8893aa1d4Sapb 9893aa1d4SapbSeveral programs that are part of NetBSD are also built as tools. Such 108c50b72eSapbprograms are typically built twice: once as a tool and once as part of 118c50b72eSapbthe release build. Tools are relevant only when the make(1) variable 128c50b72eSapbUSETOOLS=yes, which is the default for most NetBSD builds. 13893aa1d4Sapb 14893aa1d4SapbTools are built on the host platform, using the host compiler, 15893aa1d4Sapband will run on the host platform during the cross-build of the 16893aa1d4Sapbremainder of NetBSD. They are built near the beginning of a NetBSD 17893aa1d4Sapbbuild (e.g. "build.sh tools" or "make tools" from the top level src 18893aa1d4Sapbdirectory), and installed in ${TOOLDIR}. 19893aa1d4Sapb 20893aa1d4SapbTools are executed during the main part of the build, when several 21893aa1d4SapbTOOL_* variables defined in src/share/mk/bsd.*.mk will refer to the 22893aa1d4Sapbtools installed in ${TOOLDIR}. 23893aa1d4Sapb 24893aa1d4Sapb 25893aa1d4SapbPortability 26893aa1d4Sapb=========== 27893aa1d4Sapb 28893aa1d4SapbPrograms that are built as tools need to be more portable than other 29893aa1d4Sapbparts of NetBSD, because they will need to run on the host platform. 30893aa1d4Sapb 315ffb287eSapbMost tools should restrict themselves to C language features that are 32c1536369Srilligdefined in C99 (ISO/IEC 9899-1999); they should avoid using C11 language 33c1536369Srilligfeatures, such as <threads.h>, _Alignof, <uchar.h>, _Generic, 34c1536369Srilligstatic_assert, anonymous structures and unions. 358c50b72eSapb 36c1536369SrilligTools may use library features such as functions, macros, and types, 37c1536369Srilligthat are defined in C99 and in POSIX (IEEE Std 1003.1) (XXX year?), and 38c1536369Srilligfeatures that are provided by the compatibility framework 39c1536369Srillig(src/tools/compat) described in a separate section below. 408c50b72eSapb 418c50b72eSapbIf a tool attempts to use a feature that is not available on the host 428c50b72eSapbplatform, then the tools build will fail. This can be addressed by 438c50b72eSapbchanging the tool to avoid that feature, or by adding the feature to the 448c50b72eSapbsrc/tools/compat framework. It is usually easy to add new macros or 458c50b72eSapbfunctions to src/tools/compat, and that is usually better than adding 468c50b72eSapbcompatibility definitions to individual tools. 473512e573Sapb 48893aa1d4Sapb 49893aa1d4SapbCompatibility framework 50893aa1d4Sapb======================= 51893aa1d4Sapb 52893aa1d4Sapbsrc/tools/compat provides a compatibility framework for use by tools. 53893aa1d4SapbIt installs the following components, and more: 54893aa1d4Sapb 55893aa1d4Sapb${TOOLDIR}/lib/libnbcompat.a 56893aa1d4Sapb 57893aa1d4Sapb A library containing functions that are needed by some tools. 58893aa1d4Sapb 59*d24699dcSchristos${TOOLDIR}/include/compat/nbtool_compat.h 60893aa1d4Sapb 61893aa1d4Sapb A header file defining macros that are needed by some tools. 62893aa1d4Sapb 63893aa1d4Sapb${TOOLDIR}/share/compat/defs.mk 64893aa1d4Sapb 65893aa1d4Sapb A makefile fragment, to be included by other makefiles, 66893aa1d4Sapb to define make variables appropriate for building tools. 67893aa1d4Sapb 68893aa1d4Sapb Among other things, this makefile fragment automatically adds 69893aa1d4Sapb the libnbcompat.a library to the LDADD and DPADD variables, 70893aa1d4Sapb so that tools will be linked with that library, and adds 71893aa1d4Sapb -I${NETBSDSRCDIR}/tools/compat and -DHAVE_NBTOOL_CONFIG_H=1 to the 72893aa1d4Sapb HOST_CPPFLAGS variable, so that compiled programs can detect when 73893aa1d4Sapb they are being built as tools. 74893aa1d4Sapb 75893aa1d4Sapb 76893aa1d4SapbAdapting Makefiles for use with tools 77893aa1d4Sapb===================================== 78893aa1d4Sapb 798c50b72eSapbMakefiles under src/tools/*/Makefile should define the HOSTPROG 808c50b72eSapbvariable. This is typically done by tools/Makefile.hostprog, 81893aa1d4Sapbwhich is directly or indirectly included by all Makefiles in 82893aa1d4Sapbsrc/tools/*/Makefile. 83893aa1d4Sapb 848c50b72eSapbMakefiles in the non-tools part of the src tree can test whether or not 858c50b72eSapbthe HOSTPROG variable is defined, in order tell the difference between 868c50b72eSapbbuilding a tool and building part of a NetBSD release, and they may 878c50b72eSapbalter their behavior accordingly. 888c50b72eSapb 89893aa1d4SapbFor example, the Makefile may conditionally refrain from compiling and 90893aa1d4Sapblinking certain files, and the Makefile may conditionally pass macros to 91893aa1d4Sapbthe compiler via constructs like this: 92893aa1d4Sapb 93893aa1d4Sapb .if defined(HOSTPROG) 948c50b72eSapb CPPFLAGS+= -DWITH_FEATURE_X=0 # exclude feature X from tools build 95893aa1d4Sapb .else 968c50b72eSapb CPPFLAGS+= -DWITH_FEATURE_X=1 # include feature X in release build 97893aa1d4Sapb .endif 98893aa1d4Sapb 99893aa1d4SapbAdapting Programs for use with tools 100893aa1d4Sapb==================================== 101893aa1d4Sapb 1028c50b72eSapbWhen a tool is being built, the C compiler should automatically be 1038c50b72eSapbinvoked with -DHAVE_NBTOOL_CONFIG_H=1. This is done as a result of 1048c50b72eSapbsettings in ${TOOLDIR}/share/compat/defs.mk, which should be included 1058c50b72eSapbfrom src/tools/Makefile.host, which should be included directly or 1068c50b72eSapbindirectly from src/tools/*/Makefile. 107893aa1d4Sapb 1088c50b72eSapbA C source file can test whether the HAVE_NBTOOL_CONFIG_H macro is 1098c50b72eSapbdefined, in order to tell whether or not it is being compiled as part of 1108c50b72eSapba tool. 1118c50b72eSapb 1128c50b72eSapbIn order to obtain the definitions provided by the tools compatibility 1138c50b72eSapbframework, almost every C source file that is built as part of a tool 1148c50b72eSapbshould have lines like these as the first non-comment lines: 115893aa1d4Sapb 116893aa1d4Sapb #if HAVE_NBTOOL_CONFIG_H 117893aa1d4Sapb #include "nbtool_config.h" 1188c50b72eSapb #endif 119893aa1d4Sapb 1208c50b72eSapbTo omit features from the tools version of a program, the program 1218c50b72eSapbmay test the HAVE_NBTOOL_CONFIG_H macro, like this: 1228c50b72eSapb 1238c50b72eSapb #if HAVE_NBTOOL_CONFIG_H 1248c50b72eSapb ... code to be used when built as a tool 1258c50b72eSapb #else 1268c50b72eSapb ... code to be used when built as part of a release 1278c50b72eSapb #endif 1288c50b72eSapb 1298c50b72eSapbIt is often preferable to use macros whose names refer to the features 1308c50b72eSapbthat should be included or omitted. See the section on "Adapting 1318c50b72eSapbMakefiles for use with tools" for an example in which the Makefile 1328c50b72eSapbpasses -DWITH_FEATURE_X=0 or -DWITH_FEATURE_X=1 to the compiler 1338c50b72eSapbaccording to whether or not the program is being built as a tool. Then 1348c50b72eSapbthe program can use code like this: 135893aa1d4Sapb 136893aa1d4Sapb #if WITH_FEATURE_X 1378c50b72eSapb ... code to be used when FEATURE X is desired, 1388c50b72eSapb ... e.g. when being built as part of a release. 1398c50b72eSapb #else 1408c50b72eSapb ... code to be used when FEATURE X is not desired, 1418c50b72eSapb ... e.g. when being built as a tool. 1428c50b72eSapb #endif 143