Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling existing make command in cmake file

Tags:

makefile

cmake

I have a large project with multiple subdirectories. In the parent directory, I have a CMakeLists.txt file which calls functions defined in other cmake files in the same parent directory. I have a custom Makefile in one of the subdirectories that contains some target "run". When I call cmake from the parent directory, I want the "run" target located in the subdirectory makefile to execute. How should I do this ?


I understand that some people have suggested to use add_custom_target and add_custom_command, but I am still confused as to how to apply these commands to accomplish this task.

like image 329
imgolden62 Avatar asked Jun 12 '17 14:06

imgolden62


People also ask

Can CMake run Makefile?

CMake will create makefiles inside many of the folders inside a build directory and you can run make or make -j8 in any of these directories and it will do the right thing. You can even run make in your src tree, but since there is no Makefile in your source tree, only CMakeLists. txt files, nothing will happen.

How do I run a command in CMake?

CMake executes the child process using operating system APIs directly: On POSIX platforms, the command line is passed to the child process in an argv[] style array. On Windows platforms, the command line is encoded as a string such that child processes using CommandLineToArgvW will decode the original arguments.

Can I use CMake instead of make?

cmake is a system to generate make files based on the platform (i.e. CMake is cross platform) which you can then make using the generated makefiles. While make is you directly writing Makefile for a specific platform that you are working with. If your product is crossplatform, then cmake is a better choice over make .

What is CMake Makefile?

CMake is an extensible, open-source system that manages the build process in an operating system and in a compiler-independent manner. Unlike many cross-platform systems, CMake is designed to be used in conjunction with the native build environment.


1 Answers

If you know, which file(s) are produced by Makefile in the subdirectory, and want to depend on these files, use add_custom_command:

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

This assumes that your CMakeLists.txt have a target, which depends or uses given file.

Otherwise, if you do not care which files are produced by 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 directory which should be current for command executed.

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

like image 161
Tsyvarev Avatar answered Oct 11 '22 18:10

Tsyvarev