3 ms·
A useful post. A pity that CMake's scripting language is so atrocious, but it's good to see it's capable of this kind of thing. Personally I tend to go the rou
by MaxBarraclough 7y ago
A useful post. A pity that CMake's scripting language is so atrocious, but it's good to see it's capable of this kind of thing.
Personally I tend to go the route wmu mentions in their comment: add a file named MyProject.h.in, to expose CMake variables in a generated header file. I use the C preprocessor's stringification [0] to transform version numbers into strings. My approach (really just CMake basics) doesn't expose any git specifics though - it's more basic than what the blog describes.
The MyProject.h.in file looks like:
#define MyProject_VERSION_MAJOR @MyProject_VERSION_MAJOR@
#define MyProject_VERSION_MINOR @MyProject_VERSION_MINOR@
#define EXPAND_AND_STRINGIFY(s) STRINGIFY(s)
#define STRINGIFY(s) #s
#define MyProject_VERSION_MAJOR_STR EXPAND_AND_STRINGIFY(MyProject_VERSION_MAJOR)
#define MyProject_VERSION_MINOR_STR EXPAND_AND_STRINGIFY(MyProject_VERSION_MINOR)
#define MyProject_VERSION_STR MyProject_VERSION_MAJOR_STR "." MyProject_VERSION_MINOR_STR
Getting CMake to process this file is simple enough: we just add configure_file(MyProjectConfig.h.in MyProjectConfig.h) to the CMakeLists.txt file.
[0] https://gcc.gnu.org/onlinedocs/gcc-4.8.5/cpp/Stringification.html https://gcc.gnu.org/onlinedocs/gcc-4.8.5/cpp/Stringification...