Skip to content

Commit 222ecaf

Browse files
Merge #1284: cmake: Some improvements using PROJECT_IS_TOP_LEVEL variable
71f746c cmake: Include `include` directory for subtree builds (Hennadii Stepanov) 5431b9d cmake: Make `SECP256K1_INSTALL` default depend on `PROJECT_IS_TOP_LEVEL` (Hennadii Stepanov) 162608c cmake: Emulate `PROJECT_IS_TOP_LEVEL` for CMake<3.21 (Hennadii Stepanov) Pull request description: This PR: 1. Emulates [`PROJECT_IS_TOP_LEVEL`](https://cmake.org/cmake/help/latest/variable/PROJECT_IS_TOP_LEVEL.html) variable for CMake versions where it is not available. 2. Makes the `SECP256K1_INSTALL` option dependent on `PROJECT_IS_TOP_LEVEL` (a [follow up](#1263 (comment)) of #1263). 3. Makes integration of this project as a subtree easier. A top project can `#include <secp256k1.h>` with no additional `target_include_directories()` commands. For example, see https://github.com/hebasto/secp256k1-CMake-example/tree/subtree. ACKs for top commit: theuni: utACK 71f746c. Tree-SHA512: 8ccdbcc94b26f36e772611ebaab0f2846debd6ad20f9e361be31a8d2128a14273acb692b0631026e12cc6cdef6d445dce0fd3beb4f71af47b46dfcf840a18879
2 parents 024a409 + 71f746c commit 222ecaf

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

CMakeLists.txt

+14-2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,18 @@ project(libsecp256k1
1717
LANGUAGES C
1818
)
1919

20+
if(CMAKE_VERSION VERSION_LESS 3.21)
21+
get_directory_property(parent_directory PARENT_DIRECTORY)
22+
if(parent_directory)
23+
set(PROJECT_IS_TOP_LEVEL OFF CACHE INTERNAL "Emulates CMake 3.21+ behavior.")
24+
set(${PROJECT_NAME}_IS_TOP_LEVEL OFF CACHE INTERNAL "Emulates CMake 3.21+ behavior.")
25+
else()
26+
set(PROJECT_IS_TOP_LEVEL ON CACHE INTERNAL "Emulates CMake 3.21+ behavior.")
27+
set(${PROJECT_NAME}_IS_TOP_LEVEL ON CACHE INTERNAL "Emulates CMake 3.21+ behavior.")
28+
endif()
29+
unset(parent_directory)
30+
endif()
31+
2032
# The library version is based on libtool versioning of the ABI. The set of
2133
# rules for updating the version can be found here:
2234
# https://www.gnu.org/software/libtool/manual/html_node/Updating-version-info.html
@@ -31,13 +43,13 @@ set(CMAKE_C_EXTENSIONS OFF)
3143

3244
list(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake)
3345

34-
option(BUILD_SHARED_LIBS "Build shared libraries" ON)
46+
option(BUILD_SHARED_LIBS "Build shared libraries." ON)
3547
option(SECP256K1_DISABLE_SHARED "Disable shared library. Overrides BUILD_SHARED_LIBS." OFF)
3648
if(SECP256K1_DISABLE_SHARED)
3749
set(BUILD_SHARED_LIBS OFF)
3850
endif()
3951

40-
option(SECP256K1_INSTALL "Enable installation" ON)
52+
option(SECP256K1_INSTALL "Enable installation." ${PROJECT_IS_TOP_LEVEL})
4153

4254
option(SECP256K1_ENABLE_MODULE_ECDH "Enable ECDH module." ON)
4355
if(SECP256K1_ENABLE_MODULE_ECDH)

src/CMakeLists.txt

+3-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@ endif()
3131
get_target_property(use_pic secp256k1 POSITION_INDEPENDENT_CODE)
3232
set_target_properties(secp256k1_precomputed PROPERTIES POSITION_INDEPENDENT_CODE ${use_pic})
3333

34-
target_include_directories(secp256k1 PUBLIC
34+
target_include_directories(secp256k1 INTERFACE
35+
# Add the include path for parent projects so that they don't have to manually add it.
36+
$<BUILD_INTERFACE:$<$<NOT:$<BOOL:${PROJECT_IS_TOP_LEVEL}>>:${PROJECT_SOURCE_DIR}/include>>
3537
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
3638
)
3739
set_target_properties(secp256k1 PROPERTIES

0 commit comments

Comments
 (0)