macOS Build Farm for Apache NuttX RTOS (Apple Silicon)

📝 8 Dec 2024

macOS Build Farm for Apache NuttX RTOS (Apple Silicon)

Folks on macOS: Compiling Apache NuttX RTOS used to be so tiresome. Not any more! run-build-macos.sh

## Build Anything on Apple Silicon macOS:
## Arm32, RISC-V and Xtensa!
git clone https://github.com/lupyuen/nuttx-build-farm
cd nuttx-build-farm
./run-build-macos.sh raspberrypi-pico:nsh
./run-build-macos.sh ox64:nsh
./run-build-macos.sh esp32s3-devkit:nsh

## NuttX Executable will be at
## /tmp/run-build-macos/nuttx

In this article, we explain…

GNU Coreutils and Binutils on PATH are also known to break build in MacOS

“GNU Coreutils and Binutils on PATH are also known to break build in MacOS”

§1 Fix the PATH!

Super Important! NuttX won’t build correctly on macOS unless we remove Homebrew ar from PATH: run-job-macos.sh

## Remove Homebrew `ar` from PATH
## Instead: We use `/usr/bin/ar`
## https://github.com/pyenv/pyenv/issues/2862#issuecomment-1849198741
export PATH=$(
  echo $PATH \
    | tr ':' '\n' \
    | grep -v "/opt/homebrew/opt/make/libexec/gnubin" \
    | grep -v "/opt/homebrew/opt/coreutils/libexec/gnubin" \
    | grep -v "/opt/homebrew/opt/binutils/bin" \
    | tr '\n' ':'
)
if [[ $(which ar) != "/usr/bin/ar" ]]; then
  echo "ERROR: Expected 'which ar' to return /usr/bin/ar, not $(which ar)"
  exit 1
fi

Thus we should always do the above before compiling NuttX. Otherwise we’ll see a conflict between the Homebrew and Clang Linkers

ld: archive member '/' not a mach-o file in 'libgp.a'
clang++: error: linker command failed with exit code 1 (use -v to see invocation)

Building raspberrypi-pico:nsh on macOS

§2 Build Anything on macOS

Earlier we talked about compiling Any NuttX Target on macOS: run-build-macos.sh

## Build Anything on Apple Silicon macOS:
## Arm32, RISC-V and Xtensa!
git clone https://github.com/lupyuen/nuttx-build-farm
cd nuttx-build-farm
./run-build-macos.sh raspberrypi-pico:nsh
./run-build-macos.sh ox64:nsh
./run-build-macos.sh esp32s3-devkit:nsh

## NuttX Executable will be at
## /tmp/run-build-macos/nuttx

## To re-download the GCC Toolchains
## rm -rf /tmp/run-build-macos

And it works on Apple Silicon! M1, M2, M3, M4, …

$ ./run-build-macos.sh raspberrypi-pico:nsh

Configuration/Tool: raspberrypi-pico/nsh,CONFIG_ARM_TOOLCHAIN_GNU_EABI
  Cleaning...
  Configuring...
  Disabling CONFIG_ARM_TOOLCHAIN_GNU_EABI
  Enabling CONFIG_ARM_TOOLCHAIN_GNU_EABI
  Building NuttX...
  Normalize raspberrypi-pico/nsh

Huh what about the GCC Toolchains? Arm32, RISC-V, Xtensa…

Toolchains are Auto-Downloaded. Thanks to the brilliant Continuous Integration Script by Simbit18!

Just make sure we’ve installed brew, neofetch and Xcode Command-Line Tools.

(Yep the same script drives our GitHub Daily Builds)

Toolchains are downloaded in 10 mins, subsequent builds are quicker

Toolchains are downloaded in 10 mins, subsequent builds are quicker

§3 Patch the CI Script

We’re running the NuttX CI Script on our computer. How does it work?

This is how we call tools/ci/cibuild.sh to Download the Toolchains and Compile our NuttX Target: run-build-macos.sh

