1# Copyright (c) 2009, 2019, Oracle and/or its affiliates. All rights reserved.
2#
3# This program is free software; you can redistribute it and/or modify
4# it under the terms of the GNU General Public License, version 2.0,
5# as published by the Free Software Foundation.
6#
7# This program is also distributed with certain software (including
8# but not limited to OpenSSL) that is licensed under separate terms,
9# as designated in a particular file or component or in included license
10# documentation.  The authors of MySQL hereby grant you an additional
11# permission to link the program and your derivative works with the
12# separately licensed software that they have included with MySQL.
13#
14# This program is distributed in the hope that it will be useful,
15# but WITHOUT ANY WARRANTY; without even the implied warranty of
16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17# GNU General Public License, version 2.0, for more details.
18#
19# You should have received a copy of the GNU General Public License
20# along with this program; if not, write to the Free Software
21# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
22
23
24GET_FILENAME_COMPONENT(MYSQL_CMAKE_SCRIPT_DIR ${CMAKE_CURRENT_LIST_FILE} PATH)
25INCLUDE(${MYSQL_CMAKE_SCRIPT_DIR}/cmake_parse_arguments.cmake)
26
27# MYSQL_ADD_PLUGIN(plugin_name source1...sourceN
28# [STORAGE_ENGINE]
29# [MANDATORY|DEFAULT]
30# [STATIC_ONLY|DYNAMIC_ONLY]
31# [DTRACE_INSTRUMENTED]
32# [MODULE_OUTPUT_NAME module_name]
33# [STATIC_OUTPUT_NAME static_name]
34# [RECOMPILE_FOR_EMBEDDED]
35# [LINK_LIBRARIES lib1...libN]
36# [DEPENDENCIES target1...targetN]
37
38# Append collections files for the plugin to the common files
39# Make sure we don't copy twice if running cmake again
40
41MACRO(PLUGIN_APPEND_COLLECTIONS plugin)
42  SET(fcopied "${CMAKE_CURRENT_SOURCE_DIR}/tests/collections/FilesCopied")
43  IF(NOT EXISTS ${fcopied})
44    FILE(GLOB collections ${CMAKE_CURRENT_SOURCE_DIR}/tests/collections/*)
45    FOREACH(cfile ${collections})
46      FILE(READ ${cfile} contents)
47      GET_FILENAME_COMPONENT(fname ${cfile} NAME)
48      FILE(APPEND ${CMAKE_SOURCE_DIR}/mysql-test/collections/${fname} "${contents}")
49      FILE(APPEND ${fcopied} "${fname}\n")
50      MESSAGE(STATUS "Appended ${cfile}")
51    ENDFOREACH()
52  ENDIF()
53ENDMACRO()
54
55MACRO(MYSQL_ADD_PLUGIN)
56  MYSQL_PARSE_ARGUMENTS(ARG
57    "LINK_LIBRARIES;DEPENDENCIES;MODULE_OUTPUT_NAME;STATIC_OUTPUT_NAME"
58    "STORAGE_ENGINE;STATIC_ONLY;MODULE_ONLY;MANDATORY;DEFAULT;DISABLED;RECOMPILE_FOR_EMBEDDED;TEST_ONLY;SKIP_INSTALL;DTRACE_INSTRUMENTED"
59    ${ARGN}
60  )
61
62  # Add common include directories
63  INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/include
64                    ${CMAKE_SOURCE_DIR}/sql
65                    ${CMAKE_SOURCE_DIR}/regex
66                    ${SSL_INCLUDE_DIRS}
67                    )
68
69  LIST(GET ARG_DEFAULT_ARGS 0 plugin)
70  SET(SOURCES ${ARG_DEFAULT_ARGS})
71  LIST(REMOVE_AT SOURCES 0)
72  STRING(TOUPPER ${plugin} plugin)
73  STRING(TOLOWER ${plugin} target)
74
75  # Figure out whether to build plugin
76  IF(WITH_PLUGIN_${plugin})
77    SET(WITH_${plugin} 1)
78  ENDIF()
79
80  IF(WITH_MAX_NO_NDB)
81    SET(WITH_MAX 1)
82    SET(WITHOUT_NDBCLUSTER 1)
83  ENDIF()
84
85  IF(ARG_DEFAULT)
86    IF(NOT DEFINED WITH_${plugin} AND
87       NOT DEFINED WITH_${plugin}_STORAGE_ENGINE)
88      SET(WITH_${plugin} 1)
89    ENDIF()
90  ENDIF()
91
92  IF(WITH_${plugin}_STORAGE_ENGINE
93    OR WITH_{$plugin}
94    OR WITH_ALL
95    OR WITH_MAX
96    AND NOT WITHOUT_${plugin}_STORAGE_ENGINE
97    AND NOT WITHOUT_${plugin}
98    AND NOT ARG_MODULE_ONLY)
99
100    SET(WITH_${plugin} 1)
101  ELSEIF(WITHOUT_${plugin}_STORAGE_ENGINE OR WITH_NONE OR ${plugin}_DISABLED)
102    SET(WITHOUT_${plugin} 1)
103    SET(WITH_${plugin} 0)
104    IF(WITH_${plugin}_STORAGE_ENGINE)
105      # if there is a cached/global WITH_${plugin}_STORAGE_ENGINE=ON then overwrite it
106      SET(WITH_${plugin}_STORAGE_ENGINE 0 CACHE BOOL "Don't build ${plugin}" FORCE)
107    ELSE()
108      SET(WITH_${plugin}_STORAGE_ENGINE 0)
109    ENDIF()
110  ENDIF()
111
112
113  IF(ARG_MANDATORY)
114    SET(WITH_${plugin} 1)
115  ENDIF()
116
117
118  IF(ARG_STORAGE_ENGINE)
119    SET(with_var "WITH_${plugin}_STORAGE_ENGINE" )
120  ELSE()
121    SET(with_var "WITH_${plugin}")
122  ENDIF()
123
124  IF(NOT ARG_DEPENDENCIES)
125    SET(ARG_DEPENDENCIES)
126  ENDIF()
127  SET(BUILD_PLUGIN 1)
128  # Build either static library or module
129  IF (WITH_${plugin} AND NOT ARG_MODULE_ONLY)
130    ADD_LIBRARY(${target} STATIC ${SOURCES})
131    SET_TARGET_PROPERTIES(${target} PROPERTIES COMPILE_DEFINITIONS "MYSQL_SERVER")
132    IF (ARG_DTRACE_INSTRUMENTED)
133      DTRACE_INSTRUMENT(${target})
134    ENDIF()
135    ADD_DEPENDENCIES(${target} GenError ${ARG_DEPENDENCIES})
136    IF(WITH_EMBEDDED_SERVER)
137      # Embedded library should contain PIC code and be linkable
138      # to shared libraries (on systems that need PIC)
139      IF(ARG_RECOMPILE_FOR_EMBEDDED OR NOT _SKIP_PIC)
140        # Recompile some plugins for embedded
141        ADD_CONVENIENCE_LIBRARY(${target}_embedded ${SOURCES})
142        IF (ARG_DTRACE_INSTRUMENTED)
143          DTRACE_INSTRUMENT(${target}_embedded)
144        ENDIF()
145        IF(ARG_RECOMPILE_FOR_EMBEDDED)
146          SET_TARGET_PROPERTIES(${target}_embedded
147            PROPERTIES COMPILE_DEFINITIONS "MYSQL_SERVER;EMBEDDED_LIBRARY")
148        ENDIF()
149        ADD_DEPENDENCIES(${target}_embedded GenError)
150      ENDIF()
151    ENDIF()
152
153    IF(ARG_STATIC_OUTPUT_NAME)
154      SET_TARGET_PROPERTIES(${target} PROPERTIES
155      OUTPUT_NAME ${ARG_STATIC_OUTPUT_NAME})
156    ENDIF()
157
158    # Update mysqld dependencies
159    SET (MYSQLD_STATIC_PLUGIN_LIBS ${MYSQLD_STATIC_PLUGIN_LIBS}
160      ${target} ${ARG_LINK_LIBRARIES} CACHE INTERNAL "" FORCE)
161
162    IF(ARG_MANDATORY)
163      SET(${with_var} ON CACHE INTERNAL "Link ${plugin} statically to the server"
164       FORCE)
165    ELSE()
166      SET(${with_var} ON CACHE BOOL "Link ${plugin} statically to the server"
167       FORCE)
168    ENDIF()
169
170    IF(ARG_MANDATORY)
171      SET (mysql_mandatory_plugins
172        "${mysql_mandatory_plugins} builtin_${target}_plugin,"
173      PARENT_SCOPE)
174    ELSE()
175      SET (mysql_optional_plugins
176        "${mysql_optional_plugins} builtin_${target}_plugin,"
177      PARENT_SCOPE)
178    ENDIF()
179  ELSEIF(NOT WITHOUT_${plugin} AND NOT ARG_STATIC_ONLY  AND NOT WITHOUT_DYNAMIC_PLUGINS)
180    IF(NOT ARG_MODULE_OUTPUT_NAME)
181      IF(ARG_STORAGE_ENGINE)
182        SET(ARG_MODULE_OUTPUT_NAME "ha_${target}")
183      ELSE()
184        SET(ARG_MODULE_OUTPUT_NAME "${target}")
185      ENDIF()
186    ENDIF()
187
188    ADD_VERSION_INFO(${target} MODULE SOURCES)
189    ADD_LIBRARY(${target} MODULE ${SOURCES})
190    IF (ARG_DTRACE_INSTRUMENTED)
191      DTRACE_INSTRUMENT(${target})
192    ENDIF()
193    SET_TARGET_PROPERTIES (${target} PROPERTIES PREFIX ""
194      COMPILE_DEFINITIONS "MYSQL_DYNAMIC_PLUGIN")
195    TARGET_LINK_LIBRARIES (${target} mysqlservices)
196
197    GET_TARGET_PROPERTY(LINK_FLAGS ${target} LINK_FLAGS)
198    IF(NOT LINK_FLAGS)
199      # Avoid LINK_FLAGS-NOTFOUND
200      SET(LINK_FLAGS)
201    ENDIF()
202    SET_TARGET_PROPERTIES(${target} PROPERTIES
203      LINK_FLAGS "${CMAKE_SHARED_LIBRARY_C_FLAGS} ${LINK_FLAGS} "
204    )
205
206    # Plugin uses symbols defined in mysqld executable.
207    # Some operating systems like Windows and OSX and are pretty strict about
208    # unresolved symbols. Others are less strict and allow unresolved symbols
209    # in shared libraries. On Linux for example, CMake does not even add
210    # executable to the linker command line (it would result into link error).
211    # Thus we skip TARGET_LINK_LIBRARIES on Linux, as it would only generate
212    # an additional dependency.
213    IF(NOT CMAKE_SYSTEM_NAME STREQUAL "Linux")
214      TARGET_LINK_LIBRARIES (${target} mysqld ${ARG_LINK_LIBRARIES})
215    ENDIF()
216    ADD_DEPENDENCIES(${target} GenError ${ARG_DEPENDENCIES})
217
218     IF(NOT ARG_MODULE_ONLY)
219      # set cached variable, e.g with checkbox in GUI
220      SET(${with_var} OFF CACHE BOOL "Link ${plugin} statically to the server"
221       FORCE)
222     ENDIF()
223    SET_TARGET_PROPERTIES(${target} PROPERTIES
224      OUTPUT_NAME "${ARG_MODULE_OUTPUT_NAME}")
225    # Install dynamic library
226    IF(NOT ARG_SKIP_INSTALL)
227      SET(INSTALL_COMPONENT Server)
228      IF(ARG_TEST_ONLY)
229        SET(INSTALL_COMPONENT Test)
230      ENDIF()
231      MYSQL_INSTALL_TARGETS(${target}
232        DESTINATION ${INSTALL_PLUGINDIR}
233        COMPONENT ${INSTALL_COMPONENT})
234      # Add installed files to list for RPMs
235      FILE(APPEND ${CMAKE_BINARY_DIR}/support-files/plugins.files
236              "%attr(755, root, root) %{_prefix}/${INSTALL_PLUGINDIR}/${ARG_MODULE_OUTPUT_NAME}.so\n"
237              "%attr(755, root, root) %{_prefix}/${INSTALL_PLUGINDIR}/debug/${ARG_MODULE_OUTPUT_NAME}.so\n")
238      # For internal testing in PB2, append collections files
239      IF(DEFINED ENV{PB2WORKDIR})
240        PLUGIN_APPEND_COLLECTIONS(${plugin})
241      ENDIF()
242    ENDIF()
243  ELSE()
244    IF(WITHOUT_${plugin})
245      # Update cache variable
246      STRING(REPLACE "WITH_" "WITHOUT_" without_var ${with_var})
247      SET(${without_var} ON CACHE BOOL "Don't build ${plugin}"
248       FORCE)
249    ENDIF()
250    SET(BUILD_PLUGIN 0)
251  ENDIF()
252
253  IF(BUILD_PLUGIN AND ARG_LINK_LIBRARIES)
254    TARGET_LINK_LIBRARIES (${target} ${ARG_LINK_LIBRARIES})
255  ENDIF()
256
257ENDMACRO()
258
259
260# Add all CMake projects under storage  and plugin
261# subdirectories, configure sql_builtins.cc
262MACRO(CONFIGURE_PLUGINS)
263  FILE(GLOB dirs_storage ${CMAKE_SOURCE_DIR}/storage/*)
264  FILE(GLOB dirs_plugin ${CMAKE_SOURCE_DIR}/plugin/*)
265  # Move perfschema to the beginning of the storage list (as PerconaFT depends on WITH_PERFSCHEMA_STORAGE_ENGINE)
266  LIST(FIND dirs_storage "${CMAKE_SOURCE_DIR}/storage/perfschema" PERFSCHEMA_POS)
267  IF(NOT ${PERFSCHEMA_POS} EQUAL -1)
268    LIST(REMOVE_AT dirs_storage ${PERFSCHEMA_POS})
269    LIST(INSERT dirs_storage 0 "${CMAKE_SOURCE_DIR}/storage/perfschema")
270  ENDIF()
271  FOREACH(dir ${dirs_storage} ${dirs_plugin})
272    IF (EXISTS ${dir}/CMakeLists.txt)
273      ADD_SUBDIRECTORY(${dir})
274    ENDIF()
275  ENDFOREACH()
276ENDMACRO()
277