Home | History | Annotate | Line # | Download | only in tools
README revision 1.4
      1  1.4  apb $NetBSD: README,v 1.4 2015/01/03 13:20:11 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.3  apb programs are typically built twice: once as a tool and once as part of
     11  1.3  apb the release build.  Tools are relevant only when the make(1) variable
     12  1.3  apb USETOOLS=yes, which is the default for most NetBSD builds.
     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.3  apb 
     31  1.4  apb Most tools should restrict themselves to C language features that are
     32  1.4  apb defined in C89 (ISO 9899-1989); they should avoid using C99 language
     33  1.4  apb features.  There are a few tools, such as compilers, where it is not
     34  1.4  apb practical for the C89 restriction to be maintained.  There are also a
     35  1.4  apb few features, such as the long long data type, that are used by many
     36  1.4  apb tools despite not being defined in C89.
     37  1.4  apb 
     38  1.4  apb Tools may use library features such as functions, macros, and
     39  1.4  apb types, that are defined in C89 and in POSIX (IEEE Std 1003.1) (XXX
     40  1.4  apb year?), and features that are provided by the compatibility framework
     41  1.4  apb (src/tools/compat) described in a separate section below.  This is
     42  1.4  apb usually not an onerous burden, because many C99 library features, and
     43  1.4  apb NetBSD-specific features, are already provided by src/tools/compat, or
     44  1.4  apb can be added when the need for them becomes apparent.
     45  1.3  apb 
     46  1.3  apb If a tool attempts to use a feature that is not available on the host
     47  1.3  apb platform, then the tools build will fail.  This can be addressed by
     48  1.3  apb changing the tool to avoid that feature, or by adding the feature to the
     49  1.3  apb src/tools/compat framework.  It is usually easy to add new macros or
     50  1.3  apb functions to src/tools/compat, and that is usually better than adding
     51  1.3  apb compatibility definitions to individual tools.
     52  1.2  apb 
     53  1.1  apb 
     54  1.1  apb Compatibility framework
     55  1.1  apb =======================
     56  1.1  apb 
     57  1.1  apb src/tools/compat provides a compatibility framework for use by tools.
     58  1.1  apb It installs the following components, and more:
     59  1.1  apb 
     60  1.1  apb ${TOOLDIR}/lib/libnbcompat.a
     61  1.1  apb 
     62  1.1  apb     A library containing functions that are needed by some tools.
     63  1.1  apb 
     64  1.1  apb ${TOOLDIR}/include/nbtool_compat.h
     65  1.1  apb 
     66  1.1  apb     A header file defining macros that are needed by some tools.
     67  1.1  apb 
     68  1.1  apb ${TOOLDIR}/share/compat/defs.mk
     69  1.1  apb 
     70  1.1  apb     A makefile fragment, to be included by other makefiles,
     71  1.1  apb     to define make variables appropriate for building tools.
     72  1.1  apb 
     73  1.1  apb     Among other things, this makefile fragment automatically adds
     74  1.1  apb     the libnbcompat.a library to the LDADD and DPADD variables,
     75  1.1  apb     so that tools will be linked with that library, and adds
     76  1.1  apb     -I${NETBSDSRCDIR}/tools/compat and -DHAVE_NBTOOL_CONFIG_H=1 to the
     77  1.1  apb     HOST_CPPFLAGS variable, so that compiled programs can detect when
     78  1.1  apb     they are being built as tools.
     79  1.1  apb 
     80  1.1  apb 
     81  1.1  apb Adapting Makefiles for use with tools
     82  1.1  apb =====================================
     83  1.1  apb 
     84  1.3  apb Makefiles under src/tools/*/Makefile should define the HOSTPROG
     85  1.3  apb variable.  This is typically done by tools/Makefile.hostprog,
     86  1.1  apb which is directly or indirectly included by all Makefiles in
     87  1.1  apb src/tools/*/Makefile.
     88  1.1  apb 
     89  1.3  apb Makefiles in the non-tools part of the src tree can test whether or not
     90  1.3  apb the HOSTPROG variable is defined, in order tell the difference between
     91  1.3  apb building a tool and building part of a NetBSD release, and they may
     92  1.3  apb alter their behavior accordingly.
     93  1.3  apb 
     94  1.1  apb For example, the Makefile may conditionally refrain from compiling and
     95  1.1  apb linking certain files, and the Makefile may conditionally pass macros to
     96  1.1  apb the compiler via constructs like this:
     97  1.1  apb 
     98  1.1  apb     .if defined(HOSTPROG)
     99  1.3  apb     CPPFLAGS+= -DWITH_FEATURE_X=0 # exclude feature X from tools build
    100  1.1  apb     .else
    101  1.3  apb     CPPFLAGS+= -DWITH_FEATURE_X=1 # include feature X in release build
    102  1.1  apb     .endif
    103  1.1  apb 
    104  1.1  apb Adapting Programs for use with tools
    105  1.1  apb ====================================
    106  1.1  apb 
    107  1.3  apb When a tool is being built, the C compiler should automatically be
    108  1.3  apb invoked with -DHAVE_NBTOOL_CONFIG_H=1.  This is done as a result of
    109  1.3  apb settings in ${TOOLDIR}/share/compat/defs.mk, which should be included
    110  1.3  apb from src/tools/Makefile.host, which should be included directly or
    111  1.3  apb indirectly from src/tools/*/Makefile.
    112  1.3  apb 
    113  1.3  apb A C source file can test whether the HAVE_NBTOOL_CONFIG_H macro is
    114  1.3  apb defined, in order to tell whether or not it is being compiled as part of
    115  1.3  apb a tool.
    116  1.3  apb 
    117  1.3  apb In order to obtain the definitions provided by the tools compatibility
    118  1.3  apb framework, almost every C source file that is built as part of a tool
    119  1.3  apb should have lines like these as the first non-comment lines:
    120  1.1  apb 
    121  1.1  apb     #if HAVE_NBTOOL_CONFIG_H
    122  1.1  apb     #include "nbtool_config.h"
    123  1.3  apb     #endif
    124  1.1  apb 
    125  1.3  apb To omit features from the tools version of a program, the program
    126  1.3  apb may test the HAVE_NBTOOL_CONFIG_H macro, like this:
    127  1.3  apb 
    128  1.3  apb     #if HAVE_NBTOOL_CONFIG_H
    129  1.3  apb        ... code to be used when built as a tool
    130  1.3  apb     #else
    131  1.3  apb        ... code to be used when built as part of a release
    132  1.3  apb     #endif
    133  1.3  apb 
    134  1.3  apb It is often preferable to use macros whose names refer to the features
    135  1.3  apb that should be included or omitted.  See the section on "Adapting
    136  1.3  apb Makefiles for use with tools" for an example in which the Makefile
    137  1.3  apb passes -DWITH_FEATURE_X=0 or -DWITH_FEATURE_X=1 to the compiler
    138  1.3  apb according to whether or not the program is being built as a tool.  Then
    139  1.3  apb the program can use code like this:
    140  1.1  apb 
    141  1.1  apb     #if WITH_FEATURE_X 
    142  1.3  apb        ... code to be used when FEATURE X is desired,
    143  1.3  apb        ... e.g. when being built as part of a release.
    144  1.3  apb     #else
    145  1.3  apb        ... code to be used when FEATURE X is not desired,
    146  1.3  apb        ... e.g. when being built as a tool.
    147  1.3  apb     #endif
    148