| Language | Header extension | Default header filename | |
| C | C | h | build_number.h |
| C++ | CXX | hpp | build_number.hpp |
add_custom_command(
TARGET project_name_goes_here
PRE_BUILD
COMMAND ${CMAKE_COMMAND}
-DLANGUAGE:STRING="C"
-DHEADER_DIR:PATH="${CMAKE_SOURCE_DIR}"
-DCACHE_DIR:PATH="${CMAKE_CURRENT_BINARY_DIR}${CMAKE_FILES_DIRECTORY}"
-P "${MASTER_DIR}/build_number.cmake"
)
Add build_number.h to your target. And create a blank build_number.h in your source directory otherwise CMake wont configure.
Include the generated header in your source:
#include "build_number.h"Use the BUILD_NUMBER macro in your code.
| Name | Type | Required | Default value | Description |
| LANGUAGE | STRING | Yes | - | Defines the used language. (This information is used to determine the header's extension.) |
| HEADER_DIR | PATH | Yes | - | Specifies a path for the header. |
| HEADER_FILENAME | STRING | No | buildnumber | Alters the default filename for the header. The extension depends on the determined language. No extension needed here. |
| HEADER_MACRO | STRING | No | BUILD_NUMBER | Alters the default macro's name in the header. |
| CACHE_DIR | PATH | Yes | - | Specifies the path for the cache file. |
| CACHE_FILENAME | STRING | No | CMakeBuildNumber.txt | Alters the default filename for the cache file. |
You can define them by adding more parameters to the command. For more information check the documentation of add_custom_command. The following is an example where all of the possible parameters are defined/altered:
add_custom_command(
TARGET project_name_goes_here
PRE_BUILD
COMMAND ${CMAKE_COMMAND}
-DLANGUAGE:STRING="CXX"
-DHEADER_DIR:PATH="${CMAKE_SOURCE_DIR}/Build"
-DCACHE_DIR:PATH="${CMAKE_CURRENT_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/Caches"
-DHEADER_FILENAME:STRING="BuildNo"
-DCACHE_FILENAME:STRING="BuildNo.cache"
-DHEADER_MACRO:STRING="BUILDNO"
-P "${CMAKE_SOURCE_DIR}/build_number.cmake"
)
MIT license