File tree Expand file tree Collapse file tree 4 files changed +24
-26
lines changed Expand file tree Collapse file tree 4 files changed +24
-26
lines changed Original file line number Diff line number Diff 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-
76include (cmake/utils.cmake)
8- set_project_is_top_level()
97
108add_library (mylib) # initialized below
119add_library (mylib::mylib ALIAS mylib)
@@ -16,6 +14,11 @@ add_library(mylib::mylib ALIAS mylib)
1614
1715include (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
2023option (MYLIB_BUILD_TESTS "Build mylib tests" OFF )
2124option (MYLIB_BUILD_EXAMPLES "Build mylib examples" OFF )
Original file line number Diff line number Diff line change @@ -7,30 +7,14 @@ macro(set_if_undefined variable)
77 endif ()
88endmacro ()
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.
3016function (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
Original file line number Diff line number Diff line change 11cmake_minimum_required (VERSION 3.14)
22project (mylib-add LANGUAGES CXX)
3-
43include ("../../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
710if (PROJECT_IS_TOP_LEVEL)
811 find_package (mylib REQUIRED)
@@ -14,4 +17,7 @@ source_group(TREE "${CMAKE_CURRENT_SOURCE_DIR}" FILES ${sources})
1417add_executable (mylib-add)
1518target_sources (mylib-add PRIVATE ${sources} )
1619target_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 ()
Original file line number Diff line number Diff line change 11cmake_minimum_required (VERSION 3.14)
22project (mylib-tests)
3-
43include ("../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+
1114if (PROJECT_IS_TOP_LEVEL)
1215 enable_testing ()
1316endif ()
@@ -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
6166include (GoogleTest)
6267gtest_discover_tests(mylib-tests)
You can’t perform that action at this time.
0 commit comments