FindODBC.cmake 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. # file Copyright.txt or https://cmake.org/licensing for details.
  3. #[=======================================================================[.rst:
  4. FindODBC
  5. --------
  6. Find an Open Database Connectivity (ODBC) include directory and library.
  7. On Windows, when building with Visual Studio, this module assumes the ODBC
  8. library is provided by the available Windows SDK.
  9. On Unix, this module allows to search for ODBC library provided by
  10. unixODBC or iODBC implementations of ODBC API.
  11. This module reads hint about location of the config program:
  12. .. variable:: ODBC_CONFIG
  13. Location of odbc_config or iodbc-config program
  14. Otherwise, this module tries to find the config program,
  15. first from unixODBC, then from iODBC.
  16. If no config program found, this module searches for ODBC header
  17. and library in list of known locations.
  18. Imported targets
  19. ^^^^^^^^^^^^^^^^
  20. This module defines the following :prop_tgt:`IMPORTED` targets:
  21. .. variable:: ODBC::ODBC
  22. Imported target for using the ODBC library, if found.
  23. Result variables
  24. ^^^^^^^^^^^^^^^^
  25. .. variable:: ODBC_FOUND
  26. Set to true if ODBC library found, otherwise false or undefined.
  27. .. variable:: ODBC_INCLUDE_DIRS
  28. Paths to include directories listed in one variable for use by ODBC client.
  29. May be empty on Windows, where the include directory corresponding to the
  30. expected Windows SDK is already available in the compilation environment.
  31. .. variable:: ODBC_LIBRARIES
  32. Paths to libraries to linked against to use ODBC.
  33. May just a library name on Windows, where the library directory corresponding
  34. to the expected Windows SDK is already available in the compilation environment.
  35. .. variable:: ODBC_CONFIG
  36. Path to unixODBC or iODBC config program, if found or specified.
  37. Cache variables
  38. ^^^^^^^^^^^^^^^
  39. For users who wish to edit and control the module behavior, this module
  40. reads hints about search locations from the following variables:
  41. .. variable:: ODBC_INCLUDE_DIR
  42. Path to ODBC include directory with ``sql.h`` header.
  43. .. variable:: ODBC_LIBRARY
  44. Path to ODBC library to be linked.
  45. These variables should not be used directly by project code.
  46. Limitations
  47. ^^^^^^^^^^^
  48. On Windows, this module does not search for iODBC.
  49. On Unix, there is no way to prefer unixODBC over iODBC, or vice versa,
  50. other than providing the config program location using the ``ODBC_CONFIG``.
  51. This module does not allow to search for a specific ODBC driver.
  52. #]=======================================================================]
  53. # Define lists used internally
  54. set(_odbc_include_paths)
  55. set(_odbc_lib_paths)
  56. set(_odbc_lib_names)
  57. set(_odbc_required_libs_names)
  58. ### Try Windows Kits ##########################################################
  59. if(WIN32)
  60. # List names of ODBC libraries on Windows
  61. if(NOT MINGW)
  62. set(ODBC_LIBRARY odbc32.lib)
  63. else()
  64. set(ODBC_LIBRARY libodbc32.a)
  65. endif()
  66. set(_odbc_lib_names odbc32;)
  67. # List additional libraries required to use ODBC library
  68. if(MSVC OR CMAKE_CXX_COMPILER_ID MATCHES "Intel")
  69. set(_odbc_required_libs_names odbccp32;ws2_32)
  70. elseif(MINGW)
  71. set(_odbc_required_libs_names odbccp32)
  72. endif()
  73. endif()
  74. ### Try unixODBC or iODBC config program ######################################
  75. if (UNIX)
  76. find_program(ODBC_CONFIG
  77. NAMES odbc_config iodbc-config
  78. DOC "Path to unixODBC or iODBC config program")
  79. mark_as_advanced(ODBC_CONFIG)
  80. endif()
  81. if (UNIX AND ODBC_CONFIG)
  82. # unixODBC and iODBC accept unified command line options
  83. execute_process(COMMAND ${ODBC_CONFIG} --cflags
  84. OUTPUT_VARIABLE _cflags OUTPUT_STRIP_TRAILING_WHITESPACE)
  85. execute_process(COMMAND ${ODBC_CONFIG} --libs
  86. OUTPUT_VARIABLE _libs OUTPUT_STRIP_TRAILING_WHITESPACE)
  87. # Collect paths of include directories from CFLAGS
  88. separate_arguments(_cflags NATIVE_COMMAND "${_cflags}")
  89. foreach(arg IN LISTS _cflags)
  90. if("${arg}" MATCHES "^-I(.*)$")
  91. list(APPEND _odbc_include_paths "${CMAKE_MATCH_1}")
  92. endif()
  93. endforeach()
  94. unset(_cflags)
  95. # Collect paths of library names and directories from LIBS
  96. separate_arguments(_libs NATIVE_COMMAND "${_libs}")
  97. foreach(arg IN LISTS _libs)
  98. if("${arg}" MATCHES "^-L(.*)$")
  99. list(APPEND _odbc_lib_paths "${CMAKE_MATCH_1}")
  100. elseif("${arg}" MATCHES "^-l(.*)$")
  101. set(_lib_name ${CMAKE_MATCH_1})
  102. string(REGEX MATCH "odbc" _is_odbc ${_lib_name})
  103. if(_is_odbc)
  104. list(APPEND _odbc_lib_names ${_lib_name})
  105. else()
  106. list(APPEND _odbc_required_libs_names ${_lib_name})
  107. endif()
  108. unset(_lib_name)
  109. endif()
  110. endforeach()
  111. unset(_libs)
  112. endif()
  113. ### Try unixODBC or iODBC in include/lib filesystems ##########################
  114. if (UNIX AND NOT ODBC_CONFIG)
  115. # List names of both ODBC libraries, unixODBC and iODBC
  116. set(_odbc_lib_names odbc;iodbc;unixodbc;)
  117. endif()
  118. ### Find include directories ##################################################
  119. find_path(ODBC_INCLUDE_DIR
  120. NAMES sql.h
  121. PATHS ${_odbc_include_paths})
  122. if(NOT ODBC_INCLUDE_DIR AND WIN32)
  123. set(ODBC_INCLUDE_DIR "")
  124. endif()
  125. ### Find libraries ############################################################
  126. if(NOT ODBC_LIBRARY)
  127. find_library(ODBC_LIBRARY
  128. NAMES ${_odbc_lib_names}
  129. PATHS ${_odbc_lib_paths}
  130. PATH_SUFFIXES odbc)
  131. foreach(_lib IN LISTS _odbc_required_libs_names)
  132. find_library(_lib_path
  133. NAMES ${_lib}
  134. PATHS ${_odbc_lib_paths} # system parths or collected from ODBC_CONFIG
  135. PATH_SUFFIXES odbc)
  136. if(_lib_path)
  137. list(APPEND _odbc_required_libs_paths ${_lib_path})
  138. endif()
  139. unset(_lib_path CACHE)
  140. endforeach()
  141. endif()
  142. # Unset internal lists as no longer used
  143. unset(_odbc_include_paths)
  144. unset(_odbc_lib_paths)
  145. unset(_odbc_lib_names)
  146. unset(_odbc_required_libs_names)
  147. ### Set result variables ######################################################
  148. set(_odbc_required_vars ODBC_LIBRARY)
  149. if(NOT WIN32)
  150. list(APPEND _odbc_required_vars ODBC_INCLUDE_DIR)
  151. endif()
  152. include(FindPackageHandleStandardArgs)
  153. find_package_handle_standard_args(ODBC DEFAULT_MSG ${_odbc_required_vars})
  154. unset(_odbc_required_vars)
  155. mark_as_advanced(ODBC_LIBRARY ODBC_INCLUDE_DIR)
  156. set(ODBC_INCLUDE_DIRS ${ODBC_INCLUDE_DIR})
  157. list(APPEND ODBC_LIBRARIES ${ODBC_LIBRARY})
  158. list(APPEND ODBC_LIBRARIES ${_odbc_required_libs_paths})
  159. ### Import targets ############################################################
  160. if(ODBC_FOUND)
  161. if(NOT TARGET ODBC::ODBC)
  162. if(IS_ABSOLUTE "${ODBC_LIBRARY}")
  163. add_library(ODBC::ODBC UNKNOWN IMPORTED)
  164. set_target_properties(ODBC::ODBC PROPERTIES
  165. IMPORTED_LINK_INTERFACE_LANGUAGES "C"
  166. IMPORTED_LOCATION "${ODBC_LIBRARY}")
  167. else()
  168. add_library(ODBC::ODBC INTERFACE IMPORTED)
  169. set_target_properties(ODBC::ODBC PROPERTIES
  170. IMPORTED_LIBNAME "${ODBC_LIBRARY}")
  171. endif()
  172. set_target_properties(ODBC::ODBC PROPERTIES
  173. INTERFACE_INCLUDE_DIRECTORIES "${ODBC_INCLUDE_DIR}")
  174. if(_odbc_required_libs_paths)
  175. set_property(TARGET ODBC::ODBC APPEND PROPERTY
  176. INTERFACE_LINK_LIBRARIES "${_odbc_required_libs_paths}")
  177. endif()
  178. endif()
  179. endif()
  180. unset(_odbc_required_libs_paths)