I need to create the following always-out-of-date file: ${CMAKE_BINARY_DIR}/version.hpp.in
before any target is executed in the make file. The content of file is exactly as the following:
#define RELEASE_VERSION "${RELEASE}"
#define MAJOR_VERSION "${MAJOR}"
#define MINOR_VERSION "${MINOR}"
#define PATCH_VERSION "${PATCH}"
#define REVISION "${REVISION}"
#define SVNPATH "${SVNPATH}"
I've got the following piece of code in my CMake file, but it's executed only after running the cmake command:
FILE(WRITE ${CMAKE_BINARY_DIR}/version.hpp.in
"#define RELEASE_VERSION \"${RELEASE}\"\n"
"#define MAJOR_VERSION \"${MAJOR}\"\n"
"#define MINOR_VERSION \"${MINOR}\"\n"
"#define PATCH_VERSION \"${PATCH}\"\n"
"#define REVISION \"${REVISION}\"\n"
"#define SVNPATH \"${SVNPATH}\"\n"
)
I want to generate the version.hpp.in file each time I run the make command. How can I do that?
CMake generates a Unix makefile by default when run from the command line in a Unix-like environment. Of course, you can generate makefiles explicitly using the -G option. When generating a makefile, you should also define the CMAKE_BUILD_TYPE variable.
Make (or rather a Makefile) is a buildsystem - it drives the compiler and other build tools to build your code. CMake is a generator of buildsystems. It can produce Makefiles, it can produce Ninja build files, it can produce KDEvelop or Xcode projects, it can produce Visual Studio solutions.
You can use configure_file() for this:
configure_file(version.hpp.in ${DESTPATH}/version.hpp)
Where DESTPATH is optional and set to the path you want that file.
If the file is modified the build system will re-run CMake to re-configure the file and generate the build system again.
(Source: Documentation, see below)
File version.hpp:
#define RELEASE_VERSION "${RELEASE}"
#define MAJOR_VERSION "${MAJOR}"
#define MINOR_VERSION "${MINOR}"
#define PATCH_VERSION "${PATCH}"
#define REVISION "${REVISION}"
#define SVNPATH "${SVNPATH}" 
Syntax:
configure_file(<input> <output>
               [COPYONLY] [ESCAPE_QUOTES] [@ONLY]
               [NEWLINE_STYLE [UNIX|DOS|WIN32|LF|CRLF] ])
Documentation:
# 'example' is the targetname here, eg. using add_executable(example example.cpp)
add_custom_command(TARGET example PRE_BUILD COMMAND <CMAKE COMMAND HERE>)
#define VERSION ${VERSION}
Just for testing …
cmake_minimum_required(VERSION 2.8.12)
set(VERSION 1.10)
set(TEMPLATE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
add_executable(example example.cpp)
add_custom_command(TARGET example PRE_BUILD 
        COMMAND ${CMAKE_COMMAND} -P ${CMAKE_CURRENT_SOURCE_DIR}/SetVersion.cmake)
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/../Version.h.in ${CMAKE_CURRENT_SOURCE_DIR}/../Version.h)
message("Version set") # For testing only
Now run cmake and make as usual and check if HELLO is printed on make.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With