Skip to content

Commit 5745f3f

Browse files
author
Anton Pantyukhin
committed
Fix problems with determening whether project is build as subproject
1 parent 442b329 commit 5745f3f

File tree

3 files changed

+12
-21
lines changed

3 files changed

+12
-21
lines changed

CMakeLists.txt

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,19 @@ include(cmake/utils.cmake)
88
add_library(mylib) # initialized below
99
add_library(mylib::mylib ALIAS mylib)
1010

11+
string(COMPARE EQUAL "${CMAKE_SOURCE_DIR}" "${CMAKE_CURRENT_SOURCE_DIR}" is_top_level)
12+
1113
#----------------------------------------------------------------------------------------------------------------------
1214
# general settings and options
1315
#----------------------------------------------------------------------------------------------------------------------
1416

1517
include(GNUInstallDirs)
1618

17-
if(NOT DEFINED PROJECT_IS_TOP_LEVEL AND "${CMAKE_SOURCE_DIR}" STREQUAL "${CMAKE_CURRENT_SOURCE_DIR}")
18-
# support older CMake versions (prior 3.21) which do not define this variable
19-
set(PROJECT_IS_TOP_LEVEL YES)
20-
endif()
21-
2219
# MYLIB_SHARED_LIBS option (undefined by default) can be used to force shared/static build
2320
option(MYLIB_BUILD_TESTS "Build mylib tests" OFF)
2421
option(MYLIB_BUILD_EXAMPLES "Build mylib examples" OFF)
2522
option(MYLIB_BUILD_DOCS "Build mylib documentation" OFF)
26-
option(MYLIB_INSTALL "Generate target for installing mylib" ${PROJECT_IS_TOP_LEVEL})
23+
option(MYLIB_INSTALL "Generate target for installing mylib" ${is_top_level})
2724
set_if_undefined(MYLIB_INSTALL_CMAKEDIR "${CMAKE_INSTALL_LIBDIR}/cmake/mylib-${PROJECT_VERSION}" CACHE
2825
STRING "Install path for mylib package-related CMake files")
2926

@@ -148,7 +145,7 @@ endif()
148145
#----------------------------------------------------------------------------------------------------------------------
149146

150147
if(MYLIB_BUILD_TESTS)
151-
if(PROJECT_IS_TOP_LEVEL)
148+
if(is_top_level)
152149
enable_testing()
153150
endif()
154151

examples/add/CMakeLists.txt

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,9 @@ cmake_minimum_required(VERSION 3.14)
22
project(mylib-add LANGUAGES CXX)
33
include("../../cmake/utils.cmake")
44

5-
if(NOT DEFINED PROJECT_IS_TOP_LEVEL AND "${CMAKE_SOURCE_DIR}" STREQUAL "${CMAKE_CURRENT_SOURCE_DIR}")
6-
# support older CMake versions (prior 3.21) which do not define this variable
7-
set(PROJECT_IS_TOP_LEVEL YES)
8-
endif()
5+
string(COMPARE EQUAL "${CMAKE_SOURCE_DIR}" "${CMAKE_CURRENT_SOURCE_DIR}" is_top_level)
96

10-
if(PROJECT_IS_TOP_LEVEL)
7+
if(is_top_level)
118
find_package(mylib REQUIRED)
129
endif()
1310

@@ -18,6 +15,6 @@ add_executable(mylib-add)
1815
target_sources(mylib-add PRIVATE ${sources})
1916
target_link_libraries(mylib-add PRIVATE mylib::mylib)
2017

21-
if(NOT PROJECT_IS_TOP_LEVEL)
18+
if(NOT is_top_level)
2219
win_copy_deps_to_target_dir(mylib-add mylib::mylib)
2320
endif()

tests/CMakeLists.txt

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,13 @@ cmake_minimum_required(VERSION 3.14)
22
project(mylib-tests)
33
include("../cmake/utils.cmake")
44

5+
string(COMPARE EQUAL "${CMAKE_SOURCE_DIR}" "${CMAKE_CURRENT_SOURCE_DIR}" is_top_level)
6+
57
#----------------------------------------------------------------------------------------------------------------------
68
# general settings and options
79
#----------------------------------------------------------------------------------------------------------------------
810

9-
if(NOT DEFINED PROJECT_IS_TOP_LEVEL AND "${CMAKE_SOURCE_DIR}" STREQUAL "${CMAKE_CURRENT_SOURCE_DIR}")
10-
# support older CMake versions (prior 3.21) which do not define this variable
11-
set(PROJECT_IS_TOP_LEVEL YES)
12-
endif()
13-
14-
if(PROJECT_IS_TOP_LEVEL)
11+
if(is_top_level)
1512
enable_testing()
1613
endif()
1714

@@ -35,7 +32,7 @@ FetchContent_MakeAvailable(googletest)
3532
# tests dependencies
3633
#----------------------------------------------------------------------------------------------------------------------
3734

38-
if(PROJECT_IS_TOP_LEVEL)
35+
if(is_top_level)
3936
find_package(mylib REQUIRED)
4037
endif()
4138

@@ -59,7 +56,7 @@ target_link_libraries(mylib-tests
5956
mylib::mylib
6057
gtest_main)
6158

62-
if(NOT PROJECT_IS_TOP_LEVEL)
59+
if(NOT is_top_level)
6360
win_copy_deps_to_target_dir(mylib-tests mylib::mylib)
6461
endif()
6562

0 commit comments

Comments
 (0)