Cmake FindMySQL script

Cmake is a great build tool. It takes care of generating build system files, targeting, among others, Unix makefile, MS Visual Studio, ninja, XCode etc. There are various extra modules available for Cmake. One in particular allows the discovery of MySQL development files, such as headers and libraries. It can be found here. Unfortunately, it has a few issues: it doesn't work for Windows, and it has a bug towards the end. Other alternatives I found on the web were even worse. So here is an updated copy, which seems to be working much better, while supporting Windows, Linux and Mac out of the box:

  1. # - Try to find MySQL.
  2. # Once done this will define:
  3. # MYSQL_FOUND			- If false, do not try to use MySQL.
  4. # MYSQL_INCLUDE_DIRS	- Where to find mysql.h, etc.
  5. # MYSQL_LIBRARIES		- The libraries to link against.
  6. # MYSQL_VERSION_STRING	- Version in a string of MySQL.
  7. #
  8. # Created by RenatoUtsch based on eAthena implementation.
  9. # Updated by Andre Bar'yudin
  10. #
  11. # Please note that this module only supports Windows and Linux officially, but
  12. # should work on all UNIX-like operational systems too.
  13. #
  14.  
  15. #=============================================================================
  16. # Copyright 2012 RenatoUtsch
  17. #
  18. # Distributed under the OSI-approved BSD License (the "License");
  19. # see accompanying file Copyright.txt for details.
  20. #
  21. # This software is distributed WITHOUT ANY WARRANTY; without even the
  22. # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  23. # See the License for more information.
  24. #=============================================================================
  25. # (To distribute this file outside of CMake, substitute the full
  26. #  License text for the above reference.)
  27.  
  28. if( WIN32 )
  29. 	set(MYENVx86 "PROGRAMFILES(X86)")
  30.  
  31. 	set(MYSQL_SOURCE_DIRS 
  32. 		"$ENV{PROGRAMFILES}/MySQL" 
  33. 		"$ENV{${MYENVx86}}/MySQL" 
  34. 		"$ENV{SYSTEMDRIVE}/MySQL"
  35. 		"$ENV{ProgramW6432}/MySQL"
  36. 		)
  37.  
  38. 	foreach(stem ${MYSQL_SOURCE_DIRS})
  39. 		file(GLOB MYTEMPVAR1 LINK_DIRECTORIES TRUE "${stem}/*/include")
  40. 		list(APPEND MYSQL_DIRS_INC
  41. 			${MYTEMPVAR1})
  42. 		file(GLOB MYTEMPVAR2 LINK_DIRECTORIES TRUE "${stem}/*/lib")
  43. 		list(APPEND MYSQL_DIRS_LIB
  44. 			${MYTEMPVAR2})
  45. 	endforeach(stem)
  46.  
  47. 	find_path( MYSQL_INCLUDE_DIR
  48. 		NAMES "mysql.h"
  49. 		PATHS ${MYSQL_DIRS_INC})
  50.  
  51. 	find_library( MYSQL_LIBRARY
  52. 		NAMES "mysqlclient" "mysqlclient_r"
  53. 		PATHS  ${MYSQL_DIRS_LIB} )
  54. else()
  55. 	find_path( MYSQL_INCLUDE_DIR
  56. 		NAMES "mysql.h"
  57. 		PATHS "/usr/include/mysql"
  58. 			  "/usr/local/include/mysql"
  59. 			  "/usr/mysql/include/mysql" )
  60.  
  61. 	find_library( MYSQL_LIBRARY
  62. 		NAMES "mysqlclient" "mysqlclient_r"
  63. 		PATHS "/lib/mysql"
  64. 			  "/lib64/mysql"
  65. 			  "/usr/lib/mysql"
  66. 			  "/usr/lib64/mysql"
  67. 			  "/usr/local/lib/mysql"
  68. 			  "/usr/local/lib64/mysql"
  69. 			  "/usr/mysql/lib/mysql"
  70. 			  "/usr/mysql/lib64/mysql" )
  71. endif()
  72.  
  73. if( EXISTS "${MYSQL_INCLUDE_DIR}/mysql_version.h" )
  74. 	file( STRINGS "${MYSQL_INCLUDE_DIR}/mysql_version.h"
  75. 		MYSQL_VERSION_H REGEX "^#define[ \t]+MYSQL_SERVER_VERSION[ \t]+\"[^\"]+\".*$" )
  76. 	string( REGEX REPLACE
  77. 		"^.*MYSQL_SERVER_VERSION[ \t]+\"([^\"]+)\".*$" "\\1" MYSQL_VERSION_STRING
  78. 		"${MYSQL_VERSION_H}" )
  79. endif()
  80.  
  81. # handle the QUIETLY and REQUIRED arguments and set MYSQL_FOUND to TRUE if
  82. # all listed variables are TRUE
  83. include( FindPackageHandleStandardArgs )
  84. find_package_handle_standard_args( MYSQL
  85.     REQUIRED_VARS	MYSQL_LIBRARY MYSQL_INCLUDE_DIR
  86. 	VERSION_VAR		MYSQL_VERSION_STRING )
  87.  
  88. set( MYSQL_INCLUDE_DIRS ${MYSQL_INCLUDE_DIR} )
  89. set( MYSQL_LIBRARIES ${MYSQL_LIBRARY} )
  90.  
  91. mark_as_advanced( MYSQL_INCLUDE_DIR MYSQL_LIBRARY )

Add new comment

Subscribe to Comments for "Cmake FindMySQL script"