[CMake] Re: Calling a dynamically created macro

Brandon Van Every bvanevery at gmail.com
Wed Dec 19 18:04:49 EST 2007


On Dec 19, 2007 12:47 PM, Rodolfo Schulz de Lima <rodolfo at rodsoft.org> wrote:
> Brandon Van Every escreveu:
>
> > You could use a macro to generate a CMake script containing a macro,
> > and then call that script.  :-)
>
> Nice... :)

In fact, it turns out you can use an include() to cause it to exist at
the point you create the macro.  A downside is the number of escapes
you have to put into your meta-macro, such as \\\${myvar}.


# could figure out whether endline is \n or \r\n for this OS
#set(endline "\n")

macro(makemacro macro_name) #macro_args ... macro_body
  if(${ARGC} GREATER 2)
    set(macro_args ${ARGN})
    list(REMOVE_AT macro_args -1)
  else(${ARGC} GREATER 2)
    set(macro_args)
  endif(${ARGC} GREATER 2)
  if(${ARGC} GREATER 1)
    set(argn_list ${ARGN})
    list(GET argn_list -1 macro_body)
    #message("macro body")
    #message("${macro_body}")
  else(${ARGC} GREATER 1)
    set(macro_body)
  endif(${ARGC} GREATER 1)
  set(mstart "macro(${macro_name} ${macro_args})")
  set(mend "endmacro(${macro_name})")
  set(macro_file ${CMAKE_CURRENT_BINARY_DIR}/macro_${macro_name}.cmake)
  #file(REMOVE ${macro_file})
  if(EXISTS ${macro_file})
    message("can't create ${mstart}")
    message(FATAL_ERROR "${macro_file} already exists")
  endif(EXISTS ${macro_file})
  file(WRITE ${macro_file}
"${mstart}
${macro_body}
${mend}
")
  include(${macro_file})
  file(REMOVE ${macro_file})
endmacro(makemacro)

makemacro(meta string_const
"  message(\"\\\${string_const}\")
")
meta("I can do what I want")


C:\devel\src\cbugs\makemacro>cmake -P makemacro.cmake
I can do what I want


Cheers,
Brandon Van Every


More information about the CMake mailing list