FindMySQL.cmake 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. #[==[
  2. Provides the following variables:
  3. * `MySQL_INCLUDE_DIRS`: Include directories necessary to use MySQL.
  4. * `MySQL_LIBRARIES`: Libraries necessary to use MySQL.
  5. * A `MySQL::MySQL` imported target.
  6. #]==]
  7. # No .pc files are shipped with MySQL on Windows.
  8. set(_MySQL_use_pkgconfig 0)
  9. if (NOT WIN32)
  10. find_package(PkgConfig)
  11. if (PkgConfig_FOUND)
  12. set(_MySQL_use_pkgconfig 1)
  13. endif ()
  14. endif ()
  15. if (_MySQL_use_pkgconfig)
  16. pkg_check_modules(_libmariadb "libmariadb" QUIET IMPORTED_TARGET)
  17. unset(_mysql_target)
  18. if (_libmariadb_FOUND)
  19. set(_mysql_target "_libmariadb")
  20. else ()
  21. pkg_check_modules(_mariadb "mariadb" QUIET IMPORTED_TARGET)
  22. if (NOT _mariadb_FOUND)
  23. pkg_check_modules(_mysql "mysql" QUIET IMPORTED_TARGET)
  24. if (_mysql_FOUND)
  25. set(_mysql_target "_mysql")
  26. endif ()
  27. else ()
  28. set(_mysql_target "_mariadb")
  29. if (_mariadb_VERSION VERSION_LESS 10.4)
  30. get_property(_include_dirs
  31. TARGET "PkgConfig::_mariadb"
  32. PROPERTY "INTERFACE_INCLUDE_DIRECTORIES")
  33. # Remove "${prefix}/mariadb/.." from the interface since it breaks other
  34. # projects.
  35. list(FILTER _include_dirs EXCLUDE REGEX "\\.\\.")
  36. set_property(TARGET "PkgConfig::_mariadb"
  37. PROPERTY
  38. "INTERFACE_INCLUDE_DIRECTORIES" "${_include_dirs}")
  39. unset(_include_dirs)
  40. endif ()
  41. endif ()
  42. endif ()
  43. set(MySQL_FOUND 0)
  44. if (_mysql_target)
  45. set(MySQL_FOUND 1)
  46. set(MySQL_INCLUDE_DIRS ${${_mysql_target}_INCLUDE_DIRS})
  47. set(MySQL_LIBRARIES ${${_mysql_target}_LINK_LIBRARIES})
  48. if (NOT TARGET MySQL::MySQL)
  49. add_library(MySQL::MySQL INTERFACE IMPORTED)
  50. target_link_libraries(MySQL::MySQL
  51. INTERFACE "PkgConfig::${_mysql_target}")
  52. endif ()
  53. endif ()
  54. unset(_mysql_target)
  55. else ()
  56. set(_MySQL_mariadb_versions 10.2 10.3)
  57. set(_MySQL_versions 5.0)
  58. set(_MySQL_paths)
  59. foreach (_MySQL_version IN LISTS _MySQL_mariadb_versions)
  60. list(APPEND _MySQL_paths
  61. "[HKEY_LOCAL_MACHINE\\SOFTWARE\\MariaDB ${_MySQL_version};INSTALLDIR]"
  62. "[HKEY_LOCAL_MACHINE\\SOFTWARE\\MariaDB ${_MySQL_version} (x64);INSTALLDIR]")
  63. endforeach ()
  64. foreach (_MySQL_version IN LISTS _MySQL_versions)
  65. list(APPEND _MySQL_paths
  66. "C:/Program Files/MySQL/MySQL Server ${_MySQL_version}/lib/opt"
  67. "[HKEY_LOCAL_MACHINE\\SOFTWARE\\MySQL AB\\MySQL Server ${_MySQL_version};Location]"
  68. "[HKEY_LOCAL_MACHINE\\SOFTWARE\\Wow6432Node\\MySQL AB\\MySQL Server ${_MySQL_version};Location]")
  69. endforeach ()
  70. unset(_MySQL_version)
  71. unset(_MySQL_versions)
  72. unset(_MySQL_mariadb_versions)
  73. find_path(MySQL_INCLUDE_DIR
  74. NAMES mysql.h
  75. PATHS
  76. "C:/Program Files/MySQL/include"
  77. "C:/MySQL/include"
  78. ${_MySQL_paths}
  79. PATH_SUFFIXES include include/mysql
  80. DOC "Location of mysql.h")
  81. mark_as_advanced(MySQL_INCLUDE_DIR)
  82. find_library(MySQL_LIBRARY
  83. NAMES libmariadb mysql libmysql mysqlclient
  84. PATHS
  85. "C:/Program Files/MySQL/lib"
  86. "C:/MySQL/lib/debug"
  87. ${_MySQL_paths}
  88. PATH_SUFFIXES lib lib/opt
  89. DOC "Location of the mysql library")
  90. mark_as_advanced(MySQL_LIBRARY)
  91. include(FindPackageHandleStandardArgs)
  92. find_package_handle_standard_args(MySQL
  93. REQUIRED_VARS MySQL_INCLUDE_DIR MySQL_LIBRARY)
  94. if (MySQL_FOUND)
  95. set(MySQL_INCLUDE_DIRS "${MySQL_INCLUDE_DIR}")
  96. set(MySQL_LIBRARIES "${MySQL_LIBRARY}")
  97. if (NOT TARGET MySQL::MySQL)
  98. add_library(MySQL::MySQL UNKNOWN IMPORTED)
  99. set_target_properties(MySQL::MySQL PROPERTIES
  100. IMPORTED_LOCATION "${MySQL_LIBRARY}"
  101. INTERFACE_INCLUDE_DIRECTORIES "${MySQL_INCLUDE_DIR}")
  102. endif ()
  103. endif ()
  104. endif ()
  105. unset(_MySQL_use_pkgconfig)