Where to write version number only once


#1

Ideally I would like to write the version number of my project only in one place, and have it accessed/propagated throughout the whole build process, on Mac and Windows.

By that, I mean that I want the version number available not only at the level of C++ code (for that, a simple #define would be enough), but also for the pre and post build scripts executed by Xcode and Visual Studio.

Any ideas?


#2

Perhaps Projucer --set-version or Projucer --get-version could help? I use the latter all the time in post build scripts to name installer & zip files. If I could somehow inject my version number into my Inno Setup project file on Windows, though, then I would localize my version string to a single file.


#3

For the current git commit sha, I use echo '"'$(git rev-parse HEAD)'"' > ${PROJECT_DIR}/GitCommitSha.h as an Xcode pre-build script so that I can inject it into my logging code. I’m guessing the same could be done for simple text file that contains the one and only version string as a single line. For Windows it’s a bit more wordy:

@echo off

del /q GitCommitSha.h
for /f %%i in ('git rev-parse HEAD') do set currSha=%%i
echo updating git commit sha header to %currSha%
echo "%currSha%" > GitCommitSha.h

#4

For our Inno Setup scripts we get the version number directly from the binaries so we don’t have to manually (or use a script to) update version numbers:
GetFileVersion http://www.jrsoftware.org/ispphelp/index.php?topic=getfileversion


#5

I’m not using Projucer so the --set-version and --get-version options are not a feasible solution.

I resorted to using a single xcconfig file on macOS and a single property sheet on Windows.
In both cases, the custom macros can both be used as preprocessor macros during compilation, and propagated as environment variables for the pre and post build scripts. Works for me.

@RustyPine, as for propagating your version number to InnoSetup, you could echo the version emitted by Projucer --get-version to a .h file, and then #include it in your InnoSetup script. InnoSetup offers a preprocessor which allows you to do that.


#6

Won’t this cause your files that include that header to be always recompiled, even if neither their code nor the current commit have changed?


#7

Yeah I think it causes 1 source file to be recompiled. Not an issue at the moment since it’s about an extra 2 seconds.


#8

hmm I guess I could maybe make it a git post-commit hook?


#9

I’m not using Projucer

If you don’t mind, could you tell us what you are using?
I am working on JUCE.cmake (https://github.com/McMartin/JUCE.cmake) so I am interested in any alternative to Projucer.


#10

I’m not using any meta-project generator/manager because this is an old project that was born before Projucer (then Introjucer) even existed, nor I have looked for alternatives to it honestly.