General question about variable scope.

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

General question about variable scope.

Steven Truppe

Hi everyone,


i've the following code: 

macro(bsBuildLibExamples lib)
	# get all examples
	get_cmake_property(_vars VARIABLES)
	foreach(_var ${_vars})
	    string(TOUPPER ${lib} lib_upper)
	    set(WITH_LIB_${lib_upper}_EXAMPLES "")
	    if(_var MATCHES "^WITH_LIB_${lib_upper}_EXAMPLE_([A-Za-z]+)$")
		message(STATUS "Example found: ${CMAKE_MATCH_1}")
		list(APPEND ${WITH_LIB_${lib_upper}_EXAMPLES} ${CMAKE_MATCH_1})
             endif()
	endforeach()

    message(STATUS "Glad examples: ${WITH_LIB_GLAD_EXAMPLES}")
    message(STATUS "GLFW examples: ${WITH_LIB_GLFW_EXAMPLES}")

endmacro()


The problem is that ${WITH_LIB_${lib_upper}_EXAMPLES} is not available anymore after the foreach, i was not able to find something in the docs about this, i hope someone here can help me out.


best regrads!




--

Powered by www.kitware.com

Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ

Kitware offers various services to support the CMake community. For more information on each offering, please visit:

CMake Support: http://cmake.org/cmake/help/support.html
CMake Consulting: http://cmake.org/cmake/help/consulting.html
CMake Training Courses: http://cmake.org/cmake/help/training.html

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Follow this link to subscribe/unsubscribe:
https://cmake.org/mailman/listinfo/cmake
Reply | Threaded
Open this post in threaded view
|

Re: General question about variable scope.

Chuck Atkins
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

--

Powered by www.kitware.com

Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ

Kitware offers various services to support the CMake community. For more information on each offering, please visit:

CMake Support: http://cmake.org/cmake/help/support.html
CMake Consulting: http://cmake.org/cmake/help/consulting.html
CMake Training Courses: http://cmake.org/cmake/help/training.html

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Follow this link to subscribe/unsubscribe:
https://cmake.org/mailman/listinfo/cmake