Compile on Mac M1 [SOLVED]

Anyone has been successful in compiling Ardupilot on a Mac on M1 apple silicon?

I tried following the setup on the wiki but I get an error very soon:

Error: Invalid formula: /opt/homebrew/Library/Taps/ardupilot/homebrew-px4/px4-dev.rb
px4-dev: Unsupported special dependency :java
Error: Invalid formula: /opt/homebrew/Library/Taps/ardupilot/homebrew-px4/kconfig-frontends.rb
kconfig-frontends: Invalid bottle tag symbol
Error: Invalid formula: /opt/homebrew/Library/Taps/ardupilot/homebrew-px4/px4-sim.rb
px4-sim: Unsupported special dependency :x11
Error: Invalid formula: /opt/homebrew/Library/Taps/ardupilot/homebrew-px4/fastrtps.rb
fastrtps: Unsupported special dependency :java
Error: Cannot tap ardupilot/px4: invalid syntax in tap!

Those look like PX4 firmware error messages, not ArduCopter. We do not (yet) have fastrtps support.

So what is this?

https://ardupilot.org/dev/docs/building-setup-mac.html

Hey genius, did you think I followed a px4 tutorial than I came here to ask why it didn’t work?

I tried this time ago and got a successful compilation for SITL (I don’t remember if with Big Sur or Monterey); however I had a Python problem with Mavproxy, so usage was limited. Later I found a workaround for it, but trying again:

