README revision 1.2
11.2Sapb$NetBSD: README,v 1.2 2014/09/24 16:17:39 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.1Sapbprograms are typically built twice, once as a tool and once as part of
111.1Sapbthe main build.  Tools are relevant only when USETOOLS=yes, which is the
121.1Sapbdefault.
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.1SapbThey should restrict themselves to features that are defined in relevant
311.1Sapbstandards, and features that are provided by the src/tools/compat
321.1Sapbframework.
331.1Sapb
341.2SapbIt is usually easy to add new definitions to src/tools/compat, and that
351.2Sapbis usually better than adding compatibility definitions to individual
361.2Sapbtools.
371.2Sapb
381.1Sapb
391.1SapbCompatibility framework
401.1Sapb=======================
411.1Sapb
421.1Sapbsrc/tools/compat provides a compatibility framework for use by tools.
431.1SapbIt installs the following components, and more:
441.1Sapb
451.1Sapb${TOOLDIR}/lib/libnbcompat.a
461.1Sapb
471.1Sapb    A library containing functions that are needed by some tools.
481.1Sapb
491.1Sapb${TOOLDIR}/include/nbtool_compat.h
501.1Sapb
511.1Sapb    A header file defining macros that are needed by some tools.
521.1Sapb
531.1Sapb${TOOLDIR}/share/compat/defs.mk
541.1Sapb
551.1Sapb    A makefile fragment, to be included by other makefiles,
561.1Sapb    to define make variables appropriate for building tools.
571.1Sapb
581.1Sapb    Among other things, this makefile fragment automatically adds
591.1Sapb    the libnbcompat.a library to the LDADD and DPADD variables,
601.1Sapb    so that tools will be linked with that library, and adds
611.1Sapb    -I${NETBSDSRCDIR}/tools/compat and -DHAVE_NBTOOL_CONFIG_H=1 to the
621.1Sapb    HOST_CPPFLAGS variable, so that compiled programs can detect when
631.1Sapb    they are being built as tools.
641.1Sapb
651.1Sapb
661.1SapbAdapting Makefiles for use with tools
671.1Sapb=====================================
681.1Sapb
691.1SapbMakefiles under src/tools/*/Makefile should define HOSTPROG in the
701.1Sapbmake environment.  This is typically done by tools/Makefile.hostprog,
711.1Sapbwhich is directly or indirectly included by all Makefiles in
721.1Sapbsrc/tools/*/Makefile.
731.1Sapb
741.1SapbMakefiles in the non-tools part of the src tree make use tests such as
751.1Sapb".if defined(HOSTPROG)" to test whether or not the associated program
761.1Sapbis being built as a tool, and to modify their behaviour accordingly.
771.1SapbFor example, the Makefile may conditionally refrain from compiling and
781.1Sapblinking certain files, and the Makefile may conditionally pass macros to
791.1Sapbthe compiler via constructs like this:
801.1Sapb
811.1Sapb    .if defined(HOSTPROG)
821.1Sapb    CPPFLAGS+= -DWITH_FEATURE_X=0
831.1Sapb    .else
841.1Sapb    CPPFLAGS+= -DWITH_FEATURE_X=1
851.1Sapb    .endif
861.1Sapb
871.1SapbAdapting Programs for use with tools
881.1Sapb====================================
891.1Sapb
901.1SapbThe compiler should automatically be invoked with
911.1Sapb-DHAVE_NBTOOL_CONFIG_H=1, as a result of settings in
921.1Sapb${TOOLDIR}/share/compat/defs.mk, which should be included from
931.1Sapbsrc/tools/Makefile.host, which should be included directly or indirectly
941.1Sapbfrom src/tools/*/Makefile.
951.1Sapb
961.1SapbIn order to obtain the compatibility macros provided by the tools
971.1Sapbcompatibility framework, almost every C source file that is built as
981.1Sapbpart of a tool should have lines like this as the first non-comment
991.1Sapblines:
1001.1Sapb
1011.1Sapb    #if HAVE_NBTOOL_CONFIG_H
1021.1Sapb    #include "nbtool_config.h"
1031.1Sapb    #endif /* HAVE_NBTOOL_CONFIG_H */
1041.1Sapb
1051.1SapbTo omit features from the tools version of a program, the program's
1061.1Sapbsource code should use preprocessor macros that are conditionally passed
1071.1Sapbfrom its Makefile via CPPFLAGS.  For example, it could use something
1081.1Sapblike this:
1091.1Sapb
1101.1Sapb    #if WITH_FEATURE_X 
1111.1Sapb       ...
1121.1Sapb    #endif /* WITH_FEATURE_X */
1131.1Sapb
114