Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run Command after generation step in CMake

Tags:

cmake

I have a command line tool that should be run after CMake created my .sln-file. Is there any way to do that using CMake?

Using execute_process(COMMAND ..) at the end of the CMakeLists.txt does not help because this is executed after the Configure step, however, the .sln-file is created in the generation step.

Thanks a lot!

like image 764
Philipp Avatar asked Aug 17 '11 10:08

Philipp


People also ask

How do I run a command in CMake?

CMake executes the child process using operating system APIs directly: On POSIX platforms, the command line is passed to the child process in an argv[] style array. On Windows platforms, the command line is encoded as a string such that child processes using CommandLineToArgvW will decode the original arguments.

What should I run after CMake?

After you've run cmake , keep the build directory around and you should be able to just run make (it'll detect if a CMakeLists.

What is Cmake_command?

The full path to the cmake(1) executable. This is the full path to the CMake executable cmake(1) which is useful from custom commands that want to use the cmake -E option for portable system commands. ( e.g. /usr/local/bin/cmake )


1 Answers

A rather horrifying way to do it is by calling cmake from cmake and doing the post-generate stuff on the way out of the parent script.

option(RECURSIVE_GENERATE "Recursive call to cmake" OFF)

if(NOT RECURSIVE_GENERATE)
    message(STATUS "Recursive generate started")
    execute_process(COMMAND ${CMAKE_COMMAND}
        -G "${CMAKE_GENERATOR}"
        -T "${CMAKE_GENERATOR_TOOLSET}"
        -A "${CMAKE_GENERATOR_PLATFORM}" 
        -DRECURSIVE_GENERATE:BOOL=ON 
        ${CMAKE_SOURCE_DIR})
    message(STATUS "Recursive generate done")

    # your post-generate steps here

    # exit without doing anything else, since it already happened
    return()
endif()

# The rest of the script is only processed by the executed cmake, as it 
# sees RECURSIVE_GENERATE true

# all your normal configuration, targets, etc go here

This method doesn't work well if you need to invoke cmake with various combinations of command line options like "-DTHIS -DTHAT", but is probably acceptable for many projects. It works fine with the persistently cached variables, including all the cmake compiler detection when they're initially generated.

like image 125
Glen Knowles Avatar answered Oct 02 '22 08:10

Glen Knowles