Koen
Koen

Reputation: 23

CMake call add_subdirectory within custom command

I'm working with a code generator that produces C++ and a CMakeLists.txt file, unfortunately I cannot use this in my main CMakeLists.txt file for testing purposes.

For example you have the following CMakeLists.txt file:

project(SomeProject CXX C)

add_custom_command(OUTPUT ${SRCS}
    COMMAND ${CODEGEN_CLI_PATH} -i "${INPUT}" -o "${OUT}"
    COMMENT "Generating sources"
    WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
    VERBATIM
)

add_custom_target(CODEGEN
    DEPENDS
    ${SRCS}
)

# Needs to be executed after the custom command
add_subdirectory(${GENERATED_CMAKE_LISTS_LOCATION})

Is it possible to use functions such as add_subdirectory only after you execute custom commands for a particular target, such as CODEGEN?

I've already tried to execute it by adding an extra line to the existing custom command:

    COMMAND ${CMAKE_COMMAND} -D DIR=${GENERATED_CMAKE_LISTS_LOCATION} -P add_subdirectories.cmake

Unfortuantly this doesn't work because it isn't allowed to execute functions like add_subdirectory in script mode.

Neither I can manage to call custom made functions (that are executing add_subdirectory) from add_custom_command that are located in the same file.

Upvotes: 2

Views: 1467

Answers (2)

daparic
daparic

Reputation: 4474

I have a huge fixed unsigned char array that I compiled into a static library. The way I work around it is by:

if(NOT EXISTS ${PATH_TO_FOLDER}/smeagol.a)
    add_subdirectory(smeagol)
endif()

I'm still looking for a nicer kung-fu way to do it using cmake. I feel that its out there, and I will update this answer once i find it.

Upvotes: 0

arrowd
arrowd

Reputation: 34421

Nope, it is not possible. The add_subdirectory command is run during configuration step, while CODEGEN is a target that runs during build.

You seem to be doing something wrong, so the only advice I can give you is to use execute_process command to run commands you need. The execute_process command is executed during configuration stage, so it will be able to generate files you need before add_subdirectory.

But again, please describe your problem, why do you want CMake to do that.

Upvotes: 1

Related Questions