Skip to content

Commit

Permalink
Merge pull request qgis#54626 from m-kuhn/cmake-spatialite
Browse files Browse the repository at this point in the history
[cmake] Prefer pkgconfig for spatialite
  • Loading branch information
m-kuhn authored Sep 17, 2023
2 parents a9a19d1 + b9d0b4c commit f67e339
Show file tree
Hide file tree
Showing 11 changed files with 80 additions and 94 deletions.
141 changes: 76 additions & 65 deletions cmake/FindSpatiaLite.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -11,69 +11,80 @@
# SPATIALITE_INCLUDE_DIR
# SPATIALITE_LIBRARY

# This macro checks if the symbol exists
include(CheckLibraryExists)
find_package(PkgConfig REQUIRED)
pkg_search_module(PC_SPATIALITE IMPORTED_TARGET spatialite)


# FIND_PATH and FIND_LIBRARY normally search standard locations
# before the specified paths. To search non-standard paths first,
# FIND_* is invoked first with specified paths and NO_DEFAULT_PATH
# and then again with no specified paths to search the default
# locations. When an earlier FIND_* succeeds, subsequent FIND_*s
# searching for the same item do nothing.

# try to use sqlite framework on mac
# want clean framework path, not unix compatibility path
IF (APPLE AND NOT IOS)
IF (CMAKE_FIND_FRAMEWORK MATCHES "FIRST"
OR CMAKE_FRAMEWORK_PATH MATCHES "ONLY"
OR NOT CMAKE_FIND_FRAMEWORK)
SET (CMAKE_FIND_FRAMEWORK_save ${CMAKE_FIND_FRAMEWORK} CACHE STRING "" FORCE)
SET (CMAKE_FIND_FRAMEWORK "ONLY" CACHE STRING "" FORCE)
FIND_PATH(SPATIALITE_INCLUDE_DIR SQLite3/spatialite.h)
# if no SpatiaLite header, we don't want SQLite find below to succeed
IF (SPATIALITE_INCLUDE_DIR)
FIND_LIBRARY(SPATIALITE_LIBRARY SQLite3)
# FIND_PATH doesn't add "Headers" for a framework
SET (SPATIALITE_INCLUDE_DIR ${SPATIALITE_LIBRARY}/Headers CACHE PATH "Path to a file." FORCE)
ENDIF (SPATIALITE_INCLUDE_DIR)
SET (CMAKE_FIND_FRAMEWORK ${CMAKE_FIND_FRAMEWORK_save} CACHE STRING "" FORCE)
ENDIF ()
ENDIF (APPLE AND NOT IOS)

FIND_PATH(SPATIALITE_INCLUDE_DIR spatialite.h
/usr/include
"$ENV{INCLUDE}"
"$ENV{LIB_DIR}/include"
"$ENV{LIB_DIR}/include/spatialite"
)

FIND_LIBRARY(SPATIALITE_LIBRARY NAMES spatialite_i spatialite PATHS
/usr/lib
$ENV{LIB}
$ENV{LIB_DIR}/lib
)

IF (SPATIALITE_INCLUDE_DIR AND SPATIALITE_LIBRARY)
SET(SPATIALITE_FOUND TRUE)
ENDIF (SPATIALITE_INCLUDE_DIR AND SPATIALITE_LIBRARY)


IF (SPATIALITE_FOUND)

IF (NOT SPATIALITE_FIND_QUIETLY)
MESSAGE(STATUS "Found SpatiaLite: ${SPATIALITE_LIBRARY}")
ENDIF (NOT SPATIALITE_FIND_QUIETLY)

IF(APPLE)
# no extra LDFLAGS used in link test, may fail in OS X SDK
SET(CMAKE_REQUIRED_LIBRARIES "-F/Library/Frameworks" ${CMAKE_REQUIRED_LIBRARIES})
ENDIF(APPLE)

ELSE (SPATIALITE_FOUND)

IF (SPATIALITE_FIND_REQUIRED)
MESSAGE(FATAL_ERROR "Could not find SpatiaLite. Include: ${SPATIALITE_INCLUDE_DIR} Library: ${SPATIALITE_LIBRARY}")
ENDIF (SPATIALITE_FIND_REQUIRED)

ENDIF (SPATIALITE_FOUND)
if(PC_SPATIALITE_FOUND)
add_library(spatialite::spatialite ALIAS PkgConfig::PC_SPATIALITE)
else()
# This macro checks if the symbol exists
include(CheckLibraryExists)


