10

Is there a way to call a function in CMake using the name that is stored in a variable (for passing functions to functions, etc)?

Here is what I have tried:

cmake_minimum_required(VERSION 3.0)

function(doThing)
endfunction()

set(FuncVar doThing)

${FuncVar}()

Which fails with this error:

Parse error.  Expected a command name, got unquoted argument with text "${FuncVar}".
-- Configuring incomplete, errors occurred!

I can't see why this shouldn't work, but then again I am new to CMake so what do I know.

Thank you for any help!

3
  • This syntax is not supported. What are you actually trying to achieve ? I have never had the need for such a technique.
    – SirDarius
    Commented Dec 21, 2015 at 22:14
  • I am trying to create a system where the base systems configure the derived. I guess variables is the way to go then. Thanks! Commented Dec 21, 2015 at 22:22
  • I have not tested it but newest cmake 3.18 features an eval function: cmake_language(EVAL CODE <code>...) cmake.org/cmake/help/latest/command/…
    – kuga
    Commented Jun 19, 2020 at 8:00

5 Answers 5

11

Just a quick update: It seems like CMake added this functionality in the current 3.18 Versions via the cmake_language command, the Syntax:

cmake_language(CALL <command> [<args>...])
cmake_language(EVAL CODE <code>...)

Reference for cmake_language https://cmake.org/cmake/help/v3.18/command/cmake_language.html#command:cmake_language

10

I have solved this with a workaround using files.

Lets say you have:

function(do what)
  ...
endfunction()

You want to call different specializations depending on 'what'. You can then do:

function(do what)
  include("do-${what}.cmake")
  do_dynamic()
endfunction()

And in file do-something.cmake:

function(do_dynamic)
  ...
endfunction()

You can create as many specialization files as you want...

7

Hi I have written eval for cmake (and it is as fast as i can make it) here and here is the code as it is part of my cmakepp library.

I have written two versions of eval (eval and eval_ref because the first does not give you access to the PARENT_SCOPE whereas the latter does)

however this will only help if your use cmakepp and as that might be a dealbreaker for you I modified it to work with vanilla cmake:

## evals the specified cmake code.
## WARNING: there is no way to set(<var> <value> PARENT_SCOPE) 
## because of the extra function scope defined by eval.
## WARNING: allowing eval can of course be dangerous.
function(eval __eval_code)

  # one file per execution of cmake (if this file were in memory it would probably be faster...)
  # this is where the temporary eval file will be stored.  it will only be used once per eval
  # and since cmake is not multihreaded no race conditions should occure.  however if you start 
  # two cmake processes in the same project this could lead to collisions
  set(__eval_temp_file "${CMAKE_CURRENT_BINARY_DIR}/__eval_temp.cmake")


  # write the content of temp file and include it directly, this overwrite the 
  # eval function you are currently defining (initializer function pattern)
  file(WRITE "${__eval_temp_file}" "
function(eval __eval_code)
  file(WRITE ${__eval_temp_file} \"\${__eval_code}\")
  include(${__eval_temp_file})
endfunction()
  ")

include("${__eval_temp_file}")
## now eval is defined as what was just written into __eval_temp_file


## since we are still in first definition we just need to execute eval now
## (which calls the second definition of eval).
eval("${__eval_code}")


endfunction()
1
0

Without workarounds, CMake itself does not support function pointers or indirect calls. For example someone asked on the CMake mailing list in 2011 about a call function to achieve what you wanted to do: https://cmake.org/pipermail/cmake/2011-September/046124.html

0

I have my own implementation:

https://github.com/andry81/tacklelib/tree/HEAD/cmake/tacklelib/Eval.cmake

Pros:

  • Implemented through a function and as a result arguments of a function having one-pass expansion stage instead of two-pass for a macro, so you don't need to escape arguments before call to a function (any of those).
  • All locally set/unset variables does automatic propagation to a parent scope, no need to do it explicitly.
  • Implemented through the temporary unique directory in the temporary files storage in Windows or Linux, as a result the function can be called in parallel in any mode (script or build), no need to use a binary directory as a temporary storage.
  • Because evaluation string executes in the context of a function, no needs special cleanup logic only before a command like for the return().
  • Internal escaping of characters like $, \, ;.
  • Direct access to the being executed inclusion file in a temporary directory (tkl_begin* + <pre process> + tkl_eval_end), so you can pre process it before call to the tkl_eval_end and make another file, for example, through a call to the configure_file command to make a substitution of @-variables in the file or something else.
  • Builtin variables like ARGV, ARGC, ARGV0..N can be forwarded (injected) inside the eval scope through the function tkl_push*_ARGVn_*/tkl_pop_ARGVn_* (see ForwardArgs.cmake module), then the eval automatically restore them at the beggining of an expression and revert back at the end.
  • CMake tests for cmake modules from the library: https://github.com/andry81/tacklelib/tree/HEAD/cmake_tests

Cons:

  • The return executes in the context of a function, as a result the tkl_eval("return()") does nothing, but return() itself still can be used to branch the logic inside an evaluation string or file.
  • A macro parameters is not directly visible in the tkl_eval function, when the upper caller variables still visible as usual (needs a proper escaping of the $\{...} or \${...}, but the first one is better because would bypass an macro variables expansion stage).
  • Builtin variables (ARGV, ARGC, ARGV0..N) does not propagate into inside automatically, needs to be used explicit calls to standalone functions (described above) before and after an eval function to inject them in the scope of the eval.

The entire project currently is an experimental.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.