[CMake] Targets like Makefile suffix rules?

Jordi Gutiérrez Hermoso jordigh at octave.org
Wed Jun 1 11:57:38 EDT 2011


On 1 June 2011 10:41, Michael Wild <themiwi at gmail.com> wrote:
> On 06/01/2011 05:19 PM, Jordi Gutiérrez Hermoso wrote:
>> If I have several files with a .cc or .cpp filename extension that I
>> want to compile using Octave's mkoctfile script (a wrapper that turns
>> them into loadable object code in a form recognisable by the Octave
>> interpreter), how should I do it?
>>
>> I thought I could do it with add_custom_command, but as I seem to
>> understand it, I need to write one such add_custom_command per source
>> file I want to process, perhaps with a loop?
>>
>> What I'm really looking for is something like
>>
>>    .cpp.oct:
>>    mkoctfile $< $COMPILATION_OPTIONS
>>
>> like in Makefiles, a way to specify a custom command to turn files of
>> type A into type B with some flexibility of the option passed to this
>> command. Then I want to use this custom command to any specified
>> source file.
>>
>> TIA,
>> - Jordi G. H.
>
> CMake doesn't have suffix-rules. You'll need that loop, e.g.:
>
> ################
> set(CXX_SRCS
>  a.cpp
>  b.cc
>  d.cxx)
>
> find_program(MKOCTFILE_EXECUTABLE mkoctfile)
> if(NOT MKOCTFILE_EXECUTABLE)
>  message(SEND_ERROR "Failed to find mkoctfile")
> endif()
>
> set(OCT_SRCS)
> foreach(f IN LISTS CXX_SRCS)
>  if(NOT IS_ABSOLUTE "${f}")
>    set(f "${CMAKE_CURRENT_SOURCE_DIR}/${f}")
>  endif()
>  get_filename_component(fn "${f}" NAME_WE)
>  set(o "${CMAKE_CURRENT_BINARY_DIR}/${fn}.oct")
>  add_custom_command(OUTPUT "${o}"
>    COMMAND ${MKOCTFILE_EXECUTABLE -o "${o}" "${f}"
>    DEPENDS "${f}"
>    COMMENT "Generating ${o}")
>  list(APPEND OCT_SRCS "${o}")
> endforeach()
>
> add_custom_target(oct-files ALL
>  DEPENDS "${OCT_SRCS}")
> ################
>
> HTH

It does, thanks, this is perfect. I was having some weird mental block
over not doing that loop, but when I saw it written as you wrote it,
it looks much more clear now.

Happy hacking,
- Jordi G. H.


More information about the CMake mailing list