-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCMakeLists.txt
361 lines (292 loc) · 10.5 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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
#[[#############################################################################
Lucena Build Abstraction Library
“CMakeLists.txt”
Copyright © 2018-2024 Lucena
All Rights Reserved
This file is distributed under the University of Illinois Open Source
License. See LICENSE.md for details.
Library usage example:
-----
project("lbalTest")
find_package(lucenaBAL CONFIG REQUIRED)
add_executable(${PROJECT_NAME} src/main.cpp)
target_link_libraries(${PROJECT_NAME} lucenaBAL::lucenaBAL)
-----
##############################################################################]]
#[[#############################################################################
initialization
#]]
# This minimum is for
# - correct cross-compilation for all Apple OS’s (3.14)
# - correct Xcode project generation (3.15)
cmake_minimum_required (VERSION 3.15...3.30)
# Forbid poisoning the source tree, ’cuz we’re authoritarian that way. Note,
# though, that CMake will have already left a few droppings at this point, so
# maintaining a clean source directory in the face of a determined user is
# probably a hopeless endeavor.
#
# __SEEME__ Consider adding some entries to `.gitignore` to avoid polluting
# future commits.
if ("${CMAKE_SOURCE_DIR}" STREQUAL "${CMAKE_BINARY_DIR}")
message (FATAL_ERROR "FATAL: In-source builds are not allowed. "
"You must create a separate directory for build files.")
endif()
# Versioning; propagates to headers
set (PROJECT_VERSION_MAJOR 2)
set (PROJECT_VERSION_MINOR 0)
set (PROJECT_VERSION_PATCH 0)
set (PROJECT_VERSION_TWEAK 20)
set (LBAL_ABI_VERSION 2)
if (${PROJECT_VERSION_TWEAK})
set (LBAL_PROXY_VERSION ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}.${PROJECT_VERSION_PATCH}.${PROJECT_VERSION_TWEAK})
else()
set (LBAL_PROXY_VERSION ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}.${PROJECT_VERSION_PATCH})
endif()
project ("lucenaBAL"
VERSION ${LBAL_PROXY_VERSION}
DESCRIPTION
"Lucena Build Abstraction Library for C++ - compiler, library, and build platform feature detection"
HOMEPAGE_URL
"https://github.com/bitweeder/lucenaBAL"
LANGUAGES CXX)
# Define a short name for use with ancillary projects.
set (LBAL_PROJECT_PREFIX "lbal")
# Simplify finding our internal CMake tools.
set (CMAKE_MODULE_PATH "${CMAKE_MODULE_PATH}" "${CMAKE_CURRENT_SOURCE_DIR}/tools/cmake")
# Generate the version header, if necessary, so that it is up-to-date.
set (LBAL_GENERATION_MESSAGE "This file is automatically generated by the build system.")
configure_file (
"${PROJECT_SOURCE_DIR}/tools/cmake/${LBAL_PROJECT_PREFIX}Generated.hpp.in"
"${PROJECT_SOURCE_DIR}/include/${PROJECT_NAME}/details/${LBAL_PROJECT_PREFIX}Generated.hpp"
@ONLY
NEWLINE_STYLE UNIX
)
#[[#############################################################################
option setup
#]]
include (CTest)
include (CMakeDependentOption)
option (
LBAL_BUILD_DOCS
"Build documentation"
OFF)
option (
LBAL_CONFIG_DISABLE_PEDANTIC_WARNINGS
"Control whether to generate warnings from the Library when anything unusual happens"
OFF)
if (LBAL_CONFIG_DISABLE_PEDANTIC_WARNINGS)
# By default, `LBAL_CONFIG_enable_pedantic_warnings` is set to `1`.
add_compile_definitions(LBAL_CONFIG_enable_pedantic_warnings=0)
endif()
option (
LBAL_CONFIG_TREAT_UNCERTAINTY_AS_FAILURE
"Control whether an uncertain result during feature detection should be treated as unavailability. BE WARY OF CHANGING THIS!"
OFF)
if (LBAL_CONFIG_TREAT_UNCERTAINTY_AS_FAILURE)
# By default, `LBAL_CONFIG_treat_uncertainty_as_failure` is set to `0`.
add_compile_definitions(LBAL_CONFIG_treat_uncertainty_as_failure=1)
endif()
# `CMAKE_SYSTEM_NAME` is expected to be set correctly somewhere, e.g., as a
# command line option when cross-compiling. Note that as of CMake 3.14, the
# various Apple OS’s can be targeted individually, so the full list of
# recognized and supported names is {"Darwin", "iOS", "tvOS", "watchOS",
# "Windows", "Linux", "FreeBSD", "NetBSD", "OpenBSD"}.
#[[#############################################################################
global configuration
#]]
# Symbol visibility defaults; note that the source code overrides these, as
# necessary.
set (CMAKE_CXX_VISIBILITY_PRESET hidden)
set (CMAKE_VISIBILITY_INLINES_HIDDEN 1)
# Request the latest available C++ Standard. CMake doesn’t offer a “latest”
# option, so we manually specify the latest Standard understood by our minimum
# required CMake version. Setting `CMAKE_CXX_STANDARD_REQUIRED` to `OFF` allows
# us to fall back gracefully, while `CMAKE_CXX_EXTENSIONS OFF` prevents us from
# unintentionally relying on compiler-specific behaviors. The latter may be
# undesirable from a user perspective, but it can be overridden.
#
# __SEEME__ Setting the target Standard directly is discouraged by CMake, which
# prefers listing out individual language features required by the code. This
# is a bit silly as it creates too much coupling. Attempting to get by with
# proxies, e.g., picking one or two representative features of a desired
# Standard, is brittle. Unfortunately, a feature laundry list is what CMake
# expects in order to provide the best, most predictable compilation outcomes.
# Additionally, `CXX_STANDARD` appears to be completely useless for its
# intended purpose since it’s viewed as an unenforced suggestion, e.g., under
# MSVC, requiring us to invoke explicit workarounds that don’t have a fully
# equivalent effect. All that said, we, we set the global values here, anyway,
# as a clear declaration of intent.
if(NOT "${CMAKE_CXX_STANDARD}")
set(CMAKE_CXX_STANDARD 23)
endif()
if(NOT "${CMAKE_CXX_STANDARD_REQUIRED}")
set (CMAKE_CXX_STANDARD_REQUIRED OFF)
endif()
if(NOT "${CMAKE_CXX_EXTENSIONS}")
set(CMAKE_CXX_EXTENSIONS OFF)
endif()
#[[#############################################################################
set up targets
#]]
# Define standard directory locations; applies to Windows, as well.
include (GNUInstallDirs)
add_library (
${PROJECT_NAME}
INTERFACE)
# Alias to allow the library to be used with add_subdirectory.
add_library (
${PROJECT_NAME}::${PROJECT_NAME}
ALIAS
${PROJECT_NAME})
# Use generators to ensure that the headers are installed in the proper
# locations, as defined by GNUInstallDirs.
target_include_directories(
${PROJECT_NAME}
INTERFACE $<BUILD_INTERFACE:${${PROJECT_NAME}_SOURCE_DIR}/include/${PROJECT_NAME}>
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}/${PROJECT_NAME}>)
target_compile_features(
${PROJECT_NAME}
INTERFACE
cxx_std_23)
# Customized packaging boilerplate
target_compile_definitions (
${PROJECT_NAME}
INTERFACE
LBAL_PACKAGING_PRODUCT_IDENTIFIER="${CMAKE_PROJECT_NAME}"
LBAL_PACKAGING_SIGNATURE="lbal"
LBAL_PACKAGING_COPYRIGHT_HOLDER="Lucena"
LBAL_PACKAGING_COPYRIGHT_DATE="2019-2024"
LBAL_PACKAGING_PRODUCT_DOMAIN="com.lucena"
LBAL_PACKAGING_PREFIX="lbal"
LBAL_PACKAGING_DESCRIPTION="${CMAKE_PROJECT_DESCRIPTION}"
LBAL_PACKAGING_BUILD="${PROJECT_VERSION_TWEAK}"
LBAL_PACKAGING_VERSION="${PROJECT_VERSION}"
$<$<CONFIG:DEBUG>:LBAL_CONFIG_debug=1>
)
# __FIXME__ Simulator targets for Apple OS’s are effectively unavailable, as
# they will not be configured properly. Even with proper configuration,
# it will be necessary to manually specify Xcode generator arguments to CMake
# on the command line, or rely on making correct choices in Xcode itself if not
# building from the command line.
if ("${CMAKE_SYSTEM_NAME}" MATCHES "Darwin")
target_compile_definitions (
${PROJECT_NAME}
INTERFACE
LBAL_PACKAGING_API="macOS"
LBAL_TARGET_OS_MACOS=1
)
elseif ("${CMAKE_SYSTEM_NAME}" MATCHES "iOS")
target_compile_definitions (
${PROJECT_NAME}
INTERFACE
LBAL_PACKAGING_API="iOS"
LBAL_TARGET_OS_IOS=1
)
elseif ("${CMAKE_SYSTEM_NAME}" MATCHES "tvOS")
target_compile_definitions (
${PROJECT_NAME}
INTERFACE
LBAL_PACKAGING_API="tvOS"
LBAL_TARGET_OS_TVOS=1
)
elseif ("${CMAKE_SYSTEM_NAME}" MATCHES "watchOS")
target_compile_definitions (
${PROJECT_NAME}
INTERFACE
LBAL_PACKAGING_API="watchOS"
LBAL_TARGET_OS_WATCHOS=1
)
elseif ("${CMAKE_SYSTEM_NAME}" MATCHES "Windows")
target_compile_definitions (
${PROJECT_NAME}
INTERFACE
LBAL_PACKAGING_API="WinAPI"
LBAL_TARGET_OS_WINAPI=1
)
elseif ("${CMAKE_SYSTEM_NAME}" MATCHES "Linux" OR
"${CMAKE_SYSTEM_NAME}" MATCHES "FreeBSD" OR
"${CMAKE_SYSTEM_NAME}" MATCHES "NetBSD" OR
"${CMAKE_SYSTEM_NAME}" MATCHES "OpenBSD")
target_compile_definitions (
${PROJECT_NAME}
INTERFACE
LBAL_PACKAGING_API="X11"
LBAL_TARGET_OS_X11=1
)
else()
message (FATAL_ERROR "${PROJECT_NAME} does not appear to support this platform")
endif()
#[[#############################################################################
install targets
#]]
# Note that we omit INCLUDE because INSTALL_INTERFACE already specifies it.
install (
TARGETS
${PROJECT_NAME}
EXPORT
${PROJECT_NAME}_Targets
ARCHIVE
DESTINATION
${CMAKE_INSTALL_LIBDIR}
LIBRARY
DESTINATION
${CMAKE_INSTALL_LIBDIR}
RUNTIME
DESTINATION
${CMAKE_INSTALL_BINDIR}
)
#[[#############################################################################
packaging configuration
#]]
include (CMakePackageConfigHelpers)
write_basic_package_version_file (
"${PROJECT_NAME}ConfigVersion.cmake"
VERSION ${PROJECT_VERSION}
COMPATIBILITY SameMajorVersion)
configure_package_config_file (
"${PROJECT_SOURCE_DIR}/tools/cmake/${PROJECT_NAME}Config.cmake.in"
"${PROJECT_BINARY_DIR}/${PROJECT_NAME}Config.cmake"
INSTALL_DESTINATION
${CMAKE_INSTALL_DATAROOTDIR}/${PROJECT_NAME}/cmake
)
#[[#############################################################################
installation
#]]
install (
EXPORT
${PROJECT_NAME}_Targets
FILE
${PROJECT_NAME}Targets.cmake
NAMESPACE
${PROJECT_NAME}::
DESTINATION
${CMAKE_INSTALL_DATAROOTDIR}/${PROJECT_NAME}/cmake
)
install (
FILES
"${PROJECT_BINARY_DIR}/${PROJECT_NAME}Config.cmake"
"${PROJECT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake"
DESTINATION
${CMAKE_INSTALL_DATAROOTDIR}/${PROJECT_NAME}/tools/cmake
)
install (
DIRECTORY
${PROJECT_SOURCE_DIR}/include/${PROJECT_NAME}
DESTINATION
include
)
#[[#############################################################################
subdirectories
#]]
if (${BUILD_TESTING})
add_subdirectory (tests)
endif()
if (${LBAL_BUILD_DOCS})
add_subdirectory (docs)
endif()
#[[#############################################################################
packaging
__FIXME__ Currently unused.
#]]
#set(CPACK_RESOURCE_FILE_LICENSE "${PROJECT_SOURCE_DIR}/LICENSE.md")
#include(CPack)