netbsd-src/tools
riastradh ad49149131 elftoolchain: Be consistent about which ELF header files we use.
1. For tools that use elftoolchain: always use elftoolchain's
   elfdefinitions.h.  Don't even think about looking at the host's
   sys/exec_elf.h, which makes no sense and should never happen.

   (ELF tools that don't use elftoolchain, like m68k-elf2coff,
   continue to use nbincludes/sys/exec_elf.h.  But no more nbincludes
   hacks in elftoolchain.)

2. For kernel components (solaris, zfs, dtrace): always use
   sys/exec_elf.h, even in Solaris components via sys/elf.h.
   elfdefinitions.h is not wired up in the kernel build at all.

3. For most userland components that involve libelf: use
   elfdefinitions.h via libelf header files (libelf.h, gelf.h).

   libdtrace in particular requires _all_ R_* reloc type definitions,
   but sys/exec_elf.h brings in only the _current machine's_ R_*
   reloc type definitions.  (While here: Use uintptr_t instead of
   Elf_Addr for pointer-to-integer cast, since Elf_Addr is MD and
   provided only by sys/exec_elf.h, not by elfdefinitions.h.)

   And most userland components using libelf don't rely on any
   properties of the current machine from sys/exec_elf.h, so they can
   use libelf's elfdefinition.h.

   Exceptions:

   - dtrace drti.c relies on link.h -> link_elf.h -> sys/exec_elf.h,
     but it also relies on sys/dtrace.h -> sys/elf.h ->
     elfdefinitions.h like other userland components using sys/elf.h.

   - kdump-ioctl.c uses sys/exec_elf.h directly and sys/dtrace.h ->
     sys/elf.h -> elfdefinitions like other userland components using
     sys/elf.h.

   - t_ptrace_wait.c (via t_ptrace_core_wait.h) uses libelf to parse
     core files, but relies on sys/exec_elf.h for struct
     netbsd_elfcore_procinfo.

   None of these exceptions needs all R_* reloc type definitions, so
   as a workaround, we can just suppress libelf's elfdefinitions.h by
   defining _SYS_ELFDEFINITIONS_H_ and use sys/exec_elf.h in these
   exceptions.

And undo the whole BUILTIN_ELF_HEADERS mistake.  This was:

- half bogus workarounds for missing build_install dependencies in
  tools/Makefile, which are no longer missing now, and
- half futile attempt to use src/sys/sys/exec_elf.h via nbincludes in
  tools involving libelf instead of libelf's elfdefinitions.h, which
  collides.

Longer-term, we may wish to unify sys/exec_elf.h and libelf's
elfdefinitions.h, so we don't have to play these games.

