Run Command after generation step in CMake

2019-05-14 23:54发布

问题:

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!

回答1:

Yes, add_custom_command paired with add_custom_target got this covered

  • http://cmake.org/cmake/help/cmake-2-8-docs.html#command:add_custom_command
  • http://cmake.org/cmake/help/cmake-2-8-docs.html#command:add_custom_target

For an example take a look at my answer to another question

  • cmake add_custom_command


回答2:

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.



回答3:

From the following links, it seems like there is no such command to specify execution after CMake generated .sln files.

  • https://cmake.org/pipermail/cmake/2010-May/037128.html
  • https://cmake.org/pipermail/cmake/2013-April/054317.html
  • https://cmake.org/Bug/view.php?id=15725

An alternative is to write a wrapper script as described in one of the above links.

cmake ..
DoWhatYouWant.exe


标签: cmake