Wednesday, April 8, 2015

GNU Make Shellquote

Sometimes you may have a filename that contains quotes & your usual makefile routines breaks. For example, if you generate

index.Ukrayins'ka.html from index.Ukrayins'

& add index.Ukrayins'ka.html to clean variable, this classic pattern won't work anymore:

.PHONY: clean
      rm -rf $(clean)

because your shell will complain about a quote mismatch.

So you need to 'shellquote' a variable clean.

We can write a parameterized function in make that transforms 1 word into a safe shell-quoted string:

clean.shellquote = '$(subst ','\'',$(1))'

The Make manual has a nice example of a map function. That's all we need: we transform each word from clean variable w/ the map function that calls our clean.shellquote routine.

The complete example: = $(foreach a,$(2),$(call $(1),$(a)))
clean.shellquote = '$(subst ','\'',$(1))'
# '# emacs font-lock

.PHONY: clean
      rm -rf $(call,clean.shellquote,$(clean))

No comments:

Post a Comment