'Parallel execution of cmake functions?

When running cmake on a large project, can it call a given cmake function in parallel?

What I am concerned about is that when this (user defined) function stores something in the cache then that would collide with the parallel execution; how does cmake deal with this when functions are indeed executed in parallel, or do I have to make sure that even invocations of the same function will never use the same (cached) variable name with different values?

EDIT (see comments):

I wrote the following function,

function(CW_SYS_CACHELINE_SIZE)
  if (NOT DEFINED CACHE{cw_cv_sys_cacheline_size})
    set(CMAKE_TRY_COMPILE_CONFIGURATION "Release")
    try_run(RUN_WORKS
            COMPILE_WORKS
            ${CMAKE_CURRENT_BINARY_DIR}/cw_utils_sys_cacheline_size
            ${CW_SYS_CACHELINE_SIZE_MODULE_PATH}/CW_SYS_CACHELINE_SIZE.c
            COMPILE_OUTPUT_VARIABLE COMPILE_OUTPUT
            RUN_OUTPUT_VARIABLE RUN_OUTPUT)
    if (NOT COMPILE_WORKS)
      message(FATAL_ERROR "Failed to compile test program 
CW_SYS_CACHELINE_SIZE.c: ${COMPILE_OUTPUT}")
    elseif (NOT RUN_WORKS EQUAL 0)
      message(FATAL_ERROR "Failed to run test program CW_SYS_CACHELINE_SIZE.c: ${RUN_OUTPUT}")
    else ()
      set(cw_cv_sys_cacheline_size ${RUN_OUTPUT} CACHE INTERNAL "")
    endif ()
  endif()
endfunction()

Since this function doesn't take arguments, the internal values of the used variables are expected to be the same every time; but I have other very similar functions that do almost the exact same thing, except that they compile and test a different .c file.

So, each of those functions can have a different value of RUN_WORKS, which is (as it turns out) written to the cache as INTERNAL. Suppose that I am not terminating the program but doing something else with the value of RUN_WORKS here. Then it could be that I first run this function where RUN_WORKS is true, and then run the other function which sets RUN_WORKS to false.

If then I run cmake again (or inadvertently call the first function again), then it has no way of knowing if the current value of RUN_WORKS in the cache has anything to do with a previous run of THIS function (and/or with the same arguments, if it has some). So, it CANNOT use the value of the cached variable. BUT - cmake is caching it. This worries me; why would it be caching anything if it isn't planning to reuse its value later? I'd feel a lot better if it didn't cache these variables.

So, my reasoning was: if anything is being cached (INTERNAL or not) I must make sure it is 100% unique (aka, that cmake will always write the same value to the same variable, or it wouldn't have made sense to cache it in the first place). But how to do that with this RUN_WORKS variable? I'm just horribly confused about how to write safe code like this :/



Solution 1:[1]

... can it call a given CMake function in parallel?

No, configuration stage of CMake, when it processes CMakeLists.txt and, among other things, executes its functions, is performed strictly sequentially.

Only build stage, when libraries and executables are compiled and COMMAND's (in add_custom_command/add_custom_target) are executed can be performed in parallel.

Moreover, during the build stage CMake isn't running at all: this stage is controlled by the build tool, selected as a generator for CMake.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Kevin