diff options
author | Adam Thompson <tngamer@gmail.com> | 2018-02-28 02:08:20 (GMT) |
---|---|---|
committer | Adam Thompson <tngamer@gmail.com> | 2018-03-07 14:29:57 (GMT) |
commit | 694fce19b64cd5a399921d823ef2db9a07d4018a (patch) | |
tree | 6e0eae6ae4553a11c1b6d89644715fb1556f4474 /Modules | |
parent | 4da456c7fee39e366aa707871fad84fbbd0a723e (diff) | |
download | CMake-694fce19b64cd5a399921d823ef2db9a07d4018a.zip CMake-694fce19b64cd5a399921d823ef2db9a07d4018a.tar.gz CMake-694fce19b64cd5a399921d823ef2db9a07d4018a.tar.bz2 |
FindGDAL: Handle gdal-config printing absolute path to library
FindGDAL uses GDAL's 'gdal-config' utility to obtain the path to GDAL's library
(on systems identified by CMake's UNIX variable). Older versions formatted this
information like that of dependent libraries:
-L/path/to/gdal/lib -lgdal[suffix]
Newer versions instead provide the full path to the library:
/path/to/gdal/lib/[prefix]gdal[suffix]
FindGDAL now supports both formats. Entries that don't start with '-L' or '-l'
are only considered if they are absolute paths that exist on disk.
Furthermore, libraries are only considered if the name contains 'gdal'
(checked case-insensitively).
Diffstat (limited to 'Modules')
-rw-r--r-- | Modules/FindGDAL.cmake | 46 |
1 files changed, 42 insertions, 4 deletions
diff --git a/Modules/FindGDAL.cmake b/Modules/FindGDAL.cmake index 2b940b0..ff2976e 100644 --- a/Modules/FindGDAL.cmake +++ b/Modules/FindGDAL.cmake @@ -66,11 +66,49 @@ if(UNIX) if(GDAL_CONFIG) exec_program(${GDAL_CONFIG} ARGS --libs OUTPUT_VARIABLE GDAL_CONFIG_LIBS) + if(GDAL_CONFIG_LIBS) - string(REGEX MATCHALL "-l[^ ]+" _gdal_dashl ${GDAL_CONFIG_LIBS}) - string(REPLACE "-l" "" _gdal_lib "${_gdal_dashl}") - string(REGEX MATCHALL "-L[^ ]+" _gdal_dashL ${GDAL_CONFIG_LIBS}) - string(REPLACE "-L" "" _gdal_libpath "${_gdal_dashL}") + # treat the output as a command line and split it up + separate_arguments(args NATIVE_COMMAND "${GDAL_CONFIG_LIBS}") + + # only consider libraries whose name matches this pattern + set(name_pattern "[gG][dD][aA][lL]") + + # consider each entry as a possible library path, name, or parent directory + foreach(arg IN LISTS args) + # library name + if("${arg}" MATCHES "^-l(.*)$") + set(lib "${CMAKE_MATCH_1}") + + # only consider libraries whose name matches the expected pattern + if("${lib}" MATCHES "${name_pattern}") + list(APPEND _gdal_lib "${lib}") + endif() + # library search path + elseif("${arg}" MATCHES "^-L(.*)$") + list(APPEND _gdal_libpath "${CMAKE_MATCH_1}") + # assume this is a full path to a library + elseif(IS_ABSOLUTE "${arg}" AND EXISTS "${arg}") + # extract the file name + get_filename_component(lib "${arg}" NAME) + + # only consider libraries whose name matches the expected pattern + if(NOT "${lib}" MATCHES "${name_pattern}") + continue() + endif() + + # extract the file directory + get_filename_component(dir "${arg}" DIRECTORY) + + # remove library prefixes/suffixes + string(REGEX REPLACE "^(${CMAKE_SHARED_LIBRARY_PREFIX}|${CMAKE_STATIC_LIBRARY_PREFIX})" "" lib "${lib}") + string(REGEX REPLACE "(${CMAKE_SHARED_LIBRARY_SUFFIX}|${CMAKE_STATIC_LIBRARY_SUFFIX})$" "" lib "${lib}") + + # use the file name and directory as hints + list(APPEND _gdal_libpath "${dir}") + list(APPEND _gdal_lib "${lib}") + endif() + endforeach() endif() endif() endif() |