## Let's download the NuttX Toolchains and Run a NuttX Build on macOS
## First we checkout the NuttX Repo and NuttX Apps...
tmp_dir=/tmp/run-build-macos
cd $tmp_dir
git clone https://github.com/apache/nuttx
git clone https://github.com/apache/nuttx-apps apps

## Then we patch the NuttX CI Script for Apple Silicon: darwin_arm64.sh
## Which will trigger an "uncommitted files" warning later
pushd nuttx
$script_dir/patch-ci-macos.sh  ## https://github.com/lupyuen/nuttx-build-farm/blob/main/patch-ci-macos.sh
popd

## Omitted: Suppress the uncommitted darwin_arm64.sh warning:
## We copy the patched "nuttx" folder to "nuttx-patched"
## Then restore the original "nuttx" folder
...

## NuttX CI Build expects this Target Format:
## /arm/rp2040/raspberrypi-pico/configs/nsh,CONFIG_ARM_TOOLCHAIN_GNU_EABI
## /risc-v/bl808/ox64/configs/nsh
## /xtensa/esp32s3/esp32s3-devkit/configs/nsh
target_file=$tmp_dir/target.dat
rm -f $target_file
echo "/arm/*/$board/configs/$config,CONFIG_ARM_TOOLCHAIN_GNU_EABI" >>$target_file
echo "/arm64/*/$board/configs/$config"  >>$target_file
echo "/risc-v/*/$board/configs/$config" >>$target_file
echo "/sim/*/$board/configs/$config"    >>$target_file
echo "/x86_64/*/$board/configs/$config" >>$target_file
echo "/xtensa/*/$board/configs/$config" >>$target_file

## Run the NuttX CI Build in "nuttx-patched"
pushd nuttx-patched/tools/ci
(
  ./cibuild.sh -i -c -A -R $target_file \
    || echo '***** BUILD FAILED'
)
popd

What is patch-ci-macos.sh?

To run NuttX CI Locally: We made Minor Tweaks. Somehow this Python Environment runs OK at GitHub Actions: darwin_arm64.sh