But at least now the games are limited to three .c files (one of
which is generated by Makefile.ioctl-c), rather than haphazardly
applied tree-wide by monstrous kludges in widely used .h files with
broken hackarounds to get the tools build lurching to completion.
2024-04-01 18:33:22 +00:00
..
amiga-elf2bb
amiga-txlt
arm-elf2aout s/hidding/hiding/ 2022-05-28 22:00:55 +00:00
asn1_compile Fix host tools build for asn1_compile, compile_et, and slc. 2017-01-30 02:38:50 +00:00
autoconf
awk
binstall Create a new bsd.hostinit.mk file and put the build definitions for all host 2018-05-01 19:59:42 +00:00
binutils tools/binutils: Add support to mknative for binutils.old 2023-11-25 12:11:27 +00:00
cap_mkdb
cat
cksum
compat Update build instructions for Solaris 10 hosts 2023-09-02 18:45:48 +00:00
compile_et Fix host tools build for asn1_compile, compile_et, and slc. 2017-01-30 02:38:50 +00:00
config
crunchgen
ctags
ctfconvert elftoolchain: Be consistent about which ELF header files we use. 2024-04-01 18:33:22 +00:00
ctfmerge elftoolchain: Be consistent about which ELF header files we use. 2024-04-01 18:33:22 +00:00
cvslatest Create a new bsd.hostinit.mk file and put the build definitions for all host 2018-05-01 19:59:42 +00:00
date If we are building date earlier, we need to use an uninstalled copy 2021-05-27 08:41:35 +00:00
db
dbsym Create a new bsd.hostinit.mk file and put the build definitions for all host 2018-05-01 19:59:42 +00:00
disklabel Use ${_TOOL_PREFIX} instead of hardcoding "nb" for consistency. 2019-05-17 13:51:36 +00:00
dtc Create a new bsd.hostinit.mk file and put the build definitions for all host 2018-05-01 19:59:42 +00:00
elftoolchain elftoolchain: Be consistent about which ELF header files we use. 2024-04-01 18:33:22 +00:00
elftosb remove trailing newline 2018-05-19 17:48:51 +00:00
fdisk Patch the build script and toolchain to allow passing through 2021-09-18 01:47:07 +00:00
fgen
file force disable seccomp, in case it gets detected. 2018-09-28 14:45:16 +00:00
gcc version our GCC 12 as "nb2 20240221" now. 2024-02-25 02:24:19 +00:00
gdb gdb/mknative: Preset yes to rename(2) checks 2023-08-20 02:30:58 +00:00
genassym Create a new bsd.hostinit.mk file and put the build definitions for all host 2018-05-01 19:59:42 +00:00
gencat
gettext Create a new bsd.hostinit.mk file and put the build definitions for all host 2018-05-01 19:59:42 +00:00
gmake switch to the new gmake 2014-08-18 06:58:51 +00:00
gmp Patch the build script and toolchain to allow passing through 2021-09-18 01:47:07 +00:00
gpt Use ${_TOOL_PREFIX} instead of hardcoding "nb" for consistency. 2019-05-17 13:51:36 +00:00
grep Add WITHOUT_GZIP 2018-08-12 09:03:42 +00:00
groff grr another copy of the path I missed; factor out to a variable. 2016-01-14 01:13:26 +00:00
gzip Start for a TOOL_GZIP 2024-04-01 02:19:41 +00:00
hexdump
host-mkdep host-mkdep(1): Add support for -iquote. 2023-07-10 08:55:44 +00:00
hp300-mkboot
hppa-mkboot
ibmnws-ncdcs
installboot Add evbmips support for u-boot handling. 2020-06-21 17:17:01 +00:00
isl build isl tool for GCC 12. 2024-02-23 05:34:41 +00:00
join
lex PR/53544: Add a dependency on the Makefile to fix incremental builds. 2018-08-23 06:21:37 +00:00
libctf elftoolchain: Be consistent about which ELF header files we use. 2024-04-01 18:33:22 +00:00
libfdt Create a new bsd.hostinit.mk file and put the build definitions for all host 2018-05-01 19:59:42 +00:00
libprop Fix building libprop as a host tool library on platforms that don't have 2019-05-08 02:25:50 +00:00
lint lint: make path to lint data configurable 2021-01-24 16:55:09 +00:00
lint1
lint2
llvm Update LLVM build system for 249b40b558955afe5ac2b549edcf2d7f859c8cc9 2021-05-30 01:56:44 +00:00
llvm-clang Update LLVM to 10.0.0git (01f3a59fb3e2542fce74c768718f594d0debd0da) 2019-11-11 22:44:56 +00:00
llvm-clang-tblgen Update LLVM to 10.0.0git (01f3a59fb3e2542fce74c768718f594d0debd0da) 2019-11-11 22:44:56 +00:00
llvm-include
llvm-lib Fix LLVM build with host gcc 13 2023-05-30 19:05:04 +00:00
llvm-tblgen Update LLVM to 10.0.0git (01f3a59fb3e2542fce74c768718f594d0debd0da) 2019-11-11 22:44:56 +00:00
lorder Create a new bsd.hostinit.mk file and put the build definitions for all host 2018-05-01 19:59:42 +00:00
m4
m68k-elf2aout s/hidding/hiding/ 2022-05-28 22:00:55 +00:00
macppc-fixcoff
make tools/make: regen 2023-07-20 15:20:00 +00:00
makefs
makekeys Centralize NOMAN handling. 2018-05-01 12:38:39 +00:00
makestrs Support building the xorg X11 libraries under MKCOMPAT. 2015-06-24 22:20:24 +00:00
makewhatis
mandoc Expose re{c,}allocarray decls in current. 2018-08-15 14:31:22 +00:00
mdsetimage Create a new bsd.hostinit.mk file and put the build definitions for all host 2018-05-01 19:59:42 +00:00
menuc
mips-elf2ecoff refresh 2017-02-24 03:44:54 +00:00
mkcsmapper
mkdep
mkesdb
mklocale
mknod
mktemp
mkubootimage
mpc
mpfr
msgc
mtree
mvme68k-wrtvid PR port-mvme68k/56146 2021-05-06 13:23:36 +00:00
nbperf
pax
paxctl
pcc Disable stripping pcc tool. 2016-03-30 23:36:45 +00:00
pigz
pkg_install don't try to include the library if we are building it. 2020-01-17 16:50:51 +00:00
powerpc-mkbootimage
pwd_mkdb
rpcgen Update tools/compat/configure for new path of "rpc/types.h". 2019-06-22 13:42:53 +00:00
sed
sgivol
slc
sortinfo Add sortinfo 2015-12-18 18:56:33 +00:00
sparkcrc
stat
strfile
sunlabel Patch the build script and toolchain to allow passing through 2021-09-18 01:47:07 +00:00
texinfo
tic tools: Build tic with full fat terminfo 2020-04-05 12:31:39 +00:00
tsort
uudecode Patch the build script and toolchain to allow passing through 2021-09-18 01:47:07 +00:00
veriexecgen
vgrind Patch the build script and toolchain to allow passing through 2021-09-18 01:47:07 +00:00
xz-bin Prepend ${_TOOL_PREFIX} to xz, too. 2018-11-19 20:00:28 +00:00
xz-include tools: use NONLS= not MKNLS=no 2023-06-02 20:47:27 +00:00
xz-lib Update build glue for xz-5.2.4. Hook up tools version. 2018-09-25 05:42:08 +00:00
yacc
zic don't require gettext 2022-10-29 13:53:35 +00:00
Makefile hook gzip to the build so that we can test it in other OS's. Not used yet. 2024-04-01 02:22:38 +00:00
Makefile.gmakehost
Makefile.gnuhost Fix passing -j NNN to gmake 2023-05-19 10:42:34 +00:00
Makefile.gnuwrap
Makefile.host tools: revert use of <bsd.hostinit.mk> 2023-06-04 16:53:59 +00:00
Makefile.nbincludes revert previous, riastradh fixed it correctly and this is not needed. 2024-04-01 02:18:30 +00:00
README tools: allow tools to use C99 2022-02-03 20:32:38 +00:00
headerlist Add virt68k headers. 2024-01-03 03:18:31 +00:00
mkheaderlist.sh Sort the output 2016-02-23 11:43:04 +00:00

