Question

I'd like to be able to do this (if possible (trying to get pseudo polymorphism/function passing to avoid copy-paste))

 macro(do_A x)
 endmacro()

 macro(do_B x)
 endmacro()

 macro(C y x)
 do_${x}(y)
 endmacro()

 C(asdf,A)

Is there any mechanism to do such a thing?

Edit: it may be more accurate to ask, can you pass a macro as an argument to another macro?

Was it helpful?

Solution

I'm not sure if there's a better way, but you can use configure_file and include here as helpers.

So, if you create an input file called "macro_helper.cmake.in" and have its contents as just the following line:

@MacroName@(MacroArg)

Then you can configure this to a per-macro output file, and simply then include the output file:

macro(C MacroArg MacroId)
  set(MacroName do_${MacroId})
  # Need to make MacroArg a "proper" variable since we're in a macro, not a
  # function.  Run 'cmake --help-command macro' for more info.
  set(MacroArg ${MacroArg})
  set(OutputFile ${CMAKE_BINARY_DIR}/helpers/macro_helper_${MacroId}.cmake)
  configure_file(macro_helper.cmake.in ${OutputFile} @ONLY)
  include(${OutputFile})
endmacro()

Note: you don't want the comma when invoking C - just do:

C(asdf A)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top