Skip to content

Commit eca0eca

Browse files
hdf89shfdfsJuan RamosJordanMaples
authored
[cmake] Adding options for INSTALL and TEST (#964)
* [cmake] Adding GSL_INSTALL option Not all consumers of GSL automatically want to have this install logic. It's good practice to gate install logic behind an option. For an example look at magic_enum: https://github.com/Neargye/magic_enum/blob/master/CMakeLists.txt If the client wants to install GSL they still can. But they should ask for it by overriding GSL_INSTALL. * Update cmake/guidelineSupportLibrary.cmake added nl@eof * Update CMakeLists.txt * Update CMakeLists.txt Co-authored-by: Juan Ramos <juanr0911@gmail.com> Co-authored-by: Jordan Maples [MSFT] <49793787+JordanMaples@users.noreply.github.com>
1 parent a47352c commit eca0eca

3 files changed

Lines changed: 74 additions & 48 deletions

File tree

CMakeLists.txt

Lines changed: 15 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ cmake_minimum_required(VERSION 3.1.3...3.16)
33
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake/")
44
include(guidelineSupportLibrary)
55

6-
project(GSL VERSION 3.1.0 LANGUAGES CXX)
7-
8-
# Use GNUInstallDirs to provide the right locations on all platforms
9-
include(GNUInstallDirs)
6+
project(GSL
7+
VERSION 3.1.0
8+
LANGUAGES CXX
9+
)
1010

1111
# Creates a library GSL which is an interface (header files only)
1212
add_library(GSL INTERFACE)
@@ -25,6 +25,10 @@ if (CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR)
2525
set(GSL_STANDALONE_PROJECT ON)
2626
endif()
2727

28+
### Project options
29+
option(GSL_INSTALL "Generate and install GSL target" ${GSL_STANDALONE_PROJECT})
30+
option(GSL_TEST "Build and perform GSL tests" ${GSL_STANDALONE_PROJECT})
31+
2832
# This GSL implementation generally assumes a platform that implements C++14 support.
2933
set(gsl_min_cxx_standard "14")
3034

@@ -34,58 +38,21 @@ else()
3438
gsl_client_set_cxx_standard(${gsl_min_cxx_standard})
3539
endif()
3640

37-
# add include folders to the library and targets that consume it
38-
# the SYSTEM keyword suppresses warnings for users of the library
39-
if(GSL_STANDALONE_PROJECT)
40-
target_include_directories(GSL INTERFACE
41-
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
42-
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
43-
)
44-
else()
45-
target_include_directories(GSL SYSTEM INTERFACE
46-
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
47-
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
48-
)
49-
endif()
41+
# Setup the include directory
42+
gsl_target_include_directories(${GSL_STANDALONE_PROJECT})
5043

5144
# Add natvis file
5245
gsl_add_native_visualizer_support()
5346

54-
install(TARGETS GSL EXPORT Microsoft.GSLConfig)
55-
install(
56-
DIRECTORY include/gsl
57-
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
58-
)
59-
# Make library importable by other projects
60-
install(EXPORT Microsoft.GSLConfig NAMESPACE Microsoft.GSL:: DESTINATION ${CMAKE_INSTALL_DATADIR}/cmake/Microsoft.GSL)
61-
export(TARGETS GSL NAMESPACE Microsoft.GSL:: FILE Microsoft.GSLConfig.cmake)
47+
# Add packaging support
48+
gsl_create_packaging_file()
6249

63-
# Add find_package() versioning support. The version for
64-
# generated Microsoft.GSLConfigVersion.cmake will be used from
65-
# last project() command. The version's compatibility is set between all
66-
# minor versions (as it was in prev. GSL releases).
67-
include(CMakePackageConfigHelpers)
68-
if(${CMAKE_VERSION} VERSION_LESS "3.14.0")
69-
write_basic_package_version_file(
70-
${CMAKE_CURRENT_BINARY_DIR}/Microsoft.GSLConfigVersion.cmake
71-
COMPATIBILITY SameMajorVersion
72-
)
73-
else()
74-
write_basic_package_version_file(
75-
${CMAKE_CURRENT_BINARY_DIR}/Microsoft.GSLConfigVersion.cmake
76-
COMPATIBILITY SameMajorVersion
77-
ARCH_INDEPENDENT
78-
)
50+
if (GSL_INSTALL)
51+
# Setup install/export logic
52+
gsl_install_logic()
7953
endif()
80-
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/Microsoft.GSLConfigVersion.cmake DESTINATION ${CMAKE_INSTALL_DATADIR}/cmake/Microsoft.GSL)
8154

