Add `include_guard` command identical to C++ #pragma_once
I suggest adding new command
include_guard()
which prevents a CMake script file from being processed more than once, as with #pragma_once
in C++ language, for example.
We are widely using pragma_once
-style macro in our internal CMake framework to speed up CMake configure step. It would be great to have built-in functionality for that.
Current idiomatic implementation would be:
macro(my_pragma_once)
set(__filename "${CMAKE_CURRENT_LIST_FILE}")
get_property(already_included GLOBAL PROPERTY "pr_${__filename}")
if(already_included)
return()
endif()
set_property(GLOBAL PROPERTY "pr_${__filename}" TRUE)
endmacro()
usage:
my_pragma_once()
at the beginning of CMake module that is shared across multiple places.
Also there are usages in standard CMake modules library, i.e.
if(__COMPILER_CLANG)
return()
endif()
set(__COMPILER_CLANG 1)
in Modules/Compiler/Clang.cmake. There are also many others just like that.
I already have a branch to implement this feature, opening this issue to get feedback on the topic, i.e. should that be implemented or not (not generic enough/smth else).
Any suggestions are welcome.