zstd
zstd copied to clipboard
Detect when CMake is used as a subproject
CMake allows including the project in other projects (a subproject). This allows libraries to be complied in as a dependency in other CMake projects.
Zstd's current CMake declares targets named clean-all and uninstall which could pollute the namespace of other projects that include Zstd through CMake (#3279).
One way other projects (such as gflags) deal with similar situations is to not add the commands when including as a subproject. Gflags allows the user to specify a flag that indicates if the projects is a subproject and falls-back to automatic detection if the flag isn't available, like so:
# when gflags is included as subproject (e.g., as Git submodule/subtree) in the source
# tree of a project that uses it, no variables should be added to the CMake cache;
# users may set the non-cached variable GFLAGS_IS_SUBPROJECT before add_subdirectory(gflags)
if (NOT DEFINED GFLAGS_IS_SUBPROJECT)
if ("^${CMAKE_SOURCE_DIR}$" STREQUAL "^${PROJECT_SOURCE_DIR}$")
set (GFLAGS_IS_SUBPROJECT FALSE)
else ()
set (GFLAGS_IS_SUBPROJECT TRUE)
endif ()
endif ()
This same technique can be adopted to Zstd to control target inclusion only if not included as a subproject.