I have a makefile used by GNU Make 3.81 running on windows and with each build I pass to the compiler a define giving a version number. The version number is obtained from a file and incremented at the start of the makefile using a perl script with the -get option. The resulting program can then display the build version. The trouble I have is that the version number is updated even if the build fails. But this is fixed by running the perl script again, with the -dec option, to decrement the version number if an error occurs. Simplified contents of the makefile:
objects:= pov1.obj
ouf:= pov1
versionFile:= povVersion.txt
VERSION:=$(shell perl ../../../perl/uscripts/bump.pl -get -f $(versionFile))
CXX:= cl
CXXFLAGS:= -c -EHsc -std:c++17 -D$(addprefix VER=,\"$(VERSION)\")
target:=$(addsuffix .exe, $(ouf))
%.obj : %.cpp
$(CXX) $(CXXFLAGS) $< -Fo$@ || perl ../../../perl/uscripts/bump.pl -dec -f $(versionFile)
$(target) : $(objects)
$(LINKER) $(LDFLAGS) $(objects) /OUT:$(target) || perl ../../../perl/uscripts/bump.pl -dec -f $(versionFile)
Although this seems to work if I accidently run the make when up-to-date the version number is updated despite no build taking place. How can I run the perl script decrement in the event that the make detects up-to-date or, make the first run of the perl script dependent on an update being required?
Maybe there is a better way to get build version numbers into the program?
Edit to reflect answer given by dash-o, my simplified make file now contains this:
objects:= pov1.obj
ouf:= pov1
versionFile:= povVersion.txt
VERSION:=$(shell perl ../../../perl/uscripts/bump.pl -inc -dry -f $(versionFile)) #versionFile is not updated until link actually completes
CXX:= cl
CXXFLAGS:= -c -EHsc -std:c++17 -D$(addprefix VER=,\"$(VERSION)\")
target:=$(addsuffix .exe, $(ouf))
%.obj : %.cpp
$(CXX) $(CXXFLAGS) $< -Fo$@
$(target) : $(objects)
$(LINKER) $(LDFLAGS) $(objects) /OUT:$(target)
perl ../../../perl/uscripts/bump.pl -inc -f $(versionFile) #update versionFile here
In my C++ main file pov1.cpp I have
#ifdef VER
std::string version{VER};
#else
std::string version{"version not set"};
#endif
which allows access in the program to the version number. The perl script is here
The current solution bumps the version number (stored in the file) at the start of the build, and rolling it back on failure. However, this may fail in variety of situation, when the scripts does not reach the link time.
As an alternative, consider a different solution:
# The '-get' should return N+1, and should NOT update the file
VERSION:=$(shell perl ../../../perl/uscripts/bump.pl -get -f $(versionFile))
$(target) : $(objects)
$(LINKER) $(LDFLAGS) $(objects) /OUT:$(target)
# Commit the new version number, only if build is success
echo $(VERSION) > $(versionFile)