Re: [CMake] Are the current scoping rules for functions intended and documented?
Hi Brad, unfortunately I failed to file an issue to keep track of this feature request. Gitlab always recognizes the text as spam and discards it. I tried the following title and text: Title - Function and macro definitions should obey the same scoping rules as variables Text Hi all, as Brad confirmed on the [mailing list](https://www.mail-archive.com/cmake@cmake.org/msg55886.html), functions and macros are currently scoped globally. This means that any function definition in a sub scope (e.g., created via `add_subdirectory`) can redefine functions for the parent. The fact that CMake also always picks the last definition it encounters additionally introduces the problem that the order in which CMakeLists are processed can change the behaviour of the build system. This feature request therefore propose that function and macro definitions should obey the same scoping rules as variables to mitigate this potential pitfall. Do you have any recommendations on how I can resolve this? Cheers, Mario -- 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: http://public.kitware.com/mailman/listinfo/cmake
Re: [CMake] Are the current scoping rules for functions intended and documented?
Hi Brad, thank you very much for the clarification and the background information! I definitely have to scrutinize our CMake code to make sure that no additional problems are lurking in our build system. For now, I am going to file a feature request to keep track of the idea of properly scoping function and macro definitions. As soon as I have more time on my hands I may come back to it and give it try. Cheers, Mario On 2016-11-14 19:47, Brad King wrote: > On 11/14/2016 10:29 AM, Mario Werner wrote: >> I was just surprised by the scoping rules of CMake when it comes to >> function definitions and sub scopes (i.e., subdirectories). I expected >> that function definitions adhere to the same scoping rules as variables. > > That would be nice behavior. > >> However, as it turns out, (at least CMake 3.5.1) this is not the case. >> CMake seems to use the last definition of a function it encounters, >> independent of current scope. > > Commands (builtin+functions+macros) are currently scoped globally. > >> Now I am wondering if this behaviour is actually intended or if it is a >> corner case which just never surfaced before. > > It just happened to be implemented that way from the beginning back in > our wild west days without much thought to scoping. It is indeed not > documented AFAIK, but a good place to add this to the docs might be here: > > > https://cmake.org/cmake/help/v3.7/manual/cmake-language.7.html#command-definitions > > It is also something that could be changed by the introduction of a > new CMake Policy, though it would be a bit of work to get right. > > -Brad > -- 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: http://public.kitware.com/mailman/listinfo/cmake
Re: [CMake] Are the current scoping rules for functions intended and documented?
On 11/14/2016 10:29 AM, Mario Werner wrote: > I was just surprised by the scoping rules of CMake when it comes to > function definitions and sub scopes (i.e., subdirectories). I expected > that function definitions adhere to the same scoping rules as variables. That would be nice behavior. > However, as it turns out, (at least CMake 3.5.1) this is not the case. > CMake seems to use the last definition of a function it encounters, > independent of current scope. Commands (builtin+functions+macros) are currently scoped globally. > Now I am wondering if this behaviour is actually intended or if it is a > corner case which just never surfaced before. It just happened to be implemented that way from the beginning back in our wild west days without much thought to scoping. It is indeed not documented AFAIK, but a good place to add this to the docs might be here: https://cmake.org/cmake/help/v3.7/manual/cmake-language.7.html#command-definitions It is also something that could be changed by the introduction of a new CMake Policy, though it would be a bit of work to get right. -Brad -- 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: http://public.kitware.com/mailman/listinfo/cmake
[CMake] Are the current scoping rules for functions intended and documented?
Hi, I was just surprised by the scoping rules of CMake when it comes to function definitions and sub scopes (i.e., subdirectories). I expected that function definitions adhere to the same scoping rules as variables. More concretely, I assumed that function definitions in a sub scope are only valid in the same (or deeper) scope(s) but do not influence definitions in potential parent scopes. However, as it turns out, (at least CMake 3.5.1) this is not the case. CMake seems to use the last definition of a function it encounters, independent of current scope. I uploaded a quick minimal working example which shows what I mean under [1]. I just discovered this behaviour by hunting down a bug in our build system which was caused by the fact that a function definition in a sub project overwrote an identically named function in the parent project. Now I am wondering if this behaviour is actually intended or if it is a corner case which just never surfaced before. The documentation on functions [2] unfortunately also omits how this is supposed to behave. Any pointers would be appreciated. Regards, Mario [1] https://github.com/niosHD/mwe-cmake-function-scopes [2] https://cmake.org/cmake/help/v3.7/command/function.html -- 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: http://public.kitware.com/mailman/listinfo/cmake