README revision 1.1 1 1.1 apb $NetBSD: README,v 1.1 2014/09/01 13:50:15 apb Exp $
2 1.1 apb
3 1.1 apb Notes for NetBSD src/tools
4 1.1 apb
5 1.1 apb
6 1.1 apb Background
7 1.1 apb ==========
8 1.1 apb
9 1.1 apb Several programs that are part of NetBSD are also built as tools. Such
10 1.1 apb programs are typically built twice, once as a tool and once as part of
11 1.1 apb the main build. Tools are relevant only when USETOOLS=yes, which is the
12 1.1 apb default.
13 1.1 apb
14 1.1 apb Tools are built on the host platform, using the host compiler,
15 1.1 apb and will run on the host platform during the cross-build of the
16 1.1 apb remainder of NetBSD. They are built near the beginning of a NetBSD
17 1.1 apb build (e.g. "build.sh tools" or "make tools" from the top level src
18 1.1 apb directory), and installed in ${TOOLDIR}.
19 1.1 apb
20 1.1 apb Tools are executed during the main part of the build, when several
21 1.1 apb TOOL_* variables defined in src/share/mk/bsd.*.mk will refer to the
22 1.1 apb tools installed in ${TOOLDIR}.
23 1.1 apb
24 1.1 apb
25 1.1 apb Portability
26 1.1 apb ===========
27 1.1 apb
28 1.1 apb Programs that are built as tools need to be more portable than other
29 1.1 apb parts of NetBSD, because they will need to run on the host platform.
30 1.1 apb They should restrict themselves to features that are defined in relevant
31 1.1 apb standards, and features that are provided by the src/tools/compat
32 1.1 apb framework.
33 1.1 apb
34 1.1 apb
35 1.1 apb Compatibility framework
36 1.1 apb =======================
37 1.1 apb
38 1.1 apb src/tools/compat provides a compatibility framework for use by tools.
39 1.1 apb It installs the following components, and more:
40 1.1 apb
41 1.1 apb ${TOOLDIR}/lib/libnbcompat.a
42 1.1 apb
43 1.1 apb A library containing functions that are needed by some tools.
44 1.1 apb
45 1.1 apb ${TOOLDIR}/include/nbtool_compat.h
46 1.1 apb
47 1.1 apb A header file defining macros that are needed by some tools.
48 1.1 apb
49 1.1 apb ${TOOLDIR}/share/compat/defs.mk
50 1.1 apb
51 1.1 apb A makefile fragment, to be included by other makefiles,
52 1.1 apb to define make variables appropriate for building tools.
53 1.1 apb
54 1.1 apb Among other things, this makefile fragment automatically adds
55 1.1 apb the libnbcompat.a library to the LDADD and DPADD variables,
56 1.1 apb so that tools will be linked with that library, and adds
57 1.1 apb -I${NETBSDSRCDIR}/tools/compat and -DHAVE_NBTOOL_CONFIG_H=1 to the
58 1.1 apb HOST_CPPFLAGS variable, so that compiled programs can detect when
59 1.1 apb they are being built as tools.
60 1.1 apb
61 1.1 apb
62 1.1 apb Adapting Makefiles for use with tools
63 1.1 apb =====================================
64 1.1 apb
65 1.1 apb Makefiles under src/tools/*/Makefile should define HOSTPROG in the
66 1.1 apb make environment. This is typically done by tools/Makefile.hostprog,
67 1.1 apb which is directly or indirectly included by all Makefiles in
68 1.1 apb src/tools/*/Makefile.
69 1.1 apb
70 1.1 apb Makefiles in the non-tools part of the src tree make use tests such as
71 1.1 apb ".if defined(HOSTPROG)" to test whether or not the associated program
72 1.1 apb is being built as a tool, and to modify their behaviour accordingly.
73 1.1 apb For example, the Makefile may conditionally refrain from compiling and
74 1.1 apb linking certain files, and the Makefile may conditionally pass macros to
75 1.1 apb the compiler via constructs like this:
76 1.1 apb
77 1.1 apb .if defined(HOSTPROG)
78 1.1 apb CPPFLAGS+= -DWITH_FEATURE_X=0
79 1.1 apb .else
80 1.1 apb CPPFLAGS+= -DWITH_FEATURE_X=1
81 1.1 apb .endif
82 1.1 apb
83 1.1 apb Adapting Programs for use with tools
84 1.1 apb ====================================
85 1.1 apb
86 1.1 apb The compiler should automatically be invoked with
87 1.1 apb -DHAVE_NBTOOL_CONFIG_H=1, as a result of settings in
88 1.1 apb ${TOOLDIR}/share/compat/defs.mk, which should be included from
89 1.1 apb src/tools/Makefile.host, which should be included directly or indirectly
90 1.1 apb from src/tools/*/Makefile.
91 1.1 apb
92 1.1 apb In order to obtain the compatibility macros provided by the tools
93 1.1 apb compatibility framework, almost every C source file that is built as
94 1.1 apb part of a tool should have lines like this as the first non-comment
95 1.1 apb lines:
96 1.1 apb
97 1.1 apb #if HAVE_NBTOOL_CONFIG_H
98 1.1 apb #include "nbtool_config.h"
99 1.1 apb #endif /* HAVE_NBTOOL_CONFIG_H */
100 1.1 apb
101 1.1 apb To omit features from the tools version of a program, the program's
102 1.1 apb source code should use preprocessor macros that are conditionally passed
103 1.1 apb from its Makefile via CPPFLAGS. For example, it could use something
104 1.1 apb like this:
105 1.1 apb
106 1.1 apb #if WITH_FEATURE_X
107 1.1 apb ...
108 1.1 apb #endif /* WITH_FEATURE_X */
109 1.1 apb
110