Johannes
Johannes

Reputation: 3099

How to include a cmake Makefile after a target has been executed?

Given the following minimal example.

cmake_minimum_required(VERSION 2.8)
project(include_test)

add_custom_command(OUTPUT OtherCMakeLists.txt
    COMMAND "${CMAKE_CURRENT_BINARY_DIR}/create_other_cmakelists")
add_custom_target(do_something DEPENDS OtherCMakeLists.txt)

What do_something should do here is first create OtherCMakeLists.txt. Now, let's assume that do_something has to do something else afterwards, e.g. compiling some code. I'd like that when the targets from something else are executed, the CMakeLists.txt will behave as if OtherCMakeLists.txt was included with include.

Is this possible?

As an example why this could be useful: OtherCMakeLists.txt might add some compiler flags that have influence on further compiling.

Upvotes: 4

Views: 364

Answers (2)

Antwane
Antwane

Reputation: 22688

add_custom_command has 2 different signatures:

  • add_custom_command(OUTPUT ...) will be executed at build time, too late to apply rules from a generated CMakeLists.txt generated.
  • add_custom_command(TARGET ...) to attach a specific command to a target. This command can be run on PRE_BUILD, PRE_LINK or POST_BUILD. Probably not what you want to achieve...

If you are trying to add some dynamic to your compile process, adding custom commands or target may not be your best option.

You should try to read doc for some other CMake commands that can be helpful in your case:

Upvotes: 1

Peter Petrik
Peter Petrik

Reputation: 10195

To my knowledge, it is not possible to generate CMakeLists.txt file with a custom target/command and use include CMake command with generated CMakeLists.txt

The problem is that the include command is called at so-called "Configuration time" (when cmake executable tries to parse all CMakeLists.txt), but the generation of file (CMakeLists.txt) is performed at "Build time" (when make command is invoked on generated build system)

Upvotes: 1

Related Questions