Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CMake: How to create a file with make command

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?

like image 828
B Faley Avatar asked Oct 04 '14 13:10

B Faley


People also ask

Does CMake create a Makefile?

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.

Is CMake -- build same as make?

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.


1 Answers

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:

  • CMake 3.0
  • CMake 2.8.12

# 'example' is the targetname here, eg. using add_executable(example example.cpp)

add_custom_command(TARGET example PRE_BUILD COMMAND <CMAKE COMMAND HERE>)

Minimal Example

Version.h.in

#define VERSION ${VERSION}

Just for testing …

CMakeLists.txt

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)

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.

like image 67
ollo Avatar answered Oct 10 '22 23:10

ollo