# FIND_PATH and FIND_LIBRARY normally search standard locations
# before the specified paths. To search non-standard paths first,
# FIND_* is invoked first with specified paths and NO_DEFAULT_PATH
# and then again with no specified paths to search the default
# locations. When an earlier FIND_* succeeds, subsequent FIND_*s
# searching for the same item do nothing.

# try to use sqlite framework on mac
# want clean framework path, not unix compatibility path
IF (APPLE AND NOT IOS)
IF (CMAKE_FIND_FRAMEWORK MATCHES "FIRST"
OR CMAKE_FRAMEWORK_PATH MATCHES "ONLY"
OR NOT CMAKE_FIND_FRAMEWORK)
SET (CMAKE_FIND_FRAMEWORK_save ${CMAKE_FIND_FRAMEWORK} CACHE STRING "" FORCE)
SET (CMAKE_FIND_FRAMEWORK "ONLY" CACHE STRING "" FORCE)
FIND_PATH(SPATIALITE_INCLUDE_DIR SQLite3/spatialite.h)
# if no SpatiaLite header, we don't want SQLite find below to succeed
IF (SPATIALITE_INCLUDE_DIR)
FIND_LIBRARY(SPATIALITE_LIBRARY SQLite3)
# FIND_PATH doesn't add "Headers" for a framework
SET (SPATIALITE_INCLUDE_DIR ${SPATIALITE_LIBRARY}/Headers CACHE PATH "Path to a file." FORCE)
ENDIF (SPATIALITE_INCLUDE_DIR)
SET (CMAKE_FIND_FRAMEWORK ${CMAKE_FIND_FRAMEWORK_save} CACHE STRING "" FORCE)
ENDIF ()
ENDIF (APPLE AND NOT IOS)

FIND_PATH(SPATIALITE_INCLUDE_DIR spatialite.h
/usr/include
"$ENV{INCLUDE}"
"$ENV{LIB_DIR}/include"
"$ENV{LIB_DIR}/include/spatialite"
)

FIND_LIBRARY(SPATIALITE_LIBRARY NAMES spatialite_i spatialite PATHS
/usr/lib
$ENV{LIB}
$ENV{LIB_DIR}/lib
)

IF (SPATIALITE_INCLUDE_DIR AND SPATIALITE_LIBRARY)
SET(SPATIALITE_FOUND TRUE)
ENDIF (SPATIALITE_INCLUDE_DIR AND SPATIALITE_LIBRARY)


IF (SPATIALITE_FOUND)
add_library(spatialite::spatialite UNKNOWN IMPORTED)
target_link_libraries(spatialite::spatialite INTERFACE ${SPATIALITE_LIBRARY})
target_include_directories(spatialite::spatialite INTERFACE ${SPATIALITE_INCLUDE_DIR})
set_target_properties(spatialite::spatialite PROPERTIES IMPORTED_LOCATION ${SPATIALITE_LIBRARY})

IF (NOT SPATIALITE_FIND_QUIETLY)
MESSAGE(STATUS "Found SpatiaLite: ${SPATIALITE_LIBRARY}")
ENDIF (NOT SPATIALITE_FIND_QUIETLY)

IF(APPLE)
# no extra LDFLAGS used in link test, may fail in OS X SDK
SET(CMAKE_REQUIRED_LIBRARIES "-F/Library/Frameworks" ${CMAKE_REQUIRED_LIBRARIES})
ENDIF(APPLE)

ELSE (SPATIALITE_FOUND)

IF (SPATIALITE_FIND_REQUIRED)
MESSAGE(FATAL_ERROR "Could not find SpatiaLite. Include: ${SPATIALITE_INCLUDE_DIR} Library: ${SPATIALITE_LIBRARY}")
ENDIF (SPATIALITE_FIND_REQUIRED)

ENDIF (SPATIALITE_FOUND)
endif()
3 changes: 1 addition & 2 deletions external/qspatialite/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ add_library(qsqlspatialite SHARED ${QSQLSPATIALITE_SRC})
target_link_libraries(qsqlspatialite
${Qt5Core_LIBRARIES}
${Qt5Sql_LIBRARIES}
${SQLITE3_LIBRARY}
${SPATIALITE_LIBRARY}
spatialite::spatialite
qgis_core
)

Expand Down
1 change: 0 additions & 1 deletion src/analysis/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,6 @@ if (WITH_PDAL AND PDAL_2_5_OR_HIGHER)
)
endif()