82-
option(GSL_TEST "Generate tests." ${GSL_STANDALONE_PROJECT})
8355
if (GSL_TEST)
8456
enable_testing()
85-
if(IOS)
86-
add_compile_definitions(
87-
GTEST_HAS_DEATH_TEST=1
88-
)
89-
endif()
9057
add_subdirectory(tests)
9158
endif()

cmake/guidelineSupportLibrary.cmake

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ if (DEFINED guideline_support_library_include_guard)
99
endif()
1010
set(guideline_support_library_include_guard ON)
1111

12+
# Neccessary for 'write_basic_package_version_file'
13+
include(CMakePackageConfigHelpers)
14+
1215
function(gsl_set_default_cxx_standard min_cxx_standard)
1316
set(GSL_CXX_STANDARD "${min_cxx_standard}" CACHE STRING "Use c++ standard")
1417

@@ -75,3 +78,55 @@ function(gsl_add_native_visualizer_support)
7578
endif()
7679
endif()
7780
endfunction()
81+
82+
function(gsl_target_include_directories is_standalone)
83+
# Add include folders to the library and targets that consume it
84+
# the SYSTEM keyword suppresses warnings for users of the library
85+
if(${is_standalone})
86+
target_include_directories(GSL INTERFACE
87+
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
88+
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
89+
)
90+
else()
91+
target_include_directories(GSL SYSTEM INTERFACE
92+
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
93+
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
94+
)
95+
endif()
96+
endfunction()
97+
98+
function(gsl_install_logic)
99+
# Use GNUInstallDirs to provide the right locations on all platforms
100+
# NOTE: Including GNUInstallDirs automatically executes logic
101+
include(GNUInstallDirs)
102+
103+
install(TARGETS GSL EXPORT Microsoft.GSLConfig)
104+
install(
105+
DIRECTORY include/gsl
106+
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
107+
)
108+
# Make library importable by other projects
109+
install(EXPORT Microsoft.GSLConfig NAMESPACE Microsoft.GSL:: DESTINATION ${CMAKE_INSTALL_DATADIR}/cmake/Microsoft.GSL)
110+
export(TARGETS GSL NAMESPACE Microsoft.GSL:: FILE Microsoft.GSLConfig.cmake)
111+
112+
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/Microsoft.GSLConfigVersion.cmake DESTINATION ${CMAKE_INSTALL_DATADIR}/cmake/Microsoft.GSL)
113+
endfunction()
114+
115+
# Add find_package() versioning support. The version for
116+
# generated Microsoft.GSLConfigVersion.cmake will be used from
117+
# last project() command. The version's compatibility is set between all
118+
# minor versions (as it was in prev. GSL releases).
119+
function(gsl_create_packaging_file)
120+
if(${CMAKE_VERSION} VERSION_LESS "3.14.0")
121+
write_basic_package_version_file(
122+
${CMAKE_CURRENT_BINARY_DIR}/Microsoft.GSLConfigVersion.cmake
123+
COMPATIBILITY SameMajorVersion
124+
)
125+
else()
126+
write_basic_package_version_file(
127+
${CMAKE_CURRENT_BINARY_DIR}/Microsoft.GSLConfigVersion.cmake
128+
COMPATIBILITY SameMajorVersion
129+
ARCH_INDEPENDENT
130+
)
131+
endif()
132+
endfunction()

tests/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ find_package(Git REQUIRED QUIET)
1010
# will make visual studio generated project group files
1111
set_property(GLOBAL PROPERTY USE_FOLDERS ON)
1212

13+
if(IOS)
14+
add_compile_definitions(GTEST_HAS_DEATH_TEST=1)
15+
endif()
16+
1317
pkg_search_module(GTestMain gtest_main)
1418
if (NOT GTestMain_FOUND)
1519
configure_file(CMakeLists.txt.in googletest-download/CMakeLists.txt)

0 commit comments

Comments
 (0)