13

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!

Philipp
  • 11,549
  • 8
  • 66
  • 126

3 Answers3

3

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.

Glen Knowles
  • 438
  • 6
  • 8
0

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
Tomoyuki Aota
  • 867
  • 9
  • 18
  • This will break when cmake automatically reruns after update to CMakeLists file (e.g. if you pull or checkout). – yugr Jan 21 '20 at 15:12
-2

Yes, add_custom_command paired with add_custom_target got this covered

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

Community
  • 1
  • 1
Maik Beckmann
  • 5,637
  • 1
  • 23
  • 18
  • 1
    maybe I don't understand what you mean, but as far as I can tell, with these commands it's only possible to run a tool during the build. (I am using Visual Studio and there the process works as follows: I hit "Build" -> CMake generates the new .sln-file if there were any changes -> the VS Macro asks me if I want to reload the solution -> I press Yes -> Building the solution starts -> the custom target is executed. If now my command modifies the solution, I have to reload again which I want to avoid. I'd rather modify the solution file during CMake's generation phase.) – Philipp Aug 19 '11 at 09:48
  • Oh now I understand, you want to run your command still within cmake-time, but after the solution file is generated. In this case the commands I mentioned, which are meant to execute programs during build-time, are indeed not what you're looking for. I'm not sure if there is such a post generation hook, since this is the final phase of the cmake-time. – Maik Beckmann Aug 19 '11 at 13:15
  • I suggest that you ask your question at the cmake mailing list and explain what you're trying to do in greater detail. AFAIK the core devs don't follow SO too closely. – Maik Beckmann Aug 19 '11 at 13:17