PROJECT_IS_TOP_LEVEL — CMake 4.3.3 Documentation

# PROJECT_IS_TOP_LEVEL

Added in version 3.21.

A boolean variable indicating whether the most recently called `project()` command in the current scope or above was in the top level `CMakeLists.txt` file.

Some modules should only be included as part of the top level `CMakeLists.txt` file to not cause unintended side effects in the build tree, and this variable can be used to conditionally execute such code. For example, consider the `CTest` module, which creates targets and options:

    project(MyProject)
    ...
    if(PROJECT_IS_TOP_LEVEL)
      include(CTest)
    endif()

The variable value will be true in:

- the top-level directory of the project

- the top-level directory of an external project added by `ExternalProject`

- a directory added by `add_subdirectory()` that does not also contain a `project()` call

- a directory added by `FetchContent_MakeAvailable()`, if the fetched content does not contain a `project()` call

The variable value will be false in:

- a directory added by `add_subdirectory()` that also contains a `project()` call

- a directory added by `FetchContent_MakeAvailable()`, if the fetched content contains a `project()` call