-
Notifications
You must be signed in to change notification settings - Fork 4k
/
Copy pathPlugin.cmake
290 lines (267 loc) · 9.58 KB
/
Plugin.cmake
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
# Copyright (c) 2015, 2025, 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.
#
# 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 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
# add_harness_plugin - Add a new plugin target and set install
# location
#
# add_harness_plugin(name [NO_INSTALL]
# LOG_DOMAIN domain
# SOURCES file1 ...
# INTERFACE directory
# DESTINATION directory
# REQUIRES plugin ...)
#
# The add_harness_plugin command will set up a new plugin target and
# also set the install location of the target correctly.
#
# Plugins that are normally put under the "lib" directory of the build
# root, but see the caveat in the next paragraph.
#
# If NO_INSTALL is provided, it will not be installed, which is useful
# if the plugin is only for testing purposes. These plugins are also
# left in their default location and not moved to the "lib"
# directory. If you want to move the plugin to some specific
# directory, you have to set the target property
# LIBRARY_OUTPUT_DIRECTORY yourself.
#
# If LOG_DOMAIN is given, it will be used as the log domain for the
# plugin. If no LOG_DOMAIN is given, the log domain will be the name
# of the plugin.
#
# DESTINATION specifies the directory where plugins will be installed.
# Providing it is compulsory unless NO_INSTALL is provided.
#
# Files provided after the SOURCES keyword are the sources to build
# the plugin from, while the files in the directory after INTERFACE
# will be installed alongside the header files for the harness.
#
# For plugins, it is necessary to set the RPATH so that the plugin can
# find other plugins when being loaded. This, unfortunately, means
# that the plugin path need to be set at compile time and cannot be
# changed after that.
FUNCTION(add_harness_plugin NAME)
SET(_options NO_INSTALL)
SET(_single_value LOG_DOMAIN INTERFACE DESTINATION OUTPUT_NAME)
SET(_multi_value SOURCES REQUIRES)
CMAKE_PARSE_ARGUMENTS(_option
"${_options}" "${_single_value}" "${_multi_value}" ${ARGN})
IF(_option_UNPARSED_ARGUMENTS)
MESSAGE(AUTHOR_WARNING
"Unrecognized arguments: ${_option_UNPARSED_ARGUMENTS}")
ENDIF()
# Set the log domain to the name of the plugin unless an explicit
# log domain was given.
IF(NOT _option_LOG_DOMAIN)
SET(_option_LOG_DOMAIN "\"${NAME}\"")
ENDIF()
IF(NOT _option_NO_INSTALL)
ADD_VERSION_INFO(SHARED _option_SOURCES Router)
ENDIF()
# Add the library and ensure that the name is good for the plugin
# system (no "lib" before). We are using SHARED libraries since we
# intend to link against it, which is something that MODULE does not
# allow. On OSX, this means that the suffix for the library becomes
# .dylib, which we do not want, so we reset it here.
ADD_LIBRARY(${NAME} SHARED ${_option_SOURCES})
TARGET_COMPILE_FEATURES(${NAME} PUBLIC cxx_std_20)
IF(APPLE)
TARGET_LINK_OPTIONS(${NAME} PRIVATE LINKER:-no_warn_duplicate_libraries)
ENDIF()
# add plugin to build-all target
ADD_DEPENDENCIES(mysqlrouter_all ${NAME})
IF(_option_OUTPUT_NAME)
SET_TARGET_PROPERTIES(${NAME}
PROPERTIES OUTPUT_NAME ${_option_OUTPUT_NAME})
ENDIF()
TARGET_COMPILE_DEFINITIONS(${NAME} PRIVATE
MYSQL_ROUTER_LOG_DOMAIN=${_option_LOG_DOMAIN})
IF(NOT WIN32)
SET_TARGET_PROPERTIES(${NAME} PROPERTIES
PREFIX ""
SUFFIX ".so")
ENDIF()
# Declare the interface directory for this plugin, if present. It
# will be used both when compiling the plugin as well as as for any
# dependent targets.
IF(_option_INTERFACE)
TARGET_INCLUDE_DIRECTORIES(${NAME}
PUBLIC ${_option_INTERFACE})
execute_process(
COMMAND ${CMAKE_COMMAND} -E copy_directory
${CMAKE_CURRENT_SOURCE_DIR}/${_option_INTERFACE}
${MySQLRouter_BINARY_DIR}/${INSTALL_INCLUDE_DIR})
ENDIF()
# Add a dependencies on interfaces for other plugins this plugin
# requires.
TARGET_LINK_LIBRARIES(${NAME}
PUBLIC harness-library
${_option_REQUIRES})
# Need to be able to link plugins with each other
IF(APPLE)
SET_TARGET_PROPERTIES(${NAME} PROPERTIES
LINK_FLAGS "-undefined dynamic_lookup")
ENDIF()
SET_TARGET_PROPERTIES(${NAME} PROPERTIES
LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/plugin_output_directory
RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/plugin_output_directory
)
IF(APPLE_WITH_CUSTOM_SSL)
ADD_CUSTOM_COMMAND(TARGET ${NAME} POST_BUILD
COMMAND install_name_tool -change
"${CRYPTO_VERSION}" "@loader_path/${CRYPTO_VERSION}"
$<TARGET_FILE_NAME:${NAME}>
COMMAND install_name_tool -change
"${OPENSSL_VERSION}" "@loader_path/${OPENSSL_VERSION}"
$<TARGET_FILE_NAME:${NAME}>
WORKING_DIRECTORY
${CMAKE_BINARY_DIR}/plugin_output_directory/${CMAKE_CFG_INTDIR}
)
ENDIF()
# Add install rules to install the interface header files and the
# plugin correctly.
IF(NOT _option_NO_INSTALL)
IF(NOT _option_DESTINATION)
MESSAGE(ERROR "Parameter 'DESTINATION' is mandatory unless 'NO_INSTALL' is provided.")
ENDIF()
IF(WIN32)
INSTALL(TARGETS ${NAME}
RUNTIME DESTINATION ${_option_DESTINATION}
COMPONENT Router)
INSTALL(FILES $<TARGET_PDB_FILE:${NAME}>
DESTINATION ${_option_DESTINATION}
COMPONENT Router)
ELSE()
INSTALL(TARGETS ${NAME}
LIBRARY DESTINATION ${_option_DESTINATION}
COMPONENT Router)
ENDIF()
IF(_option_INTERFACE)
FILE(GLOB interface_files ${_option_INTERFACE}/*.h)
INSTALL(FILES ${interface_files}
DESTINATION ${HARNESS_INSTALL_INCLUDE_PREFIX}/${HARNESS_NAME}
COMPONENT Router)
ENDIF()
ENDIF()
ENDFUNCTION(add_harness_plugin)
#
# ROUTER_ADD_SHARED_LIBRARY(target sources [opts])
#
# @param target targetname of the created shared library.
# @param sources source files of the shared library target.
# @param opts extra options
#
# - creates a shared library "target"
# - with OUTPUT_NAME (default target-name)
# - installs into $ROUTER_INSTALL_LIBDIR (if SKIP_INSTALL is not specified)
# - generates a export-header in bindir/../include/mysqlrouter/{target}_export.h
#
# Options
# =======
#
# All options of ADD_SHARED_LIBRARY() are supported.
#
# Additionally,
#
# NO_EXPORT_HEADER
# : do not generate a export-headers for the shared library.
# default: generate a export-header
#
# PREFIX
# : PREFIX of the shared library name (cmake property)
#
# OUTPUT_NAME
# : OUTPUT_NAME of the shared library (cmake property)
# default: targetname
#
# SOVERSION
# : SOVERSION of the shared library (cmake property)
# default: 1
#
# DESTINATION
# : DESTINATION of the shared library (see MYSQL_INSTALL_TARGET)
# default: $ROUTER_INSTALL_BINDIR on windows, $ROUTER_INSTALL_LIBDIR otherwise
FUNCTION(ROUTER_ADD_SHARED_LIBRARY TARGET)
SET(_options
NO_EXPORT_HEADER
)
SET(_single_value
COMPONENT
DESTINATION
PREFIX
SOVERSION
)
SET(_multi_value
INCLUDE_DIRECTORIES
)
CMAKE_PARSE_ARGUMENTS(_option
"${_options}" "${_single_value}" "${_multi_value}" ${ARGN})
SET(ARGS ${_option_UNPARSED_ARGUMENTS})
IF(NOT DEFINED _option_SOVERSION)
LIST(APPEND ARGS SOVERSION 1)
ELSE()
LIST(APPEND ARGS SOVERSION ${_option_SOVERSION})
ENDIF()
IF(NOT DEFINED _option_COMPONENT)
LIST(APPEND ARGS COMPONENT Router)
ELSE()
LIST(APPEND ARGS COMPONENT ${_option_COMPONENT})
ENDIF()
IF(NOT DEFINED _option_INCLUDE_DIRECTORIES)
LIST(APPEND ARGS INCLUDE_DIRECTORIES ${CMAKE_CURRENT_SOURCE_DIR})
ELSE()
LIST(APPEND ARGS INCLUDE_DIRECTORIES ${_option_INCLUDE_DIRECTORIES})
ENDIF()
IF(NOT DEFINED _option_DESTINATION)
# ADD_SHARED_LIBRARY calls MYSQL_INSTALL_TARGET which only knows
# about a single DESTINATIONS, but we want
# - .dll's in the BINDIR and
# - .so in the LIBDIR.
IF(WIN32)
LIST(APPEND ARGS DESTINATION ${ROUTER_INSTALL_BINDIR})
ELSE()
LIST(APPEND ARGS DESTINATION ${ROUTER_INSTALL_LIBDIR})
ENDIF()
ELSE()
LIST(APPEND ARGS DESTINATION ${_option_DESTINATION})
ENDIF()
ADD_SHARED_LIBRARY(${TARGET} ${ARGS}
NAMELINK_SKIP
)
ADD_INSTALL_RPATH_FOR_OPENSSL(${TARGET})
SET_PATH_TO_CUSTOM_SSL_FOR_APPLE(${TARGET})
IF(_option_PREFIX)
SET_TARGET_PROPERTIES(${TARGET} PROPERTIES
PREFIX "${_option_PREFIX}"
)
ENDIF()
IF(NOT _option_NO_EXPORT_HEADER)
TARGET_INCLUDE_DIRECTORIES(${TARGET}
PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}/../include/
${CMAKE_CURRENT_BINARY_DIR}/../include/
)
GENERATE_EXPORT_HEADER(${TARGET}
EXPORT_FILE_NAME
${CMAKE_CURRENT_BINARY_DIR}/../include/mysqlrouter/${TARGET}_export.h
)
ENDIF()
ENDFUNCTION()