Solemn's Site
Buggy software since 200X
Capturing errors from the $(shell ...) function

Posted in programming on 07 Oct 2019 at 18:48 UTC

The GNU Make shell function executes a shell command, expanding to whatever it wrote to standard output. If the command fails, thats just fiiine - however much it did or didn't output is what you get.

command_output := $(shell command)

I use the shell function for (among other things) getting compiler flags necessary for using libraries (e.g. from pkg-config), and Make's behaviour of ignoring these errors is rather annoying, since it can mask the actual error with pages of output before getting to a build command that actually fails. Sometimes it can even mess up the compiler flags to the point you (or at least I) spend ages hunting down a phantom problem that doesn't exist.

Weirdly, I couldn't find any good solution on the web, with a bit of experimentation I wound up developing the following macro:

# Wrapper around the $(shell) function that aborts the build if the command
# exits with a nonzero status.
shell-or-die = \
	$(eval sod_out := $(shell $(1); echo $$?)) \
	$(if $(filter 0,$(lastword $(sod_out))), \
		$(wordlist 1, $(shell echo $$(($(words $(sod_out)) - 1))), $(sod_out)), \
		$(error $(1) exited with status $(lastword $(sod_out))))

WX_CXXFLAGS := $(call shell-or-die,wx-config --cxxflags base core aui propgrid adv)
WX_LIBS     := $(call shell-or-die,wx-config --libs     base core aui propgrid adv)

If you don't mind depending on GNU Make 4.2 (a bit too new for my tastes), if can be simplified to:

shell-or-die = \
	$(eval sod_out := $(shell $(1))) \
	$(if $(filter 0,$(.SHELLSTATUS)), \
		$(sod_out), \
		$(error $(1) exited with status $(.SHELLSTATUS)))

Comments

No comments have been posted

Comments are currently disabled