facebook / facebook/zstd

Detect when CMake is used as a subproject

Open
#3,468 0 comments 2 reactions 0 assignees View on GitHub
build
Dominant language
C
Stars
27.9k
Forks
2.6k
Avg merge
1d 3h
Merged PRs (30d)
8

Description

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:
```cmake
# 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.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.