-
-
Notifications
You must be signed in to change notification settings - Fork 16
/
CMakeLists.txt
170 lines (134 loc) · 5.69 KB
/
CMakeLists.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
cmake_minimum_required(VERSION 3.5)
if (PROJECT_NAME)
set(IS_SUBPROJECT TRUE)
endif ()
set(RESULT_CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/cmake")
set(RESULT_CMAKE_TEMPLATE_PATH "${CMAKE_CURRENT_LIST_DIR}/cmake/templates")
set(CMAKE_MODULE_PATH "${RESULT_CMAKE_MODULE_PATH}" "${CMAKE_MODULE_PATH}")
option(RESULT_COMPILE_UNIT_TESTS "Compile and run the unit tests for this library" OFF)
if (NOT CMAKE_TESTING_ENABLED AND RESULT_COMPILE_UNIT_TESTS)
enable_testing()
endif ()
project(Result
VERSION "1.0.0"
LANGUAGES CXX
)
set(RESULT_VERSION_MAJOR ${PROJECT_VERSION_MAJOR} CACHE INTERNAL "Major version of Result")
set(RESULT_VERSION_MINOR ${PROJECT_VERSION_MINOR} CACHE INTERNAL "Minor version of Result")
set(RESULT_VERSION_PATCH ${PROJECT_VERSION_PATCH} CACHE INTERNAL "Patch version of Result")
set(RESULT_VERSION ${PROJECT_VERSION} CACHE INTERNAL "Version of Result")
if (EXISTS "${CMAKE_CURRENT_BINARY_DIR}/conanbuildinfo.cmake")
include("${CMAKE_CURRENT_BINARY_DIR}/conanbuildinfo.cmake")
conan_set_find_paths()
endif ()
##############################################################################
# Targets
##############################################################################
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
set(header_files
include/result.hpp
)
add_library(${PROJECT_NAME} INTERFACE)
add_library(${PROJECT_NAME}::${PROJECT_NAME} ALIAS ${PROJECT_NAME})
target_include_directories(${PROJECT_NAME}
INTERFACE $<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/include>
INTERFACE $<INSTALL_INTERFACE:include>
)
if ("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang" AND
"${CMAKE_CXX_SIMULATE_ID}" STREQUAL "MSVC")
# clang-cl does not appear to implement '-pedantic' or 'pedantic-errors',
# so this case needs to be handled specifically
add_compile_options(-Wall -Werror -Wextra)
# clang-cl treats '-Wall' like '-Weverything' currently; so there are a few
# warnings we need to manually disable.
# Disable unnecessary compatibility and 'newline-eof'. This is a modern C++
# library -- so these serve no purpose
add_compile_options(-Wno-c++98-compat -Wno-c++98-compat-pedantic -Wno-newline-eof)
# Disable warnings on padding. This is a great warning, but not good when
# coupled with -Werror
add_compile_options(-Wno-padded)
# Several utilities require static objects that have exit-time destructors,
# including 'error_category' types from the standard.
add_compile_options(-Wno-exit-time-destructors)
# This is erroring on valid uses of the standard library, like std::lock_guard.
add_compile_options(-Wno-ctad-maybe-unsupported)
# This is a good warning *in general*, but not when using MSVC's standard
# library, which requires defining "_" prefixed macros just to have it operate
# like the C++ standard dictates.
add_compile_options(-Wno-reserved-id-macro)
# Don't warn on ignored attributes; this breaks builds using dllexport/dllimport on
# classes that may also define inline functions
add_compile_options(-Wno-ignored-attributes)
elseif ("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang" OR
"${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
add_compile_options(-Wall -Werror -Wextra -pedantic -pedantic-errors)
# Xcode 11.2 seems to enable -Wnewline-eof with '-Wextra'. Disable this,
# since it's a legacy requirement not needed in C++11 onward.
add_compile_options(-Wno-newline-eof)
# Don't warn on ignored attributes; this breaks builds using dllexport/dllimport on
# classes that may also define inline functions
add_compile_options(-Wno-ignored-attributes)
elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC")
add_compile_options(/W4 /WX)
endif ()
include(AddSelfContainmentTest)
if (RESULT_COMPILE_UNIT_TESTS)
add_self_containment_test(${PROJECT_NAME}.SelfContainmentTest
TARGET ${PROJECT_NAME}
HEADERS ${header_files}
)
add_subdirectory("test")
endif ()
##############################################################################
# Installation
##############################################################################
if (IS_SUBPROJECT)
return()
endif ()
include(GNUInstallDirs)
include(CMakePackageConfigHelpers)
set(RESULT_CMAKE_CONFIG_DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}")
# The generated *ConfigVersion is strongly tied to the architecture
# it was generated on, and sets variables such as 'SIZEOF_VOID_P'.
# Since this library is header-only, the host architecture does not
# actually affect the target that consumes this project, so we fake
# the variable to be empty, but reset it after.
#
# Otherwise a 64-bit creator would cause a 32-bit consumer to fail to
# use this library, even though it's header-only.
set(RESULT_CMAKE_SIZEOF_VOID_P ${CMAKE_SIZEOF_VOID_P})
set(CMAKE_SIZEOF_VOID_P "")
write_basic_package_version_file(
"${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake"
VERSION "${PROJECT_VERSION}"
COMPATIBILITY "SameMajorVersion"
)
set(CMAKE_SIZEOF_VOID_P ${RESULT_CMAKE_SIZEOF_VOID_P})
configure_package_config_file(
"${CMAKE_CURRENT_LIST_DIR}/cmake/${PROJECT_NAME}Config.cmake.in"
"${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.cmake"
INSTALL_DESTINATION "${RESULT_CMAKE_CONFIG_DESTINATION}"
)
# Targets
install(
TARGETS "${PROJECT_NAME}"
EXPORT "${PROJECT_NAME}Targets"
DESTINATION "${CMAKE_INSTALL_LIBDIR}"
)
install(
EXPORT "${PROJECT_NAME}Targets"
NAMESPACE "${PROJECT_NAME}::"
DESTINATION "${RESULT_CMAKE_CONFIG_DESTINATION}"
)
install(
FILES "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake"
"${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.cmake"
DESTINATION "${RESULT_CMAKE_CONFIG_DESTINATION}"
)
# Includes
install(
DIRECTORY "include/"
DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}"
)