[CMake] Targets like Makefile suffix rules?

Michael Wild themiwi at gmail.com
Wed Jun 1 11:41:00 EDT 2011


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

Michael



More information about the CMake mailing list