Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Proper way to call a found executable in a custom command?

Tags:

cmake

I have a program on my computer, let's say C:/Tools/generate_v23_debug.exe

I have a FindGenerate.cmake file which allows CMake to find that exact path to the executable.

So in my CMake code, I do:

find_program(Generate)
if (NOT Generate_FOUND)
    message(FATAL_ERROR "Generator not found!")

So CMake has found the executable. Now I want to call this program in a custom command statement. Should I use COMMAND Generator or COMMAND ${GENERATOR_EXECUTABLE}? Will both of these do the same thing? Is one preferred over the other? Is name_EXECUTABLE a variable that CMake will define (it's not in the FindGenerate.cmake file), or is it something specific to someone else's example code I'm looking at? Will COMMAND Generator be expanded to the correct path?

add_custom_command(
    OUTPUT blahblah.txt
    COMMAND Generator inputfile1.log
    DEPENDS Generator
)
like image 785
Chromozon Avatar asked Dec 14 '13 00:12

Chromozon


People also ask

What does add executable to Cmake?

Adds an executable target called <name> to be built from the source files listed in the command invocation. The <name> corresponds to the logical target name and must be globally unique within a project.

Where is executable after Cmake?

After running cmake ../ ; make , I can run the executable thusly: Project/build$ src/Executable - that is, the Executable is created in the build/src directory.

What is Add_custom_command?

This defines a command to generate specified OUTPUT file(s). A target created in the same directory ( CMakeLists.txt file) that specifies any output of the custom command as a source file is given a rule to generate the file using the command at build time.


1 Answers

find_program stores its result into the variable given as a first argument. You can verify this by inserting some debug output:

find_program(GENERATOR Generate)
message(${GENERATOR})

Note that find_program does not set any additional variables beyond that. In particular, you mentioned Generate_FOUND and GENERATOR_EXECUTABLE in your question and neither of those gets introduced implicitly by the find_program call.

The second mistake in your program is the use of the DEPENDS option on the add_custom_command. DEPENDS is used to model inter-target dependencies at build time and not to manipulate control flow in the CMakeLists. For example, additional custom command can DEPEND on the output of your command (blahblah.txt), but a custom command cannot DEPEND on the result of a previous find operation.

A working example might look something like this:

find_program(GENERATOR Generate)
if(NOT GENERATOR)
    message(FATAL_ERROR "Generator not found!")
endif()

add_custom_command(
    OUTPUT blahblah.txt
    COMMAND ${GENERATOR} inputfile1.log
)

P.S.: You asked why the code examples were not properly formatted in your question. You indented everything correctly, but you need an additional newline between normal text and code paragraphs. I edited your question accordingly.

like image 79
ComicSansMS Avatar answered Oct 20 '22 03:10

ComicSansMS