$ cat $HOME/version.txt
version=1.2.3.4
$ cat hello.c
#include <stdio.h>
int main()
{
printf("Software Version = [%s]\n",VERSION);
return 0;
}
$ cat hello.mk
FILE=$(HOME)/version.txt
VERSION:=`cat $(FILE) | cut -d = -f2 | sed -e 's-^-\\\"-g' -e 's-$-\\\"-g'`
compile :
gcc -o hello hello.c -DVERSION=$(VERSION)
$ cat test.sh
FILE=$HOME/version.txt
# Append with \" at the beginning and end of the string for the compiler
cat $FILE | cut -d = -f2 | sed -e 's-^-\\\"-g' -e 's-$-\\\"-g'
OUTPUT of test.sh :
\"1.2.3.4\"
Problem :
I am trying to write a makefile (hello.mk) which reads a static file ($HOME/version.txt) which stores version of the software being developed in the form version=x.x.x.x. Makefile reads the file and extracts the version value and passes to the compile line through -D flag. In the 'C' code (hello.c), printing the VERSION detail. The idea is to just maintain the version file for various builds and the build will have the appropriate version number in various logs etc.,
I wrote a simple script test.sh to test the cut | sed option and it works fine from the command line. When I have them in the makefile as a MACRO, I get syntax error for the last $ sed option. Also, when I use some other hacks to get the values from the file, the compile line -DVERSION=$(VERSION) seem to expand to the macro VERSION commands rather than the actual VERSION value.
To test, in the makefile, if I replace VERSION to hardcode value like \"1.2.3.4\, then the compile goes through with desired effect.
The current format of the version file cannot be changed (legacy code and is used by many other tools).
Appreciate if you could help me to resolve this issue or any better way to parse the file.