Search code examples
unixmakefilegnu-make

Escaping colons in filenames in a Makefile


Is there a way to get GNU make to work correctly with filenames that contain colons?

The specific problem I'm running into happens to involve a pattern rule. Here's a simplified version that does not depend on cutting and pasting tab characters:

% make --version
GNU Make 3.81
Copyright (C) 2006  Free Software Foundation, Inc.
This is free software; see the source for copying conditions.
There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE.

This program built for x86_64-redhat-linux-gnu
% cat Makefile
COLON := \:
all: ; true
%.bar: ; cp $< $@
x.bar: x.foo
%.foo: ; touch $@
a$(COLON)b.bar: a$(COLON)b.foo
all: x.bar a$(COLON)b.bar
clean: ; rm -f *.foo *.bar
% make clean
rm -f *.foo *.bar
% make
touch x.foo
cp x.foo x.bar
cp  a\:b.bar
cp: missing destination file operand after `a:b.bar'
Try `cp --help' for more information.
make: *** [a\:b.bar] Error 1

Replacing $(COLON) with a literal : produces exactly the same result. Without the backslash, it does this:

Makefile:6: *** target pattern contains no `%'.  Stop.

Solution

  • The answers here all seemed too complex to be helpful. I finally found a solution here:

    colon := :
    $(colon) := :
    

    and then used the macro in the filename as:

    filename$(:)
    

    which successfully translated to "filename:" upon evaluation.