[CMake] General question about variable scope.

Chuck Atkins chuck.atkins at kitware.com
Fri Jun 14 09:08:29 EDT 2019


So, a couple things:

	    string(TOUPPER ${lib} lib_upper)
> 	    set(WITH_LIB_${lib_upper}_EXAMPLES "")
>
> This needs to be outside the foreach loop.  It's getting reset to empty on
every iteration rather than accumulating results

		list(APPEND ${WITH_LIB_${lib_upper}_EXAMPLES} ${CMAKE_MATCH_1})
>
> Don't de-reference the first argument, just use the variable name itself,
i.e.:
list(APPEND WITH_LIB_${lib_upper}_EXAMPLES ${CMAKE_MATCH_1})

This is also well suited to a function, which are generally preferred over
macros as it will avoid polluting the current variable scope with temporary
variables:

function(bsBuildLibExamples lib)
  string(TOUPPER ${lib} lib_upper)
  set(all_lib_examples)
  get_cmake_property(all_vars VARIABLES)
  foreach(var IN LISTS all_vars)
    if(var MATCHES "^WITH_LIB_${lib_upper}_EXAMPLE_([A-Za-z]+)$")
      list(APPEND all_lib_examples ${CMAKE_MATCH_1})
    endif()
  endforeach()
  set(WITH_LIB_${lib_upper}_EXAMPLES "${all_lib_examples}" PARENT_SCOPE)
endmacro()

- Chuck
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://cmake.org/pipermail/cmake/attachments/20190614/f39e3767/attachment-0001.html>


More information about the CMake mailing list