include_directories(SYSTEM ${SPATIALITE_INCLUDE_DIR})
include_directories(SYSTEM ${SPATIALINDEX_INCLUDE_DIR})
include_directories(SYSTEM ${SQLITE3_INCLUDE_DIR})
include_directories(BEFORE raster)
Expand Down
8 changes: 1 addition & 7 deletions src/core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2262,12 +2262,6 @@ target_include_directories(qgis_core SYSTEM PUBLIC
${EXIV2_INCLUDE_DIR}
)

if (WITH_SPATIALITE)
target_include_directories(qgis_core SYSTEM PUBLIC
${SPATIALITE_INCLUDE_DIR}
)
endif()

target_include_directories(qgis_core PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}
${CMAKE_CURRENT_BINARY_DIR}
Expand Down Expand Up @@ -2444,7 +2438,7 @@ if (NOT IOS)
endif()

if (WITH_SPATIALITE)
target_link_libraries(qgis_core ${SPATIALITE_LIBRARY})
target_link_libraries(qgis_core spatialite::spatialite)
endif()

if (BUILD_WITH_QT6)
Expand Down
6 changes: 0 additions & 6 deletions src/plugins/offline_editing/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,6 @@ target_compile_features(plugin_offlineediting PRIVATE cxx_std_17)
target_link_libraries(plugin_offlineediting
qgis_core
qgis_gui
${SPATIALITE_LIBRARY}
)

include_directories(SYSTEM
${SPATIALITE_INCLUDE_DIR}
${SQLITE3_INCLUDE_DIR}
)

include_directories(
Expand Down
4 changes: 2 additions & 2 deletions src/providers/spatialite/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ target_include_directories(provider_spatialite_a PUBLIC

target_link_libraries(provider_spatialite_a
qgis_core
${SPATIALITE_LIBRARY}
spatialite::spatialite
)

# require c++17
Expand Down Expand Up @@ -95,7 +95,7 @@ else()

target_link_libraries(provider_spatialite
qgis_core
${SPATIALITE_LIBRARY}
spatialite::spatialite
)

if (WITH_GUI)
Expand Down
4 changes: 0 additions & 4 deletions src/providers/virtual/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@ target_include_directories(provider_virtuallayer_a PUBLIC

target_link_libraries(provider_virtuallayer_a
qgis_core
${SQLITE3_LIBRARY}
${SPATIALITE_LIBRARY}
)

# require c++17
Expand Down Expand Up @@ -97,8 +95,6 @@ else()
qgis_core
${QT_VERSION_BASE}::Core
${QT_VERSION_BASE}::Widgets
${SQLITE3_LIBRARY}
${SPATIALITE_LIBRARY}
)

if (WITH_GUI)
Expand Down
2 changes: 0 additions & 2 deletions src/quickgui/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ include_directories(SYSTEM
${SPATIALINDEX_INCLUDE_DIR}
${GEOS_INCLUDE_DIR}
${EXPAT_INCLUDE_DIR}
${SQLITE3_INCLUDE_DIR}
${SPATIALITE_INCLUDE_DIR}
${QCA_INCLUDE_DIR}
${QTKEYCHAIN_INCLUDE_DIR}
)
Expand Down
1 change: 0 additions & 1 deletion src/quickgui/plugin/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ include_directories(SYSTEM
${GEOS_INCLUDE_DIR}
${EXPAT_INCLUDE_DIR}
${SQLITE3_INCLUDE_DIR}
${SPATIALITE_INCLUDE_DIR}
${QCA_INCLUDE_DIR}
${QTKEYCHAIN_INCLUDE_DIR}
)
Expand Down
2 changes: 0 additions & 2 deletions tests/src/quickgui/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ include_directories(SYSTEM
${SPATIALINDEX_INCLUDE_DIR}
${GDAL_INCLUDE_DIR}
${EXPAT_INCLUDE_DIR}
${SQLITE3_INCLUDE_DIR}
${SPATIALITE_INCLUDE_DIR}
${QCA_INCLUDE_DIR}
${QTKEYCHAIN_INCLUDE_DIR}
)
Expand Down
2 changes: 0 additions & 2 deletions tests/src/quickgui/app/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ include_directories(SYSTEM
${SPATIALINDEX_INCLUDE_DIR}
${GEOS_INCLUDE_DIR}
${EXPAT_INCLUDE_DIR}
${SQLITE3_INCLUDE_DIR}
${SPATIALITE_INCLUDE_DIR}
${QCA_INCLUDE_DIR}
${QTKEYCHAIN_INCLUDE_DIR}
)
Expand Down

0 comments on commit f67e339

Please sign in to comment.