Run Command after generation step in CMake

2019-05-14 23:51发布

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!

标签: cmake
3条回答
Deceive 欺骗
2楼-- · 2019-05-14 23:56

Yes, add_custom_command paired with add_custom_target got this covered

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

查看更多
孤傲高冷的网名
3楼-- · 2019-05-14 23:58

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.

查看更多
看我几分像从前
4楼-- · 2019-05-15 00:02

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

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

cmake ..
DoWhatYouWant.exe
查看更多
登录 后发表回答