## Original Python Environment:
## Works OK for GitHub Actions
python_tools() { ...
  python3 \
    -m venv \
    --system-site-packages /opt/homebrew ...

But it doesn’t work locally. Hence we patch darwin_arm64.sh to Run Locally: patch-ci-macos.sh

## Modified Python Environment:
## For Local macOS
python_tools() {
  python3 -m venv .venv
  source .venv/bin/activate

Why the “uncommitted darwin_arm64.sh warning”?

Remember we just patched darwin_arm64.sh? NuttX CI is super picky about Modified Files, it will warn us because we changed darwin_arm64.sh.

Our Workaround: We copy the modified nuttx folder to nuttx-patched. Then we run NuttX CI from nuttx-patched folder: run-build-macos.sh

## Suppress the uncommitted darwin_arm64.sh warning:
## We copy the patched "nuttx" folder to "nuttx-patched"
## Then restore the original "nuttx" folder
cp -r nuttx nuttx-patched
pushd nuttx
git restore tools/ci
popd

## Patch the CI Job cibuild.sh to point to "nuttx-patched"
## Change: CIPLAT=${CIWORKSPACE}/nuttx/tools/ci/platforms
## To:     CIPLAT=${CIWORKSPACE}/nuttx-patched/tools/ci/platforms
file=nuttx-patched/tools/ci/cibuild.sh
tmp_file=$tmp_dir/cibuild.sh
search='\/nuttx\/tools\/'
replace='\/nuttx-patched\/tools\/'
cat $file \
  | sed "s/$search/$replace/g" \
  >$tmp_file
mv $tmp_file $file
chmod +x $file

## Run the NuttX CI Build in "nuttx-patched"
pushd nuttx-patched/tools/ci
./cibuild.sh -i -c -A -R $target_file
...

macOS won’t compile sim:nsh

§4 Except These Targets

Awesome! We can compile Everything NuttX on macOS Arm64?

Erm sorry not quite. These NuttX Targets won’t compile on macOS

GroupTargetTroubles
arm-05nrf5340-dk :
rpmsghci_nimble_cpuapp
ble_svc_gatt.c: rc set but not used
arm-07ucans32k146 :
se05x
mv: illegal option T
arm64-01imx93-evk :
bootloader
ld: library not found for -lcrt0.o
othermicropendous3 :
hello
avr-objcopy: Bad CPU type in executable
sim-01 to 03sim :
nsh
clang: invalid argument ‘medium’ to -mcmodel=
x86_64-01qemu-intel64 :
jumbo
arg_rex.c: setjmp.h: No such file or directory
xtensa-02esp32s3-devkit :
qemu_debug
xtensa_hostfs.c: SIMCALL_O_NONBLOCK undeclared
xtensa-02esp32s3-devkit :
knsh
sed: invalid command code .
Clang GroupsClang Targetsclang++: configuration file cannot be found
 

We’ll come back to this. First we talk about NuttX Build Farm…

macOS Build Farm

§5 macOS Build Farm

What’s this macOS Build Farm?

Earlier we compiled NuttX for One Single Target. Now we scale up and Compile All NuttX Targets… Non-Stop 24 by 7!

This becomes our Community-Hosted macOS Build Farm for NuttX.

(Why? So we can Catch Build Errors without depending on GitHub Actions)

If Your Mac has Spare CPU Cycles: Please join our macOS Build Farm! 🙏 Like so: run.sh

## Run the NuttX Build Farm for macOS.
## Set the GitHub Token: (Should have Gist Permission)
## export GITHUB_TOKEN=...
. $HOME/github-token.sh
brew install neofetch gh

## Run All NuttX CI Jobs on macOS
## Will repeat forever
git clone https://github.com/lupyuen/nuttx-build-farm
cd nuttx-build-farm
./run-ci-macos.sh

## To re-download the GCC Toolchains:
## rm -rf /tmp/run-job-macos

## For Testing:
## Run One Single NuttX CI Job on macOS
## ./run-job-macos.sh risc-v-01

And please tell me your Gist User ID. (Also works for GitLab Snippets)

The Build Outcomes will appear in NuttX Dashboard

macOS Build Farm for Apache NuttX RTOS (Apple Silicon)

How does it work?

macOS Build Farm shall run (nearly) All NuttX CI Jobs, forever and ever: run-ci-macos.sh

## Run All NuttX CI Jobs on macOS, forever and ever.
## Arm32 Jobs run hotter (80 deg C) than RISC-V Jobs (70 deg C).
## So we stagger the jobs.
## risc-v-05: CI Test may hang, we move to the end
for (( ; ; )); do
  for job in \
    arm-08 risc-v-06 \
    arm-09 xtensa-01 \
    arm-10 arm-11 arm-12 arm-13 arm-14 \
    arm-01 risc-v-01 \
    arm-02 risc-v-02 \
    arm-03 risc-v-03 \
    arm-04 risc-v-04 \
    arm-06 risc-v-05
  do
    ## Run the CI Job and find Errors / Warnings
    run_job $job
    clean_log
    find_messages

    ## Upload the log to GitLab Snippet or GitHub Gist
    upload_log $job $nuttx_hash $apps_hash
  done
done

## Run the NuttX CI Job (e.g. risc-v-01)
## Capture the output
function run_job {
  local job=$1
  pushd /tmp
  script $log_file \
    $script_option \
    $script_dir/run-job-macos.sh $job
  popd
}

(Some Target Groups won’t compile)

(clean_log removes Control Chars)

(find_messages searches for Errors)

(upload_log uploads to GitLab Snippet or GitHub Gist)

What’s inside run-job-macos.sh?

It will run one single NuttX CI Job. Similar to the NuttX Build Script we saw earlier: run-job-macos.sh

## Run one single NuttX CI Job on macOS (e.g. risc-v-01)
## Checkout the NuttX Repo and NuttX Apps
tmp_dir=/tmp/run-job-macos
cd $tmp_dir
git clone https://github.com/apache/nuttx
git clone https://github.com/apache/nuttx-apps apps

## Patch the macOS CI Job for Apple Silicon: darwin_arm64.sh
## Which will trigger an "uncommitted files" warning later
pushd nuttx
$script_dir/patch-ci-macos.sh  ## https://github.com/lupyuen/nuttx-build-farm/blob/main/patch-ci-macos.sh
popd

## Omitted: Suppress the uncommitted darwin_arm64.sh warning:
## We copy the patched "nuttx" folder to "nuttx-patched"
## Then restore the original "nuttx" folder
...

## Exclude clang Targets from macOS Build
## Because they will fail due to unknown arch
## "/arm/lpc54xx,CONFIG_ARM_TOOLCHAIN_CLANG"
## https://github.com/apache/nuttx/pull/14691#issuecomment-2466518544
tmp_file=$tmp_dir/rewrite-testlist.dat
for file in nuttx-patched/tools/ci/testlist/*.dat; do
  grep -v "CLANG" \
    $file \
    >$tmp_file
  mv $tmp_file $file
done

## If CI Test Hangs: Kill it after 1 hour
( sleep 3600 ; echo Killing pytest after timeout... ; pkill -f pytest )&

## Run the CI Job in "nuttx-patched"
## ./cibuild.sh -i -c -A -R testlist/risc-v-01.dat
pushd nuttx-patched/tools/ci
(
  ./cibuild.sh -i -c -A -R testlist/$job.dat \
    || echo '***** BUILD FAILED'
)
popd

Now we can cook some NuttX on macOS…

Mac Mini will get (nearly) Boiling Hot (90 deg C) when running the NuttX Build Farm

§6 Mac Gets Smokin’ Hot

Anything we should worry about?

Yeah Mac Mini will get (nearly) Boiling Hot (90°C) when running the NuttX Build Farm! All CPU Cores will be 100% Maxed Out. (M2 Pro, pic above)

I recommend TG Pro for Fan Control. Set the Fan Speed to Auto-Max. (Pic below)

Which will trigger the fans at 70°C (red bar below), keeping things cooler. (Compare the green bars with above)

Do you have a Mac Pro or M4 Pro? Please test the NuttX Build Farm! 🙏

(Xcode Benchmark suggests Your Mac might be twice as fast as my M2 Pro)

TG Pro will trigger the fans at 70 deg C

Is macOS Arm64 faster than Intel PC? For compiling NuttX Arm32?

Not really, Compiling Arm on Arm isn’t much faster. I still prefer Ubuntu PC for compiling NuttX, lemme explain…

Refurbished 12-Core Xeon ThinkStation ($400 / 24 kg!) becomes (hefty) Ubuntu Build Farm for Apache NuttX RTOS. 4 times the throughput of a PC!

Refurbished 12-Core Xeon ThinkStation ($400 / 24 kg!) becomes (hefty) Ubuntu Build Farm for Apache NuttX RTOS. 4 times the throughput of a PC!

§7 macOS Reconsidered

Is macOS good enough for NuttX Development?

If we’re Compiling NuttX for One Single Target: Arm32 / RISC-V / Xtensa… Yep sure!

But as NuttX Maintainer: I find it tough to reproduce All Possible NuttX Builds on macOS…

VSCode on macOS controlling a Refurbished Xeon Workstation for Ubuntu Docker Builds

(Watch the Video on YouTube)

Hopefully we’ll find a reliable way to compile sim:nsh on macOS…

## macOS Arm64 won't compile sim:nsh
$ git clone https://github.com/lupyuen/nuttx-build-farm
$ cd nuttx-build-farm
$ ./run-build-macos.sh sim:nsh
clang: error: invalid argument 'medium' to -mcmodel=

(See the Complete Log)

(It was Previously Working!)

Build Farm for Apache NuttX RTOS

§8 What’s Next

Next Article: We chat about a new tool to “Rewind The Build” when something breaks the Daily Build.

Then we study the internals of a Mystifying Bug that concerns PyTest, QEMU RISC-V and expect.

Many Thanks to the awesome NuttX Admins and NuttX Devs! And my GitHub Sponsors, for sticking with me all these years.

Got a question, comment or suggestion? Create an Issue or submit a Pull Request here…

lupyuen.github.io/src/ci5.md