README

$NetBSD: README,v 1.5 2022/02/03 20:32:38 rillig Exp $

Notes for NetBSD src/tools


Background
==========

Several programs that are part of NetBSD are also built as tools.  Such
programs are typically built twice: once as a tool and once as part of
the release build.  Tools are relevant only when the make(1) variable
USETOOLS=yes, which is the default for most NetBSD builds.

Tools are built on the host platform, using the host compiler,
and will run on the host platform during the cross-build of the
remainder of NetBSD.  They are built near the beginning of a NetBSD
build (e.g. "build.sh tools" or "make tools" from the top level src
directory), and installed in ${TOOLDIR}.

Tools are executed during the main part of the build, when several
TOOL_* variables defined in src/share/mk/bsd.*.mk will refer to the
tools installed in ${TOOLDIR}.


Portability
===========

Programs that are built as tools need to be more portable than other
parts of NetBSD, because they will need to run on the host platform.

Most tools should restrict themselves to C language features that are
defined in C99 (ISO/IEC 9899-1999); they should avoid using C11 language
features, such as <threads.h>, _Alignof, <uchar.h>, _Generic,
static_assert, anonymous structures and unions.

Tools may use library features such as functions, macros, and types,
that are defined in C99 and in POSIX (IEEE Std 1003.1) (XXX year?), and
features that are provided by the compatibility framework
(src/tools/compat) described in a separate section below.