[881/881] Linking build/sitl/bin/arducopter
ld: warning: pointer not aligned at address 0x100227A9B (__ZN6Copter13log_structureE + 7043 from ArduCopter/Log.cpp.45.o)
… (many similar lines) …
ld: warning: pointer not aligned at address 0x100225F1A (__ZN6Copter13log_structureE + 2 from ArduCopter/Log.cpp.45.o)
ld: unaligned pointer(s) for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Waf: Leaving directory `/Users/…/ardupilot/build/sitl’
Build failed

Compilers:

Setting board to : sitl
Using toolchain : native
Checking for ‘g++’ (C++ compiler) : not found
Checking for ‘clang++’ (C++ compiler) : /opt/homebrew/opt/ccache/libexec/clang++
Checking for ‘gcc’ (C compiler) : not found
Checking for ‘clang’ (C compiler) : /opt/homebrew/opt/ccache/libexec/clang
Checking for c flags ‘-MMD’ : yes
Checking for cxx flags ‘-MMD’ : yes
CXX Compiler : clang++ 13.1.6

So I left it for better times or looking what happens at ArduCopter/Log.cpp.45.

Anyhow, for your case, with gcc-arm-none-eabi-10.3-2021.10-mac.pkg installed (which I think it is not the standard compiler to use) I tried a single board:
./waf configure --board Pixhawk1
./waf copter
showing:
Autoconfiguration : enabled
Setting board to : Pixhawk1
Using toolchain : arm-none-eabi
Checking for ‘g++’ (C++ compiler) : /Applications/ARM/bin/arm-none-eabi-g++
Checking for ‘gcc’ (C compiler) : /Applications/ARM/bin/arm-none-eabi-gcc
Checking for c flags ‘-MMD’ : yes
Checking for cxx flags ‘-MMD’ : yes
CXX Compiler : g++ 10.3.1
Checking for program ‘make’ : /usr/bin/make
and leaving in build/Pixhawk1/bin:
$ file build/Pixhawk1/bin/*
build/Pixhawk1/bin/arducopter: ELF 32-bit LSB executable, ARM, EABI5 version 1 (SYSV), statically linked, with debug_info, not stripped
build/Pixhawk1/bin/arducopter.apj: ASCII text, with very long lines (65436)
build/Pixhawk1/bin/arducopter.bin: TTComp archive data, binary, 4K dictionary
build/Pixhawk1/bin/arducopter.hex: ASCII text
build/Pixhawk1/bin/arducopter_with_bl.hex: ASCII text
after compiling and linking 828 files in only 1m40.601s (M1).

Thank you.
At the moment my workaround is ubuntu on parallels, it compiles fine.

Corrado

p.s. still don’t understand what is the wiki talking about. Maybe @rmackay9 can take a look:

https://ardupilot.org/dev/docs/building-setup-mac.html

About that, apart from the compiler and mavproxy (at /opt/homebrew/bin/mavproxy.py) this is what I have installed on the M1:
$ brew list|grep px4
$ brew list|grep genromfs
$ brew list|grep gawk
gawk
$ pip list|grep pyserial
pyserial 3.5
$ pip list|grep future
future 0.18.2
$ pip list|grep empy
empy 3.3.4
$ ls -l /Library/Developer/CommandLineTools/SDKs
total 0
lrwxr-xr-x 1 root wheel 14 4 may 2021 MacOSX.sdk → MacOSX11.3.sdk
drwxr-xr-x 8 root wheel 256 4 may 2021 MacOSX10.15.sdk
drwxr-xr-x 7 root wheel 224 30 nov 2020 MacOSX11.1.sdk
drwxr-xr-x 7 root wheel 224 16 mar 2021 MacOSX11.3.sdk
lrwxr-xr-x 1 root wheel 14 4 may 2021 MacOSX11.sdk → MacOSX11.3.sdk
so yes, that page needs a look, and also it would be good that sitl version would compile on the M1 (M1 code).

So uninstall what you installed following that page and try, and install (brew+pip) what you see missing.

I get the following error at the very first line of components install:

brew tap ardupilot/homebrew-px4
==> Tapping ardupilot/px4
Cloning into ‘/opt/homebrew/Library/Taps/ardupilot/homebrew-px4’…
remote: Enumerating objects: 242, done.
remote: Total 242 (delta 0), reused 0 (delta 0), pack-reused 242
Receiving objects: 100% (242/242), 305.35 KiB | 2.52 MiB/s, done.
Resolving deltas: 100% (137/137), done.
Error: Invalid formula: /opt/homebrew/Library/Taps/ardupilot/homebrew-px4/px4-dev.rb
px4-dev: Unsupported special dependency :java
Error: Invalid formula: /opt/homebrew/Library/Taps/ardupilot/homebrew-px4/kconfig-frontends.rb
kconfig-frontends: Invalid bottle tag symbol
Error: Invalid formula: /opt/homebrew/Library/Taps/ardupilot/homebrew-px4/px4-sim.rb
px4-sim: Unsupported special dependency :x11
Error: Invalid formula: /opt/homebrew/Library/Taps/ardupilot/homebrew-px4/fastrtps.rb
fastrtps: Unsupported special dependency :java
Error: Cannot tap ardupilot/px4: invalid syntax in tap!

I have everything you have. When I go into /ardupilot and do:

./waf configure --board CubeBlack

i get:
env: python: No such file or directory

Install brew and python.

Have both installed

Corrado

That cannot happen. Check your installation. Do basic checks:
$ which python
$ locate python
$ python → it should show something similar to:
$ python
Python 3.9.10 (main, Jan 15 2022, 11:48:04)
[Clang 13.0.0 (clang-1300.0.29.3)] on darwin
Type “help”, “copyright”, “credits” or “license” for more information.
>>>
(or perhaps 3.10)
Test any python script with first line:
#!/usr/bin/env python
Check not to use python2.

The problem I think arises from the fact that to have python refer to python3 I made an alias in .zprofile and in .zshrc

alias python=‘python3.9’

I think the script doesn’t get the alias when started.

% which python
python: aliased to python3.9

Python 3.9 was installed using brew:

which python3
/opt/homebrew/bin/python3

linked python to python 3 and now it starts. Now working on next error:

Could not find the program [‘arm-none-eabi-ar’]

SUCCESS!!!

Built CubeBlack and CubeOrange!!

Had to install:

p.s. Installed MAVProxy too.

Just one problem left, but maybe is due to Apple silicon, during sitl compile:

ld: warning: pointer not aligned at address 0x100227A9B

I get hundreds of misaligned pointers.

Looks like we are at same point now. Misaligned pointer.

Thanks for all your Help.

The problem is in this line of code:
logger.Init(log_structure, ARRAY_SIZE(log_structure));
in each of the Log.cpp for each SITL model.

So a simple dirty workaround would be to modify the beginning of the file as follows
//#if LOGGING_ENABLED == ENABLED
#if 0
so that every routine is compiled as {}.

Of course, the best is to accomodate log_structure to M1 arquitecture.

We would only miss the logging capability in sitl you think?

Possibly. At the beginning:
// Code to Write and Read packets from AP_Logger log memory
// Code to interact with the user to dump or erase logs

I’ll try shortly a simulation and see what happens, but the best is having log_structure adapted for M1 arquitecture.

At ardupilot/libraries/AP_Logger/LogStructure.h:
#define LOG_PACKET_HEADER_LEN 3 // bytes required for LOG_PACKET_HEADER
// once the logging code is all converted we will remove these from
// this header

So once the logging code is all converted…

I changed ardupilot/libraries/AP_Logger/LogStructure.h deleting PACKED:
// structure used to define logging format
struct LogStructure {

It links with original Log.cpp. Replay: I’ll try shortly.