Skip to content

Commit 442b329

Browse files
author
Anton Pantyukhin
committed
Fix bug with determening whether project is top level
1 parent f59c009 commit 442b329

File tree

4 files changed

+24
-26
lines changed

4 files changed

+24
-26
lines changed

CMakeLists.txt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@ project(mylib
33
VERSION 1.0.0
44
DESCRIPTION "Template for C++ library built with CMake"
55
LANGUAGES CXX)
6-
76
include(cmake/utils.cmake)
8-
set_project_is_top_level()
97

108
add_library(mylib) # initialized below
119
add_library(mylib::mylib ALIAS mylib)
@@ -16,6 +14,11 @@ add_library(mylib::mylib ALIAS mylib)
1614

1715
include(GNUInstallDirs)
1816

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+
1922
# MYLIB_SHARED_LIBS option (undefined by default) can be used to force shared/static build
2023
option(MYLIB_BUILD_TESTS "Build mylib tests" OFF)
2124
option(MYLIB_BUILD_EXAMPLES "Build mylib examples" OFF)

cmake/utils.cmake

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,30 +7,14 @@ macro(set_if_undefined variable)
77
endif()
88
endmacro()
99

10-
# set_project_is_top_level()
11-
#
12-
# Sets variable PROJECT_IS_TOP_LEVEL for older CMake versions.
13-
macro(set_project_is_top_level)
14-
if(NOT DEFINED PROJECT_IS_TOP_LEVEL)
15-
if("${CMAKE_SOURCE_DIR}" STREQUAL "${CMAKE_CURRENT_SOURCE_DIR}")
16-
set(PROJECT_IS_TOP_LEVEL YES)
17-
else()
18-
set(PROJECT_IS_TOP_LEVEL NO)
19-
endif()
20-
endif()
21-
endmacro()
22-
2310
# win_copy_deps_to_target_dir(<target> [<target-dep>]...)
2411
#
2512
# Creates custom command to copy runtime dependencies to target's directory after building the target.
26-
# Function does nothing if platform is not Windows or current project is built as stand-alone (top-level)
27-
# project. Additionally, it ignores all dependencies except shared libraries.
13+
# Function does nothing if platform is not Windows and ignores all dependencies except shared libraries.
2814
# On CMake 3.21 or newer, function uses TARGET_RUNTIME_DLLS generator expression to obtain list of runtime
2915
# dependencies. Specified dependencies (if any) are still used to find and copy PDB files for debug builds.
3016
function(win_copy_deps_to_target_dir target)
31-
set_project_is_top_level()
32-
33-
if(NOT WIN32 OR PROJECT_IS_TOP_LEVEL)
17+
if(NOT WIN32)
3418
return()
3519
endif()
3620

examples/add/CMakeLists.txt

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
cmake_minimum_required(VERSION 3.14)
22
project(mylib-add LANGUAGES CXX)
3-
43
include("../../cmake/utils.cmake")
5-
set_project_is_top_level()
4+
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()
69

710
if(PROJECT_IS_TOP_LEVEL)
811
find_package(mylib REQUIRED)
@@ -14,4 +17,7 @@ source_group(TREE "${CMAKE_CURRENT_SOURCE_DIR}" FILES ${sources})
1417
add_executable(mylib-add)
1518
target_sources(mylib-add PRIVATE ${sources})
1619
target_link_libraries(mylib-add PRIVATE mylib::mylib)
17-
win_copy_deps_to_target_dir(mylib-add mylib::mylib)
20+
21+
if(NOT PROJECT_IS_TOP_LEVEL)
22+
win_copy_deps_to_target_dir(mylib-add mylib::mylib)
23+
endif()

tests/CMakeLists.txt

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
cmake_minimum_required(VERSION 3.14)
22
project(mylib-tests)
3-
43
include("../cmake/utils.cmake")
5-
set_project_is_top_level()
64

75
#----------------------------------------------------------------------------------------------------------------------
86
# general settings and options
97
#----------------------------------------------------------------------------------------------------------------------
108

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+
1114
if(PROJECT_IS_TOP_LEVEL)
1215
enable_testing()
1316
endif()
@@ -56,7 +59,9 @@ target_link_libraries(mylib-tests
5659
mylib::mylib
5760
gtest_main)
5861

59-
win_copy_deps_to_target_dir(mylib-tests mylib::mylib)
62+
if(NOT PROJECT_IS_TOP_LEVEL)
63+
win_copy_deps_to_target_dir(mylib-tests mylib::mylib)
64+
endif()
6065

6166
include(GoogleTest)
6267
gtest_discover_tests(mylib-tests)

0 commit comments

Comments
 (0)