-
Notifications
You must be signed in to change notification settings - Fork 25
/
CMakeLists.txt
517 lines (427 loc) · 15.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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
# Policies <= CMP0128 default to NEW
# 3.20 - because is used NEWLINE_STYLE in the file(GENERATE) command
# 3.22 - because is used CMP0127: cmake_dependent_option() supports full Condition Syntax
# Maximum version allowed for the minimum version is 3.22 because of Ubuntu 22.04 LTS so
# currently can't use the target_sources(FILE_SET) 😔
# Minimum version must always correspond to the version that is in the latest Ubuntu LTS
# TODO use LINKER_TYPE target property when min. version will be CMake v3.29 (Noble has 3.28.3 😭) silverqx
# TODO use CMAKE_COMPILE_WARNING_AS_ERROR when Ubuntu 24.04.1 will be out, in increases minimum version to CMake v3.24 silverqx
cmake_minimum_required(VERSION 3.22...3.30 FATAL_ERROR)
list(APPEND CMAKE_MODULE_PATH
"${CMAKE_CURRENT_SOURCE_DIR}/cmake/CommonModules"
"${CMAKE_CURRENT_SOURCE_DIR}/cmake/Modules"
)
# Initialize Project Version
# ---
include(TinyHelpers)
include(TinyInitDefaultVariables)
# Initialize Tiny variables, early init.
tiny_init_tiny_variables_pre()
tiny_read_version(TINY_VERSION
TINY_VERSION_MAJOR TINY_VERSION_MINOR TINY_VERSION_PATCH TINY_VERSION_TWEAK
VERSION_HEADER "${CMAKE_CURRENT_SOURCE_DIR}/include/orm/version.hpp"
PREFIX TINYORM
HEADER_FOR "${TinyOrm_ns}"
)
# Basic project and CMake build setup
# ---
# TODO add support for POSITION_INDEPENDENT_CODE silverqx
project(${TinyOrm_ns}
DESCRIPTION "Modern C++ ORM library for Qt framework"
HOMEPAGE_URL "https://www.tinyorm.org"
LANGUAGES CXX
VERSION ${TINY_VERSION}
)
# Specify the C++ standard
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED YES)
set(CMAKE_CXX_EXTENSIONS OFF)
# Set the AUTOMOC property explicitly only when needed (eg. unit tests need AUTOMOC)
set(CMAKE_AUTOMOC OFF)
# Version requirements
# ---
# Older versions may work, but you are on your own
# Req - requirement, throws error
# Rec - recommended, shows info message
# Compilers
# 16.10/16.11 (1929) to support pragma system_header
set(minReqMsvcVersion 19.29)
set(minRecGCCVersion 10.2)
set(minRecClangVersion 12)
set(minReqClangClVersion 14.0.3)
# Packages
set(minReqQtVersion 6.2)
# Unused
# As the range-v3 uses exact version policy in the package config file so passing version
# makes real problems on CI pipelines where different OS images can have installed
# different versions of range-v3 (doesn't matter if it's vcpkg or some package manager)
#set(minReqRangeV3Version 0.12.0)
# tabulate doesn't provide Package Version File
#set(minReqTabulateVersion 1.5.0)
# Make minimum toolchain version a requirement
include(TinyToolchainRequirement)
tiny_toolchain_requirement(
MSVC ${minReqMsvcVersion}
CLANG_CL ${minReqClangClVersion}
GCC ${minRecGCCVersion}
CLANG ${minRecClangVersion}
QT ${minReqQtVersion}
)
# TinyORM build options
# ---
include(FeatureSummary)
include(TinyFeatureOptions)
include(TinyOptions)
# Initialize default CMake variables on which CMake options depend
tiny_init_cmake_variables_pre()
feature_option(BUILD_SHARED_LIBS
"Build using shared libraries" ON
)
feature_option(BUILD_TESTS
"Build TinyORM unit tests" OFF
)
feature_option(BUILD_TREE_DEPLOY
"Copy TinyDrivers and TinyMySql libraries to the root of the build tree" ON
)
# TinyDrivers options
feature_option(BUILD_DRIVERS
"Build TinyDrivers SQL database drivers (core/common code; replaces QtSql module)" OFF
)
feature_string_option_dependent(DRIVERS_TYPE
"Shared;Loadable;Static"
"How to build and link against TinyDrivers SQL database drivers (supported values: \
Shared, Loadable, and Static)" Shared
"BUILD_DRIVERS AND BUILD_SHARED_LIBS" ${TINY_DRIVERS_TYPE_FORCE}
)
feature_option_dependent(BUILD_MYSQL_DRIVER
"Build TinyDrivers MySQL database driver" ON
"BUILD_DRIVERS" OFF
)
# Depends on tiny_init_cmake_variables_pre() call
feature_option_dependent(MATCH_EQUAL_EXPORTED_BUILDTREE
"Exported package configuration from the build tree is considered to match only \
when the build type is equal" OFF
"CMAKE_EXPORT_PACKAGE_REGISTRY AND NOT TINY_IS_MULTI_CONFIG" OFF
)
# The condition allows to use both mechanism to control dynamic/static MSVC CRT linkage,
# the CMAKE_MSVC_RUNTIME_LIBRARY or MSVC_RUNTIME_DYNAMIC
# Also this isn't allowed with vcpkg as it controls this using triplet (VCPKG_CRT_LINKAGE)
feature_option_dependent(MSVC_RUNTIME_DYNAMIC
"Use MSVC dynamic runtime library (-MD) instead of static (-MT), also considers \
a Debug configuration (-MTd, -MDd)" ON
"MSVC AND NOT TINY_VCPKG AND NOT DEFINED VCPKG_CRT_LINKAGE AND \
NOT DEFINED CMAKE_MSVC_RUNTIME_LIBRARY"
MSVC_RUNTIME_DYNAMIC-NOTFOUND
)
feature_option_environment(STRICT_MODE
"Propagate strict compiler/linker options and Qt definitions using \
the ${TinyOrm_ns}::${CommonConfig_target} interface library to the user code \
(highly recommended; can also be set with the TINYORM_STRICT_MODE environment variable)"
TINYORM_STRICT_MODE OFF
)
feature_option(VERBOSE_CONFIGURE
"Show information about PACKAGES_FOUND and PACKAGES_NOT_FOUND in the configure \
output" OFF
)
# Initialize INLINE_CONSTANTS CMake feature dependent option
tiny_initialize_inline_constants_option()
mark_as_advanced(MATCH_EQUAL_EXPORTED_BUILDTREE)
# MSVC_PARALLEL option
include(TinyMsvcParallel)
tiny_msvc_parallel("\
Enables /MP flag for parallel builds using MSVC. Specify an \
integer value to control the number of threads used (Only \
works on some older versions of Visual Studio). Setting to \
ON lets the toolchain decide how many threads to use. Set to \
OFF to disable /MP completely.")
# Initialize Tiny and default CMake variables
tiny_init_cmake_variables()
tiny_init_tiny_variables()
# Throw a fatal error for unsupported environments
tiny_check_unsupported_build()
# TinyORM library
# ---
add_library(${TinyOrm_target})
add_library(${TinyOrm_ns}::${TinyOrm_target} ALIAS ${TinyOrm_target})
# TinyORM build options - target is needed
# ---
target_optional_compile_definitions(${TinyOrm_target}
PUBLIC
FEATURE NAME MYSQL_PING
DEFAULT OFF
DESCRIPTION "Enable MySQL ping on Orm::MySqlConnection"
ENABLED TINYORM_MYSQL_PING
)
target_optional_compile_definitions(${TinyOrm_target}
PUBLIC
ADVANCED FEATURE NAME DISABLE_THREAD_LOCAL
DEFAULT OFF
DESCRIPTION "Remove all the thread_local storage duration specifiers \
(disables multi-threading support)"
ENABLED TINYORM_DISABLE_THREAD_LOCAL
)
target_optional_compile_definitions(${TinyOrm_target}
PUBLIC
FEATURE NAME ORM
DEFAULT ON
DESCRIPTION "Controls the compilation of all ORM-related source code, when this \
option is disabled, then only the query builder without ORM is compiled"
DISABLED TINYORM_DISABLE_ORM
)
target_optional_compile_definitions(${TinyOrm_target}
PUBLIC
FEATURE NAME TOM
DEFAULT ON
DESCRIPTION "Controls the compilation of all Tom-related source code \
(command-line interface)"
DISABLED TINYORM_DISABLE_TOM
)
# Depends on the TOM build option
feature_option_dependent(TOM_EXAMPLE
"Build the Tom command-line application example" OFF
"TOM" TOM_EXAMPLE-NOTFOUND
)
# Depends on the TOM_EXAMPLE build option
tiny_init_tom_database_dirs()
# TinyORM library header and source files
# ---
include(TinySources)
tinyorm_sources(${TinyOrm_target}_headers ${TinyOrm_target}_sources)
target_sources(${TinyOrm_target} PRIVATE
${${TinyOrm_target}_headers}
${${TinyOrm_target}_sources}
)
# Use Precompiled headers (PCH)
# ---
target_precompile_headers(${TinyOrm_target} PRIVATE $<$<COMPILE_LANGUAGE:CXX>:"pch.h">)
if(NOT CMAKE_DISABLE_PRECOMPILE_HEADERS)
target_compile_definitions(${TinyOrm_target} PRIVATE TINYORM_USING_PCH)
endif()
# TinyORM library specific configuration
# ---
set_target_properties(${TinyOrm_target}
PROPERTIES
C_VISIBILITY_PRESET "hidden"
CXX_VISIBILITY_PRESET "hidden"
VISIBILITY_INLINES_HIDDEN YES
VERSION ${PROJECT_VERSION}
SOVERSION 0
EXPORT_NAME ${TinyOrm_ns}
)
# Append a major version number for shared or static library (Windows/MinGW only)
if(CMAKE_SYSTEM_NAME STREQUAL "Windows")
# TODO use a new CMAKE_DLL_NAME_WITH_SOVERSION in CMake v3.27 silverqx
set_property(
TARGET ${TinyOrm_target}
PROPERTY OUTPUT_NAME "${TinyOrm_target}${PROJECT_VERSION_MAJOR}"
)
endif()
# Order of #include-s is very important because of pch.h as its name is the same for all
# projects, the ${PROJECT_SOURCE_DIR}/include must be as the first #include!
target_include_directories(${TinyOrm_target}
PUBLIC "$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>"
)
# TinyORM defines
# ---
target_compile_definitions(${TinyOrm_target}
PUBLIC
PROJECT_TINYORM
# Release build
$<$<NOT:$<CONFIG:Debug>>:TINYORM_NO_DEBUG>
# Do not log queries
$<$<NOT:$<CONFIG:Debug>>:TINYORM_NO_DEBUG_SQL>
# Debug build
$<$<CONFIG:Debug>:TINYORM_DEBUG>
# Log queries with time measurements
$<$<CONFIG:Debug>:TINYORM_DEBUG_SQL>
PRIVATE
# To disable #pragma system_header if compiling TinyORM project itself
TINYORM_PRAGMA_SYSTEM_HEADER_OFF
)
if(BUILD_SHARED_LIBS)
target_compile_definitions(${TinyOrm_target}
PRIVATE
# TODO cmake uses target_EXPORTS, use cmake convention instead silverqx
TINYORM_BUILDING_SHARED
INTERFACE
TINYORM_LINKING_SHARED
)
endif()
# Specifies which global constant types will be used
if(TINY_EXTERN_CONSTANTS)
target_compile_definitions(${TinyOrm_target} PUBLIC TINYORM_EXTERN_CONSTANTS)
else()
target_compile_definitions(${TinyOrm_target} PUBLIC TINYORM_INLINE_CONSTANTS)
endif()
# Using the TinyDrivers instead of QtSql drivers
if(BUILD_DRIVERS)
target_compile_definitions(${TinyOrm_target} PUBLIC TINYORM_USING_TINYDRIVERS)
else()
target_compile_definitions(${TinyOrm_target} PUBLIC TINYORM_USING_QTSQLDRIVERS)
endif()
# Enable code needed by tests, eg. connection overriding in the Model or
# MySqlConnection::setConfigVersion()
if(BUILD_TESTS)
target_compile_definitions(${TinyOrm_target} PUBLIC TINYORM_TESTS_CODE)
endif()
# Tom related header and source files
# ---
if(TOM)
tinytom_sources(${TomExample_target}_headers ${TomExample_target}_sources)
target_sources(${TinyOrm_target} PRIVATE
${${TomExample_target}_headers}
${${TomExample_target}_sources}
)
endif()
# Tom related specific configuration
# ---
if(TOM)
target_include_directories(${TinyOrm_target}
PUBLIC "$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/tom/include>"
)
endif()
# Tom related defines
# ---
if(TOM)
target_compile_definitions(${TinyOrm_target}
PUBLIC
# Release build
$<$<NOT:$<CONFIG:Debug>>:TINYTOM_NO_DEBUG>
# Debug build
$<$<CONFIG:Debug>:TINYTOM_DEBUG>
PRIVATE
# For the tom about command
# TINYORM_ is correct, it can be potentially used somewhere else
TINYORM_MSVC_RUNTIME_DYNAMIC=${MSVC_RUNTIME_DYNAMIC}
# TINYTOM_ is correct, it's strictly for the tom about command only
TINYTOM_CMAKE_MSVC_RUNTIME_LIBRARY=${CMAKE_MSVC_RUNTIME_LIBRARY}
TINYORM_STRICT_MODE=${STRICT_MODE}
)
# Enable code needed by tests (modify the migrate:status command for tests need)
if(BUILD_TESTS)
target_compile_definitions(${TinyOrm_target} PUBLIC TINYTOM_TESTS_CODE)
endif()
# Set TINYORM_LTO macro based on the INTERPROCEDURAL_OPTIMIZATION target property
tiny_set_lto_compile_definition(${TinyOrm_target})
endif()
if(TOM_EXAMPLE)
target_compile_definitions(${TinyOrm_target}
PUBLIC
# Will be stringified in the tom/application.cpp
TINYTOM_MIGRATIONS_DIR=${TOM_MIGRATIONS_DIR}
# Will be stringified in the tom/application.cpp
TINYTOM_MODELS_DIR=${TOM_MODELS_DIR}
# Will be stringified in the tom/application.cpp
TINYTOM_SEEDERS_DIR=${TOM_SEEDERS_DIR}
PRIVATE
# For the tom about command
TINYORM_TOM_EXAMPLE
)
endif()
# Windows resource and manifest files
# ---
# Find icons, orm/version.hpp, and Windows manifest file for MinGW
if(CMAKE_SYSTEM_NAME STREQUAL "Windows")
tiny_set_rc_flags("-I \"${PROJECT_SOURCE_DIR}/resources\"")
endif()
include(TinyResourceAndManifest)
tiny_resource_and_manifest(${TinyOrm_target}
OUTPUT_DIR "${TINY_BUILD_GENDIR}/tmp/"
)
# Resolve and link dependencies
# ---
# Must be before the TinyCommon, to exclude WINVER for the MSYS2 Qt6 builds to avoid:
# 'WINVER' macro redefined [-Wmacro-redefined]
# Look also to the TinyCommon for conditional WINVER definition
# TinyQtComponentsRequired: Core and Sql (if NOT BUILD_DRIVERS)
# Leaving this find_package(QT NAMES) search pattern even if the Qt v5 support was removed
# because I like it and can be used with future Qt version
find_package(QT NAMES Qt6 REQUIRED COMPONENTS ${TinyQtComponentsRequired})
tiny_find_package(Qt${QT_VERSION_MAJOR} ${minReqQtVersion} CONFIG
REQUIRED COMPONENTS ${TinyQtComponentsRequired}
)
tiny_find_package(range-v3 CONFIG REQUIRED)
# Common configuration as interface library
include(TinyCommon)
tiny_common(${CommonConfig_target}
NAMESPACE ${TinyOrm_ns}
EXPORT NAME ${CommonConfig_target}
)
# Unconditional dependencies
target_link_libraries(${TinyOrm_target}
PUBLIC
# Never use versionless Qt targets (as QT_NO_CREATE_VERSIONLESS_TARGETS might be
# defined)
Qt${QT_VERSION_MAJOR}::Core
range-v3::range-v3
)
# Conditional dependencies
if(NOT BUILD_DRIVERS)
target_link_libraries(${TinyOrm_target} PUBLIC Qt${QT_VERSION_MAJOR}::Sql)
endif()
if(STRICT_MODE)
target_link_libraries(${TinyOrm_target}
PUBLIC ${TinyOrm_ns}::${CommonConfig_target}
)
else()
target_link_libraries(${TinyOrm_target}
PRIVATE ${TinyOrm_ns}::${CommonConfig_target}
)
endif()
if(MYSQL_PING)
tiny_find_and_link_mysql(${TinyOrm_target})
endif()
if(TOM)
# tabulate doesn't provide Package Version File
tiny_find_package(tabulate CONFIG REQUIRED)
target_link_libraries(${TinyOrm_target} PUBLIC tabulate::tabulate)
endif()
# Build TinyDrivers
# ---
if(BUILD_DRIVERS)
add_subdirectory(drivers)
target_link_libraries(${TinyOrm_target}
PUBLIC ${TinyDrivers_ns}::${TinyDrivers_target}
)
endif()
# Build auto tests
# ---
if(BUILD_TESTS)
enable_testing()
find_package(Qt${QT_VERSION_MAJOR} ${minReqQtVersion} REQUIRED COMPONENTS Test)
add_subdirectory(tests)
endif()
# Build examples
# ---
if(TOM_EXAMPLE)
add_subdirectory(examples)
endif()
# Deployment
# ---
include(TinyDeployment)
# Generate find_dependency calls for the TinyORM package config file
tiny_generate_find_dependency_calls(tiny_find_dependency_calls)
# Create Package Config and Config Package Version files and install the TinyORM Project
tiny_install_tinyorm()
# Create Package Config and Package Config Version files for the Build Tree and export it
tiny_export_build_tree()
# Copy TinyDrivers and TinyMySql libraries to the root of the build tree
if(BUILD_TREE_DEPLOY)
tiny_build_tree_deployment()
endif()
# Generate pkg-config file
#if(NOT MSVC)
# generate_and_install_pkg_config_file(torrent-rasterbar libtorrent-rasterbar)
#endif()
# Some info output
# ---
# Set up package properties using the set_package_properties()
set_packages_properties()
if(VERBOSE_CONFIGURE)
if(NOT TINY_IS_MULTI_CONFIG AND NOT TINY_BUILD_TYPE_LOWER STREQUAL "debug")
message(STATUS "Disabled debug output and asserts")
endif()
feature_summary(WHAT ALL)
else()
feature_summary(WHAT ENABLED_FEATURES DISABLED_FEATURES)
endif()