[CMake] (no subject)

Mark Stijnman mark.stijnman at gmail.com
Tue Jul 9 18:18:39 EDT 2013


On Tue, Jul 9, 2013 at 5:59 PM, Teto <mattator at gmail.com> wrote:
> Hi,
>
> I am trying to pass several arguments of add_custom_target via a
> custom proxy function that takes 1 argument only. I find it hard to
> explain but this piece of code should be clearer:
>
> === custom function ===
> function(myfunc PROGRAM command_line)
>       .... // some code here
>       add_custom_target(
>             ${PROGRAM}
>                         COMMAND ${PROGRAM_BINARY}
>                          ${command_line}
>                         )
> endfunction()
>
> == how I call it ==
> myfunc( gengetopt "-i menudescriptor.ggo WORKING_DIRECTORY
> ${CMAKE_CURRENT_LIST_DIR}")
>
[snip]
> Is there any "eval" function I could use ?
>
> Best regards
>
> Matt

What I would do is more something like this:

include(CMakeParseArguments)
function(myfunc PROGRAM command_line)
  CMAKE_PARSE_ARGUMENTS(myfunc "" "WORKING_DIRECTORY" "" ${ARGN})
  if(NOT myfunc_WORKING_DIRECTORY)
    set(myfunc_WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}) # or
some other suitable default
  endif()
  add_custom_target(${PROGRAM}
    COMMAND ${PROGRAM_BINARY} ${command_line}
    WORKING_DIRECTORY ${myfunc_WORKING_DIRECTORY})
endfunction()

Now your function has a "WORKING_DIRECTORY" keyword with a single
parameter, which it will read from any arguments beyond the
fixed-position arguments (available in ${ARGN}). Check the
documentation for CMAKE_PARSE_ARGUMENTS for more info. Call your new
function like
myfunc( gengetopt "-i menudescriptor.ggo" WORKING_DIRECTORY
"${CMAKE_CURRENT_LIST_DIR}")

Good luck,

regards Mark


More information about the CMake mailing list