Escaping colons in filenames in a Makefile

Viewed 14092

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.
6 Answers

I doubt it's possible: see this discussion about colons in Makefiles. In summary, GNU make has never worked well with filenames that contain whitespace or colons. The maintainer, Paul D. Smith, says that adding support for escaping would tend to break existing makefiles. Furthermore, adding such support would require significant changes to the code.

You might be able to work around with some sort of nasty temporary file arrangement.

Good luck!

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.

I am not positivie this should work, but the reason it says "missing destination file" is simple:

%.bar: ; cp $< $@

That line says to copy the target from the first dependency. your a:b.bar does not have any dependency, so the cp fails. what did you want it to copy ? a:b.foo ? in that case, you would need:

%.bar: %.foo ; cp $< $@

I could not get the answer posted by @navjotk to work, so I am just gonna cheat and do this;

FILENAME:=foo:bar
foo_bar:
    touch $(FILENAME)

run:
    if [ ! -e "$(FILENAME)" ]; then $(MAKE) foo_bar; fi

output:

$ make run
if [ ! -e "foo:bar" ]; then /Library/Developer/CommandLineTools/usr/bin/make foo_bar; fi
touch foo:bar

$ ls
Makefile foo:bar

Close enough for me.

Related