MantisBT - CMake
View Issue Details
0004034CMakeCMakepublic2006-11-09 03:372011-05-02 14:46
Gaetan Lehmann 
David Cole 
normalfeaturealways
closedno change required 
 
CMake 2.8.4 
0004034: using a variable in the name of a macro.
Currently with cmake, we can do:

   SET(type "US")
   SET(IUS "tralala")

then writting ${I{type}} is the same than writting ${IUS}. That's very convenient in many cases (and that's the base of WrapITK).
However, it's not possible to do the same with the name of the macro:

   MACRO(MY_MACRO_PYTHON)
     ...
   ENDMACRO(MY_MACRO_PYTHON)
   SET(lang PYTHON)

 then calling

  MY_MACRO_${lang}()

doesn't work. This feature would be very useful to easily add a language support in ITK (for example).
No tags attached.
has duplicate 0011845closed David Cole Add an EVAL command to evaluate a string as a CMake script 
Issue History
2008-10-07 16:51Bill HoffmanAssigned ToBill Hoffman => Ken Martin
2010-12-14 11:16David ColeAssigned ToKen Martin =>
2010-12-15 08:59David ColeAssigned To => Brad King
2010-12-15 09:01David ColeNote Added: 0024142
2010-12-16 08:10Gaetan LehmannNote Added: 0024201
2010-12-16 08:15David ColeAssigned ToBrad King => David Cole
2010-12-16 08:19David ColeNote Added: 0024202
2010-12-16 08:19David ColeStatusassigned => resolved
2010-12-16 08:19David ColeFixed in Version => CMake 2.8.4
2010-12-16 08:19David ColeResolutionopen => no change required
2010-12-16 08:26Gaetan LehmannNote Added: 0024203
2010-12-16 08:26Gaetan LehmannStatusresolved => feedback
2010-12-16 08:26Gaetan LehmannResolutionno change required => reopened
2011-01-18 11:23David ColeNote Added: 0024865
2011-01-18 11:23David ColeStatusfeedback => assigned
2011-01-18 11:24David ColeNote Added: 0024866
2011-01-18 11:24David ColeStatusassigned => resolved
2011-01-18 11:24David ColeResolutionreopened => no change required
2011-05-02 14:46David ColeNote Added: 0026360
2011-05-02 14:46David ColeStatusresolved => closed
2012-06-01 11:21David ColeRelationship addedhas duplicate 0011845

Notes
(0024142)
David Cole   
2010-12-15 09:01   
Is this even feasible, or a good idea?

You could certainly achieve the same thing, although it would be more verbose, using a bunch of if("${lang}" STREQUAL "PYTHON") commands...
(0024201)
Gaetan Lehmann   
2010-12-16 08:10   
It wouldn't be possible if the lang value is not known when writing the cmake script.
I wanted to use that feature in wrapitk to be able to add a new language simply by adding a new folder in the Languages folder. We can then do something like (and much more, this is simply an example):

file(GLOB local_files *)
foreach(f ${local_files})
  file(RELATIVE_PATH lang "${CMAKE_CURRENT_SOURCE_DIR}" "${f}")
  if(IS_DIRECTORY "${f}")
    init_${lang}()
  endif()
endforeach(f)
(0024202)
David Cole   
2010-12-16 08:19   
Rather than:

  init_${lang}()

You could instead write:

  include(${lang}.cmake)

And have the ${lang}.cmake file contain the code that would have been in your macro, but have it just at file scope.

While this technique requires more files, and probably care taken in those sets of files not to trounce on each other's variables and what not, it should be achievable with present day CMake. You could even do this with much older versions of CMake...

Since this workaround to the problem at hand exists, and the implementation of this feature would add much complexity (if it's even do-able...), I am going to resolve this issue as "no change required".

Please re-open and comment with further notes and elaborations if you disagree with this resolution.

Thanks.
(0024203)
Gaetan Lehmann   
2010-12-16 08:26   
This would work, but would make quite difficult to pass parameters to the code, and would probably be quite inefficient if the code needs do be called many times.
But if that's too difficult to add, I'll do without that feature :-)
(0024865)
David Cole   
2011-01-18 11:23   
If you require your "${lang}.cmake" file to declare a single macro called MyLanguageMacro (or whatever), and have the parameters be well defined to that macro, then you could:

  include(${lang}.cmake)
  MyLanguageMacro("${a0}" "${a1}" "${a2}")

  include(${other_lang}.cmake)
  MyLanguageMacro("${a0}" "${a1}" "${a2}")
  MyLanguageMacro("${b0}" "${b1}" "${b2}")
(0024866)
David Cole   
2011-01-18 11:24   
Suitable workarounds for this "issue" are noted in the various notes on this bug. Please use a work-around. We will not be adding "variable expansion into function call" capabilities to the CMake language any time soon.
(0026360)
David Cole   
2011-05-02 14:46   
Closing resolved issues that have not been updated in more than 3 months.