CMake detect if project is top level
CMake can detect if a project is “top level” that is, NOT via FetchContent using PROJECT_IS_TOP_LEVEL.
cmake_minimum_required(VERSION 3.21)
project(child)
if(PROJECT_IS_TOP_LEVEL)
message(STATUS "${PROJECT_NAME} directly building, not FetchContent")
endif()
Note that directory property
PARENT_DIRECTORY
and PROJECT_IS_TOP_LEVEL
properties are NOT useful for detecting if the child project is being used as an ExternalProject.
For CMake < 3.21, “PROJECT_IS_TOP_LEVEL” can be set like:
if(CMAKE_VERSION VERSION_LESS 3.21)
get_property(not_top DIRECTORY PROPERTY PARENT_DIRECTORY)
if(NOT not_top)
set(PROJECT_IS_TOP_LEVEL true)
endif()
endif()