Skip to content

Commit

Permalink
Modernize CMake include and linking resolution. (#555)
Browse files Browse the repository at this point in the history
* chore: Modernise cmake file.

In modern cmake, linking a target to a library with target_link_libraries
automatically sets the include paths.
This modern approach also resolves issues with transitive dependencies
(e.g. protobuf using abseil).

* chore: Update the required minimum CMake to a lower working value.

* fixed MSVC SQLite3 issue

* Use an alias, avoiding second IF(MSVC)
  • Loading branch information
FunMiles authored Oct 21, 2023
1 parent ba3fc62 commit 5b6bb37
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 17 deletions.
35 changes: 18 additions & 17 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.0)
cmake_minimum_required(VERSION 3.18)

if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE "Release")
Expand All @@ -18,9 +18,6 @@ if (POLICY CMP0074)
cmake_policy(SET CMP0074 NEW)
endif()

include_directories(include)
include_directories(${CMAKE_BINARY_DIR}) # for generated files

IF (TILEMAKER_BUILD_STATIC)
MESSAGE (STATUS "Staticly linking with Boost")
SET (Boost_USE_STATIC_LIBS TRUE)
Expand All @@ -35,16 +32,12 @@ IF ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC")
ENDIF ()

find_package(Boost 1.66 REQUIRED COMPONENTS system filesystem program_options iostreams)
include_directories(SYSTEM ${Boost_INCLUDE_DIRS})

find_package(Protobuf REQUIRED)
include_directories(${PROTOBUF_INCLUDE_DIR})
find_package(Protobuf CONFIG REQUIRED)

find_package(libshp REQUIRED)
include_directories(${LIBSHP_INCLUDE_DIR})

find_package(Rapidjson REQUIRED)
include_directories(${RAPIDJSON_INCLUDEDIR})

find_package(Lua)

Expand All @@ -57,7 +50,6 @@ else()
endif()

find_package(ZLIB REQUIRED)
include_directories(${ZLIB_INCLUDE_DIR})

set(CMAKE_CXX_STANDARD 17)

Expand All @@ -70,14 +62,19 @@ endif()

if(MSVC)
find_package(unofficial-sqlite3 CONFIG REQUIRED)
add_library(SQLite::SQLite3 ALIAS unofficial::sqlite3::sqlite3)
add_definitions(-D_USE_MATH_DEFINES -DWIN32_LEAN_AND_MEAN -DNOGDI)
set(THREAD_LIB "")
else()
find_package(SQLite3 REQUIRED)
include_directories(${SQLITE3_INCLUDE_DIRS})
set(THREAD_LIB pthread)
endif()

if(NOT PROTOBUF_PROTOC_EXECUTABLE)
set (PROTOBUF_PROTOC_EXECUTABLE "protobuf::protoc")
endif()


ADD_CUSTOM_COMMAND(OUTPUT vector_tile.pb.cc vector_tile.pb.h
COMMAND ${PROTOBUF_PROTOC_EXECUTABLE}
ARGS --cpp_out ${CMAKE_BINARY_DIR} -I ${CMAKE_SOURCE_DIR}/include ${CMAKE_SOURCE_DIR}/include/vector_tile.proto)
Expand Down Expand Up @@ -107,19 +104,23 @@ file(GLOB tilemaker_src_files
src/write_geometry.cpp
)
add_executable(tilemaker vector_tile.pb.cc osmformat.pb.cc ${tilemaker_src_files})
target_link_libraries(tilemaker ${PROTOBUF_LIBRARY} ${LIBSHP_LIBRARIES} ${SQLITE3_LIBRARIES} ${LUAJIT_LIBRARY} ${LUA_LIBRARIES} ${ZLIB_LIBRARY} ${THREAD_LIB} ${CMAKE_DL_LIBS}
Boost::system Boost::filesystem Boost::program_options Boost::iostreams)
target_include_directories(tilemaker PRIVATE include)
target_include_directories(tilemaker PRIVATE ${CMAKE_BINARY_DIR}) # for generated files
target_link_libraries(tilemaker
${LUA_LIBRARIES}
protobuf::libprotobuf
shapelib::shp
SQLite::SQLite3
ZLIB::ZLIB
Rapidjson::rapidjson
Boost::system Boost::filesystem Boost::program_options Boost::iostreams)

include(CheckCxxAtomic)
if(NOT HAVE_CXX11_ATOMIC)
string(APPEND CMAKE_CXX_STANDARD_LIBRARIES
" ${LIBATOMIC_LINK_FLAGS}")
endif()

if(MSVC)
target_link_libraries(tilemaker unofficial::sqlite3::sqlite3)
endif()

install(FILES docs/man/tilemaker.1 DESTINATION share/man/man1)

install(TARGETS tilemaker RUNTIME DESTINATION bin)
3 changes: 3 additions & 0 deletions cmake/FindRapidjson.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@ if(RAPIDJSON_FOUND)
message(STATUS "Found rapidjson C++ extra compilation flags: ${RAPIDJSON_CXX_FLAGS}")
endif()
endif()
add_library(Rapidjson::rapidjson INTERFACE IMPORTED)
set_target_properties(Rapidjson::rapidjson PROPERTIES
INTERFACE_INCLUDE_DIRECTORIES ${RAPIDJSON_INCLUDE_DIRS})
elseif(Rapidjson_FIND_REQUIRED)
message(FATAL_ERROR "Could not find rapidjson")
else()
Expand Down
8 changes: 8 additions & 0 deletions cmake/FindSQLite3.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,12 @@ else (SQLITE3_LIBRARIES AND SQLITE3_INCLUDE_DIRS)
mark_as_advanced(SQLITE3_INCLUDE_DIRS SQLITE3_LIBRARIES)

endif (SQLITE3_LIBRARIES AND SQLITE3_INCLUDE_DIRS)
if (SQLITE3_FOUND)
add_library(SQLite::SQLite3 UNKNOWN IMPORTED)
set_target_properties(SQLite::SQLite3 PROPERTIES
INTERFACE_INCLUDE_DIRECTORIES ${SQLITE3_INCLUDE_DIRS})
set_property(TARGET SQLite::SQLite3 APPEND PROPERTY
IMPORTED_LOCATION "${SQLITE3_LIBRARIES}")
endif ()


7 changes: 7 additions & 0 deletions cmake/Findlibshp.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,10 @@ else(LIBSHP_INCLUDE_DIR AND LIBSHP_LIBRARIES)

mark_as_advanced(LIBSHP_INCLUDE_DIR LIBSHP_LIBRARIES)
endif(LIBSHP_INCLUDE_DIR AND LIBSHP_LIBRARIES)
if (LIBSHP_FOUND)
add_library(shapelib::shp UNKNOWN IMPORTED)
set_target_properties(shapelib::shp PROPERTIES
INTERFACE_INCLUDE_DIRECTORIES ${LIBSHP_INCLUDE_DIR})
set_property(TARGET shapelib::shp APPEND PROPERTY
IMPORTED_LOCATION "${LIBSHP_LIBRARIES}")
endif()

0 comments on commit 5b6bb37

Please sign in to comment.