Skip to content

Commit

Permalink
Update FindODBC with module proposed to CMake
Browse files Browse the repository at this point in the history
This is copy of FindODBC I proposed to CMake
https://gitlab.kitware.com/cmake/cmake/merge_requests/2069
  • Loading branch information
mloskot committed May 19, 2018
1 parent f4927b2 commit 3849b50
Showing 1 changed file with 236 additions and 59 deletions.
295 changes: 236 additions & 59 deletions cmake/modules/FindODBC.cmake
Original file line number Diff line number Diff line change
@@ -1,65 +1,242 @@
#
# Find the ODBC driver manager includes and library.
#
# ODBC is an open standard for connecting to different databases in a
# semi-vendor-independent fashion. First you install the ODBC driver
# manager. Then you need a driver for each separate database you want
# to connect to (unless a generic one works). VTK includes neither
# the driver manager nor the vendor-specific drivers: you have to find
# those yourself.
#
# This module defines
# ODBC_INCLUDE_DIR, where to find sql.h
# ODBC_LIBRARIES, the libraries to link against to use ODBC
# ODBC_FOUND. If false, you cannot build anything that requires MySQL.

# also defined, but not for general use is
# ODBC_LIBRARY, where to find the ODBC driver manager library.

set(ODBC_FOUND FALSE)

find_path(ODBC_INCLUDE_DIR sqlext.h
/usr/include
/usr/include/odbc
/usr/local/include
/usr/local/include/odbc
/usr/local/odbc/include
"C:/Program Files (x86)/Windows Kits/8.0/include/um"
"C:/Program Files (x86)/Microsoft SDKs/Windows/v7.0A/Include"
"C:/Program Files/ODBC/include"
"C:/Program Files/Microsoft SDKs/Windows/v7.0/include"
"C:/Program Files/Microsoft SDKs/Windows/v6.0a/include"
"C:/ODBC/include"
DOC "Specify the directory containing sql.h."
)

if(MSVC)
# msvc knows where to find sdk libs
set(ODBC_LIBRARY odbc32)
else()
find_library(ODBC_LIBRARY
NAMES iodbc odbc odbcinst odbc32
PATHS
/usr/lib
/usr/lib/odbc
/usr/local/lib
/usr/local/lib/odbc
/usr/local/odbc/lib
"C:/Program Files (x86)/Windows Kits/8.0/Lib/win8/um/x86/"
"C:/Program Files (x86)/Microsoft SDKs/Windows/v7.0A/Lib"
"C:/Program Files/ODBC/lib"
"C:/ODBC/lib/debug"
DOC "Specify the ODBC driver manager library here."
)
endif()
# Distributed under the OSI-approved BSD 3-Clause License. See accompanying
# file Copyright.txt or https://cmake.org/licensing for details.

#[=======================================================================[.rst:
FindODBC
--------
Find the ODBC include directory and library.
Use this module by invoking find_package with the form::
.. code-block:: cmake
find_package(ODBC
[REQUIRED] # Fail with error if ODBC is not found
)
On Windows, when building with Visual Studio, this module assumes the ODBC
library is provided by the available Windows SDK.
On Unix, this module allows to search for ODBC library provided by
unixODBC or iODBC implementations of ODBC API.
This module reads hint about location of the config program:
.. variable:: ODBC_CONFIG
Location of odbc_config or iodbc-config program
Otherwise, this module tries to find the config program,
first from unixODBC, then from iODBC.
If no config program found, this module searches for ODBC header
and library in list of known locations.
Imported targets
^^^^^^^^^^^^^^^^
This module defines the following :prop_tgt:`IMPORTED` targets:
.. variable:: ODBC::ODBC
Imported target for using the ODBC library, if found.
Result variables
^^^^^^^^^^^^^^^^
.. variable:: ODBC_FOUND
Set to true if ODBC library found, otherwise false or undefined.
.. variable:: ODBC_INCLUDE_DIRS
Paths to include directories listed in one variable for use by ODBC client.
May be empty on Windows, where the include directory corresponding to the
expected Windows SDK is already available in the compilation environment.
.. variable:: ODBC_LIBRARIES
Paths to libraries to linked against to use ODBC.
May just a library name on Windows, where the library directory corresponding
to the expected Windows SDK is already available in the compilation environment.
.. variable:: ODBC_CONFIG
Path to unixODBC or iODBC config program, if found or specified.
Cache variables
^^^^^^^^^^^^^^^
For users who wish to edit and control the module behavior, this module
reads hints about search locations from the following variables::
.. variable:: ODBC_INCLUDE_DIR
Path to ODBC include directory with ``sql.h`` header.
.. variable:: ODBC_LIBRARY
Path to ODBC library to be linked.
NOTE: The variables above should not usually be used in CMakeLists.txt files!
if(ODBC_LIBRARY)
if(ODBC_INCLUDE_DIR OR HAVE_SQLEXT_H)
set( ODBC_FOUND 1 )
Limitations
^^^^^^^^^^^
On Windows, this module does not search for iODBC.
On Unix, there is no way to prefer unixODBC over iODBC, or vice versa,
other than providing the config program location using the ``ODBC_CONFIG``.
This module does not allow to search for a specific ODBC driver.
#]=======================================================================]

### Try Windows Kits ##########################################################
if(WIN32)
# List names of ODBC libraries on Windows
set(ODBC_LIBRARY odbc32.lib)
set(_odbc_lib_names odbc32;)

# List additional libraries required to use ODBC library
if(MSVC OR CMAKE_CXX_COMPILER_ID MATCHES "Intel")
set(_odbc_required_libs_names odbccp32;ws2_32)
elseif(MINGW)
set(_odbc_required_libs_names odbccp32)
endif()
endif()

