Where to write version number only once

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?

1 Like

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.

1 Like

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

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


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.

1 Like

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?

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.

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

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.

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.