1*0a6a1f1dSLionel Sambuc$NetBSD: README,v 1.4 2015/01/03 13:20:11 apb Exp $ 2*0a6a1f1dSLionel Sambuc 3*0a6a1f1dSLionel SambucNotes for NetBSD src/tools 4*0a6a1f1dSLionel Sambuc 5*0a6a1f1dSLionel Sambuc 6*0a6a1f1dSLionel SambucBackground 7*0a6a1f1dSLionel Sambuc========== 8*0a6a1f1dSLionel Sambuc 9*0a6a1f1dSLionel SambucSeveral programs that are part of NetBSD are also built as tools. Such 10*0a6a1f1dSLionel Sambucprograms are typically built twice: once as a tool and once as part of 11*0a6a1f1dSLionel Sambucthe release build. Tools are relevant only when the make(1) variable 12*0a6a1f1dSLionel SambucUSETOOLS=yes, which is the default for most NetBSD builds. 13*0a6a1f1dSLionel Sambuc 14*0a6a1f1dSLionel SambucTools are built on the host platform, using the host compiler, 15*0a6a1f1dSLionel Sambucand will run on the host platform during the cross-build of the 16*0a6a1f1dSLionel Sambucremainder of NetBSD. They are built near the beginning of a NetBSD 17*0a6a1f1dSLionel Sambucbuild (e.g. "build.sh tools" or "make tools" from the top level src 18*0a6a1f1dSLionel Sambucdirectory), and installed in ${TOOLDIR}. 19*0a6a1f1dSLionel Sambuc 20*0a6a1f1dSLionel SambucTools are executed during the main part of the build, when several 21*0a6a1f1dSLionel SambucTOOL_* variables defined in src/share/mk/bsd.*.mk will refer to the 22*0a6a1f1dSLionel Sambuctools installed in ${TOOLDIR}. 23*0a6a1f1dSLionel Sambuc 24*0a6a1f1dSLionel Sambuc 25*0a6a1f1dSLionel SambucPortability 26*0a6a1f1dSLionel Sambuc=========== 27*0a6a1f1dSLionel Sambuc 28*0a6a1f1dSLionel SambucPrograms that are built as tools need to be more portable than other 29*0a6a1f1dSLionel Sambucparts of NetBSD, because they will need to run on the host platform. 30*0a6a1f1dSLionel Sambuc 31*0a6a1f1dSLionel SambucMost tools should restrict themselves to C language features that are 32*0a6a1f1dSLionel Sambucdefined in C89 (ISO 9899-1989); they should avoid using C99 language 33*0a6a1f1dSLionel Sambucfeatures. There are a few tools, such as compilers, where it is not 34*0a6a1f1dSLionel Sambucpractical for the C89 restriction to be maintained. There are also a 35*0a6a1f1dSLionel Sambucfew features, such as the long long data type, that are used by many 36*0a6a1f1dSLionel Sambuctools despite not being defined in C89. 37*0a6a1f1dSLionel Sambuc 38*0a6a1f1dSLionel SambucTools may use library features such as functions, macros, and 39*0a6a1f1dSLionel Sambuctypes, that are defined in C89 and in POSIX (IEEE Std 1003.1) (XXX 40*0a6a1f1dSLionel Sambucyear?), and features that are provided by the compatibility framework 41*0a6a1f1dSLionel Sambuc(src/tools/compat) described in a separate section below. This is 42*0a6a1f1dSLionel Sambucusually not an onerous burden, because many C99 library features, and 43*0a6a1f1dSLionel SambucNetBSD-specific features, are already provided by src/tools/compat, or 44*0a6a1f1dSLionel Sambuccan be added when the need for them becomes apparent. 45*0a6a1f1dSLionel Sambuc 46*0a6a1f1dSLionel SambucIf a tool attempts to use a feature that is not available on the host 47*0a6a1f1dSLionel Sambucplatform, then the tools build will fail. This can be addressed by 48*0a6a1f1dSLionel Sambucchanging the tool to avoid that feature, or by adding the feature to the 49*0a6a1f1dSLionel Sambucsrc/tools/compat framework. It is usually easy to add new macros or 50*0a6a1f1dSLionel Sambucfunctions to src/tools/compat, and that is usually better than adding 51*0a6a1f1dSLionel Sambuccompatibility definitions to individual tools. 52*0a6a1f1dSLionel Sambuc 53*0a6a1f1dSLionel Sambuc 54*0a6a1f1dSLionel SambucCompatibility framework 55*0a6a1f1dSLionel Sambuc======================= 56*0a6a1f1dSLionel Sambuc 57*0a6a1f1dSLionel Sambucsrc/tools/compat provides a compatibility framework for use by tools. 58*0a6a1f1dSLionel SambucIt installs the following components, and more: 59*0a6a1f1dSLionel Sambuc 60*0a6a1f1dSLionel Sambuc${TOOLDIR}/lib/libnbcompat.a 61*0a6a1f1dSLionel Sambuc 62*0a6a1f1dSLionel Sambuc A library containing functions that are needed by some tools. 63*0a6a1f1dSLionel Sambuc 64*0a6a1f1dSLionel Sambuc${TOOLDIR}/include/nbtool_compat.h 65*0a6a1f1dSLionel Sambuc 66*0a6a1f1dSLionel Sambuc A header file defining macros that are needed by some tools. 67*0a6a1f1dSLionel Sambuc 68*0a6a1f1dSLionel Sambuc${TOOLDIR}/share/compat/defs.mk 69*0a6a1f1dSLionel Sambuc 70*0a6a1f1dSLionel Sambuc A makefile fragment, to be included by other makefiles, 71*0a6a1f1dSLionel Sambuc to define make variables appropriate for building tools. 72*0a6a1f1dSLionel Sambuc 73*0a6a1f1dSLionel Sambuc Among other things, this makefile fragment automatically adds 74*0a6a1f1dSLionel Sambuc the libnbcompat.a library to the LDADD and DPADD variables, 75*0a6a1f1dSLionel Sambuc so that tools will be linked with that library, and adds 76*0a6a1f1dSLionel Sambuc -I${NETBSDSRCDIR}/tools/compat and -DHAVE_NBTOOL_CONFIG_H=1 to the 77*0a6a1f1dSLionel Sambuc HOST_CPPFLAGS variable, so that compiled programs can detect when 78*0a6a1f1dSLionel Sambuc they are being built as tools. 79*0a6a1f1dSLionel Sambuc 80*0a6a1f1dSLionel Sambuc 81*0a6a1f1dSLionel SambucAdapting Makefiles for use with tools 82*0a6a1f1dSLionel Sambuc===================================== 83*0a6a1f1dSLionel Sambuc 84*0a6a1f1dSLionel SambucMakefiles under src/tools/*/Makefile should define the HOSTPROG 85*0a6a1f1dSLionel Sambucvariable. This is typically done by tools/Makefile.hostprog, 86*0a6a1f1dSLionel Sambucwhich is directly or indirectly included by all Makefiles in 87*0a6a1f1dSLionel Sambucsrc/tools/*/Makefile. 88*0a6a1f1dSLionel Sambuc 89*0a6a1f1dSLionel SambucMakefiles in the non-tools part of the src tree can test whether or not 90*0a6a1f1dSLionel Sambucthe HOSTPROG variable is defined, in order tell the difference between 91*0a6a1f1dSLionel Sambucbuilding a tool and building part of a NetBSD release, and they may 92*0a6a1f1dSLionel Sambucalter their behavior accordingly. 93*0a6a1f1dSLionel Sambuc 94*0a6a1f1dSLionel SambucFor example, the Makefile may conditionally refrain from compiling and 95*0a6a1f1dSLionel Sambuclinking certain files, and the Makefile may conditionally pass macros to 96*0a6a1f1dSLionel Sambucthe compiler via constructs like this: 97*0a6a1f1dSLionel Sambuc 98*0a6a1f1dSLionel Sambuc .if defined(HOSTPROG) 99*0a6a1f1dSLionel Sambuc CPPFLAGS+= -DWITH_FEATURE_X=0 # exclude feature X from tools build 100*0a6a1f1dSLionel Sambuc .else 101*0a6a1f1dSLionel Sambuc CPPFLAGS+= -DWITH_FEATURE_X=1 # include feature X in release build 102*0a6a1f1dSLionel Sambuc .endif 103*0a6a1f1dSLionel Sambuc 104*0a6a1f1dSLionel SambucAdapting Programs for use with tools 105*0a6a1f1dSLionel Sambuc==================================== 106*0a6a1f1dSLionel Sambuc 107*0a6a1f1dSLionel SambucWhen a tool is being built, the C compiler should automatically be 108*0a6a1f1dSLionel Sambucinvoked with -DHAVE_NBTOOL_CONFIG_H=1. This is done as a result of 109*0a6a1f1dSLionel Sambucsettings in ${TOOLDIR}/share/compat/defs.mk, which should be included 110*0a6a1f1dSLionel Sambucfrom src/tools/Makefile.host, which should be included directly or 111*0a6a1f1dSLionel Sambucindirectly from src/tools/*/Makefile. 112*0a6a1f1dSLionel Sambuc 113*0a6a1f1dSLionel SambucA C source file can test whether the HAVE_NBTOOL_CONFIG_H macro is 114*0a6a1f1dSLionel Sambucdefined, in order to tell whether or not it is being compiled as part of 115*0a6a1f1dSLionel Sambuca tool. 116*0a6a1f1dSLionel Sambuc 117*0a6a1f1dSLionel SambucIn order to obtain the definitions provided by the tools compatibility 118*0a6a1f1dSLionel Sambucframework, almost every C source file that is built as part of a tool 119*0a6a1f1dSLionel Sambucshould have lines like these as the first non-comment lines: 120*0a6a1f1dSLionel Sambuc 121*0a6a1f1dSLionel Sambuc #if HAVE_NBTOOL_CONFIG_H 122*0a6a1f1dSLionel Sambuc #include "nbtool_config.h" 123*0a6a1f1dSLionel Sambuc #endif 124*0a6a1f1dSLionel Sambuc 125*0a6a1f1dSLionel SambucTo omit features from the tools version of a program, the program 126*0a6a1f1dSLionel Sambucmay test the HAVE_NBTOOL_CONFIG_H macro, like this: 127*0a6a1f1dSLionel Sambuc 128*0a6a1f1dSLionel Sambuc #if HAVE_NBTOOL_CONFIG_H 129*0a6a1f1dSLionel Sambuc ... code to be used when built as a tool 130*0a6a1f1dSLionel Sambuc #else 131*0a6a1f1dSLionel Sambuc ... code to be used when built as part of a release 132*0a6a1f1dSLionel Sambuc #endif 133*0a6a1f1dSLionel Sambuc 134*0a6a1f1dSLionel SambucIt is often preferable to use macros whose names refer to the features 135*0a6a1f1dSLionel Sambucthat should be included or omitted. See the section on "Adapting 136*0a6a1f1dSLionel SambucMakefiles for use with tools" for an example in which the Makefile 137*0a6a1f1dSLionel Sambucpasses -DWITH_FEATURE_X=0 or -DWITH_FEATURE_X=1 to the compiler 138*0a6a1f1dSLionel Sambucaccording to whether or not the program is being built as a tool. Then 139*0a6a1f1dSLionel Sambucthe program can use code like this: 140*0a6a1f1dSLionel Sambuc 141*0a6a1f1dSLionel Sambuc #if WITH_FEATURE_X 142*0a6a1f1dSLionel Sambuc ... code to be used when FEATURE X is desired, 143*0a6a1f1dSLionel Sambuc ... e.g. when being built as part of a release. 144*0a6a1f1dSLionel Sambuc #else 145*0a6a1f1dSLionel Sambuc ... code to be used when FEATURE X is not desired, 146*0a6a1f1dSLionel Sambuc ... e.g. when being built as a tool. 147*0a6a1f1dSLionel Sambuc #endif 148