-
Notifications
You must be signed in to change notification settings - Fork 245
/
mysql-concpp-config.cmake.in
675 lines (502 loc) · 19.5 KB
/
mysql-concpp-config.cmake.in
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
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
# Copyright (c) 2023, 2024, 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 designed to work 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 either included with
# the program or referenced in the documentation.
#
# Without limiting anything contained in the foregoing, this file,
# which is part of Connector/C++, is also subject to the
# Universal FOSS Exception, version 1.0, a copy of which can be found at
# https://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 configuration file for MySQL Connector/C++ package `mysql-concpp`
# =======================================================================
#
# See also: https://cmake.org/cmake/help/latest/manual/cmake-packages.7.html
#
# The following import targets are created by the package. All these targets
# are in the `mysql::` namespace.
#
# - concpp-xdevapi, concpp -- the XDevAPI variant of the library
# - concpp-xdevapi-static, concpp-static -- static variant of the above
# - concpp-jdbc -- the JDBC variant of the library
# - concpp-jdbc-static -- static variant of the above
#
# Targets `mysql::concpp` and `mysql::concpp-static` are aliases for
# `mysql::concpp-xdevapi` and `mysql::concpp-xdevapi-static`, respectively.
#
# Also, the following variables are set:
#
# - Xxx_FOUND
# - Xxx_DEBUG_FOUND, Xxx_debug_FOUND -- (1)
# - Xxx_VERSION, Xxx_VERSION_CC
# - Xxx_ROOT_DIR -- (2)
# - Xxx_RUNTIME_LIBRARY_DIRS, Xxx_RUNTIME_LIBRARY_DIR -- (2,3)
# - Xxx_PLUGIN_DIR -- (4)
#
# In these variable names Xxx is either `MYSQL_CONCPP` or `mysql-concpp`,
# CC is version component: one of `MAJOR`, `MINOR` or `PATCH`.
#
# Note (1): These are set to true if debug libraries are available
# (see below).
#
# Note (2): Set only in case of a monolithic install (TGZ, ZIP, MSI).
#
# Note (3): Application that links to shared connector libraries must find
# these libraries at runtime. Depending on the platform this is achieved by
# configuring RPATH in the executable, editing environment variables
# or copying shared libraries to the location of the executable. In either
# case the _RUNTIME_LIBRARY_DIR(S) variable gives the location where shared
# connector libraries can be found.
#
# Note (4): The JDBC connector might require loading of authentication plugins
# at connection time (depending on authentication mechaism being used). These
# plugins are in the location given by _PLUGIN_DIR variable. Depending on
# the installation type it might be necessary to specify this location
# with connection configuration options for plugins to be correctly found
# at runtime (see: https://dev.mysql.com/doc/connector-cpp/8.2/en/connector-cpp-authentication.html)
#
# Note: The variables are put in the cache but if Xxx_FOUND is not set or
# is false then the module will be re-loaded and the other variables in
# the cache will be overwritten with newly detected values.
#
# Note: If mysql-concpp_FIND_VERBOSE is true when loading package diagnostic
# messages will be printed by this script.
#
# OpenSSL dependency
# ------------------
#
# Connector/C++ requires OpenSSL libraires. Depending on the platform and
# the installation type it is either expected that OpenSSL will be installed
# on the system or these libraries are bundled in the connector package.
# Connector library targets are configured to use OpenSSL from appropriate
# locations. This can be overriden by user -- if `mysql::openssl` target
# is defined prior to loading `mysql-concpp` package then this target is used
# to resolve dependency on the OpenSSL library.
#
# MySQL client library dependency
# -------------------------------
#
# If JDBC connector library is built with the MySQL client library linked
# dynamically then the `concpp-jdbc-static` target depends on `libmysqlclient`
# which should be available on the build host. If it is installed
# in non-standard location then config variable WITH_MYSQL should be set
# to point at the MySQL install location. The library search path will
# be augmented to find the client library installed at that location.
#
# Note that connector libraries published by MySQL have the client library
# statically linked in so that there is no external dependency on it.
#
# Debug libraries
# ---------------
#
# When linking with static Connector/C++ library on Windows and building
# in debug mode the library built in debug mode is needed. Such debug builds
# of Connector/C++ are distributed as a separate package to be installed
# on top of the regular one. If debug libraries are available this script
# will detect them and configure mysql:: import targets so that they use
# debug variants of the library for builds in debug mode. The presence
# of debug libraries is indicated by setting _DEBUG_FOUND variable to true.
# It is also possible to request "debug" component using `REQUIRE debug`
# clause of `find_package()` command. If this is done then `find_package()`
# will fail if debug libraries were not found.
#
# Note: Debug libraries are needed and used only on Windows. For Linux
# separate packages with debug symbols are available that can be used
# to debug connector libraries but only release builds of these libararies
# are distributed.
#
# message(STATUS "mysql-concpp module config (${MYSQL_CONCPP_FOUND}, ${mysql-concpp_FOUND})")
if(mysql-concpp_FOUND)
return()
endif()
function(message_info)
if(NOT mysql-concpp_FIND_VERBOSE OR mysql-concpp_FIND_QUIETLY)
return()
endif()
message(STATUS "mysql-concpp: " ${ARGV})
endfunction()
#
# If WITH_MYSQL is not false then it is assumed that the `concpp-jdbc-static`
# target depends on the MySQL client library. Additionally, if WITH_MYSQL is a
# path pointing at MySQL install location, the library path will be extended so
# that linker looks for the client library at that location.
#
# Note: The default value is set based on whether the JDBC connector was
# statically linked with the client library or not.
#
if(NOT @MYSQLCLIENT_STATIC_LINKING@)
set(JDBC_MYSQL_DEP ON)
else()
set(JDBC_MYSQL_DEP OFF)
endif()
set(WITH_MYSQL ${JDBC_MYSQL_DEP}
CACHE INTERNAL
"MySQL install location (to find the client library)"
)
macro(main)
# Set paths to look for headers and libraries either relative to this file
# or in system-wide locations.
#
# FIXME: lib64/ or lib/ as appropriate for the platform
if(EXISTS "${CMAKE_CURRENT_LIST_DIR}/include/mysqlx/xdevapi.h")
# Case of monolithic install
set(MYSQL_CONCPP_ROOT_DIR "${CMAKE_CURRENT_LIST_DIR}")
set(LIB_PATH "${MYSQL_CONCPP_ROOT_DIR}/@INSTALL_LIB_DIR@")
message_info("Module installed at: ${MYSQL_CONCPP_ROOT_DIR}")
set(INCLUDE_PATH "${MYSQL_CONCPP_ROOT_DIR}/include")
else()
# System-wide install, DEB or RPM layout.
set(INCLUDE_PATH "/usr/include/mysql-cppconn")
get_filename_component(LIB_PATH "${CMAKE_CURRENT_LIST_DIR}/../.." ABSOLUTE)
file(GLOB found "${LIB_PATH}/*libmysqlcppconn*")
if(NOT found)
# Note: Use RPM layout as fallback
set(LIB_PATH "/usr/@INSTALL_LIB_DIR@")
endif()
endif()
# Headers must always be found first.
find_includes()
if(fail_message_includes)
set_not_found(
"Could NOT find MySQL Connector/C++ headers (${fail_message_includes})."
)
# Note: Error out early.
message(FATAL_ERROR ${mysql-concpp_NOT_FOUND_MESSAGE})
endif()
message_info("Include path: ${MYSQL_CONCPP_INCLUDE_DIR}")
# Note: On windows we look for the import library, not the DLL. This import
# library normally is in a ./vsNN subfolder of the library path.
unset(vs_suffix)
if(WIN32)
set(vs_suffix vs14)
endif()
# Find required dependencies. Currtently this looks for OpenSSL and defines
# `mysql::openssl` interface library for it if found.
find_deps()
# Find connector libraries and define interface targets for the ones that
# were found.
find_connector(XDevAPI)
find_connector(JDBC)
# Aliases for -xdevapi* targets.
foreach(suffix "" "-static" "-debug" "-static-debug")
if(TARGET mysql::concpp-xdevapi${suffix})
set(MYSQL_CONCPP_FOUND 1)
add_library(mysql::concpp${suffix} ALIAS mysql::concpp-xdevapi${suffix})
endif()
endforeach()
# Build the NOT_FOUND message.
# Note: The different find_xxx() functions set the specific part
# of the message, such as ${fail_message_devapi}, in case of failure.
set(fail_message "Could NOT find MySQL Connector/C++ at ${MYSQL_CONCPP_RUNTIME_LIBRARY_DIR}.")
if(fail_message_devapi) # AND MYSQL_CONCPP_FIND_REQUIRED_devapi)
list(APPEND fail_message ${fail_message_devapi})
elseif(fail_message_jdbc) # AND MYSQL_CONCPP_FIND_REQUIRED_jdbc)
list(APPEND fail_message ${fail_message_jdbc})
endif()
set_not_found(${fail_message})
include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(mysql-concpp
REQUIRED_VARS
mysql-concpp_VERSION
MYSQL_CONCPP_FOUND
MYSQL_CONCPP_INCLUDE_DIR
VERSION_VAR mysql-concpp_VERSION
# HANDLE_COMPONENTS
FAIL_MESSAGE "${mysql-concpp_NOT_FOUND_MESSAGE}"
)
# Set alternative variables
foreach(var
ROOT_DIR PLUGIN_DIR RUNTIME_LIBRARY_DIR RUNTIME_LIBRARY_DIRS DEBUG_FOUND
)
if(NOT DEFINED MYSQL_CONCPP_${var})
continue()
endif()
set(mysql-concpp_${var} ${MYSQL_CONCPP_${var}} CACHE INTERNAL "mysql-concpp module config variable" FORCE)
set(MYSQL_CONCPP_${var} ${MYSQL_CONCPP_${var}} CACHE INTERNAL "mysql-concpp module config variable" FORCE)
if(var STREQUAL "RUNTIME_LIBRARY_DIR")
set(MYSQL_CONCPP_RUNTIME_LIBRARY_DIRS
"${MYSQL_CONCPP_RUNTIME_LIBRARY_DIR}"
)
endif()
endforeach(var)
foreach(ver "" _MAJOR _MINOR _PATCH _TWEAK _COUNT)
if(NOT DEFINED mysql-concpp_VERSION${ver})
continue()
endif()
set(mysql-concpp_VERSION${ver} ${mysql-concpp_VERSION${ver}} CACHE INTERNAL "mysql-concpp module config variable" FORCE)
set(MYSQL_CONCPP_VERSION${ver} ${mysql-concpp_VERSION${ver}} CACHE INTERNAL "mysql-concpp module config variable" FORCE)
endforeach(ver)
endmacro(main)
# Set the _NOT_FOUND message for the module from a list of strings passed
# to the function.
function(set_not_found)
list(JOIN ARGN " " message)
set(mysql-concpp_NOT_FOUND_MESSAGE "${message}" PARENT_SCOPE)
endfunction()
function(find_includes)
#message(STATUS "Looking for headers at: ${INCLUDE_PATH}")
find_path(MYSQL_CONCPP_INCLUDE_DIR
NAMES mysqlx/xdevapi.h
PATHS ${INCLUDE_PATH}
NO_DEFAULT_PATH
NO_CACHE
)
if(NOT MYSQL_CONCPP_INCLUDE_DIR)
if(MYSQL_CONCPP_ROOT_DIR)
set(fail_message_includes
"at MYSQL_CONCPP_ROOT_DIR: ${INCLUDE_PATH}"
)
else()
set(fail_message_includes "at ${INCLUDE_PATH}")
endif()
set_parent(fail_message_includes)
return()
endif()
set_parent(MYSQL_CONCPP_INCLUDE_DIR)
endfunction()
# Find XDevAPI or JDBC connector libraries, as specified by parameter `which`
# and create interface library targets for them. Both shared and static
# variants are searched for.
#
# TODO: Currently this will fail if release libraries are not found, even
# if debug ones are present. Should this be changed?
function(find_connector which)
if(which STREQUAL "JDBC")
set(base_name "mysqlcppconn")
set(target_name "concpp-jdbc")
else()
set(base_name "mysqlcppconnx")
set(target_name "concpp-xdevapi")
endif()
# Set location of shared libraries or DLLs in case of Win (if not already
# done).
if(NOT MYSQL_CONCPP_RUNTIME_LIBRARY_DIR)
set(MYSQL_CONCPP_RUNTIME_LIBRARY_DIR "${LIB_PATH}")
# Note: set MYSQL_CONCPP_RUNTIME_LIBRARY_DIR as output variable only
# in case of monolithic install.
if(MYSQL_CONCPP_ROOT_DIR)
message_info(
"Runtime library folder: ${MYSQL_CONCPP_RUNTIME_LIBRARY_DIR}"
)
set_parent(MYSQL_CONCPP_RUNTIME_LIBRARY_DIR)
endif()
set(
MYSQL_CONCPP_PLUGIN_DIR "${MYSQL_CONCPP_RUNTIME_LIBRARY_DIR}/plugin"
PARENT_SCOPE
)
endif()
add_connector_target(${which} ${target_name} ${base_name})
# Note: if the mysql::openssl target is not defined then we are on Windows
# and no suitable OpenSSL was found. In that case static libraries will
# not work and we do not define these targets.
if(TARGET mysql::openssl)
add_connector_target(${which} ${target_name}-static ${base_name}-static)
endif()
set_parent(MYSQL_CONCPP_DEBUG_FOUND)
foreach(tgt ${target_name} ${target_name}-static)
if(NOT TARGET mysql::${tgt})
continue()
endif()
unset(libs)
# JDBC dependency on the client library if specified via WITH_MYSQL
if(target_name MATCHES "-jdbc" AND WITH_MYSQL)
list(APPEND libs mysqlclient)
if(EXISTS "${WITH_MYSQL}/lib")
message_info("Client library path: ${WITH_MYSQL}/lib")
target_link_directories(mysql::${tgt}
INTERFACE "${WITH_MYSQL}/lib"
)
endif()
endif()
# OpenSSL dependency (target `mysql::openssl` is defined by find_deps())
#
# Note: Even though legacy connector does not use OpenSSL directly it migth
# have the client library statically linked in and get dependency
# on OpenSSL that way.
if(TARGET mysql::openssl)
list(APPEND libs mysql::openssl)
endif()
if(tgt MATCHES "-static")
set_target_properties(mysql::${tgt} PROPERTIES
IMPORTED_LINK_INTERFACE_LANGUAGES CXX
INTERFACE_COMPILE_DEFINITIONS STATIC_CONCPP
)
# Handle additional dependencies required for static library.
if(WIN32)
list(APPEND libs Dnsapi)
else()
if(NOT APPLE)
list(APPEND libs pthread)
endif()
# On Solaris we additionally need couple more libs.
if(CMAKE_SYSTEM_NAME MATCHES "SunOS")
list(APPEND libs socket nsl)
endif()
if(NOT CMAKE_SYSTEM_NAME MATCHES "FreeBSD")
list(APPEND libs resolv dl)
endif()
endif()
endif()
target_link_libraries(mysql::${tgt} INTERFACE ${libs})
string(JOIN " " libs ${libs})
message_info("Link libraries for target ${tgt}: ${libs}")
endforeach(tgt)
endfunction(find_connector)
# Create connector library import target named ${tgt} and pointing at library
# with base name ${base_name}. Target is not created if the library could
# not be found. If debug variant of library is found then it is used
# for IMPORT_LOCATION_DEBUG. In this case _DEBUG_FOUND variable is set to 1.
function(add_connector_target which tgt base_name)
set(type "SHARED")
if(tgt MATCHES "-static")
set(type "STATIC")
endif()
unset(lib_path CACHE)
find_library(lib_path
NAMES ${base_name}
PATHS ${MYSQL_CONCPP_RUNTIME_LIBRARY_DIR}/${vs_suffix}
NO_DEFAULT_PATH
NO_CACHE
)
if(NOT lib_path)
message_info("Did not find ${which} ${type} library")
return()
endif()
message_info("${which} ${type} library at: ${lib_path}")
#message(STATUS "Creating target: mysql::${tgt}")
add_library(mysql::${tgt} ${type} IMPORTED GLOBAL)
target_compile_features(mysql::${tgt} INTERFACE cxx_std_11)
set_target_properties(mysql::${tgt} PROPERTIES
INTERFACE_INCLUDE_DIRECTORIES "${MYSQL_CONCPP_INCLUDE_DIR}"
)
# TODO: On Win set IMPORTED_LOCATION to the DLL path rather than the import library...
set_target_properties(mysql::${tgt} PROPERTIES
IMPORTED_LOCATION "${lib_path}"
)
if(WIN32)
set_target_properties(mysql::${tgt} PROPERTIES
IMPORTED_IMPLIB "${lib_path}"
)
endif()
# Look for debug variant of the library.
unset(lib_path_debug CACHE)
find_library(lib_path_debug
NAMES ${base_name}
PATHS ${MYSQL_CONCPP_RUNTIME_LIBRARY_DIR}/debug/${vs_suffix}
NO_DEFAULT_PATH
NO_CACHE
)
if(lib_path_debug)
message_info(
"Found debug variant of ${which} ${type} library at: ${lib_path_debug}"
)
set(MYSQL_CONCPP_DEBUG_FOUND 1 PARENT_SCOPE)
endif()
# Note: On non-Windows platforms we set the debug location of the connector
# target only if the debug library variant was found so that if it was
# not found then the release variant will be used.
#
# However, on Windows it is not possible to mix debug and release code.
# Therefore we always set the debug location of the connector target
# on Windows so that if it is used in debug builds and the debug library
# variant is not present a linker error will be reported.
if(WIN32)
set(lib_path_debug
"${MYSQL_CONCPP_RUNTIME_LIBRARY_DIR}/debug/${vs_suffix}/${base_name}.lib"
)
endif()
if(lib_path_debug)
set_target_properties(mysql::${tgt} PROPERTIES
IMPORTED_LOCATION_DEBUG "${lib_path_debug}"
)
if(WIN32)
set_target_properties(mysql::${tgt} PROPERTIES
IMPORTED_IMPLIB_DEBUG "${lib_path_debug}"
)
endif()
endif()
endfunction(add_connector_target)
function(find_deps)
if(TARGET mysql::openssl)
message_info(
"Using custom mysql::openssl target to resolve dependency on OpenSSL"
)
return()
endif()
unset(ssl_lib)
if(MYSQL_CONCPP_ROOT_DIR)
# Try to find the bundled OpenSSL
# Note: On Windows we look for the import library with .lib extension.
set(CMAKE_FIND_LIBRARY_SUFFIXES ".so" ".lib" ".dylib")
unset(ssl_lib CACHE)
find_library(ssl_lib
NAMES ssl libssl
PATHS ${LIB_PATH}
PATH_SUFFIXES private ${vs_suffix}
NO_DEFAULT_PATH
NO_CACHE
)
unset(ssl_crypto CACHE)
find_library(ssl_crypto
NAMES crypto libcrypto
PATHS ${LIB_PATH}
PATH_SUFFIXES private ${vs_suffix}
NO_DEFAULT_PATH
NO_CACHE
)
if(NOT ssl_lib OR NOT ssl_crypto)
message_info("Bundled OpenSSL was not found")
set(ssl_lib false)
endif()
endif()
# Note: For some reason interface libraries can not have names with "::"
add_library(mysql_concpp_openssl INTERFACE)
if(ssl_lib)
message_info("Using bundled OpenSSL")
elseif(WIN32)
message(STATUS "mysql-concpp:"
" For static linking the OpenSSL libraries are required but they are"
" not bundled with the connector -- static connector targets will not"
" be created. To use static connector libraries define mysql::openssl"
" target to point at an OpenSSL installation to be used."
)
return()
else()
message_info("Using system OpenSSL libraries")
set(ssl_lib "ssl")
set(ssl_crypto "crypto")
endif()
target_link_libraries(mysql_concpp_openssl INTERFACE
${ssl_lib} ${ssl_crypto}
)
add_library(mysql::openssl ALIAS mysql_concpp_openssl)
endfunction(find_deps)
# Sets given variable in the parent scope to its current value in this scope.
# Optionally, if new value is given after variable name, variable's value
# is changed first.
macro(set_parent var)
#message(STATUS "set_parent: ${var} (${ARGN})")
if(${ARGC} GREATER 1)
set(${var} "${ARGN}")
endif()
set(${var} "${${var}}" PARENT_SCOPE)
endmacro()
main()