Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to keep an extra space from breaking this Makefile?

I am accustomed to GNU make ignoring extra whitespace within variables, so I was surprised by the following.

## Makefile ##
PKGS = FOO BAR

FOO_DIR = foo
BAR_DIR = bar 
#            ^-------- Extra space at end of line

include $(foreach pkg, $(PKGS), $($(pkg)_DIR)/comp.mk)

default:
    @echo "Hello world!"

If there is a space after BAR_DIR = bar, make fails with this error:

'make: *** bar: Is a directory.  Stop.'

I think I understand what's happening here - there is a space in the include file path so make thinks I want to include a directory, hence the error. If the space is removed, and files foo/comp.mk and bar/comp.mk exist, make will run without error.

My question is, is there some way to protect against an extra space causing this failure?

like image 568
dwikle Avatar asked Dec 29 '25 14:12

dwikle


1 Answers

You can use strip to protect against that:

include $(foreach pkg, $(PKGS), $(strip $($(pkg)_DIR))/comp.mk)

For a similar reason, it's often wise to avoid spaces after commas when passing arguments to functions, including foreach.

like image 56
Idelic Avatar answered Jan 03 '26 11:01

Idelic



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!