set(ODBC_LIBRARIES ${ODBC_LIBRARY})
### Try unixODBC or iODBC config program ######################################
if (UNIX AND NOT ODBC_CONFIG)
find_program(ODBC_CONFIG
NAMES odbc_config iodbc-config
DOC "Path to unixODBC or iODBC config program")
endif()

if (UNIX AND ODBC_CONFIG)
# unixODBC and iODBC accept unified command line options
execute_process(COMMAND ${ODBC_CONFIG} --cflags
OUTPUT_VARIABLE _cflags OUTPUT_STRIP_TRAILING_WHITESPACE)
execute_process(COMMAND ${ODBC_CONFIG} --libs
OUTPUT_VARIABLE _libs OUTPUT_STRIP_TRAILING_WHITESPACE)

# Collect paths of include directories from CFLAGS
separate_arguments(_cflags NATIVE_COMMAND "${_cflags}")
foreach(arg IN LISTS _cflags)
if("${arg}" MATCHES "^-I(.*)$")
list(APPEND _odbc_include_paths "${CMAKE_MATCH_1}")
endif()
endforeach()
unset(_cflags)

# Collect paths of library names and directories from LIBS
separate_arguments(_libs NATIVE_COMMAND "${_libs}")
foreach(arg IN LISTS _libs)
if("${arg}" MATCHES "^-L(.*)$")
list(APPEND _odbc_lib_paths "${CMAKE_MATCH_1}")
elseif("${arg}" MATCHES "^-l(.*)$")
set(_lib_name ${CMAKE_MATCH_1})
string(REGEX MATCH "odbc" _is_odbc ${_lib_name})
if(_is_odbc)
list(APPEND _odbc_lib_names ${_lib_name})
else()
list(APPEND _odbc_required_libs_names ${_lib_name})
endif()
unset(_lib_name)
endif()
endforeach()
unset(_libs)
endif()

### Try unixODBC or iODBC in include/lib filesystems ##########################
if (UNIX AND NOT ODBC_CONFIG)
# List names of both ODBC libraries, unixODBC and iODBC
set(_odbc_lib_names odbc;iodbc;unixodbc;)

set(_odbc_include_paths
/usr/local/odbc/include)

set(_odbc_lib_paths
/usr/local/odbc/lib)
endif()

# DEBUG
#message("ODBC_CONFIG=${ODBC_CONFIG}")
#message("_odbc_include_hints=${_odbc_include_hints}")
#message("_odbc_include_paths=${_odbc_include_paths}")
#message("_odbc_lib_paths=${_odbc_lib_paths}")
#message("_odbc_lib_names=${_odbc_lib_names}")

mark_as_advanced(ODBC_FOUND ODBC_LIBRARY ODBC_EXTRA_LIBRARIES ODBC_INCLUDE_DIR)
### Find include directories ##################################################
find_path(ODBC_INCLUDE_DIR
NAMES sql.h
HINTS ${_odbc_include_hints}
PATHS ${_odbc_include_paths})

if(NOT ODBC_INCLUDE_DIR AND WIN32)
set(ODBC_INCLUDE_DIR "")
endif()

### Find libraries ############################################################
if(NOT ODBC_LIBRARY)
find_library(ODBC_LIBRARY
NAMES ${_odbc_lib_names}
PATHS ${_odbc_lib_paths}
PATH_SUFFIXES odbc)

foreach(_lib IN LISTS _odbc_required_libs_names)
find_library(_lib_path
NAMES ${_lib}
PATHS ${_odbc_lib_paths} # system parths or collected from ODBC_CONFIG
PATH_SUFFIXES odbc)
if (_lib_path)
list(APPEND _odbc_required_libs_paths ${_lib_path})
endif()
unset(_lib_path CACHE)
endforeach()

unset(_odbc_lib_names)
unset(_odbc_lib_paths)
unset(_odbc_required_libs_names)
endif()

### Set result variables ######################################################
set(REQUIRED_VARS ODBC_LIBRARY)
if(NOT WIN32)
list(APPEND REQUIRED_VARS ODBC_INCLUDE_DIR)
endif()

include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(ODBC DEFAULT_MSG ${REQUIRED_VARS})

mark_as_advanced(FORCE ODBC_LIBRARY ODBC_INCLUDE_DIR)

if(ODBC_CONFIG)
mark_as_advanced(FORCE ODBC_CONFIG)
endif()

set(ODBC_INCLUDE_DIRS ${ODBC_INCLUDE_DIR})
list(APPEND ODBC_LIBRARIES ${ODBC_LIBRARY})
list(APPEND ODBC_LIBRARIES ${_odbc_required_libs_paths})

### Import targets ############################################################
if(ODBC_FOUND)
if(NOT TARGET ODBC::ODBC)
if(IS_ABSOLUTE "${ODBC_LIBRARY}")
add_library(ODBC::ODBC UNKNOWN IMPORTED)
set_target_properties(ODBC::ODBC PROPERTIES
IMPORTED_LINK_INTERFACE_LANGUAGES "C"
IMPORTED_LOCATION "${ODBC_LIBRARY}")
else()
add_library(ODBC::ODBC INTERFACE IMPORTED)
set_target_properties(ODBC::ODBC PROPERTIES
IMPORTED_LIBNAME "${ODBC_LIBRARY}")
endif()
set_target_properties(ODBC::ODBC PROPERTIES
INTERFACE_INCLUDE_DIRECTORIES "${ODBC_INCLUDE_DIR}")

if(_odbc_required_libs_paths)
set_property(TARGET ODBC::ODBC APPEND PROPERTY
INTERFACE_LINK_LIBRARIES "${_odbc_required_libs_paths}")
endif()
endif()
endif()

unset(_odbc_required_libs_paths)

0 comments on commit 3849b50

Please sign in to comment.