Calling an existing make command in a cmake file

I have a large project with several subdirectories. In the parent directory, I have a CMakeLists.txt file that calls functions defined in other cmake files in the same parent directory. I have my own Makefile in one of the subdirectories that contains some kind of target "launch". When I call cmake from the parent directory, I want the "run" target in the subdirectory makefile to be executed. How should I do it? I understand that some people have suggested using add_custom_target and add_custom_command, but I am still confused about how to use these commands to accomplish this task.

+3


source to share


1 answer


If you know which file the Makefile is creating in a subdirectory and want to depend on those files, use add_custom_command

:

add_custom_command(OUTPUT <output-file>
                   COMMAND make run
                   WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/<subdir>
)

      

This assumes that yours CMakeLists.txt

has a target that depends on or uses the given file.

Otherwise, if you don't like the files generated by the Makefile, use add_custom_target

:



add_custom_target(<target_name> COMMAND make run
                   WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/<subdir>
)

      


In both cases WORKING_DIRECTORY specifies the directory that should be current for the command to be executed.

If you want the target (in the second case) to be the default, add ALL before the COMMAND.

+4


source







All Articles