Re: [CMake] Question about variables, cache, and scope
This works: set( project_count 0 CACHE INTERNAL ) function( define_project ) math( EXPR count ${project_count}+1 ) set( project_count ${count} CACHE INTERNAL ) endfunction() define_project() message(${project_count}) define_project() message(${project_count}) define_project() message(${project_count}) It prints out 1 2 3 Unfortunately this isn't a valid test case. You need to add each call to define_project() into a CMakeLists.txt script in a subdirectory, and then call add_subdirectory() from the root script. It won't work in that case in my tests. -- Powered by www.kitware.com Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ Follow this link to subscribe/unsubscribe: http://www.cmake.org/mailman/listinfo/cmake
Re: [CMake] Question about variables, cache, and scope
Doh :-) Thanks for pointing out what should perhaps have been obvious in retrospect. Cache variables are one of the more confusing areas of cmake. -- Glenn On 10 October 2011 22:38, Bill Hoffman bill.hoff...@kitware.com wrote: On 10/10/2011 3:52 PM, Robert Dailey wrote: Yes, this works perfectly. It's a bit disappointing that cache variables are, for all intents and purposes, read-only in functions. The property approach is a bit more verbose but it functions! I think 'set' needs a new override specifically for cases like this. Something similar to PARENT_SCOPE, but something like CACHE_SCOPE, that forces CMake to first check for the existance of a cache variable with that name, and it would take precedence over any identically named variable in function scope. On another note, you'd think this would work too but it doesn't: set( project_count ${new_count} CACHE INTERNAL FORCE ) This works: set( project_count 0 CACHE INTERNAL ) function( define_project ) math( EXPR count ${project_count}+1 ) set( project_count ${count} CACHE INTERNAL ) endfunction() define_project() message(${project_count}) define_project() message(${project_count}) define_project() message(${project_count}) It prints out 1 2 3 -Bill -- Powered by www.kitware.com Visit other Kitware open-source projects at http://www.kitware.com/** opensource/opensource.htmlhttp://www.kitware.com/opensource/opensource.html Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/**CMake_FAQhttp://www.cmake.org/Wiki/CMake_FAQ Follow this link to subscribe/unsubscribe: http://www.cmake.org/mailman/**listinfo/cmakehttp://www.cmake.org/mailman/listinfo/cmake -- Powered by www.kitware.com Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ Follow this link to subscribe/unsubscribe: http://www.cmake.org/mailman/listinfo/cmake
[CMake] Question about variables, cache, and scope
I have a function that I define in my top-most CMakeLists.txt file (on Windows using CMake version 2.8.6) called define_project() that calls add_executable, sets up compile defintions, etc etc. For each time define_project() is called *anywhere* in the directory hierarchy, I need to increment a global project_count variable to keep track of how many projects were created and print that at the very end of the root CMakeLists.txt file. So far my attempts at this have been unsuccessful. I tried creating a cache variable in the root script: set( project_count 0 CACHE INTERNAL ) Then inside of my function, I do this: function( define_project ... ) math( EXPR count ${project_count}+1 ) set( project_count ${count} ) endfunction() However, 'project_count' is always 0 each time that the function is executed. How can I make this work? - Robert Dailey -- Powered by www.kitware.com Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ Follow this link to subscribe/unsubscribe: http://www.cmake.org/mailman/listinfo/cmake
Re: [CMake] Question about variables, cache, and scope
Calling a function pushs a new variable scope. All variables visible in the callers scope are copied into the new scope but changes by default only affect the callee scope. You could try using the PARENT_SCOPE option to the set command but I'm not sure that will achieve what you want as it only gets you to the next scope whereas you really want a global variable. You can use properties instead of variables as those are explicitly scoped. So something like this: set_property(GLOBAL PROPERTY project_count 0) function( define_project ... ) get_property(old_count GLOBAL PROPERTY project_count) math( EXPR new_count ${old_count}+1 ) set_property(GLOBAL PROPERTY project_count ${new_count} endfunction() will probably work. -- Glenn On 10 October 2011 17:11, Robert Dailey rcdai...@gmail.com wrote: I have a function that I define in my top-most CMakeLists.txt file (on Windows using CMake version 2.8.6) called define_project() that calls add_executable, sets up compile defintions, etc etc. For each time define_project() is called *anywhere* in the directory hierarchy, I need to increment a global project_count variable to keep track of how many projects were created and print that at the very end of the root CMakeLists.txt file. So far my attempts at this have been unsuccessful. I tried creating a cache variable in the root script: set( project_count 0 CACHE INTERNAL ) Then inside of my function, I do this: function( define_project ... ) math( EXPR count ${project_count}+1 ) set( project_count ${count} ) endfunction() However, 'project_count' is always 0 each time that the function is executed. How can I make this work? - Robert Dailey -- Powered by www.kitware.com Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ Follow this link to subscribe/unsubscribe: http://www.cmake.org/mailman/listinfo/cmake -- Powered by www.kitware.com Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ Follow this link to subscribe/unsubscribe: http://www.cmake.org/mailman/listinfo/cmake
Re: [CMake] Question about variables, cache, and scope
Yes, this works perfectly. It's a bit disappointing that cache variables are, for all intents and purposes, read-only in functions. The property approach is a bit more verbose but it functions! I think 'set' needs a new override specifically for cases like this. Something similar to PARENT_SCOPE, but something like CACHE_SCOPE, that forces CMake to first check for the existance of a cache variable with that name, and it would take precedence over any identically named variable in function scope. On another note, you'd think this would work too but it doesn't: set( project_count ${new_count} CACHE INTERNAL FORCE ) Thanks! - Robert Dailey On Mon, Oct 10, 2011 at 2:01 PM, Glenn Coombs glenn.coo...@gmail.comwrote: Calling a function pushs a new variable scope. All variables visible in the callers scope are copied into the new scope but changes by default only affect the callee scope. You could try using the PARENT_SCOPE option to the set command but I'm not sure that will achieve what you want as it only gets you to the next scope whereas you really want a global variable. You can use properties instead of variables as those are explicitly scoped. So something like this: set_property(GLOBAL PROPERTY project_count 0) function( define_project ... ) get_property(old_count GLOBAL PROPERTY project_count) math( EXPR new_count ${old_count}+1 ) set_property(GLOBAL PROPERTY project_count ${new_count} endfunction() will probably work. -- Glenn On 10 October 2011 17:11, Robert Dailey rcdai...@gmail.com wrote: I have a function that I define in my top-most CMakeLists.txt file (on Windows using CMake version 2.8.6) called define_project() that calls add_executable, sets up compile defintions, etc etc. For each time define_project() is called *anywhere* in the directory hierarchy, I need to increment a global project_count variable to keep track of how many projects were created and print that at the very end of the root CMakeLists.txt file. So far my attempts at this have been unsuccessful. I tried creating a cache variable in the root script: set( project_count 0 CACHE INTERNAL ) Then inside of my function, I do this: function( define_project ... ) math( EXPR count ${project_count}+1 ) set( project_count ${count} ) endfunction() However, 'project_count' is always 0 each time that the function is executed. How can I make this work? - Robert Dailey -- Powered by www.kitware.com Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ Follow this link to subscribe/unsubscribe: http://www.cmake.org/mailman/listinfo/cmake -- Powered by www.kitware.com Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ Follow this link to subscribe/unsubscribe: http://www.cmake.org/mailman/listinfo/cmake
Re: [CMake] Question about variables, cache, and scope
On 10/10/2011 3:52 PM, Robert Dailey wrote: Yes, this works perfectly. It's a bit disappointing that cache variables are, for all intents and purposes, read-only in functions. The property approach is a bit more verbose but it functions! I think 'set' needs a new override specifically for cases like this. Something similar to PARENT_SCOPE, but something like CACHE_SCOPE, that forces CMake to first check for the existance of a cache variable with that name, and it would take precedence over any identically named variable in function scope. On another note, you'd think this would work too but it doesn't: set( project_count ${new_count} CACHE INTERNAL FORCE ) This works: set( project_count 0 CACHE INTERNAL ) function( define_project ) math( EXPR count ${project_count}+1 ) set( project_count ${count} CACHE INTERNAL ) endfunction() define_project() message(${project_count}) define_project() message(${project_count}) define_project() message(${project_count}) It prints out 1 2 3 -Bill -- Powered by www.kitware.com Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ Follow this link to subscribe/unsubscribe: http://www.cmake.org/mailman/listinfo/cmake