FindMySQL.cmake 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914
  1. # -*- indent-tabs-mode:nil; -*-
  2. # vim: set expandtab:
  3. #
  4. # Copyright (c) 2011, 2024, Oracle and/or its affiliates.
  5. #
  6. # This program is free software; you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License, version 2.0, as
  8. # published by the Free Software Foundation.
  9. #
  10. # This program is designed to work with certain software (including
  11. # but not limited to OpenSSL) that is licensed under separate terms, as
  12. # designated in a particular file or component or in included license
  13. # documentation. The authors of MySQL hereby grant you an additional
  14. # permission to link the program and your derivative works with the
  15. # separately licensed software that they have either included with
  16. # the program or referenced in the documentation.
  17. #
  18. # Without limiting anything contained in the foregoing, this file,
  19. # which is part of Connector/ODBC, is also subject to the
  20. # Universal FOSS Exception, version 1.0, a copy of which can be found at
  21. # https://oss.oracle.com/licenses/universal-foss-exception.
  22. #
  23. # This program is distributed in the hope that it will be useful, but
  24. # WITHOUT ANY WARRANTY; without even the implied warranty of
  25. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  26. # See the GNU General Public License, version 2.0, for more details.
  27. #
  28. # You should have received a copy of the GNU General Public License
  29. # along with this program; if not, write to the Free Software Foundation, Inc.,
  30. # 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  31. ##########################################################################
  32. ##########################################################################
  33. #
  34. # Configuration variables, all optional, are
  35. #
  36. # MYSQL_DIR - Set in environment or as parameter to "cmake",
  37. # this is the top directory of the MySQL Server or
  38. # Connector/C install
  39. # MYSQL_INCLUDE_DIR - Set in environment or as parameter to "cmake",
  40. # this is the include directory where to find
  41. # the client library
  42. # MYSQL_LIB_DIR - Set in environment or as parameter to "cmake",
  43. # this is the library directory where to find
  44. # the client library
  45. # MYSQLCLIENT_STATIC_LINKING
  46. # - Specify that you want static linking, dynamic
  47. # linking is the default
  48. # MYSQLCLIENT_NO_THREADS
  49. # - Specify to link against the single threaded
  50. # library, "libmysqlclient". Note that in 5.5
  51. # and up "libmysqlclient" is multithreaded and
  52. # "libmysqlclient_r" just a soft link to it
  53. # MYSQL_CONFIG_EXECUTABLE
  54. # - "mysql_config" executable to use
  55. # MYSQL_CXX_LINKAGE - Specify that client library needs C++ linking
  56. # MYSQL_EXTRA_LIBRARIES
  57. # - Libraries to add to the linkage
  58. # MYSQL_CFLAGS - C compiler flags
  59. # MYSQL_CXXFLAGS - C++ compiler flags
  60. # MYSQL_LINK_FLAGS - User defined extra linkage flags
  61. # FINDMYSQL_DEBUG - Set if want debug output from this script
  62. #
  63. # Note that most variables above, if not set by the user they will be
  64. # set by this include file.
  65. #
  66. # In addition, the below CMake variables are set by this include file
  67. #
  68. # MYSQL_VERSION - Three position numeric version, like 5.6.41
  69. # MYSQL_VERSION_ID - Numeric padded version, 5.13.4 => 51304
  70. # MYSQL_NUM_VERSION - Same as MYSQL_VERSION_ID, for compatibility
  71. # MYSQL_LIB - Path to the client library
  72. # MYSQL_LIBRARIES - Library name, might be "-lmysqlclient" while
  73. # MYSQL_LIB is the path to the library
  74. # MYSQL_CLIENT_LIBS - Same as MYSQL_LIBRARIES, for compatibility
  75. #
  76. # (1) If MYSQL_INCLUDE_DIR or MYSQL_LIB_DIR are given, these are
  77. # used and an error is reported if can't be used
  78. # (2) If MYSQL_CONFIG_EXECUTABLE is given, it is used to get
  79. # headers and libraries
  80. # (3) If MYSQL_DIR is given and "${MYSQL_DIR}/bin/mysql_config" is
  81. # found, then same as (2)
  82. # (4) If MYSQL_DIR is given and no "${MYSQL_DIR}/bin/mysql_config",
  83. # search MYSQL_DIR
  84. #
  85. # FIXME if we get a "mysql_config" on Windows, things needs to change
  86. # FIXME rename the VERSION variables above
  87. # FIXME let MYSQL_VERSION include "-beta" etc?
  88. # FIXME can mysql_config --version be C/C verson?
  89. # FIXME if no mysql_config, find version from include/mysql_version.h?
  90. # #define MYSQL_SERVER_VERSION "5.7.5-m15"
  91. # #define MYSQL_VERSION_ID 50705
  92. # #define LIBMYSQL_VERSION "6.1.5"
  93. # #define LIBMYSQL_VERSION_ID 60105
  94. # FIXME can MYSQL_LIB_DIR be a list of paths?
  95. # FIXME is MYSQLCLIENT_LIBS a better name?
  96. # FIXME cache variables, makes them command line args?
  97. # FIXME really do include_directories() and link_directories()? Likely
  98. # FIXME add check that if not static, not picked up .a or mysqlclient.lib
  99. # FIXME MYSQL_VERSION_ID need to take into account Cluster versions
  100. # and Connector/C versions
  101. # FIXME handle MYSQL_VERSION_ID, LIBMYSQL_VERSION and LIBMYSQL_VERSION_ID?
  102. #
  103. ##########################################################################
  104. ##########################################################################
  105. #
  106. # Check the input data
  107. #
  108. ##########################################################################
  109. # If using both MYSQL_DIR as a cmake argument and set in environment,
  110. # and not empty strings, they better be the same. Else stop and complain
  111. set(ENV_OR_OPT_VARS
  112. MYSQL_DIR
  113. MYSQL_INCLUDE_DIR
  114. MYSQL_LIB_DIR
  115. MYSQL_LIB_DIR_LIST
  116. MYSQL_PLUGIN_DIR
  117. MYSQL_CFLAGS
  118. MYSQL_CXXFLAGS
  119. MYSQL_CONFIG_EXECUTABLE
  120. MYSQLCLIENT_STATIC_LINKING
  121. MYSQLCLIENT_NO_THREADS
  122. MYSQL_CXX_LINKAGE
  123. MYSQL_EXTRA_LIBRARIES
  124. MYSQL_LINK_FLAGS
  125. )
  126. # Mark the variable names that have values that are paths
  127. set(ENV_OR_OPT_PATH_VARS MYSQL_DIR MYSQL_INCLUDE_DIR MYSQL_LIB_DIR MYSQL_PLUGIN_DIR)
  128. foreach(_xvar ${ENV_OR_OPT_VARS})
  129. if((DEFINED ${_xvar})
  130. AND (DEFINED ENV{${_xvar}})
  131. AND (NOT "${${_xvar}}" STREQUAL "")
  132. AND (NOT "$ENV{${_xvar}}" STREQUAL "")
  133. AND (NOT "$ENV{${_xvar}}" STREQUAL "${${_xvar}}")
  134. )
  135. message(FATAL_ERROR "Please pass -D${_xvar}=... as an argument or "
  136. "set ${_xvar} in the environment, but not both"
  137. )
  138. endif()
  139. # Now we know both are not set, set the CMake variable if needed
  140. if((DEFINED ENV{${_xvar}}) AND (NOT "$ENV{${_xvar}}" STREQUAL ""))
  141. set(${_xvar} $ENV{${_xvar}})
  142. endif()
  143. # Notmalize the path if the variable is set and is a path
  144. if(${_xvar})
  145. list(FIND ENV_OR_OPT_PATH_VARS ${_xvar} _index)
  146. if(${_index} GREATER -1)
  147. file(TO_CMAKE_PATH "${${_xvar}}" ${_xvar})
  148. get_filename_component(${_xvar} ${${_xvar}} ABSOLUTE)
  149. endif()
  150. endif()
  151. endforeach()
  152. # Bail out if both MYSQL_DIR/MYSQL_CONFIG_EXECUTABLE and MYSQL_INCLUDE/LIB_DIR
  153. # were given
  154. if(MYSQL_DIR AND (MYSQL_INCLUDE_DIR OR MYSQL_LIB_DIR))
  155. message(FATAL_ERROR "Both MYSQL_DIR and MYSQL_INCLUDE_DIR/MYSQL_LIB_DIR were specified,"
  156. " use either one or the other way of pointing at MySQL location."
  157. )
  158. endif()
  159. if(MYSQL_CONFIG_EXECUTABLE AND (MYSQL_INCLUDE_DIR OR MYSQL_LIB_DIR))
  160. message(
  161. FATAL_ERROR
  162. "Both MYSQL_CONFIG_EXECUTABLE and MYSQL_INCLUDE_DIR/MYSQL_LIB_DIR were specified,"
  163. " mixing settings detected with mysql_config and manually set by variables"
  164. " is not supported and would confuse our build logic."
  165. )
  166. endif()
  167. if(MYSQL_CONFIG_EXECUTABLE)
  168. set(_mysql_config_set_by_user 1)
  169. else()
  170. # If MYSQL_DIR is set, set MYSQL_CONFIG_EXECUTABLE
  171. if((NOT WIN32)
  172. AND (DEFINED MYSQL_DIR)
  173. AND (EXISTS "${MYSQL_DIR}/bin/mysql_config")
  174. )
  175. set(MYSQL_CONFIG_EXECUTABLE "${MYSQL_DIR}/bin/mysql_config")
  176. set(_mysql_config_in_mysql_dir 1)
  177. endif()
  178. endif()
  179. ##########################################################################
  180. #
  181. # Data and basic settings
  182. #
  183. ##########################################################################
  184. # Set sub directory to search in
  185. # dist = for mysql binary distributions
  186. # build = for custom built tree
  187. if(CMAKE_BUILD_TYPE STREQUAL Debug)
  188. set(_lib_suffix_dist debug)
  189. set(_lib_suffix_build Debug)
  190. else()
  191. set(_lib_suffix_dist opt)
  192. set(_lib_suffix_build Release)
  193. add_definitions(-DNDEBUG) # FIXME what?!
  194. endif()
  195. set(_exe_fallback_path /usr/bin /usr/local/bin /opt/mysql/mysql/bin /usr/local/mysql/bin)
  196. set(_include_fallback_path
  197. /usr/include/mysql
  198. /usr/local/include/mysql
  199. /opt/mysql/mysql/include
  200. /opt/mysql/mysql/include/mysql
  201. /usr/local/mysql/include
  202. /usr/local/mysql/include/mysql
  203. $ENV{ProgramFiles}/MySQL/*/include
  204. $ENV{SystemDrive}/MySQL/*/include
  205. )
  206. set(_lib_fallback_path
  207. /usr/lib/mysql
  208. /usr/local/lib/mysql
  209. /usr/local/mysql/lib
  210. /usr/local/mysql/lib/mysql
  211. /opt/mysql/mysql/lib
  212. /opt/mysql/mysql/lib/mysql
  213. $ENV{ProgramFiles}/MySQL/*/lib/${_lib_suffix_dist}
  214. $ENV{ProgramFiles}/MySQL/*/lib
  215. $ENV{SystemDrive}/MySQL/*/lib/${_lib_suffix_dist}
  216. $ENV{SystemDrive}/MySQL/*/lib
  217. )
  218. set(_lib_subdirs
  219. # Paths in build tree, really being too nice
  220. libmysql/${_lib_suffix_build}
  221. client/${_lib_suffix_build}
  222. libmysql_r/.libs
  223. libmysql/.libs
  224. libmysql
  225. # Install sub directories
  226. lib/mysql
  227. lib/${_lib_suffix_dist} # Need to be before "lib"
  228. lib
  229. )
  230. set(_static_subdirs mysql ${_lib_suffix_dist})
  231. if(MSVC90)
  232. set(_vs_subdir vs9)
  233. elseif(MSVC10)
  234. set(_vs_subdir vs10)
  235. elseif(MSVC11)
  236. set(_vs_subdir vs11)
  237. elseif(MSVC12)
  238. set(_vs_subdir vs12)
  239. elseif(MSVC13)
  240. set(_vs_subdir vs13)
  241. elseif(MSVC14)
  242. set(_vs_subdir vs14)
  243. elseif(MSVC15)
  244. set(_vs_subdir vs15)
  245. endif()
  246. if(_vs_subdir)
  247. if("${_lib_suffix_dist}" STREQUAL "debug")
  248. set(_vs_subdir "${_vs_subdir}/debug")
  249. endif()
  250. list(INSERT _lib_subdirs 0 "lib/${_vs_subdir}")
  251. endif()
  252. # For Windows, the client library name differs, so easy to
  253. # make sure find_library() picks the right one. For Unix, it
  254. # is the file extension that differs. In the static library
  255. # case we know it is ".a", so we add it to the library name
  256. # we search for to make sure it is picked in the static case.
  257. if(WIN32)
  258. set(_dynamic_libs "libmysql")
  259. set(_static_libs "mysqlclient")
  260. set(_static_lib_ext ".lib") # Careful, can be import library for DLL
  261. elseif(MYSQLCLIENT_NO_THREADS)
  262. # In 5.1 and below there is a single threaded library
  263. set(_dynamic_libs "mysqlclient")
  264. set(_static_libs "libmysqlclient.a")
  265. set(_static_lib_ext ".a")
  266. else()
  267. # We try the multithreaded "libmysqlclient_r" first and if not
  268. # there, pick "libmysqlclient" that in 5.5 and up is multithreaded
  269. # anyway (soft link "libmysqlclient_r" is not installed MySQL Server
  270. # 5.6 and Debian/Ubuntu and might go in 5.7 for all installs)
  271. set(_dynamic_libs "mysqlclient_r" "mysqlclient")
  272. set(_static_libs "libmysqlclient_r.a" "libmysqlclient.a")
  273. set(_static_lib_ext ".a")
  274. endif()
  275. if(MYSQLCLIENT_STATIC_LINKING)
  276. set(_link_type "static")
  277. set(_search_libs ${_static_libs})
  278. else()
  279. set(_link_type "dynamic")
  280. set(_search_libs ${_dynamic_libs})
  281. endif()
  282. # Just to pretty print in error messages
  283. string(REPLACE ";" " " _pp_search_libs "${_search_libs}")
  284. string(REPLACE ";" " " _pp_lib_subdirs "${_lib_subdirs}")
  285. string(REPLACE ";" " " _pp_lib_fallback_path "${_lib_fallback_path}")
  286. string(REPLACE ";" " " _pp_include_fallback_path "${_include_fallback_path}")
  287. message(STATUS "You will link ${_link_type}ally to the MySQL client"
  288. " library (set with -DMYSQLCLIENT_STATIC_LINKING=<bool>)"
  289. )
  290. message(STATUS "Searching for ${_link_type} libraries with the base name(s) \"${_pp_search_libs}\"")
  291. ##########################################################################
  292. #
  293. # Macros
  294. #
  295. ##########################################################################
  296. # ----------------------------------------------------------------------
  297. #
  298. # Macro that runs "mysql_config ${_opt}" and return the line after
  299. # trimming away ending space/newline.
  300. #
  301. # _mysql_conf(
  302. # _var - output variable name, will contain a ';' separated list
  303. # _opt - the flag to give to mysql_config
  304. #
  305. # ----------------------------------------------------------------------
  306. macro(_mysql_conf _var _opt)
  307. execute_process(
  308. COMMAND ${MYSQL_CONFIG_EXECUTABLE} ${_opt}
  309. OUTPUT_VARIABLE ${_var}
  310. OUTPUT_STRIP_TRAILING_WHITESPACE
  311. )
  312. endmacro()
  313. # ----------------------------------------------------------------------
  314. #
  315. # Macro that runs "mysql_config ${_opt}", selects output args using a
  316. # regex, and clean it up a bit removing space/tab/newline before
  317. # setting it to a variable.
  318. #
  319. # _mysql_config(
  320. # _var - output variable name, will contain a ';' separated list
  321. # _regex - regular expression matching the prefix of args to select
  322. # _opt - the flag to give to mysql_config
  323. #
  324. # ----------------------------------------------------------------------
  325. macro(_mysql_config _var _regex _opt)
  326. _mysql_conf(_mysql_config_output ${_opt})
  327. string(REGEX MATCHALL "${_regex}([^ ]+)" _mysql_config_output "${_mysql_config_output}")
  328. string(REGEX REPLACE "^[ \t]+" "" _mysql_config_output "${_mysql_config_output}")
  329. if(CMAKE_SYSTEM_NAME MATCHES "SunOS")
  330. string(REGEX REPLACE " -latomic" "" _mysql_config_output "${_mysql_config_output}")
  331. endif()
  332. string(REGEX REPLACE "${_regex}" "" _mysql_config_output "${_mysql_config_output}")
  333. separate_arguments(_mysql_config_output)
  334. set(${_var} ${_mysql_config_output})
  335. endmacro()
  336. # ----------------------------------------------------------------------
  337. #
  338. # Macro that runs "mysql_config ${_opt}" and selects output using a
  339. # prefix regex. Cleans it up a bit removing space/tab/newline. Then
  340. # removes the prefix on all in the list, and finally replace what
  341. # matches another regular expression with a replacement string.
  342. #
  343. # _mysql_config_replace(
  344. # _var - output variable name, will contain a ';' separated list
  345. # _regex1 - regular expression to match out arguments
  346. # _replace - what to replace match _regex1 with
  347. # _regex2 - regular expression matching the prefix of args to select
  348. # _opt - the flag to give to mysql_config
  349. #
  350. # ----------------------------------------------------------------------
  351. macro(_mysql_config_replace _var _regex1 _replace _regex2 _opt)
  352. _mysql_conf(_mysql_config_output ${_opt})
  353. string(REGEX MATCHALL "${_regex2}([^ ]+)" _mysql_config_output "${_mysql_config_output}")
  354. string(REGEX REPLACE "^[ \t]+" "" _mysql_config_output "${_mysql_config_output}")
  355. if(CMAKE_SYSTEM_NAME MATCHES "SunOS")
  356. string(REGEX REPLACE " -latomic" "" _mysql_config_output "${_mysql_config_output}")
  357. endif()
  358. string(REGEX REPLACE "${_regex2}" "" _mysql_config_output "${_mysql_config_output}")
  359. string(REGEX REPLACE "${_regex1}" "${_replace}" _mysql_config_output "${_mysql_config_output}")
  360. separate_arguments(_mysql_config_output)
  361. set(${_var} ${_mysql_config_output})
  362. endmacro()
  363. # ----------------------------------------------------------------------
  364. #
  365. # Macro to check that we found a library and that we got the right type
  366. #
  367. # ----------------------------------------------------------------------
  368. macro(_check_lib_search_error _lib_dir_var _lib_var _exta_err_string)
  369. set(_lib "${${_lib_var}}")
  370. set(_lib_dir "${${_lib_dir_var}}")
  371. if(FINDMYSQL_DEBUG)
  372. message("_lib \"${_lib}\"")
  373. message("_lib_dir \"${_lib_dir}\"")
  374. message("_lib_var \"${_lib_var}\"")
  375. message("_lib_dir_var \"${_lib_dir_var}\"")
  376. endif()
  377. set(_err_string "Could not find ${_link_type} " "\"${_pp_search_libs}\" in ${_lib_dir_var} "
  378. "\"${_lib_dir}\" ${_exta_err_string}"
  379. )
  380. if(NOT ${_lib_var})
  381. message(FATAL_ERROR ${_err_string})
  382. endif()
  383. # find_library() try find a shared library first, then a static
  384. # one. For Windows the library has a different name, but for
  385. # Unix only the extension differs. So we check here that we
  386. # got the library kind we expected.
  387. if(NOT WIN32)
  388. if(NOT MYSQLCLIENT_STATIC_LINKING)
  389. get_filename_component(_ext ${_lib} EXT)
  390. if(${_ext} STREQUAL ${_static_lib_ext})
  391. message(FATAL_ERROR ${_err_string})
  392. endif()
  393. endif()
  394. endif()
  395. endmacro()
  396. ##########################################################################
  397. #
  398. # Try find MYSQL_CONFIG_EXECUTABLE if not set, and find version
  399. #
  400. ##########################################################################
  401. if(NOT WIN32)
  402. if(NOT MYSQL_CONFIG_EXECUTABLE)
  403. find_program(
  404. MYSQL_CONFIG_EXECUTABLE
  405. NAMES mysql_config
  406. DOC "full path of mysql_config"
  407. PATHS ${_exe_fallback_path}
  408. )
  409. endif()
  410. if(MYSQL_CONFIG_EXECUTABLE)
  411. message(STATUS "mysql_config was found ${MYSQL_CONFIG_EXECUTABLE}")
  412. _mysql_conf(MYSQL_VERSION "--version")
  413. endif()
  414. endif()
  415. ##########################################################################
  416. #
  417. # Find MYSQL_INCLUDE_DIR
  418. #
  419. ##########################################################################
  420. if(FINDMYSQL_DEBUG AND MYSQL_INCLUDE_DIR)
  421. message("DBG: User gave MYSQL_INCLUDE_DIR = \"${MYSQL_INCLUDE_DIR}\"")
  422. endif()
  423. if(FINDMYSQL_DEBUG AND MYSQL_DIR)
  424. message("DBG: User gave MYSQL_DIR = \"${MYSQL_DIR}\"")
  425. endif()
  426. if(MYSQL_INCLUDE_DIR)
  427. if(FINDMYSQL_DEBUG)
  428. message("DBG: Using MYSQL_INCLUDE_DIR to find \"mysql.h\"")
  429. endif()
  430. if(NOT EXISTS "${MYSQL_INCLUDE_DIR}/mysql.h")
  431. message(FATAL_ERROR "MYSQL_INCLUDE_DIR given, but no \"mysql.h\" "
  432. "in \"${MYSQL_INCLUDE_DIR}\""
  433. )
  434. endif()
  435. elseif(
  436. MYSQL_DIR
  437. AND (NOT _mysql_config_in_mysql_dir)
  438. AND (NOT _mysql_config_set_by_user)
  439. )
  440. if(FINDMYSQL_DEBUG)
  441. message("DBG: Using MYSQL_DIR without \"mysql_config\" to find \"mysql.h\"")
  442. endif()
  443. set(MYSQL_INCLUDE_DIR "${MYSQL_DIR}/include")
  444. if(NOT EXISTS "${MYSQL_INCLUDE_DIR}/mysql.h")
  445. message(FATAL_ERROR "MYSQL_DIR given, but no \"mysql.h\" " "in \"${MYSQL_INCLUDE_DIR}\"")
  446. endif()
  447. elseif(MYSQL_CONFIG_EXECUTABLE)
  448. if(FINDMYSQL_DEBUG)
  449. message("DBG: Using \"mysql_config\" to find \"mysql.h\"")
  450. endif()
  451. # This code assumes there is just one "-I...." and that
  452. # no space between "-I" and the path
  453. _mysql_config(MYSQL_INCLUDE_DIR "(^| )-I" "--include")
  454. if(NOT MYSQL_INCLUDE_DIR)
  455. message(FATAL_ERROR "Could not find the include dir from running "
  456. "\"${MYSQL_CONFIG_EXECUTABLE}\""
  457. )
  458. endif()
  459. if(NOT EXISTS "${MYSQL_INCLUDE_DIR}/mysql.h")
  460. message(FATAL_ERROR "Could not find \"mysql.h\" in \"${MYSQL_INCLUDE_DIR}\" "
  461. "found from running \"${MYSQL_CONFIG_EXECUTABLE}\""
  462. )
  463. endif()
  464. else()
  465. if(FINDMYSQL_DEBUG)
  466. message("DBG: Using find_path() searching "
  467. "\"${_pp_include_fallback_path}\" to find \"mysql.h\""
  468. )
  469. endif()
  470. # No specific paths, try some common install paths
  471. find_path(MYSQL_INCLUDE_DIR mysql.h ${_include_fallback_path})
  472. if(NOT MYSQL_INCLUDE_DIR)
  473. message(FATAL_ERROR "Could not find \"mysql.h\" from searching "
  474. "\"${_pp_include_fallback_path}\""
  475. )
  476. endif()
  477. endif()
  478. if(FINDMYSQL_DEBUG)
  479. message("DBG: MYSQL_INCLUDE_DIR = \"${MYSQL_INCLUDE_DIR}\"")
  480. endif()
  481. ##########################################################################
  482. #
  483. # Find MYSQL_LIB_DIR, MYSQL_LIB, MYSQL_PLUGIN_DIR and MYSQL_LIBRARIES
  484. #
  485. ##########################################################################
  486. if(FINDMYSQL_DEBUG AND MYSQL_LIB_DIR)
  487. message("DBG: User gave MYSQL_LIB_DIR = \"${MYSQL_LIB_DIR}\"")
  488. endif()
  489. if(MYSQL_LIB_DIR)
  490. if(FINDMYSQL_DEBUG)
  491. message("DBG: Using find_library() searching MYSQL_LIB_DIR")
  492. endif()
  493. find_library(
  494. MYSQL_LIB
  495. NAMES ${_search_libs}
  496. PATHS "${MYSQL_LIB_DIR}"
  497. NO_DEFAULT_PATH
  498. )
  499. _check_lib_search_error(MYSQL_LIB_DIR MYSQL_LIB "")
  500. set(MYSQL_LIBRARIES ${MYSQL_LIB})
  501. if(NOT DEFINED MYSQL_PLUGIN_DIR)
  502. set(MYSQL_PLUGIN_DIR "${MYSQL_LIB_DIR}/plugin")
  503. endif()
  504. elseif(
  505. MYSQL_DIR
  506. AND (NOT _mysql_config_in_mysql_dir)
  507. AND (NOT _mysql_config_set_by_user)
  508. )
  509. if(FINDMYSQL_DEBUG)
  510. message("DBG: Using find_library() searching " "MYSQL_DIR and \"${_pp_lib_subdirs}\"")
  511. endif()
  512. find_library(
  513. MYSQL_LIB
  514. NAMES ${_search_libs}
  515. PATHS "${MYSQL_DIR}"
  516. PATH_SUFFIXES ${_lib_subdirs}
  517. NO_DEFAULT_PATH
  518. )
  519. _check_lib_search_error(MYSQL_DIR MYSQL_LIB "in \"${_pp_lib_subdirs}\"")
  520. get_filename_component(MYSQL_LIB_DIR "${MYSQL_LIB}" PATH)
  521. set(MYSQL_LIBRARIES "${MYSQL_LIB}")
  522. if(((NOT DEFINED MYSQL_PLUGIN_DIR) OR (NOT ${MYSQL_PLUGIN_DIR})) AND MYSQL_LIB_DIR)
  523. if(EXISTS "${MYSQL_LIB_DIR}/plugin")
  524. set(MYSQL_PLUGIN_DIR "${MYSQL_LIB_DIR}/plugin")
  525. else()
  526. # FIXME: If directory does not exist it must be a debug dir layout
  527. if(EXISTS "${MYSQL_LIB_DIR}/../plugin/")
  528. set(MYSQL_PLUGIN_DIR "${MYSQL_LIB_DIR}/../plugin")
  529. endif()
  530. endif()
  531. endif()
  532. elseif(MYSQL_CONFIG_EXECUTABLE)
  533. if(FINDMYSQL_DEBUG)
  534. message("DBG: Using \"mysql_config\" to find the libraries")
  535. endif()
  536. # This code assumes there is just one "-L...." and that
  537. # no space between "-L" and the path
  538. _mysql_config(MYSQL_LIB_DIR "(^| )-L" "--libs")
  539. # Note: Allow overriding plugin dir reported by mysql_config because
  540. # that can be wrong sometimes
  541. if(NOT MYSQL_PLUGIN_DIR)
  542. _mysql_conf(MYSQL_PLUGIN_DIR "--variable=plugindir")
  543. endif()
  544. if(CMAKE_SYSTEM_NAME MATCHES "SunOS")
  545. # This is needed to make Solaris binaries using the default runtime lib path
  546. _mysql_config(DEV_STUDIO_RUNTIME_DIR "(^| )-R" "--libs")
  547. endif()
  548. list(LENGTH MYSQL_LIB_DIR dir_cnt)
  549. message(STATUS "Libraries paths found: ${n}")
  550. if(${dir_cnt} GREATER 1)
  551. set(MYSQL_LIB_DIR_LIST ${MYSQL_LIB_DIR})
  552. message(STATUS "MYSQL_LIB_DIR_LIST = ${MYSQL_LIB_DIR_LIST}")
  553. foreach(_path_to_check IN LISTS MYSQL_LIB_DIR)
  554. find_library(
  555. _mysql_client_lib_var
  556. NAMES ${_search_libs}
  557. PATHS ${_path_to_check}
  558. NO_DEFAULT_PATH
  559. )
  560. if(_mysql_client_lib_var)
  561. message(STATUS "CLIENT LIB VAR: ${_mysql_client_lib_var}")
  562. unset(_mysql_client_lib_var CACHE)
  563. set(MYSQL_LIB_DIR ${_path_to_check})
  564. endif()
  565. endforeach(_path_to_check)
  566. endif()
  567. if(NOT MYSQL_LIB_DIR)
  568. message(FATAL_ERROR "Could not find the library dir from running "
  569. "\"${MYSQL_CONFIG_EXECUTABLE}\""
  570. )
  571. endif()
  572. if(NOT EXISTS "${MYSQL_LIB_DIR}")
  573. message(FATAL_ERROR "Could not find the directory \"${MYSQL_LIB_DIR}\" "
  574. "found from running \"${MYSQL_CONFIG_EXECUTABLE}\""
  575. )
  576. endif()
  577. # We have the assumed MYSQL_LIB_DIR. The output from "mysql_config"
  578. # might not be correct for static libraries, so we might need to
  579. # adjust MYSQL_LIB_DIR later on.
  580. if(MYSQLCLIENT_STATIC_LINKING)
  581. # Find the static library, might be one level down
  582. find_library(
  583. MYSQL_LIB
  584. NAMES ${_search_libs}
  585. PATHS ${MYSQL_LIB_DIR}
  586. PATH_SUFFIXES ${_static_subdirs}
  587. NO_DEFAULT_PATH
  588. )
  589. _check_lib_search_error(MYSQL_LIB_DIR MYSQL_LIB "in \"${_static_subdirs}\"")
  590. # Adjust MYSQL_LIB_DIR in case it changes
  591. get_filename_component(MYSQL_LIB_DIR "${MYSQL_LIB}" PATH)
  592. # Replace the current library references with the full path
  593. # to the library, i.e. the -L will be ignored
  594. _mysql_config_replace(
  595. MYSQL_LIBRARIES "(mysqlclient|mysqlclient_r)" "${MYSQL_LIB}" "(^| )-l" "--libs"
  596. )
  597. else()
  598. _mysql_config(MYSQL_LIBRARIES "(^| )-l" "--libs")
  599. foreach(__lib IN LISTS MYSQL_LIBRARIES)
  600. string(REGEX MATCH "mysqlclient([^ ]*)" _matched_lib __lib)
  601. if(_matched_lib)
  602. set(_search_libs ${matched_lib})
  603. endif()
  604. endforeach()
  605. # First library is assumed to be the client library
  606. # list(GET MYSQL_LIBRARIES 0 _search_libs)
  607. find_library(
  608. MYSQL_LIB
  609. NAMES ${_search_libs}
  610. PATHS ${MYSQL_LIB_DIR}
  611. NO_DEFAULT_PATH
  612. )
  613. _check_lib_search_error(MYSQL_LIB_DIR MYSQL_LIB "")
  614. endif()
  615. else()
  616. if(FINDMYSQL_DEBUG)
  617. message("DBG: Using find_library() searching "
  618. "\"${_pp_lib_fallback_path}\" to find the client library"
  619. )
  620. endif()
  621. # Search standard places
  622. find_library(
  623. MYSQL_LIB
  624. NAMES ${_search_libs}
  625. PATHS ${_lib_fallback_path}
  626. )
  627. if(NOT MYSQL_LIB)
  628. message(FATAL_ERROR "Could not find \"${_pp_search_libs}\" from searching "
  629. "\"${_pp_lib_fallback_path}\""
  630. )
  631. endif()
  632. get_filename_component(MYSQL_LIB_DIR "${MYSQL_LIB}" PATH)
  633. endif()
  634. ##########################################################################
  635. #
  636. # Add more libraries to MYSQL_LIBRARIES
  637. #
  638. ##########################################################################
  639. # FIXME needed?!
  640. if(MYSQLCLIENT_STATIC_LINKING
  641. AND NOT WIN32
  642. AND NOT ${CMAKE_SYSTEM_NAME} MATCHES "Darwin"
  643. )
  644. list(APPEND MYSQL_LIBRARIES "rt")
  645. endif()
  646. # The built-in sys and strings are used for static and dynamic linking.
  647. list(APPEND SYS_LIBRARIES "mysql_sys")
  648. list(APPEND SYS_LIBRARIES "mysql_strings")
  649. list(APPEND SYS_LIBRARIES ${MYSQL_LIBRARIES})
  650. set(MYSQL_LIBRARIES ${SYS_LIBRARIES})
  651. if(MYSQL_EXTRA_LIBRARIES)
  652. separate_arguments(MYSQL_EXTRA_LIBRARIES)
  653. list(APPEND MYSQL_LIBRARIES ${MYSQL_EXTRA_LIBRARIES})
  654. endif()
  655. list(LENGTH MYSQL_LIBRARIES len)
  656. if(MYSQL_STATIC_LINKING AND (len LESS 2))
  657. message(
  658. WARNING "Statically linking MySQL client library normally requires linking"
  659. " additional libraries that the client library depends on. It seems"
  660. " no extra libraries have been specified. Provide the list of required"
  661. " dependencies through MYSQL_EXTRA_LIBRARIES."
  662. )
  663. endif()
  664. # For compatibility
  665. set(MYSQL_CLIENT_LIBS ${MYSQL_LIBRARIES})
  666. ##########################################################################
  667. #
  668. # If not found MySQL Serverv version, compile a small client app
  669. # and let it write a small cmake file with the settings
  670. #
  671. ##########################################################################
  672. if(MYSQL_INCLUDE_DIR AND NOT MYSQL_VERSION)
  673. # Write the C source file that will include the MySQL headers
  674. set(GETMYSQLVERSION_SOURCEFILE "${CMAKE_CURRENT_BINARY_DIR}/getmysqlversion.c")
  675. file(WRITE "${GETMYSQLVERSION_SOURCEFILE}"
  676. "#include <mysql.h>\n" "#include <stdio.h>\n" "int main() {\n"
  677. " printf(\"%s\", MYSQL_SERVER_VERSION);\n" "}\n"
  678. )
  679. # Compile and run the created executable, store output in MYSQL_VERSION
  680. try_run(
  681. _run_result _compile_result "${CMAKE_BINARY_DIR}" "${GETMYSQLVERSION_SOURCEFILE}"
  682. CMAKE_FLAGS "-DINCLUDE_DIRECTORIES:STRING=${MYSQL_INCLUDE_DIR}"
  683. RUN_OUTPUT_VARIABLE MYSQL_VERSION
  684. )
  685. if(FINDMYSQL_DEBUG)
  686. if(NOT _compile_result)
  687. message("DBG: Could not compile \"getmysqlversion.c\"")
  688. endif()
  689. if(_run_result)
  690. message("DBG: Running \"getmysqlversion\" returned ${_run_result}")
  691. endif()
  692. endif()
  693. endif()
  694. ##########################################################################
  695. #
  696. # Clean up MYSQL_VERSION and create MYSQL_VERSION_ID/MYSQL_NUM_VERSION
  697. #
  698. ##########################################################################
  699. if(NOT MYSQL_VERSION)
  700. message(FATAL_ERROR "Could not determine the MySQL Server version")
  701. endif()
  702. # Clean up so only numeric, in case of "-alpha" or similar
  703. string(REGEX MATCHALL "([0-9]+.[0-9]+.[0-9]+)" MYSQL_VERSION "${MYSQL_VERSION}")
  704. # To create a fully numeric version, first normalize so N.NN.NN
  705. string(REGEX REPLACE "[.]([0-9])[.]" ".0\\1." MYSQL_VERSION_ID "${MYSQL_VERSION}")
  706. string(REGEX REPLACE "[.]([0-9])$" ".0\\1" MYSQL_VERSION_ID "${MYSQL_VERSION_ID}")
  707. # Finally remove the dot
  708. string(REGEX REPLACE "[.]" "" MYSQL_VERSION_ID "${MYSQL_VERSION_ID}")
  709. set(MYSQL_NUM_VERSION ${MYSQL_VERSION_ID})
  710. ##########################################################################
  711. #
  712. # Try determine if to use C++ linkage, and also find C++ flags
  713. #
  714. ##########################################################################
  715. if(NOT WIN32)
  716. if(MYSQL_CONFIG_EXECUTABLE)
  717. if(NOT MYSQL_CFLAGS)
  718. _mysql_conf(MYSQL_CFLAGS "--cflags")
  719. endif()
  720. if(NOT MYSQL_CXXFLAGS)
  721. if(MYSQL_CXX_LINKAGE OR MYSQL_VERSION_ID GREATER 50603)
  722. _mysql_conf(MYSQL_CXXFLAGS "--cxxflags")
  723. set(MYSQL_CXX_LINKAGE 1)
  724. else()
  725. set(MYSQL_CXXFLAGS "${MYSQL_CFLAGS}")
  726. endif()
  727. endif()
  728. # FIXME this should not be needed, caller of this module should set
  729. # it's own flags and just use the library on it's on terms
  730. # (change the infe message if enabling this code)
  731. # if(NOT MYSQL_LINK_FLAGS)
  732. # # Find -mcpu -march -mt -m32 -m64 and other flags starting with "-m"
  733. # string(REGEX MATCHALL "(^| )-m([^\r\n ]+)" MYSQL_LINK_FLAGS "${MYSQL_CXXFLAGS}")
  734. # string(REGEX REPLACE "^ " "" MYSQL_LINK_FLAGS "${MYSQL_LINK_FLAGS}")
  735. # string(REGEX REPLACE "; " ";" MYSQL_LINK_FLAGS "${MYSQL_LINK_FLAGS}")
  736. # endif()
  737. endif()
  738. endif()
  739. ##########################################################################
  740. #
  741. # Inform CMake where to look for headers and libraries
  742. #
  743. ##########################################################################
  744. # string(TOUPPER "${CMAKE_BUILD_TYPE}" CMAKEBT)
  745. # set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${MYSQL_CXXFLAGS}")
  746. # set(CMAKE_CXX_FLAGS_${CMAKEBT} "${CMAKE_CXX_FLAGS_${CMAKEBT}} ${MYSQL_CXXFLAGS}")
  747. include_directories("${MYSQL_INCLUDE_DIR}")
  748. link_directories("${MYSQL_LIB_DIR}")
  749. message(STATUS "MYSQL_LIB_DIR_LIST = ${MYSQL_LIB_DIR_LIST}")
  750. if(MYSQL_LIB_DIR_LIST)
  751. foreach(__libpath IN LISTS MYSQL_LIB_DIR_LIST)
  752. link_directories("${__libpath}")
  753. endforeach()
  754. endif()
  755. ##########################################################################
  756. #
  757. # Report
  758. #
  759. ##########################################################################
  760. message(STATUS "MySQL client environment/cmake variables set that the user can override")
  761. message(STATUS " MYSQL_DIR : ${MYSQL_DIR}")
  762. message(STATUS " MYSQL_INCLUDE_DIR : ${MYSQL_INCLUDE_DIR}")
  763. message(STATUS " MYSQL_LIB_DIR : ${MYSQL_LIB_DIR}")
  764. message(STATUS " MYSQL_PLUGIN_DIR : ${MYSQL_PLUGIN_DIR}")
  765. message(STATUS " MYSQL_CONFIG_EXECUTABLE : ${MYSQL_CONFIG_EXECUTABLE}")
  766. message(STATUS " MYSQL_CXX_LINKAGE : ${MYSQL_CXX_LINKAGE}")
  767. message(STATUS " MYSQL_CFLAGS : ${MYSQL_CFLAGS}")
  768. message(STATUS " MYSQL_CXXFLAGS : ${MYSQL_CXXFLAGS}")
  769. message(STATUS " MYSQLCLIENT_STATIC_LINKING : ${MYSQLCLIENT_STATIC_LINKING}")
  770. message(STATUS " MYSQLCLIENT_NO_THREADS : ${MYSQLCLIENT_NO_THREADS}")
  771. message(STATUS "MySQL client optional environment/cmake variables set by the user")
  772. message(STATUS " MYSQL_EXTRA_LIBRARIES : ${MYSQL_EXTRA_LIBRARIES}")
  773. message(STATUS " MYSQL_LINK_FLAGS : ${MYSQL_LINK_FLAGS}")
  774. message(STATUS "MySQL client settings that the user can't override")
  775. message(STATUS " MYSQL_VERSION : ${MYSQL_VERSION}")
  776. message(STATUS " MYSQL_VERSION_ID : ${MYSQL_VERSION_ID}")
  777. message(STATUS " MYSQL_LIB : ${MYSQL_LIB}")
  778. message(STATUS " MYSQL_LIBRARIES : ${MYSQL_LIBRARIES}")