Search code examples
makefilegnu-make

gnu make hash symbol in the middle of string


I'm trying to print out the perforce file version of the make file when it's executed. I'm using the $Id$ tag, which expands to $Id: //repository/path/check.make#6 $ or the like and I want to print //repository/path/check.make#6 to a file (currently using echo). I can't figure out how to get make to take the # as part of the string and not the beginning of a comment. I tried:

str1 = $(subst \#,\\\#,'$Id: //repository/path/check.make#6 $')

and other variations but I keep getting:

 unterminated call to function `subst': missing `)'.  Stop.

Solution

  • You can't have an unescaped literal # in a make assignment and not have it be interpreted as a comment character. But as a hack, you can have the shell extract this token from the current Makefile.

    # $Id: //repository/path/check.make#6 $
    str1 := $(shell sed '/[$$]Id[$$:]/!d;s/^\# [$$]Id: \(.*\) [$$].*/\1/' Makefile)
    

    The sed script looks for the $Id$ or $Id: token in the Makefile itself by way of a regex which doesn't match itself; the doubled dollar sign is how you put a literal dollar sign in a Makefile. It extracts the contents of the field, and make assigns the output to str1. Because there is no literal # in the code which assigns the variable, no further escaping is necessary.

    Demo: https://ideone.com/hWjnCp

    This requires GNU Make, but that's apparently what you are using already. (Please tag such questions explicitly as .)