about include_guard

Hi guys,

I have the following setup

CMakeLists.txt

list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR})
add_subdirectory(sub)
add_subdirectory(sub2)

tools.cmake

include_guard(DIRECTORY) # A
message("tools.cmake")

sub\CMakeLists.txt

message(STATUS "in sub")
include(tools)
add_subdirectory(sub)

sub\sub\CMakeLists.txt

message(STATUS "in sub/sub")
include(tools)

sub2\CMakeLists.txt

message(STATUS "in sub2")
include(tools)

got the following output

-- in sub
tools.cmake
-- in sub/sub
-- in sub2
tools.cmake

change line A to include_guard() got the same output

-- in sub
tools.cmake
-- in sub/sub
-- in sub2
tools.cmake

does this mean include_guard(DIRECTORY) is the same as include_guard()?

The documentation clearly specifies:

DIRECTORY: The include guard applies within the current directory and below.

So the guard apply to the subdirectories…

include_guard() only apply to the current directory and not include its subdirectory? If so, the output should be

-- in sub
tools.cmake
-- in sub/sub
tools.cmake
-- in sub2
tools.cmake

but it is not.

include_guard() has variable scope, include_guard(DIRECTORY) has directory scope, include_guard(GLOBAL) has global scope.

In your example, variable scope and directory scope have the same results.

thank you. I got it. If put include(tools) in a function, include_guard() and include_guard(DIRECTORY) will behave differently.