forked from mysql/mysql-connector-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CMakeLists.txt
636 lines (460 loc) · 14.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
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
# Copyright (c) 2015, 2023, Oracle and/or its affiliates.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License, version 2.0, as
# published by the Free Software Foundation.
#
# This program is also distributed with certain software (including
# but not limited to OpenSSL) that is licensed under separate terms,
# as designated in a particular file or component or in included license
# documentation. The authors of MySQL hereby grant you an
# additional permission to link the program and your derivative works
# with the separately licensed software that they have included with
# MySQL.
#
# Without limiting anything contained in the foregoing, this file,
# which is part of MySQL Connector/C++, is also subject to the
# Universal FOSS Exception, version 1.0, a copy of which can be found at
# http://oss.oracle.com/licenses/universal-foss-exception.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the GNU General Public License, version 2.0, for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
CMAKE_MINIMUM_REQUIRED(VERSION 3.8)
CMAKE_POLICY(VERSION 3.1)
cmake_policy(SET CMP0022 NEW)
SET(BUILDTYPE_DOCSTRING
"Choose the type of build, options are: None(CMAKE_CXX_FLAGS or
CMAKE_C_FLAGS used) Debug Release RelWithDebInfo MinSizeRel")
#
# If cmake is invoked with -DCMAKE_BUILD_TYPE,
# respect user wishes and do not (re)define CMAKE_BUILD_TYPE. Otherwise,
# use Debug by default.
#
IF(NOT DEFINED CMAKE_BUILD_TYPE)
SET(CMAKE_BUILD_TYPE "Debug" CACHE STRING
${BUILDTYPE_DOCSTRING} FORCE)
ENDIF()
#
# Prevent cmake from setting its default value of CMAKE_INSTALL_PREFIX
# inside PROJECT() command.
# Thanks to this, we can detect if user has set CMAKE_INSTALL_PREFIX
# or not, and then set our own default in the latter case.
#
set(CMAKE_INSTALL_PREFIX "" CACHE PATH "Install location")
#
# Enable grouping targets into folders for IDE tools
#
set_property(GLOBAL PROPERTY USE_FOLDERS ON)
include(cdk/cmake/setup.cmake)
include(cmake/setup.cmake)
include(bootstrap)
# Note: Does not work well with Ninja -- not sure why
if(NOT CMAKE_GENERATOR MATCHES "Ninja")
bootstrap()
endif()
##########################################################################
PROJECT(MySQL_CONCPP)
# Load cmake modules
include(platform)
include(dependency) # find_dependency()
include(config_options) # add_config_option()
include(libutils) # merge_libraries()
#
# Detect if we are configured as stand-alone project, or sub-project.
#
if(PROJECT_NAME STREQUAL CMAKE_PROJECT_NAME)
SET(concpp_stand_alone 1)
else()
MESSAGE("Building Connector/C++ as part of ${CMAKE_PROJECT_NAME} project")
SET(concpp_stand_alone 0)
set(WITH_TESTS OFF)
set(WITH_DOC OFF)
set(WITH_HEADER_CHECKS OFF)
endif()
include(version.cmake)
message("Building on system: ${CMAKE_SYSTEM} (${CMAKE_SYSTEM_PROCESSOR})")
message("Using cmake generator: ${CMAKE_GENERATOR}")
#message("Compiler identification: ${CMAKE_CXX_COMPILER_ID}")
if(DEFINED CMAKE_GENERATOR_TOOLSET)
message("Using toolset: ${CMAKE_GENERATOR_TOOLSET}")
endif()
if(SUNPRO)
if(SUNPRO VERSION_LESS "5.15")
message(WARNING "Connector/C++ requires SunPro version 5.15 or later.")
endif()
set_arch_m64()
endif()
#
# Note: On Solaris cmake's built-in checks for bit size use plain compiler
# options which imply 32-bit architecture. Above we force 64-bit but this
# is not reflected in IS64BIT flag.
#
if(IS64BIT OR SUNPRO)
message("Building 64bit code")
else()
message("Building 32bit code")
endif()
#
# Install settings
# ================
#
include(install_layout.cmake)
add_config_option(BUNDLE_DEPENDENCIES BOOL ADVANCED DEFAULT OFF
"If enabled, external libraries used by the connector, such as openSSL,"
" will be installed together with the connector library"
)
#
# Compiler settings
# =================
#
enable_pic()
enable_cxx17()
#
# Configure static runtime library on Windows if requested
#
add_config_option(STATIC_MSVCRT BOOL ADVANCED DEFAULT OFF
"Use static MSVC runtime library"
)
if(WIN32 AND STATIC_MSVCRT)
message("Using static runtime library")
set_msvcrt(STATIC)
endif()
# Other MSVC settings
if(MSVC)
add_compile_options(/bigobj)
add_definitions(-DNOMINMAX)
endif()
#
# Gcov support (Linux only)
#
find_dependency(Coverage) # defines add_coverage() command
#message("WITH_COVERAGE: ${WITH_COVERAGE}")
if(0)
message("flags: ${CMAKE_C_FLAGS}")
message("c++ flags: ${CMAKE_CXX_FLAGS}")
foreach(TYPE DEBUG RELEASE RELWITHDEBINFO MINSIZEREL)
message("${TYPE} flags: ${CMAKE_C_FLAGS_${TYPE}}")
message("c++ ${TYPE} flags: ${CMAKE_CXX_FLAGS_${TYPE}}")
endforeach()
endif()
#
# Linker settings
# ===============
#
#
# Produce rpath dependent libraries on MacOS
# see: <https://developer.apple.com/library/content/documentation/DeveloperTools/Conceptual/DynamicLibraries/100-Articles/RunpathDependentLibraries.html#//apple_ref/doc/uid/TP40008306-SW1>
# and: <https://cmake.org/cmake/help/v3.0/prop_tgt/MACOSX_RPATH.html>
#
if(APPLE)
set(CMAKE_MACOSX_RPATH ON)
endif()
#
# Main project and its dependencies
# =================================
#
add_config_option(BUILD_STATIC BOOLEAN DEFAULT OFF
"Build static version of connector library"
)
if(BUILD_STATIC)
message("Building static connector library")
set(BUILD_SHARED_LIBS OFF)
set_property(
DIRECTORY .
APPEND PROPERTY COMPILE_DEFINITIONS
CONCPP_BUILD_STATIC
)
else()
message("Building shared connector library")
set(BUILD_SHARED_LIBS ON)
if(WIN32 AND STATIC_MSVCRT)
message(SEND_ERROR "Shared library should not use static runtime.")
endif()
if(WITH_COVERAGE)
message(WARNING "To get good coverage results use static build of the"
" connector (BUILD_STATIC)")
endif()
# Note: Using set_propery() instead of add_compile_options() because we
# want to remove these defs later, when building test code.
set_property(
DIRECTORY .
APPEND PROPERTY COMPILE_DEFINITIONS
CONCPP_BUILD_SHARED
)
#add_compile_options($<$<NOT:$<CONFIG:Static>>:-DCONCPP_BUILD_SHARED>)
endif()
# Hide all symbols that are not explicitly exported.
set_visibility(hidden)
add_config_option(THROW_AS_ASSERT BOOL ADVANCED DEFAULT OFF
"Turn THROW() statements in the code into asserts for easier debugging"
)
if(THROW_AS_ASSERT)
add_definitions(-DTHROW_AS_ASSERT)
endif()
#
# Warnings
#
# TODO: Fix these warnings.
#
if(MSVC)
add_definitions(-D_CRT_SECURE_NO_WARNINGS -D_SCL_SECURE_NO_WARNINGS)
if(CXX_FRONTEND_MSVC)
# Disable MSVC unreachable code warnings unless requested.
add_compile_options(/wd4702)
# Disable MSVC warning C4297 as it seems to be buggy.
# Seehttps://connect.microsoft.com/VisualStudio/feedback/details/897611/incorrect-warning-of-c4297-for-destructor-with-try-catch-statement
add_compile_options(/wd4297)
endif()
endif()
if(SUNPRO)
add_compile_options(
-errtags=yes -erroff=hidevf,wvarhidemem
)
endif()
#
# Testing framework
#
add_config_option(WITH_TESTS BOOL ADVANCED DEFAULT OFF
"Build project's unit tests"
)
include(testing)
setup_testing() # Note: this must be called in top-level CMakeLists.txt
add_subdirectory(testing)
#
# CDK
#
add_subdirectory(cdk)
foreach(T cdk cdk_foundation cdk_mysqlx cdk_proto_mysqlx cdk_parser)
set_target_properties(${T} PROPERTIES FOLDER "CDK")
endforeach()
#
# Project's public headers
#
ADD_SUBDIRECTORY(include)
INCLUDE_DIRECTORIES(include)
#
# The legacy connector, if selected.
# Note: it is included before setting higher warning levels.
#
add_config_option(WITH_JDBC BOOL DEFAULT OFF
"Whether to build a variant of connector library which implements legacy JDBC API"
)
if(WITH_JDBC)
add_subdirectory(jdbc)
# Note: These include paths are to be used in a build tree. In
# this situation the jdbc public headers are not installed yet and
# we use a copy of them placed inside the build tree.
target_include_directories(connector-jdbc
PUBLIC "${PROJECT_BINARY_DIR}/include/jdbc"
PUBLIC "${PROJECT_BINARY_DIR}/include/jdbc/cppconn"
PUBLIC "${PROJECT_SOURCE_DIR}/include"
)
endif()
#
# Set higher warning level for the main connector code.
#
if(MAINTAINER_MODE)
if(MSVC)
# 4127 = conditional expression is constant (needed for do {...} while(false))
# 4512 = assignment operator could not be generated
#
# Note: 4512 is disabled because according to C++11 standard the situations
# that triggers this warning should be handled automatically by the compiler
# (and this is the case for MSVC 2015).
# See: http://en.cppreference.com/w/cpp/language/copy_assignment
add_compile_options(/W4 /wd4512 /wd4127)
elseif(SUNPRO)
else()
add_compile_options(-Wextra)
endif()
endif(MAINTAINER_MODE)
#
# Connector/C++ components
#
add_subdirectory(doc)
add_subdirectory(common)
add_subdirectory(xapi)
add_subdirectory(devapi)
#
# Target which builds the final connector library
# ===============================================
#
# Generate the main connector library.
merge_libraries(connector xapi devapi)
target_include_directories(connector PUBLIC "${PROJECT_SOURCE_DIR}/include")
#
# Embed rpath information in the connector library.
#
set_property(TARGET connector PROPERTY BUILD_WITH_INSTALL_RPATH ON)
# The $ORIGIN/@loader_path entry tells to look for dependent libraries in the
# location where our connector library is stored.
if(APPLE)
set_property(TARGET connector APPEND PROPERTY INSTALL_RPATH "@loader_path")
if(CMAKE_BUILD_TYPE MATCHES "[Dd][Ee][Bb][Uu][Gg]")
set_property(TARGET connector APPEND PROPERTY INSTALL_RPATH "@loader_path/../private")
else()
set_property(TARGET connector APPEND PROPERTY INSTALL_RPATH "@loader_path/private")
endif()
elseif(NOT WIN32)
set_property(TARGET connector APPEND PROPERTY INSTALL_RPATH "$ORIGIN")
if(CMAKE_BUILD_TYPE MATCHES "[Dd][Ee][Bb][Uu][Gg]")
set_property(TARGET connector APPEND PROPERTY INSTALL_RPATH "$ORIGIN/../private")
else()
set_property(TARGET connector APPEND PROPERTY INSTALL_RPATH "$ORIGIN/private")
endif()
endif()
if(0)
#
# Add command to show rpath information
#
if(APPLE)
set(list_rpath_cmd otool -l $<TARGET_FILE:libconcpp> "|" grep RPATH -A2)
elseif(NOT WIN32)
set(list_rpath_cmd objdump -x $<TARGET_FILE:libconcpp> "|" grep RPATH -A2)
endif()
add_custom_command(TARGET connector POST_BUILD
COMMAND ${list_rpath_cmd}
COMMENT "RPATH setting for: $<TARGET_FILE_NAME:mysqlcppconn>"
)
endif()
# To be able to use connector library from its build location on Windows we
# need to make OpenSSL DLLs available to it (in case they are not installed
# system-wide). Here we arrange for the OpenSSL DLLs to be copied to the build
# location of the connector library after building it.
if(WIN32 AND OPENSSL_LIB_DIR)
# Note: For simplicity we just copy any DLLs we can find at the given
# OpenSSL location.
file(GLOB glob1
"${OPENSSL_LIB_DIR}/*${CMAKE_SHARED_LIBRARY_SUFFIX}*"
)
file(GLOB glob2
"${OPENSSL_LIB_DIR}/../bin/*${CMAKE_SHARED_LIBRARY_SUFFIX}*"
)
add_custom_command(TARGET connector POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy
${glob1} ${glob2} $<TARGET_FILE_DIR:connector>
COMMENT "# Copy OpenSSL dependency of connector library."
)
endif()
#
# Stop here if this is a sub-project of a bigger project.
#
if (NOT concpp_stand_alone)
return()
endif()
#
# Install specifications
# ----------------------
#
# Note: Locations and names are configured in install_layout.cmake
#
set_property(TARGET connector PROPERTY OUTPUT_NAME ${LIB_NAME})
message("Connector library name: ${LIB_NAME}")
if(NOT BUILD_STATIC)
set_property(TARGET connector PROPERTY ARCHIVE_OUTPUT_NAME ${LIB_NAME_BASE})
endif()
set_target_properties(connector PROPERTIES
VERSION "${ABI_VERSION_MAJOR}.${CONCPP_VERSION}"
SOVERSION "${ABI_VERSION_MAJOR}"
)
install(TARGETS connector
CONFIGURATIONS Release RelWithDebInfo
ARCHIVE DESTINATION "${INSTALL_LIB_DIR_STATIC}" COMPONENT XDevAPIDev
RUNTIME DESTINATION "${INSTALL_LIB_DIR}" COMPONENT XDevAPIDll
LIBRARY DESTINATION "${INSTALL_LIB_DIR}" COMPONENT XDevAPIDll
)
install(TARGETS connector
CONFIGURATIONS Debug
ARCHIVE DESTINATION "${INSTALL_LIB_DIR_STATIC}/debug" COMPONENT XDevAPIDev
RUNTIME DESTINATION "${INSTALL_LIB_DIR}/debug" COMPONENT XDevAPIDll
LIBRARY DESTINATION "${INSTALL_LIB_DIR}/debug" COMPONENT XDevAPIDll
)
if(MSVC AND NOT BUILD_STATIC)
install(FILES $<TARGET_PDB_FILE:connector>
CONFIGURATIONS RelWithDebInfo
DESTINATION "${INSTALL_LIB_DIR}"
COMPONENT Debuginfo
)
install(FILES $<TARGET_PDB_FILE:connector>
CONFIGURATIONS Debug
DESTINATION "${INSTALL_LIB_DIR}/debug"
COMPONENT Debuginfo
)
endif()
#
# Tests
# =====
#
#
# Note: We must clear compile flags - the ones used to build the connector
# are not good for building client code that uses the connector.
#
set_property(
DIRECTORY .
PROPERTY COMPILE_DEFINITIONS ""
)
if(BUILD_STATIC)
add_definitions(-DSTATIC_CONCPP)
endif()
#
# Sample code to try things out
#
add_executable(try EXCLUDE_FROM_ALL try.cc)
target_link_libraries(try connector)
if(WITH_JDBC)
add_executable(try_jdbc EXCLUDE_FROM_ALL try_jdbc.cc)
target_link_libraries(try_jdbc connector-jdbc)
endif()
#
# Show dynamic library dependencies for try program.
#
# TODO: Use a cmake module for that
# TODO: Do it also for the shared library
#
if(NOT WIN32)
find_program(LDD ldd)
if(NOT LDD)
find_program(LDD otool)
if(LDD)
set(LDD_OPTS "-L" CACHE INTERNAL "options for linker debugger")
endif()
endif()
if(LDD)
add_custom_command(TARGET try POST_BUILD
COMMAND ${LDD} ${LDD_OPTS} $<TARGET_FILE:try>
COMMENT "Checking dynamic library dependencies:"
)
if(WITH_JDBC)
add_custom_command(TARGET try_jdbc POST_BUILD
COMMAND ${LDD} ${LDD_OPTS} $<TARGET_FILE:try_jdbc>
COMMENT "Checking dynamic library dependencies:"
)
endif()
endif()
endif()
#
# Other tests.
#
include(testing/tests.cmake)
#
# Create the INFO_SRC and INFO_BIN files
# =========================
#
include(buildinfo.cmake)
#
# Packaging specifications
# ========================
#
message("Install location: ${CMAKE_INSTALL_PREFIX}")
message("Connector libraries will be installed at: ${INSTALL_LIB_DIR}")
option(WITH_PACKAGES "Configure for building binary/source packages" OFF)
if(WITH_PACKAGES)
ADD_SUBDIRECTORY(packaging)
endif()
show_config_options()