Python / Cmake utility to automatically update a project's version number based on the latest git tag
- Put
make-version-file.py
andupdate-version-info.py
in your project directory. - Insert
CMakeLists.snippet
into yourCMakeLists.txt
file. Modify it as necessary. - Create a template file named
<outputfile>.template
to define the format of your version info file. Seeversion.h.template
for example. - Define the version number of your project by making a git tag with either
git tag
or a GitHub release. The tag must be named something like "1.0" or "1.2.3" or "v1.2.3". - Build your project. The output file (e.g.
version.h
) will be created with definitions for the latest version info, which you can use in your code.
The output file (version.h
or whatever) will only be updated when necessary, i.e. when the git repository is modified. This ensures that your project files which use version.h
are only re-compiled when the version info changes, not every time you do a build.
See the example version.h.template
file for a list of the available version info parameters. Since this uses Python's formatted string syntax, curly braces "{}" are special and if you want a curly brace in your output file, it must be doubled in the template file "{{ }}".
If the latest commit to the git repo isn't explicitly tagged, a build number and commit hash will be generated from git describe to precisely identify the source code version. If there are un-committed changes in the repo at build time, verIsDevBuild
is set to 1 and "-dev" is appended to verString
.
If you end up with the version number 0.0.0.0, it's probably because the most recent git tag doesn't look like a version number.