[Toybox] scripts/prereq/build.sh

Rob Landley rob at landley.net
Fri Apr 5 05:13:28 PDT 2024


I recently added scripts/prereq/build.sh which runs a "cc -I dir *.c" style
build against canned headers. Theoretically a portable build not requiring a
system to have any command line utilities except "cc" and a shell. (Ok, you
still need bash to run scripts/make.sh and scripts/install.sh until toysh is
promoted. And until I replace kconfig, you still need gmake to run "make
defconfig", but I've got a design for that one now.)

Both that build.sh script and the saved scripts/prereq/generated headers are
created by scripts/recreate-prereq.sh which figures out what commands a toybox
build uses out of the $PATH (by doing a defconfig build under
mkroot/record-commands.sh), makes a .config file with just those commands
enabled and all dependencies switched off (and hardwires the two not-android
not-mmu symbols that get compiler probed), then strips down the resulting
headers to have just the symbols those commands need. (Well, I haven't stripped
down config.h yet but all the OTHERS are hit with sed/grep to remove stuff for
the commands that aren't enabled.)

Of course when I ran it on macos it went "boing":

toys/other/taskset.c:52:17: error: use of undeclared identifier
'__NR_sched_getaffinity'
toys/other/taskset.c:81:15: error: use of undeclared identifier
'__NR_sched_setaffinity'
toys/other/taskset.c:119:29: error: use of undeclared identifier
'__NR_sched_getaffinity'
3 warnings and 3 errors generated.

It's trying to build nproc, which scripts/make.sh uses out of the $PATH to query
available processors. And yes, nproc calls sched_getaffinity() on linux (even
the debian one, according to strace) which isn't really portable...

In theory, I've got some workaround code for nproc being unavailable in
scripts/portability.sh already:

# Probe number of available processors, and add one.
: ${CPUS:=$(($(nproc 2>/dev/null || sysctl -n hw.ncpu 2>/dev/null)+1))}

I'm uncomfortable leaning in to "linux else bsd/mac" because I was also thinking
about stuff like qnx and vxworks and so on with the new "canned" build, but if
all the probes fail that becomes CPUS=$((+1)) and thus sets it to 1, which
should still work if I filter out nproc and sysctl isn't there either?

But I'd also like to build nproc for other targets if I could. Which sounds like
it turns into a portability.c mess pretty quickly...

Rob


More information about the Toybox mailing list