The simplest solution is to use $\<newline>
to split the line. Using the
following fake paths for brevity:
CP = one$\
two$\
three$\
four
all:
echo $(CP)
The output will be "onetwothreefour" with no spaces. This is because GNU Make
will replace backslash-newline-whitespace with a single space, making the
assignment to CP
be equivalent to:
CP = one$ two$ three$ four
From
https://www.gnu.org/software/make/manual/html_node/Reference.html#Reference: "A
dollar sign followed by a character other than a dollar sign, open-parenthesis
or open-brace treats that single character as the variable name."
So the $<space>
pairs are expansions of the variable whose name is a single
space character. Since this variable is not defined by default, it will expand
to the empty string.
Note that the variable CP
will still contain the $<space>
pairs until it
is expanded. Most of the time, this doesn't matter, but if your makefile
depends on using $(value CP)
to process the underlying (unexpanded) value,
the above technique may provide surprising results.
Also, the recently released GNU Make 4.3 now explicitly documents this
technique for splitting lines (https://www.gnu.org/software/make/manual/make.html#Splitting-Lines):
Splitting Without Adding Whitespace
If you need to split a line but do not want any whitespace added, you can utilize a subtle trick: replace your backslash/newline pairs with the three characters dollar sign/backslash/newline:
var := one$\
word
After make removes the backslash/newline and condenses the following line into a single space, this is equivalent to:
var := one$ word
Then make will perform variable expansion. The variable reference ‘$ ’ refers to a variable with the one-character name “ ” (space) which does not exist, and so expands to the empty string, giving a final assignment which is the equivalent of:
var := oneword
Another technique is to launder the value of CP
. This requires GNU Make
3.80 or above because it relies on $(value)
and $(eval)
. It makes
use of a few variables and functions for the laundering process.
First, define the variable empty
to be the empty string and the variables
space
and newline
to contain a literal space and newline, respectively:
empty :=
space := $(empty) $(empty)
define newline
endef
Next, use $(eval)
to programmatically create a recursively expanded variable
with a given value:
# Define recursively expanded variable $1 with value $2.
defineVar = $(eval define $1$(newline)$2$(newline)endef)
And define the function resubst
to repeated substitute one string with
another:
# Replace $1 with $2 in $3 until no more changes are made.
resubst = $\
$(if $(findstring $1,$3),$\
$(call resubst,$\
$1,$\
$2,$\
$(subst $1,$2,$3)),$\
$3)
These are sufficient to define functions two-dimensionally with arbitrary
newlines and indentation. The general method comprises three steps:
- Prepend a newline to the function body;
- Repeatedly replace newline-space pairs with newlines;
- Remove all newlines.
The function def
removes the newline/whitespace line
continuations from the $(value)
of a recursively expanded
variable defined via define
/endef
:
# $1 - name of function to redefine as a normalized single-line function.
def = $\
$(call defineVar,$\
$1,$\
$(subst $(newline),$\
$(empty),$\
$(call resubst,$\
$(newline)$(space),$\
$(newline),$\
$(newline)$(value $1))))
Now def
may be used to post-process a recursively expanded variable. For
example:
define CP
one
two
three
four
endef
$(call def,CP)
Now, $(value CP)
will return the desired onetwothreefour
.
The above is a distillation from my article "GNU Make line continuations":
http://drmikehenry.com/gnu-make-line-continuations/