Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cmake add_custom_command not working

I am trying to run gperf from a cmake file.

I created a very minimal CMakeLists.txt below.

When I run it by

$ cmake .
$ make 

It does not create the example.hpp file

What could be problem with the below CMakeLists.txt?

cmake_minimum_required( VERSION 2.6 )

function(gperf_generate_new source target)

        add_custom_target(${target} echo "Creating ${target}")

        add_custom_command(
                SOURCE ${source}
                TARGET ${target}
                COMMAND gperf -L c++ ${source} > ${target}
                OUTPUTS ${target}
                DEPENDS ${source}
                )

endfunction()

gperf_generate_new(command_options.new.gperf example.hpp)
like image 520
Sri Kant Avatar asked Oct 20 '22 00:10

Sri Kant


1 Answers

Files, produced by source-files generators(like gpref) are rarely needed as standalone. Instead, these source files are usually used for creating executables or libraries inside a project.

So, standard pattern of using source-file generators in the CMake looks like:

# Call add_custom_command() with appropriate arguments for generate output file
# Note, that *gperf* will work in the build tree,
# so for file in the source tree full path should be used.
function(gperf_generate_new input output)
    add_custom_command(
        OUTPUT ${output}
        COMMAND gperf -L c++ ${input} > ${output}
        DEPENDS ${input}
        COMMENT "Generate ${output}" # Just for nice message during build
    )
endfunction()

# Generate *example.hpp* file ...
gperf_generate_new(${CMAKE_CURRENT_SOURCE_DIR}/command_options.new.gperf example.hpp)

# ... for use it in executable
add_executable(my_program ${CMAKE_CURRENT_BINARY_DIR}/example.hpp <other sources>)

If you want only to test whether example.hpp is generating, instead of add_executable() use

add_custom_target(my_target
    ALL # Force target to be built with default build target.
    DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/example.hpp
)

Note, that linkage between add_custom_command and add_custom_target is expressed using same filename in their OUTPUT and DEPENDS options correspondingly. With such link order of these commands is insignificant (but both commands should be called from the same CMakeLists.txt script).

like image 60
Tsyvarev Avatar answered Oct 22 '22 21:10

Tsyvarev