If a tool attempts to use a feature that is not available on the host
platform, then the tools build will fail.  This can be addressed by
changing the tool to avoid that feature, or by adding the feature to the
src/tools/compat framework.  It is usually easy to add new macros or
functions to src/tools/compat, and that is usually better than adding
compatibility definitions to individual tools.


Compatibility framework
=======================

src/tools/compat provides a compatibility framework for use by tools.
It installs the following components, and more:

${TOOLDIR}/lib/libnbcompat.a

    A library containing functions that are needed by some tools.

${TOOLDIR}/include/nbtool_compat.h

    A header file defining macros that are needed by some tools.

${TOOLDIR}/share/compat/defs.mk

    A makefile fragment, to be included by other makefiles,
    to define make variables appropriate for building tools.

    Among other things, this makefile fragment automatically adds
    the libnbcompat.a library to the LDADD and DPADD variables,
    so that tools will be linked with that library, and adds
    -I${NETBSDSRCDIR}/tools/compat and -DHAVE_NBTOOL_CONFIG_H=1 to the
    HOST_CPPFLAGS variable, so that compiled programs can detect when
    they are being built as tools.


Adapting Makefiles for use with tools
=====================================

Makefiles under src/tools/*/Makefile should define the HOSTPROG
variable.  This is typically done by tools/Makefile.hostprog,
which is directly or indirectly included by all Makefiles in
src/tools/*/Makefile.

Makefiles in the non-tools part of the src tree can test whether or not
the HOSTPROG variable is defined, in order tell the difference between
building a tool and building part of a NetBSD release, and they may
alter their behavior accordingly.

For example, the Makefile may conditionally refrain from compiling and
linking certain files, and the Makefile may conditionally pass macros to
the compiler via constructs like this:

    .if defined(HOSTPROG)
    CPPFLAGS+= -DWITH_FEATURE_X=0 # exclude feature X from tools build
    .else
    CPPFLAGS+= -DWITH_FEATURE_X=1 # include feature X in release build
    .endif

Adapting Programs for use with tools
====================================

When a tool is being built, the C compiler should automatically be
invoked with -DHAVE_NBTOOL_CONFIG_H=1.  This is done as a result of
settings in ${TOOLDIR}/share/compat/defs.mk, which should be included
from src/tools/Makefile.host, which should be included directly or
indirectly from src/tools/*/Makefile.

A C source file can test whether the HAVE_NBTOOL_CONFIG_H macro is
defined, in order to tell whether or not it is being compiled as part of
a tool.

In order to obtain the definitions provided by the tools compatibility
framework, almost every C source file that is built as part of a tool
should have lines like these as the first non-comment lines:

    #if HAVE_NBTOOL_CONFIG_H
    #include "nbtool_config.h"
    #endif

To omit features from the tools version of a program, the program
may test the HAVE_NBTOOL_CONFIG_H macro, like this:

    #if HAVE_NBTOOL_CONFIG_H
       ... code to be used when built as a tool
    #else
       ... code to be used when built as part of a release
    #endif

It is often preferable to use macros whose names refer to the features
that should be included or omitted.  See the section on "Adapting
Makefiles for use with tools" for an example in which the Makefile
passes -DWITH_FEATURE_X=0 or -DWITH_FEATURE_X=1 to the compiler
according to whether or not the program is being built as a tool.  Then
the program can use code like this:

    #if WITH_FEATURE_X 
       ... code to be used when FEATURE X is desired,
       ... e.g. when being built as part of a release.
    #else
       ... code to be used when FEATURE X is not desired,
       ... e.g. when being built as a tool.
    #endif