README revision 1.3
11.3Sapb$NetBSD: README,v 1.3 2014/09/30 07:34:50 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.3SapbTools should restrict themselves to C language features that are defined
321.3Sapbin C89 (ISO 9899-1989); they should avoid using C99 features.
331.3Sapb
341.3SapbTools may library features defined in C89 and in POSIX (IEEE Std 1003.1)
351.3Sapb(XXX year?), and features that are provided by the src/tools/compat
361.3Sapbframework described below.
371.3Sapb
381.3SapbIf a tool attempts to use a feature that is not available on the host
391.3Sapbplatform, then the tools build will fail.  This can be addressed by
401.3Sapbchanging the tool to avoid that feature, or by adding the feature to the
411.3Sapbsrc/tools/compat framework.  It is usually easy to add new macros or
421.3Sapbfunctions to src/tools/compat, and that is usually better than adding
431.3Sapbcompatibility definitions to individual tools.
441.2Sapb
451.1Sapb
461.1SapbCompatibility framework
471.1Sapb=======================
481.1Sapb
491.1Sapbsrc/tools/compat provides a compatibility framework for use by tools.
501.1SapbIt installs the following components, and more:
511.1Sapb
521.1Sapb${TOOLDIR}/lib/libnbcompat.a
531.1Sapb
541.1Sapb    A library containing functions that are needed by some tools.
551.1Sapb
561.1Sapb${TOOLDIR}/include/nbtool_compat.h
571.1Sapb
581.1Sapb    A header file defining macros that are needed by some tools.
591.1Sapb
601.1Sapb${TOOLDIR}/share/compat/defs.mk
611.1Sapb
621.1Sapb    A makefile fragment, to be included by other makefiles,
631.1Sapb    to define make variables appropriate for building tools.
641.1Sapb
651.1Sapb    Among other things, this makefile fragment automatically adds
661.1Sapb    the libnbcompat.a library to the LDADD and DPADD variables,
671.1Sapb    so that tools will be linked with that library, and adds
681.1Sapb    -I${NETBSDSRCDIR}/tools/compat and -DHAVE_NBTOOL_CONFIG_H=1 to the
691.1Sapb    HOST_CPPFLAGS variable, so that compiled programs can detect when
701.1Sapb    they are being built as tools.
711.1Sapb
721.1Sapb
731.1SapbAdapting Makefiles for use with tools
741.1Sapb=====================================
751.1Sapb
761.3SapbMakefiles under src/tools/*/Makefile should define the HOSTPROG
771.3Sapbvariable.  This is typically done by tools/Makefile.hostprog,
781.1Sapbwhich is directly or indirectly included by all Makefiles in
791.1Sapbsrc/tools/*/Makefile.
801.1Sapb
811.3SapbMakefiles in the non-tools part of the src tree can test whether or not
821.3Sapbthe HOSTPROG variable is defined, in order tell the difference between
831.3Sapbbuilding a tool and building part of a NetBSD release, and they may
841.3Sapbalter their behavior accordingly.
851.3Sapb
861.1SapbFor example, the Makefile may conditionally refrain from compiling and
871.1Sapblinking certain files, and the Makefile may conditionally pass macros to
881.1Sapbthe compiler via constructs like this:
891.1Sapb
901.1Sapb    .if defined(HOSTPROG)
911.3Sapb    CPPFLAGS+= -DWITH_FEATURE_X=0 # exclude feature X from tools build
921.1Sapb    .else
931.3Sapb    CPPFLAGS+= -DWITH_FEATURE_X=1 # include feature X in release build
941.1Sapb    .endif
951.1Sapb
961.1SapbAdapting Programs for use with tools
971.1Sapb====================================
981.1Sapb
991.3SapbWhen a tool is being built, the C compiler should automatically be
1001.3Sapbinvoked with -DHAVE_NBTOOL_CONFIG_H=1.  This is done as a result of
1011.3Sapbsettings in ${TOOLDIR}/share/compat/defs.mk, which should be included
1021.3Sapbfrom src/tools/Makefile.host, which should be included directly or
1031.3Sapbindirectly from src/tools/*/Makefile.
1041.3Sapb
1051.3SapbA C source file can test whether the HAVE_NBTOOL_CONFIG_H macro is
1061.3Sapbdefined, in order to tell whether or not it is being compiled as part of
1071.3Sapba tool.
1081.3Sapb
1091.3SapbIn order to obtain the definitions provided by the tools compatibility
1101.3Sapbframework, almost every C source file that is built as part of a tool
1111.3Sapbshould have lines like these as the first non-comment lines:
1121.1Sapb
1131.1Sapb    #if HAVE_NBTOOL_CONFIG_H
1141.1Sapb    #include "nbtool_config.h"
1151.3Sapb    #endif
1161.1Sapb
1171.3SapbTo omit features from the tools version of a program, the program
1181.3Sapbmay test the HAVE_NBTOOL_CONFIG_H macro, like this:
1191.3Sapb
1201.3Sapb    #if HAVE_NBTOOL_CONFIG_H
1211.3Sapb       ... code to be used when built as a tool
1221.3Sapb    #else
1231.3Sapb       ... code to be used when built as part of a release
1241.3Sapb    #endif
1251.3Sapb
1261.3SapbIt is often preferable to use macros whose names refer to the features
1271.3Sapbthat should be included or omitted.  See the section on "Adapting
1281.3SapbMakefiles for use with tools" for an example in which the Makefile
1291.3Sapbpasses -DWITH_FEATURE_X=0 or -DWITH_FEATURE_X=1 to the compiler
1301.3Sapbaccording to whether or not the program is being built as a tool.  Then
1311.3Sapbthe program can use code like this:
1321.1Sapb
1331.1Sapb    #if WITH_FEATURE_X 
1341.3Sapb       ... code to be used when FEATURE X is desired,
1351.3Sapb       ... e.g. when being built as part of a release.
1361.3Sapb    #else
1371.3Sapb       ... code to be used when FEATURE X is not desired,
1381.3Sapb       ... e.g. when being built as a tool.
1391.3Sapb    #endif
1401.1Sapb
141