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