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