@@ -11,32 +11,51 @@ Modifications from upstream:
1111
1212 * `ODBC_DRIVER`
1313
14- Name of the found driver, if any. For example, `unixODBC`, `iODBC`.
14+ Name of the found driver, if any. For example, `unixODBC`, `iODBC`. On
15+ Windows in MinGW environment it is set to `unixODBC`, and to `Windows` for
16+ the rest of the Windows system.
1517
1618 * `ODBC_VERSION`
1719
1820 Version of the found ODBC library if it was retrieved from config utilities.
1921
2022* Additional cache variables:
2123
22- * `ODBC_COMPILE_DEFINITIONS` - a `;`-list of compile definitions.
23- * `ODBC_COMPILE_OPTIONS` - a `;`-list of compile options.
24- * `ODBC_LINK_OPTIONS` - a `;`-list of linker options.
24+ * `ODBC_COMPILE_DEFINITIONS`
25+
26+ A `;`-list of compile definitions.
27+
28+ * `ODBC_COMPILE_OPTIONS`
29+
30+ A `;`-list of compile options.
31+
32+ * `ODBC_LINK_OPTIONS`
33+
34+ A `;`-list of linker options.
35+
36+ * `ODBC_LIBRARY_DIR`
37+
38+ The path to the ODBC library directory that contains the ODBC library.
2539
2640* Additional hints:
2741
2842 * `ODBC_USE_DRIVER`
2943
3044 Set to `unixODBC` or `iODBC` to limit searching for specific ODBC driver
31- instead of any driver.
45+ instead of any driver. On Windows, the searched driver will be the core ODBC
46+ Windows implementation only. On Windows in MinGW environment, there is at
47+ the time of writing `unixODBC` implementation available in the default
48+ MinGW installation and as a standalone package. The driver name is
49+ case-insensitive and if supported it will be adjusted to the expected case.
3250
3351* Added pkg-config integration.
3452
3553* Fixed limitation where the upstream module can't (yet) select which specific
36- ODBC driver to use. Except on Windows, where the driver searching is the same
37- as upstream.
54+ ODBC driver to use.
3855
3956* Added package meta-data for FeatureSummary.
57+
58+ * Fixed finding ODBC on Windows and MinGW.
4059#]=============================================================================]
4160
4261include (FeatureSummary)
@@ -59,15 +78,54 @@ mark_as_advanced(
5978 ODBC_LINK_OPTIONS
6079)
6180
62- ### Try Windows Kits ##########################################################
63- if (WIN32 )
81+ # Adjust ODBC driver string case sensitivity.
82+ if (ODBC_USE_DRIVER)
83+ string (TOLOWER "${ODBC_USE_DRIVER} " _odbc_use_driver)
84+ if (_odbc_use_driver STREQUAL "unixodbc" )
85+ set (ODBC_USE_DRIVER "unixODBC" )
86+ elseif (_odbc_use_driver STREQUAL "iodbc" )
87+ set (ODBC_USE_DRIVER "iODBC" )
88+ endif ()
89+ unset (_odbc_use_driver)
90+ endif ()
91+
92+ ### Try unixODBC or iODBC config program ######################################
93+ if (ODBC_USE_DRIVER STREQUAL "unixODBC" )
94+ set (_odbc_config_names odbc_config)
95+ elseif (ODBC_USE_DRIVER STREQUAL "iODBC" )
96+ set (_odbc_config_names iodbc-config)
97+ else ()
98+ set (_odbc_config_names odbc_config iodbc-config)
99+ endif ()
100+
101+ find_program (ODBC_CONFIG
102+ NAMES ${_odbc_config_names}
103+ DOC "Path to unixODBC or iODBC config program" )
104+ mark_as_advanced (ODBC_CONFIG)
105+
106+ ### Try pkg-config ############################################################
107+ if (NOT ODBC_CONFIG)
108+ find_package (PkgConfig QUIET )
109+ if (PKG_CONFIG_FOUND)
110+ if (ODBC_USE_DRIVER STREQUAL "unixODBC" )
111+ pkg_check_modules(PC_ODBC QUIET odbc)
112+ elseif (ODBC_USE_DRIVER STREQUAL "iODBC" )
113+ pkg_check_modules(PC_ODBC QUIET libiodbc)
114+ else ()
115+ pkg_search_module(PC_ODBC QUIET odbc libiodbc)
116+ endif ()
117+ endif ()
118+ endif ()
119+
120+ ### Try Windows ###############################################################
121+ if (NOT ODBC_CONFIG AND NOT PC_ODBC_FOUND AND CMAKE_SYSTEM_NAME STREQUAL "Windows" )
64122 # List names of ODBC libraries on Windows
65123 if (NOT MINGW)
66- set (ODBC_LIBRARY odbc32.lib)
124+ set (_odbc_lib_names odbc32.lib)
67125 else ()
68- set (ODBC_LIBRARY libodbc32.a)
126+ set (_odbc_lib_names libodbc32.a)
69127 endif ()
70- set ( _odbc_lib_names odbc32; )
128+ list ( APPEND _odbc_lib_names odbc32)
71129
72130 # List additional libraries required to use ODBC library
73131 if (MSVC OR CMAKE_CXX_COMPILER_ID MATCHES "Intel" )
@@ -77,23 +135,7 @@ if(WIN32)
77135 endif ()
78136endif ()
79137
80- ### Try unixODBC or iODBC config program ######################################
81- if (UNIX )
82- if (ODBC_USE_DRIVER MATCHES "^(unixODBC|unixodbc|UNIXODBC)$" )
83- set (_odbc_config_names odbc_config)
84- elseif (ODBC_USE_DRIVER MATCHES "^(iODBC|iodbc|IODBC)$" )
85- set (_odbc_config_names iodbc-config)
86- else ()
87- set (_odbc_config_names odbc_config iodbc-config)
88- endif ()
89-
90- find_program (ODBC_CONFIG
91- NAMES ${_odbc_config_names}
92- DOC "Path to unixODBC or iODBC config program" )
93- mark_as_advanced (ODBC_CONFIG)
94- endif ()
95-
96- if (UNIX AND ODBC_CONFIG)
138+ if (ODBC_CONFIG)
97139 # unixODBC and iODBC accept unified command line options
98140 execute_process (COMMAND ${ODBC_CONFIG} --cflags
99141 OUTPUT_VARIABLE _cflags OUTPUT_STRIP_TRAILING_WHITESPACE)
@@ -128,25 +170,11 @@ if(UNIX AND ODBC_CONFIG)
128170 unset (_libs)
129171endif ()
130172
131- ### Try pkg-config ############################################################
132- if (NOT ODBC_CONFIG)
133- find_package (PkgConfig QUIET )
134- if (PKG_CONFIG_FOUND)
135- if (ODBC_USE_DRIVER MATCHES "^(unixODBC|unixodbc|UNIXODBC)$" )
136- pkg_check_modules(PC_ODBC QUIET odbc)
137- elseif (ODBC_USE_DRIVER MATCHES "^(iODBC|iodbc|IODBC)$" )
138- pkg_check_modules(PC_ODBC QUIET libiodbc)
139- else ()
140- pkg_search_module(PC_ODBC QUIET odbc libiodbc)
141- endif ()
142- endif ()
143- endif ()
144-
145173### Try unixODBC or iODBC in include/lib filesystems ##########################
146174if (UNIX AND NOT ODBC_CONFIG)
147- if (ODBC_USE_DRIVER MATCHES "^( unixODBC|unixodbc|UNIXODBC)$ " )
175+ if (ODBC_USE_DRIVER STREQUAL " unixODBC" )
148176 set (_odbc_lib_names odbc;unixodbc;)
149- elseif (ODBC_USE_DRIVER MATCHES "^( iODBC|iodbc|IODBC)$ " )
177+ elseif (ODBC_USE_DRIVER STREQUAL " iODBC" )
150178 set (_odbc_lib_names iodbc;)
151179 else ()
152180 # List names of both ODBC libraries, unixODBC and iODBC
@@ -160,7 +188,7 @@ find_path(ODBC_INCLUDE_DIR
160188 PATHS ${_odbc_include_paths}
161189 HINTS ${PC_ODBC_INCLUDE_DIRS} )
162190
163- if (NOT ODBC_INCLUDE_DIR AND WIN32 )
191+ if (NOT ODBC_INCLUDE_DIR AND CMAKE_SYSTEM_NAME STREQUAL "Windows" )
164192 set (ODBC_INCLUDE_DIR "" )
165193endif ()
166194
@@ -184,6 +212,17 @@ if(NOT ODBC_LIBRARY)
184212 endforeach ()
185213endif ()
186214
215+ # Find library directory when ODBC_LIBRARY is set as a library name. For
216+ # example, when looking for ODBC with ODBC_ROOT or CMAKE_PREFIX_PATH set.
217+ if (NOT ODBC_LIBRARY_DIR AND ODBC_LIBRARY AND NOT IS_ABSOLUTE "${ODBC_LIBRARY} " )
218+ find_library (ODBC_LIBRARY_DIR ${ODBC_LIBRARY} PATH_SUFFIXES odbc)
219+ if (ODBC_LIBRARY_DIR)
220+ cmake_path(GET ODBC_LIBRARY_DIR PARENT_PATH _parent)
221+ set_property (CACHE ODBC_LIBRARY_DIR PROPERTY VALUE ${_parent} )
222+ unset (_parent)
223+ endif ()
224+ endif ()
225+
187226# Unset internal lists as no longer used
188227unset (_odbc_include_paths)
189228unset (_odbc_lib_paths)
@@ -213,15 +252,15 @@ block(PROPAGATE ODBC_VERSION)
213252 OUTPUT_STRIP_TRAILING_WHITESPACE
214253 ERROR_QUIET
215254 )
216- if (_odbc_version MATCHES "[0-9]+\. [0-9.]* " )
255+ if (_odbc_version MATCHES "[0-9]+\. [0-9.]+ " )
217256 set (ODBC_VERSION ${_odbc_version} )
218257 endif ()
219258 endif ()
220259endblock()
221260
222261### Set result variables ######################################################
223262set (_odbc_required_vars ODBC_LIBRARY)
224- if (NOT WIN32 )
263+ if (NOT CMAKE_SYSTEM_NAME STREQUAL "Windows" )
225264 list (APPEND _odbc_required_vars ODBC_INCLUDE_DIR)
226265endif ()
227266
@@ -262,6 +301,10 @@ if(ODBC_FOUND)
262301 add_library (ODBC::ODBC INTERFACE IMPORTED )
263302 set_target_properties (ODBC::ODBC PROPERTIES
264303 IMPORTED_LIBNAME "${ODBC_LIBRARY} " )
304+
305+ if (EXISTS "${ODBC_LIBRARY_DIR} " )
306+ target_link_directories (ODBC::ODBC INTERFACE "${ODBC_LIBRARY_DIR} " )
307+ endif ()
265308 endif ()
266309 set_target_properties (ODBC::ODBC PROPERTIES
267310 INTERFACE_INCLUDE_DIRECTORIES "${ODBC_INCLUDE_DIR} " )
@@ -305,7 +348,9 @@ if(ODBC_FOUND)
305348 elseif (PC_ODBC_MODULE_NAME STREQUAL "odbc" )
306349 set (ODBC_DRIVER "unixODBC" )
307350 endif ()
308- elseif (WIN32 )
351+ elseif (MINGW)
352+ set (ODBC_DRIVER "unixODBC" )
353+ elseif (CMAKE_SYSTEM_NAME STREQUAL "Windows" )
309354 set (ODBC_DRIVER "Windows" )
310355 endif ()
311356
0 commit comments