1
0

FindMySQL.cmake 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. # Find MySQL.
  2. #
  3. # This module prefers the MariaDB implementation of MySQL (it works better, especially on Windows). It will
  4. # only look for standard MySQL if MariaDB is not found.
  5. #
  6. # Set this variable to any additional path you want the module to search:
  7. # MySQL_DIR
  8. #
  9. # Imported targets defined by this module:
  10. # MySQL::mysql - shared library if available, or static if that's all there is
  11. # MySQL::mysql_shared - shared library
  12. # MySQL::mysql_static - static library
  13. #
  14. # Informational variables:
  15. # MySQL_FOUND - MySQL (or all requested components of MySQL) was found.
  16. # MySQL_VERSION - the version of MySQL that was found
  17. # MySQL_IS_MARIADB - TRUE if the MySQL that was found is MariaDB
  18. #
  19. include(CacheLog)
  20. include(FindPackageHandleStandardArgs)
  21. # Helper function for globbing for directories.
  22. function(append_glob_dirs list_name glob_path)
  23. file(TO_CMAKE_PATH "${glob_path}" glob_path)
  24. file(GLOB dirs LIST_DIRECTORIES true "${glob_path}")
  25. if (dirs)
  26. list(APPEND ${list_name} "${dirs}")
  27. set(${list_name} "${${list_name}}" PARENT_SCOPE)
  28. endif ()
  29. endfunction()
  30. # If MySQL_DIR has changed since the last invocation, wipe internal cache variables so we can search for everything
  31. # again.
  32. if (NOT "${MySQL_DIR}" STREQUAL "${_internal_old_mysql_dir}")
  33. unset_cachelog_entries()
  34. endif ()
  35. reset_cachelog()
  36. set(_old_suffixes "${CMAKE_FIND_LIBRARY_SUFFIXES}")
  37. # Set path guesses.
  38. set(_paths)
  39. if (CMAKE_HOST_WIN32)
  40. if (CMAKE_SIZEOF_VOID_P EQUAL 8)
  41. set(archdir "$ENV{ProgramFiles}")
  42. set(notarchdir "$ENV{ProgramFiles\(x86\)}")
  43. else ()
  44. set(archdir "$ENV{ProgramFiles\(x86\)}")
  45. set(notarchdir "$ENV{ProgramFiles}")
  46. endif ()
  47. # MariaDB
  48. append_glob_dirs(_paths "${archdir}/MariaDB*/")
  49. append_glob_dirs(_paths "${notarchdir}/MariaDB*/")
  50. # MySQL
  51. append_glob_dirs(_paths "${archdir}/MySQL/MySQL Connector.C*/")
  52. append_glob_dirs(_paths "${notarchdir}/MySQL/MySQL Connector.C*/")
  53. else ()
  54. if (CMAKE_SIZEOF_VOID_P EQUAL 8)
  55. set(arch 64)
  56. else ()
  57. set(arch 32)
  58. endif ()
  59. list(APPEND _paths
  60. /usr/local/mariadb${arch}
  61. /usr/local/mysql${arch}
  62. /usr/local/mariadb
  63. /usr/local/mysql
  64. )
  65. if (APPLE)
  66. list(APPEND _paths /usr/local/opt/mariadb-connector-c)
  67. append_glob_dirs(_paths "/usr/local/Cellar/mariadb-connector-c/*/")
  68. list(APPEND _paths /usr/local/opt/mysql-connector-c)
  69. append_glob_dirs(_paths "/usr/local/Cellar/mysql-connector-c/*/")
  70. endif ()
  71. endif ()
  72. # Find include directory. Try hints set by variable and stuff from _paths, first.
  73. set(_suffixes
  74. mariadb
  75. include/mariadb
  76. mariadb/include
  77. mysql
  78. include/mysql include
  79. )
  80. find_path(MySQL_INCLUDE_DIR
  81. NAMES mysql.h
  82. HINTS ${MySQL_DIR}
  83. $ENV{MySQL_DIR}
  84. ${MYSQL_ROOT_DIR}
  85. PATHS ${_paths}
  86. PATH_SUFFIXES ${_suffixes}
  87. NO_DEFAULT_PATH
  88. )
  89. if (NOT MySQL_INCLUDE_DIR)
  90. # If we didn't find anything using those hints, proceed to checking system paths. We need
  91. # to do this in two steps in order to prevent a MySQL installation installed in system paths
  92. # from being found before a MariaDB installation in /usr/local.
  93. find_path(MySQL_INCLUDE_DIR
  94. NAMES mysql.h
  95. PATH_SUFFIXES ${_suffixes}
  96. )
  97. endif ()
  98. add_to_cachelog(MySQL_INCLUDE_DIR)
  99. if (MySQL_INCLUDE_DIR)
  100. # Figure out if these headers belong to MariaDB or not.
  101. if (MySQL_INCLUDE_DIR MATCHES "[Mm][Aa][Rr][Ii][Aa][Dd][Bb]")
  102. set(MySQL_IS_MARIADB TRUE)
  103. else ()
  104. set(MySQL_IS_MARIADB FALSE)
  105. endif ()
  106. # Calculate root directory of installation from include directory.
  107. string(REGEX REPLACE "(/)*include(/)*.*$" "" _root_dir "${MySQL_INCLUDE_DIR}")
  108. set(MySQL_DIR "${_root_dir}" CACHE PATH "Root directory of MySQL installation" FORCE)
  109. set(_internal_old_mysql_dir "${MySQL_DIR}" CACHE INTERNAL "" FORCE)
  110. mark_as_advanced(FORCE MySQL_DIR)
  111. # Find version by parsing the mysql_version.h or mariadb_version.h header.
  112. set(ver_file "${MySQL_INCLUDE_DIR}/mysql_version.h")
  113. if (NOT EXISTS "${ver_file}")
  114. # Note: some (but not all) mariadb installations in the wild have mariadb_version.h instead of mysql_version.h
  115. set(ver_file "${MySQL_INCLUDE_DIR}/mariadb_version.h")
  116. if (EXISTS "${ver_file}")
  117. set(MySQL_IS_MARIADB TRUE)
  118. else ()
  119. set(ver_file)
  120. endif ()
  121. endif ()
  122. if (ver_file)
  123. file(STRINGS "${ver_file}" _id_str REGEX "^[ \t]*#[ \t]*define[\t ]+MYSQL_VERSION_ID[ \t]+[0-9]+")
  124. if (_id_str MATCHES "^[ \t]*#[ \t]*define[\t ]+MYSQL_VERSION_ID[ \t]+([0-9]+)")
  125. math(EXPR _major "${CMAKE_MATCH_1} / 10000")
  126. math(EXPR _minor "( ${CMAKE_MATCH_1} % 10000 ) / 100")
  127. math(EXPR _patch "${CMAKE_MATCH_1} % 100")
  128. set(MySQL_VERSION "${_major}.${_minor}.${_patch}")
  129. endif ()
  130. # If we haven't detected mariadb yet, try scanning the version file for mentions.
  131. if (NOT MySQL_IS_MARIADB)
  132. file(STRINGS "${MySQL_INCLUDE_DIR}/mysql_version.h" _mariadb REGEX [Mm][Aa][Rr][Ii][Aa][Dd][Bb])
  133. if (_mariadb)
  134. set(MySQL_IS_MARIADB TRUE)
  135. endif ()
  136. endif ()
  137. endif ()
  138. # Set library names, based on which mysql we found.
  139. if (MySQL_IS_MARIADB)
  140. set(_static_names mariadbclient mariadb)
  141. set(_shared_names libmariadb mariadb)
  142. set(_dll_names libmariadb)
  143. else ()
  144. set(_static_names mysqlclient)
  145. set(_shared_names libmysql mysqlclient)
  146. set(_dll_names libmysql)
  147. endif ()
  148. # Find static library.
  149. set(CMAKE_FIND_LIBRARY_SUFFIXES ${CMAKE_STATIC_LIBRARY_SUFFIX})
  150. foreach(defpath "NO_DEFAULT_PATH" "")
  151. find_library(MySQL_STATIC_LIBRARY
  152. NAMES ${_static_names}
  153. NAMES_PER_DIR
  154. HINTS ${MySQL_DIR}
  155. PATH_SUFFIXES lib lib/mariadb
  156. ${defpath}
  157. )
  158. if (MySQL_STATIC_LIBRARY)
  159. break()
  160. endif ()
  161. endforeach()
  162. add_to_cachelog(MySQL_STATIC_LIBRARY)
  163. if (MySQL_STATIC_LIBRARY)
  164. set(MySQL_mysql_static_FOUND TRUE)
  165. set(MySQL_mysql_FOUND TRUE)
  166. endif ()
  167. # Find shared library (will pick up static lib if shared not found).
  168. set(CMAKE_FIND_LIBRARY_SUFFIXES "${_old_suffixes}")
  169. foreach(defpath "NO_DEFAULT_PATH" "")
  170. find_library(MySQL_LIBRARY
  171. NAMES ${_shared_names}
  172. NAMES_PER_DIR
  173. HINTS ${MySQL_DIR}
  174. PATH_SUFFIXES lib lib/mariadb
  175. ${defpath}
  176. )
  177. if (MySQL_LIBRARY)
  178. break()
  179. endif ()
  180. endforeach()
  181. add_to_cachelog(MySQL_LIBRARY)
  182. if (MySQL_LIBRARY AND NOT MySQL_LIBRARY STREQUAL MySQL_STATIC_LIBRARY)
  183. set(MySQL_mysql_shared_FOUND TRUE)
  184. set(MySQL_mysql_FOUND TRUE)
  185. endif ()
  186. # Find the DLL (if any).
  187. if (WIN32)
  188. set(CMAKE_FIND_LIBRARY_SUFFIXES .dll)
  189. foreach(defpath "NO_DEFAULT_PATH" "")
  190. find_library(MySQL_DLL_LIBRARY
  191. NAMES ${_dll_names}
  192. NAMES_PER_DIR
  193. HINTS ${MySQL_DIR}
  194. PATH_SUFFIXES bin lib bin/mariadb lib/mariadb ""
  195. ${defpath}
  196. )
  197. if (MySQL_DLL_LIBRARY)
  198. break()
  199. endif ()
  200. endforeach()
  201. add_to_cachelog(MySQL_DLL_LIBRARY)
  202. endif ()
  203. endif ()
  204. set(_reqs MySQL_INCLUDE_DIR)
  205. if (NOT MySQL_FIND_COMPONENTS) # If user didn't request any particular component explicitly:
  206. if (NOT MySQL_mysql_FOUND)
  207. list(APPEND _reqs MySQL_LIBRARY)
  208. endif ()
  209. endif ()
  210. find_package_handle_standard_args(MySQL
  211. REQUIRED_VARS ${_reqs}
  212. VERSION_VAR MySQL_VERSION
  213. HANDLE_COMPONENTS
  214. FAIL_MESSAGE "MySQL not found, try -DMySQL_DIR=<path>"
  215. )
  216. add_to_cachelog(FIND_PACKAGE_MESSAGE_DETAILS_MySQL)
  217. # Static library.
  218. if (MySQL_mysql_static_FOUND AND NOT TARGET MySQL::mysql_static)
  219. add_library(MySQL::mysql_static STATIC IMPORTED)
  220. set_target_properties(MySQL::mysql_static PROPERTIES
  221. IMPORTED_LOCATION "${MySQL_STATIC_LIBRARY}"
  222. IMPORTED_LINK_INTERFACE_LANGUAGES "C"
  223. INTERFACE_INCLUDE_DIRECTORIES "${MySQL_INCLUDE_DIR}"
  224. )
  225. set(_mysql_any MySQL::mysql_static)
  226. endif ()
  227. # Shared library.
  228. if (MySQL_mysql_shared_FOUND AND NOT TARGET MySQL::mysql_shared)
  229. add_library(MySQL::mysql_shared SHARED IMPORTED)
  230. set_target_properties(MySQL::mysql_shared PROPERTIES
  231. IMPORTED_LINK_INTERFACE_LANGUAGES "C"
  232. INTERFACE_INCLUDE_DIRECTORIES "${MySQL_INCLUDE_DIR}"
  233. )
  234. if (WIN32)
  235. set_target_properties(MySQL::mysql_shared PROPERTIES
  236. IMPORTED_IMPLIB "${MySQL_LIBRARY}"
  237. )
  238. if (MySQL_DLL_LIBRARY)
  239. set_target_properties(MySQL::mysql_shared PROPERTIES
  240. IMPORTED_LOCATION "${MySQL_DLL_LIBRARY}"
  241. )
  242. endif ()
  243. else ()
  244. set_target_properties(MySQL::mysql_shared PROPERTIES
  245. IMPORTED_LOCATION "${MySQL_LIBRARY}"
  246. )
  247. endif ()
  248. set(_mysql_any MySQL::mysql_shared)
  249. endif ()
  250. # I-don't-care library (shared, or static if shared not available).
  251. if (MySQL_mysql_FOUND AND NOT TARGET MySQL::mysql)
  252. add_library(MySQL::mysql INTERFACE IMPORTED)
  253. set_target_properties(MySQL::mysql PROPERTIES
  254. INTERFACE_LINK_LIBRARIES ${_mysql_any}
  255. )
  256. endif ()
  257. set(CMAKE_FIND_LIBRARY_SUFFIXES "${_old_suffixes}")
  258. # From https://github.com/Monetra/mstdlib/blob/master/CMakeModules
  259. # The MIT License (MIT)
  260. # Copyright (c) 2015-2017 Main Street Softworks, Inc.
  261. # Permission is hereby granted, free of charge, to any person obtaining a copy
  262. # of this software and associated documentation files (the "Software"), to deal
  263. # in the Software without restriction, including without limitation the rights
  264. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  265. # copies of the Software, and to permit persons to whom the Software is
  266. # furnished to do so, subject to the following conditions:
  267. # The above copyright notice and this permission notice shall be included in
  268. # all copies or substantial portions of the Software.
  269. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  270. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  271. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  272. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  273. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  274. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  275. # THE SOFTWARE.