Local build error on os x / homebrew: stat: invalid option -- '%'

Posting for posterity because I didn’t find anything in a search:

If you’re running particle workbench, you might see this error with a local build:

stat: invalid option -- '%' Try 'stat --help' for more information.

The reason is make is executing the following:

head -c $((stat -f%z …/…/…/build/target/system-part1/platform-8-m/system-part1.bin.pre_crc - 38)) ../../../build/target/system-part1/platform-8-m/system-part1.bin.pre_crc > ../../../build/target/system-part1/platform-8-m/system-part1.bin.no_crc

The reason it is executing stat -f%z is because of the module.mk directive that sets it looks for windows and linux, then assumes vanilla bsd/osx without GNU tools /homebrew installed.

This command does work with /usr/sbin/stat on osx machines. But if you’ve got homebrew with coreutils, your stat may be the GNU version in /opt/homebrew/opt/coreutils/libexec/gnubin/stat

The fix is either to expand the full path of stat like so:

filesize=/usr/sbin/stat -f%z $1`

Or you can simply remove that entire ifeq block so filesize always = stat -c %s $1

I imagine this would also cause problems building on Cygwin for the same reason. One could do something like:

strings which stat | grep gnu and if that exits 0, assume gnu stat regardless of OS since that’s what really matters.

Hope this helps someone. (Probably me, later.)