FindMySQL.cmake 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964
  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
  128. MYSQL_PLUGIN_DIR
  129. )
  130. foreach(_xvar ${ENV_OR_OPT_VARS})
  131. if((DEFINED ${_xvar})
  132. AND (DEFINED ENV{${_xvar}})
  133. AND (NOT "${${_xvar}}" STREQUAL "")
  134. AND (NOT "$ENV{${_xvar}}" STREQUAL "")
  135. AND (NOT "$ENV{${_xvar}}" STREQUAL "${${_xvar}}")
  136. )
  137. message(FATAL_ERROR "Please pass -D${_xvar}=... as an argument or "
  138. "set ${_xvar} in the environment, but not both"
  139. )
  140. endif()
  141. # Now we know both are not set, set the CMake variable if needed
  142. if((DEFINED ENV{${_xvar}}) AND (NOT "$ENV{${_xvar}}" STREQUAL ""))
  143. set(${_xvar} $ENV{${_xvar}})
  144. endif()
  145. # Notmalize the path if the variable is set and is a path
  146. if(${_xvar})
  147. list(FIND ENV_OR_OPT_PATH_VARS ${_xvar} _index)
  148. if(${_index} GREATER -1)
  149. file(TO_CMAKE_PATH "${${_xvar}}" ${_xvar})
  150. get_filename_component(${_xvar} ${${_xvar}} ABSOLUTE)
  151. endif()
  152. endif()
  153. endforeach()
  154. # Bail out if both MYSQL_DIR/MYSQL_CONFIG_EXECUTABLE and MYSQL_INCLUDE/LIB_DIR
  155. # were given
  156. if(MYSQL_DIR AND (MYSQL_INCLUDE_DIR OR MYSQL_LIB_DIR))
  157. message(
  158. FATAL_ERROR
  159. "Both MYSQL_DIR and MYSQL_INCLUDE_DIR/MYSQL_LIB_DIR were specified,"
  160. " use either one or the other way of pointing at MySQL location."
  161. )
  162. endif()
  163. if(MYSQL_CONFIG_EXECUTABLE AND (MYSQL_INCLUDE_DIR OR MYSQL_LIB_DIR))
  164. message(
  165. FATAL_ERROR
  166. "Both MYSQL_CONFIG_EXECUTABLE and MYSQL_INCLUDE_DIR/MYSQL_LIB_DIR were specified,"
  167. " mixing settings detected with mysql_config and manually set by variables"
  168. " is not supported and would confuse our build logic."
  169. )
  170. endif()
  171. if(MYSQL_CONFIG_EXECUTABLE)
  172. set(_mysql_config_set_by_user 1)
  173. else()
  174. # If MYSQL_DIR is set, set MYSQL_CONFIG_EXECUTABLE
  175. if((NOT WIN32)
  176. AND (DEFINED MYSQL_DIR)
  177. AND (EXISTS "${MYSQL_DIR}/bin/mysql_config")
  178. )
  179. set(MYSQL_CONFIG_EXECUTABLE "${MYSQL_DIR}/bin/mysql_config")
  180. set(_mysql_config_in_mysql_dir 1)
  181. endif()
  182. endif()
  183. ##########################################################################
  184. #
  185. # Data and basic settings
  186. #
  187. ##########################################################################
  188. # Set sub directory to search in
  189. # dist = for mysql binary distributions
  190. # build = for custom built tree
  191. if(CMAKE_BUILD_TYPE STREQUAL Debug)
  192. set(_lib_suffix_dist debug)
  193. set(_lib_suffix_build Debug)
  194. else()
  195. set(_lib_suffix_dist opt)
  196. set(_lib_suffix_build Release)
  197. add_definitions(-DNDEBUG) # FIXME what?!
  198. endif()
  199. set(_exe_fallback_path /usr/bin /usr/local/bin /opt/mysql/mysql/bin
  200. /usr/local/mysql/bin
  201. )
  202. set(_include_fallback_path
  203. /usr/include/mysql
  204. /usr/local/include/mysql
  205. /opt/mysql/mysql/include
  206. /opt/mysql/mysql/include/mysql
  207. /usr/local/mysql/include
  208. /usr/local/mysql/include/mysql
  209. $ENV{ProgramFiles}/MySQL/*/include
  210. $ENV{SystemDrive}/MySQL/*/include
  211. )
  212. set(_lib_fallback_path
  213. /usr/lib/mysql
  214. /usr/local/lib/mysql
  215. /usr/local/mysql/lib
  216. /usr/local/mysql/lib/mysql
  217. /opt/mysql/mysql/lib
  218. /opt/mysql/mysql/lib/mysql
  219. $ENV{ProgramFiles}/MySQL/*/lib/${_lib_suffix_dist}
  220. $ENV{ProgramFiles}/MySQL/*/lib
  221. $ENV{SystemDrive}/MySQL/*/lib/${_lib_suffix_dist}
  222. $ENV{SystemDrive}/MySQL/*/lib
  223. )
  224. set(_lib_subdirs
  225. # Paths in build tree, really being too nice
  226. libmysql/${_lib_suffix_build}
  227. client/${_lib_suffix_build}
  228. libmysql_r/.libs
  229. libmysql/.libs
  230. libmysql
  231. # Install sub directories
  232. lib/mysql
  233. lib/${_lib_suffix_dist} # Need to be before "lib"
  234. lib
  235. )
  236. set(_static_subdirs mysql ${_lib_suffix_dist})
  237. if(MSVC90)
  238. set(_vs_subdir vs9)
  239. elseif(MSVC10)
  240. set(_vs_subdir vs10)
  241. elseif(MSVC11)
  242. set(_vs_subdir vs11)
  243. elseif(MSVC12)
  244. set(_vs_subdir vs12)
  245. elseif(MSVC13)
  246. set(_vs_subdir vs13)
  247. elseif(MSVC14)
  248. set(_vs_subdir vs14)
  249. elseif(MSVC15)
  250. set(_vs_subdir vs15)
  251. endif()
  252. if(_vs_subdir)
  253. if("${_lib_suffix_dist}" STREQUAL "debug")
  254. set(_vs_subdir "${_vs_subdir}/debug")
  255. endif()
  256. list(INSERT _lib_subdirs 0 "lib/${_vs_subdir}")
  257. endif()
  258. # For Windows, the client library name differs, so easy to
  259. # make sure find_library() picks the right one. For Unix, it
  260. # is the file extension that differs. In the static library
  261. # case we know it is ".a", so we add it to the library name
  262. # we search for to make sure it is picked in the static case.
  263. if(WIN32)
  264. set(_dynamic_libs "libmysql")
  265. set(_static_libs "mysqlclient")
  266. set(_static_lib_ext ".lib") # Careful, can be import library for DLL
  267. elseif(MYSQLCLIENT_NO_THREADS)
  268. # In 5.1 and below there is a single threaded library
  269. set(_dynamic_libs "mysqlclient")
  270. set(_static_libs "libmysqlclient.a")
  271. set(_static_lib_ext ".a")
  272. else()
  273. # We try the multithreaded "libmysqlclient_r" first and if not
  274. # there, pick "libmysqlclient" that in 5.5 and up is multithreaded
  275. # anyway (soft link "libmysqlclient_r" is not installed MySQL Server
  276. # 5.6 and Debian/Ubuntu and might go in 5.7 for all installs)
  277. set(_dynamic_libs "mysqlclient_r" "mysqlclient")
  278. set(_static_libs "libmysqlclient_r.a" "libmysqlclient.a")
  279. set(_static_lib_ext ".a")
  280. endif()
  281. if(MYSQLCLIENT_STATIC_LINKING)
  282. set(_link_type "static")
  283. set(_search_libs ${_static_libs})
  284. else()
  285. set(_link_type "dynamic")
  286. set(_search_libs ${_dynamic_libs})
  287. endif()
  288. # Just to pretty print in error messages
  289. string(REPLACE ";" " " _pp_search_libs "${_search_libs}")
  290. string(REPLACE ";" " " _pp_lib_subdirs "${_lib_subdirs}")
  291. string(REPLACE ";" " " _pp_lib_fallback_path "${_lib_fallback_path}")
  292. string(REPLACE ";" " " _pp_include_fallback_path "${_include_fallback_path}")
  293. message(STATUS "You will link ${_link_type}ally to the MySQL client"
  294. " library (set with -DMYSQLCLIENT_STATIC_LINKING=<bool>)"
  295. )
  296. message(
  297. STATUS
  298. "Searching for ${_link_type} libraries with the base name(s) \"${_pp_search_libs}\""
  299. )
  300. ##########################################################################
  301. #
  302. # Macros
  303. #
  304. ##########################################################################
  305. # ----------------------------------------------------------------------
  306. #
  307. # Macro that runs "mysql_config ${_opt}" and return the line after
  308. # trimming away ending space/newline.
  309. #
  310. # _mysql_conf(
  311. # _var - output variable name, will contain a ';' separated list
  312. # _opt - the flag to give to mysql_config
  313. #
  314. # ----------------------------------------------------------------------
  315. macro(_mysql_conf _var _opt)
  316. execute_process(
  317. COMMAND ${MYSQL_CONFIG_EXECUTABLE} ${_opt}
  318. OUTPUT_VARIABLE ${_var}
  319. OUTPUT_STRIP_TRAILING_WHITESPACE
  320. )
  321. endmacro()
  322. # ----------------------------------------------------------------------
  323. #
  324. # Macro that runs "mysql_config ${_opt}", selects output args using a
  325. # regex, and clean it up a bit removing space/tab/newline before
  326. # setting it to a variable.
  327. #
  328. # _mysql_config(
  329. # _var - output variable name, will contain a ';' separated list
  330. # _regex - regular expression matching the prefix of args to select
  331. # _opt - the flag to give to mysql_config
  332. #
  333. # ----------------------------------------------------------------------
  334. macro(_mysql_config _var _regex _opt)
  335. _mysql_conf(_mysql_config_output ${_opt})
  336. string(REGEX MATCHALL "${_regex}([^ ]+)" _mysql_config_output
  337. "${_mysql_config_output}"
  338. )
  339. string(REGEX REPLACE "^[ \t]+" "" _mysql_config_output
  340. "${_mysql_config_output}"
  341. )
  342. if(CMAKE_SYSTEM_NAME MATCHES "SunOS")
  343. string(REGEX REPLACE " -latomic" "" _mysql_config_output
  344. "${_mysql_config_output}"
  345. )
  346. endif()
  347. string(REGEX REPLACE "${_regex}" "" _mysql_config_output
  348. "${_mysql_config_output}"
  349. )
  350. separate_arguments(_mysql_config_output)
  351. set(${_var} ${_mysql_config_output})
  352. endmacro()
  353. # ----------------------------------------------------------------------
  354. #
  355. # Macro that runs "mysql_config ${_opt}" and selects output using a
  356. # prefix regex. Cleans it up a bit removing space/tab/newline. Then
  357. # removes the prefix on all in the list, and finally replace what
  358. # matches another regular expression with a replacement string.
  359. #
  360. # _mysql_config_replace(
  361. # _var - output variable name, will contain a ';' separated list
  362. # _regex1 - regular expression to match out arguments
  363. # _replace - what to replace match _regex1 with
  364. # _regex2 - regular expression matching the prefix of args to select
  365. # _opt - the flag to give to mysql_config
  366. #
  367. # ----------------------------------------------------------------------
  368. macro(_mysql_config_replace _var _regex1 _replace _regex2 _opt)
  369. _mysql_conf(_mysql_config_output ${_opt})
  370. string(REGEX MATCHALL "${_regex2}([^ ]+)" _mysql_config_output
  371. "${_mysql_config_output}"
  372. )
  373. string(REGEX REPLACE "^[ \t]+" "" _mysql_config_output
  374. "${_mysql_config_output}"
  375. )
  376. if(CMAKE_SYSTEM_NAME MATCHES "SunOS")
  377. string(REGEX REPLACE " -latomic" "" _mysql_config_output
  378. "${_mysql_config_output}"
  379. )
  380. endif()
  381. string(REGEX REPLACE "${_regex2}" "" _mysql_config_output
  382. "${_mysql_config_output}"
  383. )
  384. string(REGEX REPLACE "${_regex1}" "${_replace}" _mysql_config_output
  385. "${_mysql_config_output}"
  386. )
  387. separate_arguments(_mysql_config_output)
  388. set(${_var} ${_mysql_config_output})
  389. endmacro()
  390. # ----------------------------------------------------------------------
  391. #
  392. # Macro to check that we found a library and that we got the right type
  393. #
  394. # ----------------------------------------------------------------------
  395. macro(_check_lib_search_error _lib_dir_var _lib_var _exta_err_string)
  396. set(_lib "${${_lib_var}}")
  397. set(_lib_dir "${${_lib_dir_var}}")
  398. if(FINDMYSQL_DEBUG)
  399. message("_lib \"${_lib}\"")
  400. message("_lib_dir \"${_lib_dir}\"")
  401. message("_lib_var \"${_lib_var}\"")
  402. message("_lib_dir_var \"${_lib_dir_var}\"")
  403. endif()
  404. set(_err_string
  405. "Could not find ${_link_type} "
  406. "\"${_pp_search_libs}\" in ${_lib_dir_var} "
  407. "\"${_lib_dir}\" ${_exta_err_string}"
  408. )
  409. if(NOT ${_lib_var})
  410. message(FATAL_ERROR ${_err_string})
  411. endif()
  412. # find_library() try find a shared library first, then a static
  413. # one. For Windows the library has a different name, but for
  414. # Unix only the extension differs. So we check here that we
  415. # got the library kind we expected.
  416. if(NOT WIN32)
  417. if(NOT MYSQLCLIENT_STATIC_LINKING)
  418. get_filename_component(_ext ${_lib} EXT)
  419. if(${_ext} STREQUAL ${_static_lib_ext})
  420. message(FATAL_ERROR ${_err_string})
  421. endif()
  422. endif()
  423. endif()
  424. endmacro()
  425. ##########################################################################
  426. #
  427. # Try find MYSQL_CONFIG_EXECUTABLE if not set, and find version
  428. #
  429. ##########################################################################
  430. if(NOT WIN32)
  431. if(NOT MYSQL_CONFIG_EXECUTABLE)
  432. find_program(
  433. MYSQL_CONFIG_EXECUTABLE
  434. NAMES mysql_config
  435. DOC "full path of mysql_config"
  436. PATHS ${_exe_fallback_path}
  437. )
  438. endif()
  439. if(MYSQL_CONFIG_EXECUTABLE)
  440. message(STATUS "mysql_config was found ${MYSQL_CONFIG_EXECUTABLE}")
  441. _mysql_conf(MYSQL_VERSION "--version")
  442. endif()
  443. endif()
  444. ##########################################################################
  445. #
  446. # Find MYSQL_INCLUDE_DIR
  447. #
  448. ##########################################################################
  449. if(FINDMYSQL_DEBUG AND MYSQL_INCLUDE_DIR)
  450. message("DBG: User gave MYSQL_INCLUDE_DIR = \"${MYSQL_INCLUDE_DIR}\"")
  451. endif()
  452. if(FINDMYSQL_DEBUG AND MYSQL_DIR)
  453. message("DBG: User gave MYSQL_DIR = \"${MYSQL_DIR}\"")
  454. endif()
  455. if(MYSQL_INCLUDE_DIR)
  456. if(FINDMYSQL_DEBUG)
  457. message("DBG: Using MYSQL_INCLUDE_DIR to find \"mysql.h\"")
  458. endif()
  459. if(NOT EXISTS "${MYSQL_INCLUDE_DIR}/mysql.h")
  460. message(FATAL_ERROR "MYSQL_INCLUDE_DIR given, but no \"mysql.h\" "
  461. "in \"${MYSQL_INCLUDE_DIR}\""
  462. )
  463. endif()
  464. elseif(
  465. MYSQL_DIR
  466. AND (NOT _mysql_config_in_mysql_dir)
  467. AND (NOT _mysql_config_set_by_user)
  468. )
  469. if(FINDMYSQL_DEBUG)
  470. message("DBG: Using MYSQL_DIR without \"mysql_config\" to find \"mysql.h\"")
  471. endif()
  472. set(MYSQL_INCLUDE_DIR "${MYSQL_DIR}/include")
  473. if(NOT EXISTS "${MYSQL_INCLUDE_DIR}/mysql.h")
  474. message(FATAL_ERROR "MYSQL_DIR given, but no \"mysql.h\" "
  475. "in \"${MYSQL_INCLUDE_DIR}\""
  476. )
  477. endif()
  478. elseif(MYSQL_CONFIG_EXECUTABLE)
  479. if(FINDMYSQL_DEBUG)
  480. message("DBG: Using \"mysql_config\" to find \"mysql.h\"")
  481. endif()
  482. # This code assumes there is just one "-I...." and that
  483. # no space between "-I" and the path
  484. _mysql_config(MYSQL_INCLUDE_DIR "(^| )-I" "--include")
  485. if(NOT MYSQL_INCLUDE_DIR)
  486. message(FATAL_ERROR "Could not find the include dir from running "
  487. "\"${MYSQL_CONFIG_EXECUTABLE}\""
  488. )
  489. endif()
  490. if(NOT EXISTS "${MYSQL_INCLUDE_DIR}/mysql.h")
  491. message(
  492. FATAL_ERROR "Could not find \"mysql.h\" in \"${MYSQL_INCLUDE_DIR}\" "
  493. "found from running \"${MYSQL_CONFIG_EXECUTABLE}\""
  494. )
  495. endif()
  496. else()
  497. if(FINDMYSQL_DEBUG)
  498. message("DBG: Using find_path() searching "
  499. "\"${_pp_include_fallback_path}\" to find \"mysql.h\""
  500. )
  501. endif()
  502. # No specific paths, try some common install paths
  503. find_path(MYSQL_INCLUDE_DIR mysql.h ${_include_fallback_path})
  504. if(NOT MYSQL_INCLUDE_DIR)
  505. message(FATAL_ERROR "Could not find \"mysql.h\" from searching "
  506. "\"${_pp_include_fallback_path}\""
  507. )
  508. endif()
  509. endif()
  510. if(FINDMYSQL_DEBUG)
  511. message("DBG: MYSQL_INCLUDE_DIR = \"${MYSQL_INCLUDE_DIR}\"")
  512. endif()
  513. ##########################################################################
  514. #
  515. # Find MYSQL_LIB_DIR, MYSQL_LIB, MYSQL_PLUGIN_DIR and MYSQL_LIBRARIES
  516. #
  517. ##########################################################################
  518. if(FINDMYSQL_DEBUG AND MYSQL_LIB_DIR)
  519. message("DBG: User gave MYSQL_LIB_DIR = \"${MYSQL_LIB_DIR}\"")
  520. endif()
  521. if(MYSQL_LIB_DIR)
  522. if(FINDMYSQL_DEBUG)
  523. message("DBG: Using find_library() searching MYSQL_LIB_DIR")
  524. endif()
  525. find_library(
  526. MYSQL_LIB
  527. NAMES ${_search_libs}
  528. PATHS "${MYSQL_LIB_DIR}"
  529. NO_DEFAULT_PATH
  530. )
  531. _check_lib_search_error(MYSQL_LIB_DIR MYSQL_LIB "")
  532. set(MYSQL_LIBRARIES ${MYSQL_LIB})
  533. if(NOT DEFINED MYSQL_PLUGIN_DIR)
  534. set(MYSQL_PLUGIN_DIR "${MYSQL_LIB_DIR}/plugin")
  535. endif()
  536. elseif(
  537. MYSQL_DIR
  538. AND (NOT _mysql_config_in_mysql_dir)
  539. AND (NOT _mysql_config_set_by_user)
  540. )
  541. if(FINDMYSQL_DEBUG)
  542. message("DBG: Using find_library() searching "
  543. "MYSQL_DIR and \"${_pp_lib_subdirs}\""
  544. )
  545. endif()
  546. find_library(
  547. MYSQL_LIB
  548. NAMES ${_search_libs}
  549. PATHS "${MYSQL_DIR}"
  550. PATH_SUFFIXES ${_lib_subdirs}
  551. NO_DEFAULT_PATH
  552. )
  553. _check_lib_search_error(MYSQL_DIR MYSQL_LIB "in \"${_pp_lib_subdirs}\"")
  554. get_filename_component(MYSQL_LIB_DIR "${MYSQL_LIB}" PATH)
  555. set(MYSQL_LIBRARIES "${MYSQL_LIB}")
  556. if(((NOT DEFINED MYSQL_PLUGIN_DIR) OR (NOT ${MYSQL_PLUGIN_DIR}))
  557. AND MYSQL_LIB_DIR
  558. )
  559. if(EXISTS "${MYSQL_LIB_DIR}/plugin")
  560. set(MYSQL_PLUGIN_DIR "${MYSQL_LIB_DIR}/plugin")
  561. else()
  562. # FIXME: If directory does not exist it must be a debug dir layout
  563. if(EXISTS "${MYSQL_LIB_DIR}/../plugin/")
  564. set(MYSQL_PLUGIN_DIR "${MYSQL_LIB_DIR}/../plugin")
  565. endif()
  566. endif()
  567. endif()
  568. elseif(MYSQL_CONFIG_EXECUTABLE)
  569. if(FINDMYSQL_DEBUG)
  570. message("DBG: Using \"mysql_config\" to find the libraries")
  571. endif()
  572. # This code assumes there is just one "-L...." and that
  573. # no space between "-L" and the path
  574. _mysql_config(MYSQL_LIB_DIR "(^| )-L" "--libs")
  575. # Note: Allow overriding plugin dir reported by mysql_config because
  576. # that can be wrong sometimes
  577. if(NOT MYSQL_PLUGIN_DIR)
  578. _mysql_conf(MYSQL_PLUGIN_DIR "--variable=plugindir")
  579. endif()
  580. if(CMAKE_SYSTEM_NAME MATCHES "SunOS")
  581. # This is needed to make Solaris binaries using the default runtime lib path
  582. _mysql_config(DEV_STUDIO_RUNTIME_DIR "(^| )-R" "--libs")
  583. endif()
  584. list(LENGTH MYSQL_LIB_DIR dir_cnt)
  585. message(STATUS "Libraries paths found: ${n}")
  586. if(${dir_cnt} GREATER 1)
  587. set(MYSQL_LIB_DIR_LIST ${MYSQL_LIB_DIR})
  588. message(STATUS "MYSQL_LIB_DIR_LIST = ${MYSQL_LIB_DIR_LIST}")
  589. foreach(_path_to_check IN LISTS MYSQL_LIB_DIR)
  590. find_library(
  591. _mysql_client_lib_var
  592. NAMES ${_search_libs}
  593. PATHS ${_path_to_check}
  594. NO_DEFAULT_PATH
  595. )
  596. if(_mysql_client_lib_var)
  597. message(STATUS "CLIENT LIB VAR: ${_mysql_client_lib_var}")
  598. unset(_mysql_client_lib_var CACHE)
  599. set(MYSQL_LIB_DIR ${_path_to_check})
  600. endif()
  601. endforeach(_path_to_check)
  602. endif()
  603. if(NOT MYSQL_LIB_DIR)
  604. message(FATAL_ERROR "Could not find the library dir from running "
  605. "\"${MYSQL_CONFIG_EXECUTABLE}\""
  606. )
  607. endif()
  608. if(NOT EXISTS "${MYSQL_LIB_DIR}")
  609. message(FATAL_ERROR "Could not find the directory \"${MYSQL_LIB_DIR}\" "
  610. "found from running \"${MYSQL_CONFIG_EXECUTABLE}\""
  611. )
  612. endif()
  613. # We have the assumed MYSQL_LIB_DIR. The output from "mysql_config"
  614. # might not be correct for static libraries, so we might need to
  615. # adjust MYSQL_LIB_DIR later on.
  616. if(MYSQLCLIENT_STATIC_LINKING)
  617. # Find the static library, might be one level down
  618. find_library(
  619. MYSQL_LIB
  620. NAMES ${_search_libs}
  621. PATHS ${MYSQL_LIB_DIR}
  622. PATH_SUFFIXES ${_static_subdirs}
  623. NO_DEFAULT_PATH
  624. )
  625. _check_lib_search_error(MYSQL_LIB_DIR MYSQL_LIB "in \"${_static_subdirs}\"")
  626. # Adjust MYSQL_LIB_DIR in case it changes
  627. get_filename_component(MYSQL_LIB_DIR "${MYSQL_LIB}" PATH)
  628. # Replace the current library references with the full path
  629. # to the library, i.e. the -L will be ignored
  630. _mysql_config_replace(
  631. MYSQL_LIBRARIES "(mysqlclient|mysqlclient_r)" "${MYSQL_LIB}" "(^| )-l"
  632. "--libs"
  633. )
  634. else()
  635. _mysql_config(MYSQL_LIBRARIES "(^| )-l" "--libs")
  636. foreach(__lib IN LISTS MYSQL_LIBRARIES)
  637. string(REGEX MATCH "mysqlclient([^ ]*)" _matched_lib __lib)
  638. if(_matched_lib)
  639. set(_search_libs ${matched_lib})
  640. endif()
  641. endforeach()
  642. # First library is assumed to be the client library
  643. # list(GET MYSQL_LIBRARIES 0 _search_libs)
  644. find_library(
  645. MYSQL_LIB
  646. NAMES ${_search_libs}
  647. PATHS ${MYSQL_LIB_DIR}
  648. NO_DEFAULT_PATH
  649. )
  650. _check_lib_search_error(MYSQL_LIB_DIR MYSQL_LIB "")
  651. endif()
  652. else()
  653. if(FINDMYSQL_DEBUG)
  654. message("DBG: Using find_library() searching "
  655. "\"${_pp_lib_fallback_path}\" to find the client library"
  656. )
  657. endif()
  658. # Search standard places
  659. find_library(
  660. MYSQL_LIB
  661. NAMES ${_search_libs}
  662. PATHS ${_lib_fallback_path}
  663. )
  664. if(NOT MYSQL_LIB)
  665. message(FATAL_ERROR "Could not find \"${_pp_search_libs}\" from searching "
  666. "\"${_pp_lib_fallback_path}\""
  667. )
  668. endif()
  669. get_filename_component(MYSQL_LIB_DIR "${MYSQL_LIB}" PATH)
  670. endif()
  671. ##########################################################################
  672. #
  673. # Add more libraries to MYSQL_LIBRARIES
  674. #
  675. ##########################################################################
  676. # FIXME needed?!
  677. if(MYSQLCLIENT_STATIC_LINKING
  678. AND NOT WIN32
  679. AND NOT ${CMAKE_SYSTEM_NAME} MATCHES "Darwin"
  680. )
  681. list(APPEND MYSQL_LIBRARIES "rt")
  682. endif()
  683. # The built-in sys and strings are used for static and dynamic linking.
  684. list(APPEND SYS_LIBRARIES "mysql_sys")
  685. list(APPEND SYS_LIBRARIES "mysql_strings")
  686. list(APPEND SYS_LIBRARIES ${MYSQL_LIBRARIES})
  687. set(MYSQL_LIBRARIES ${SYS_LIBRARIES})
  688. if(MYSQL_EXTRA_LIBRARIES)
  689. separate_arguments(MYSQL_EXTRA_LIBRARIES)
  690. list(APPEND MYSQL_LIBRARIES ${MYSQL_EXTRA_LIBRARIES})
  691. endif()
  692. list(LENGTH MYSQL_LIBRARIES len)
  693. if(MYSQL_STATIC_LINKING AND (len LESS 2))
  694. message(
  695. WARNING
  696. "Statically linking MySQL client library normally requires linking"
  697. " additional libraries that the client library depends on. It seems"
  698. " no extra libraries have been specified. Provide the list of required"
  699. " dependencies through MYSQL_EXTRA_LIBRARIES."
  700. )
  701. endif()
  702. # For compatibility
  703. set(MYSQL_CLIENT_LIBS ${MYSQL_LIBRARIES})
  704. ##########################################################################
  705. #
  706. # If not found MySQL Serverv version, compile a small client app
  707. # and let it write a small cmake file with the settings
  708. #
  709. ##########################################################################
  710. if(MYSQL_INCLUDE_DIR AND NOT MYSQL_VERSION)
  711. # Write the C source file that will include the MySQL headers
  712. set(GETMYSQLVERSION_SOURCEFILE
  713. "${CMAKE_CURRENT_BINARY_DIR}/getmysqlversion.c"
  714. )
  715. file(WRITE "${GETMYSQLVERSION_SOURCEFILE}"
  716. "#include <mysql.h>\n" "#include <stdio.h>\n" "int main() {\n"
  717. " printf(\"%s\", MYSQL_SERVER_VERSION);\n" "}\n"
  718. )
  719. # Compile and run the created executable, store output in MYSQL_VERSION
  720. try_run(
  721. _run_result _compile_result "${CMAKE_BINARY_DIR}"
  722. "${GETMYSQLVERSION_SOURCEFILE}"
  723. CMAKE_FLAGS "-DINCLUDE_DIRECTORIES:STRING=${MYSQL_INCLUDE_DIR}"
  724. RUN_OUTPUT_VARIABLE MYSQL_VERSION
  725. )
  726. if(FINDMYSQL_DEBUG)
  727. if(NOT _compile_result)
  728. message("DBG: Could not compile \"getmysqlversion.c\"")
  729. endif()
  730. if(_run_result)
  731. message("DBG: Running \"getmysqlversion\" returned ${_run_result}")
  732. endif()
  733. endif()
  734. endif()
  735. ##########################################################################
  736. #
  737. # Clean up MYSQL_VERSION and create MYSQL_VERSION_ID/MYSQL_NUM_VERSION
  738. #
  739. ##########################################################################
  740. if(NOT MYSQL_VERSION)
  741. message(FATAL_ERROR "Could not determine the MySQL Server version")
  742. endif()
  743. # Clean up so only numeric, in case of "-alpha" or similar
  744. string(REGEX MATCHALL "([0-9]+.[0-9]+.[0-9]+)" MYSQL_VERSION "${MYSQL_VERSION}")
  745. # To create a fully numeric version, first normalize so N.NN.NN
  746. string(REGEX REPLACE "[.]([0-9])[.]" ".0\\1." MYSQL_VERSION_ID
  747. "${MYSQL_VERSION}"
  748. )
  749. string(REGEX REPLACE "[.]([0-9])$" ".0\\1" MYSQL_VERSION_ID
  750. "${MYSQL_VERSION_ID}"
  751. )
  752. # Finally remove the dot
  753. string(REGEX REPLACE "[.]" "" MYSQL_VERSION_ID "${MYSQL_VERSION_ID}")
  754. set(MYSQL_NUM_VERSION ${MYSQL_VERSION_ID})
  755. ##########################################################################
  756. #
  757. # Try determine if to use C++ linkage, and also find C++ flags
  758. #
  759. ##########################################################################
  760. if(NOT WIN32)
  761. if(MYSQL_CONFIG_EXECUTABLE)
  762. if(NOT MYSQL_CFLAGS)
  763. _mysql_conf(MYSQL_CFLAGS "--cflags")
  764. endif()
  765. if(NOT MYSQL_CXXFLAGS)
  766. if(MYSQL_CXX_LINKAGE OR MYSQL_VERSION_ID GREATER 50603)
  767. _mysql_conf(MYSQL_CXXFLAGS "--cxxflags")
  768. set(MYSQL_CXX_LINKAGE 1)
  769. else()
  770. set(MYSQL_CXXFLAGS "${MYSQL_CFLAGS}")
  771. endif()
  772. endif()
  773. # FIXME this should not be needed, caller of this module should set
  774. # it's own flags and just use the library on it's on terms
  775. # (change the infe message if enabling this code)
  776. # if(NOT MYSQL_LINK_FLAGS)
  777. # # Find -mcpu -march -mt -m32 -m64 and other flags starting with "-m"
  778. # string(REGEX MATCHALL "(^| )-m([^\r\n ]+)" MYSQL_LINK_FLAGS "${MYSQL_CXXFLAGS}")
  779. # string(REGEX REPLACE "^ " "" MYSQL_LINK_FLAGS "${MYSQL_LINK_FLAGS}")
  780. # string(REGEX REPLACE "; " ";" MYSQL_LINK_FLAGS "${MYSQL_LINK_FLAGS}")
  781. # endif()
  782. endif()
  783. endif()
  784. ##########################################################################
  785. #
  786. # Inform CMake where to look for headers and libraries
  787. #
  788. ##########################################################################
  789. # string(TOUPPER "${CMAKE_BUILD_TYPE}" CMAKEBT)
  790. # set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${MYSQL_CXXFLAGS}")
  791. # set(CMAKE_CXX_FLAGS_${CMAKEBT} "${CMAKE_CXX_FLAGS_${CMAKEBT}} ${MYSQL_CXXFLAGS}")
  792. include_directories("${MYSQL_INCLUDE_DIR}")
  793. link_directories("${MYSQL_LIB_DIR}")
  794. message(STATUS "MYSQL_LIB_DIR_LIST = ${MYSQL_LIB_DIR_LIST}")
  795. if(MYSQL_LIB_DIR_LIST)
  796. foreach(__libpath IN LISTS MYSQL_LIB_DIR_LIST)
  797. link_directories("${__libpath}")
  798. endforeach()
  799. endif()
  800. ##########################################################################
  801. #
  802. # Report
  803. #
  804. ##########################################################################
  805. message(
  806. STATUS
  807. "MySQL client environment/cmake variables set that the user can override"
  808. )
  809. message(STATUS " MYSQL_DIR : ${MYSQL_DIR}")
  810. message(STATUS " MYSQL_INCLUDE_DIR : ${MYSQL_INCLUDE_DIR}")
  811. message(STATUS " MYSQL_LIB_DIR : ${MYSQL_LIB_DIR}")
  812. message(STATUS " MYSQL_PLUGIN_DIR : ${MYSQL_PLUGIN_DIR}")
  813. message(STATUS " MYSQL_CONFIG_EXECUTABLE : ${MYSQL_CONFIG_EXECUTABLE}")
  814. message(STATUS " MYSQL_CXX_LINKAGE : ${MYSQL_CXX_LINKAGE}")
  815. message(STATUS " MYSQL_CFLAGS : ${MYSQL_CFLAGS}")
  816. message(STATUS " MYSQL_CXXFLAGS : ${MYSQL_CXXFLAGS}")
  817. message(STATUS " MYSQLCLIENT_STATIC_LINKING : ${MYSQLCLIENT_STATIC_LINKING}")
  818. message(STATUS " MYSQLCLIENT_NO_THREADS : ${MYSQLCLIENT_NO_THREADS}")
  819. message(
  820. STATUS "MySQL client optional environment/cmake variables set by the user"
  821. )
  822. message(STATUS " MYSQL_EXTRA_LIBRARIES : ${MYSQL_EXTRA_LIBRARIES}")
  823. message(STATUS " MYSQL_LINK_FLAGS : ${MYSQL_LINK_FLAGS}")
  824. message(STATUS "MySQL client settings that the user can't override")
  825. message(STATUS " MYSQL_VERSION : ${MYSQL_VERSION}")
  826. message(STATUS " MYSQL_VERSION_ID : ${MYSQL_VERSION_ID}")
  827. message(STATUS " MYSQL_LIB : ${MYSQL_LIB}")
  828. message(STATUS " MYSQL_LIBRARIES : ${MYSQL_LIBRARIES}")