From 747eb3a4401523983144eb1f03b3a46f236a7c73 Mon Sep 17 00:00:00 2001 From: kiibimees <93144752+kiibimees@users.noreply.github.com> Date: Mon, 14 Feb 2022 18:18:29 +0200 Subject: [PATCH] [qt] Build improvements and better handling of map settings (#197) * QMapboxGLSettings functions for changing default options and getting default styles * [qt] allow to build without widgets and other cmake improvements * [qt] Install include/mbgl for development * [qt] library static/shared install+export * [qt] Add missing mbgl-compiler-options dependency for 'qmapboxgl' * [qt] Raise CMAKE_OSX_DEPLOYMENT_TARGET from 10.13 to 13.0 * [qt] Add Qt::Sql dependency also to the final library. Build qmapboxgl as OBJECT lib inside QtLocation plugin. * Remove Qt::Sql dependency from 'qmapboxgl'. Not really needed. * Set CMake config files path back to ${CMAKE_INSTALL_LIBDIR}/cmake/QMapboxGL/ * [qt] Set CMAKE_OSX_DEPLOYMENT_TARGET only if it is not yet set * [qt] Support CMake v3.11 and earlier versions * [qt] Comment out 'iOS_static' from CI until it is fixed. Co-authored-by: Rinigus --- .github/workflows/qt-ci.yml | 12 +-- CMakeLists.txt | 14 ++- platform/qt/QMapboxGLConfig.cmake.in | 15 +++ platform/qt/include/qmapbox.hpp | 10 +- platform/qt/include/qmapboxgl.hpp | 11 ++ platform/qt/qt.cmake | 145 ++++++++++++++++----------- platform/qt/src/qmapboxgl.cpp | 37 +++++++ vendor/csscolorparser.cmake | 10 +- vendor/nunicode.cmake | 10 +- vendor/parsedate.cmake | 10 +- 10 files changed, 192 insertions(+), 82 deletions(-) create mode 100644 platform/qt/QMapboxGLConfig.cmake.in diff --git a/.github/workflows/qt-ci.yml b/.github/workflows/qt-ci.yml index 955bf4df3..55144e2bf 100644 --- a/.github/workflows/qt-ci.yml +++ b/.github/workflows/qt-ci.yml @@ -38,11 +38,11 @@ jobs: static: ON qt: 5.15.2 qt_target: desktop - - name: iOS_static - os: macos-latest - static: ON - qt: 5.15.2 - qt_target: ios +# - name: iOS_static +# os: macos-latest +# static: ON +# qt: 5.15.2 +# qt_target: ios - name: macOS os: macos-latest static: ON @@ -133,7 +133,7 @@ jobs: mkdir build && cd build cmake ../source/ \ -DCMAKE_TOOLCHAIN_FILE=../source/platform/ios/platform/ios/toolchain.cmake \ - -G Xcode \ + -G XCode \ -DPLATFORM=OS64COMBINED \ -DDEPLOYMENT_TARGET=12.0 \ -DENABLE_BITCODE=ON \ diff --git a/CMakeLists.txt b/CMakeLists.txt index 93c5f883d..01f62a1a8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,14 +3,15 @@ cmake_minimum_required(VERSION 3.10 FATAL_ERROR) option(MBGL_WITH_CORE_ONLY "Build only the core bits, no platform code" OFF) option(MBGL_WITH_COVERAGE "Enable code coverage collection" OFF) option(MBGL_WITH_QT "Build Mapbox GL Qt bindings" OFF) +option(MBGL_QT_STATIC "Build Mapbox GL Qt bindings staticly" OFF) option(MBGL_WITH_SANITIZER "Use [address|thread|undefined] here" OFF) option(MBGL_WITH_RTTI "Compile with runtime type information" OFF) option(MBGL_WITH_OPENGL "Build with OpenGL renderer" ON) option(MBGL_WITH_EGL "Build with EGL renderer" OFF) option(MBGL_WITH_WERROR "Make all compilation warnings errors" ON) -if (MBGL_WITH_QT) - set(CMAKE_OSX_DEPLOYMENT_TARGET 10.13) +if (MBGL_WITH_QT AND NOT CMAKE_OSX_DEPLOYMENT_TARGET) + set(CMAKE_OSX_DEPLOYMENT_TARGET 13.0) endif() project("Mapbox GL Native" LANGUAGES CXX C) @@ -97,8 +98,13 @@ set(CMAKE_INTERPROCEDURAL_OPTIMIZATION_MINSIZEREL ON) set(CMAKE_POSITION_INDEPENDENT_CODE ON) set(CMAKE_VISIBILITY_INLINES_HIDDEN 1) -add_library( - mbgl-core STATIC +if(MBGL_QT_STATIC) + add_library(mbgl-core OBJECT) +else() + add_library(mbgl-core STATIC) +endif() +target_sources( + mbgl-core PRIVATE ${PROJECT_SOURCE_DIR}/include/mbgl/actor/actor.hpp ${PROJECT_SOURCE_DIR}/include/mbgl/actor/actor_ref.hpp ${PROJECT_SOURCE_DIR}/include/mbgl/actor/aspiring_actor.hpp diff --git a/platform/qt/QMapboxGLConfig.cmake.in b/platform/qt/QMapboxGLConfig.cmake.in new file mode 100644 index 000000000..f1ed73a38 --- /dev/null +++ b/platform/qt/QMapboxGLConfig.cmake.in @@ -0,0 +1,15 @@ +@PACKAGE_INIT@ + +include(CMakeFindDependencyMacro) +find_dependency(Qt@QT_VERSION_MAJOR@ + COMPONENTS Gui @REQUIRED_QT_VERSION@) +find_dependency(Qt@QT_VERSION_MAJOR@ + COMPONENTS Network @REQUIRED_QT_VERSION@) + +if(@MBGL_WITH_QT_HEADLESS@ OR NOT @MBGL_WITH_QT_LIB_ONLY@) + find_dependency(Qt@QT_VERSION_MAJOR@ + COMPONENTS OpenGL @REQUIRED_QT_VERSION@) +endif() + +include("${CMAKE_CURRENT_LIST_DIR}/QMapboxGLTargets.cmake") +@PACKAGE_INCLUDE_QCHTARGETS@ diff --git a/platform/qt/include/qmapbox.hpp b/platform/qt/include/qmapbox.hpp index e9642343b..3093e46d2 100644 --- a/platform/qt/include/qmapbox.hpp +++ b/platform/qt/include/qmapbox.hpp @@ -9,14 +9,10 @@ // This header follows the Qt coding style: https://wiki.qt.io/Qt_Coding_Style -#if !defined(QT_MAPBOXGL_STATIC) -# if defined(QT_BUILD_MAPBOXGL_LIB) -# define Q_MAPBOXGL_EXPORT Q_DECL_EXPORT -# else -# define Q_MAPBOXGL_EXPORT Q_DECL_IMPORT -# endif +#if defined(QT_BUILD_MAPBOXGL_LIB) +# define Q_MAPBOXGL_EXPORT Q_DECL_EXPORT #else -# define Q_MAPBOXGL_EXPORT +# define Q_MAPBOXGL_EXPORT Q_DECL_IMPORT #endif namespace QMapbox { diff --git a/platform/qt/include/qmapboxgl.hpp b/platform/qt/include/qmapboxgl.hpp index 58fe922fd..d4b12951e 100644 --- a/platform/qt/include/qmapboxgl.hpp +++ b/platform/qt/include/qmapboxgl.hpp @@ -49,6 +49,13 @@ public: FlippedYViewport }; + enum SettingsTemplate { + DefaultSettings = 0, + MapLibreSettings, + MapTilerSettings, + MapboxSettings + }; + GLContextMode contextMode() const; void setContextMode(GLContextMode); @@ -82,6 +89,10 @@ public: std::function resourceTransform() const; void setResourceTransform(const std::function &); + void resetToTemplate(SettingsTemplate); + + QVector> defaultStyles(); + mbgl::TileServerOptions *tileServerOptionsInternal() const; private: diff --git a/platform/qt/qt.cmake b/platform/qt/qt.cmake index 4085d740d..cb11d5352 100644 --- a/platform/qt/qt.cmake +++ b/platform/qt/qt.cmake @@ -7,21 +7,27 @@ set(MBGL_QT_VERSION_COMPATIBILITY 2.0.0) message(STATUS "Build type: ${CMAKE_BUILD_TYPE}") message(STATUS "Version ${MBGL_QT_VERSION}") -option(MBGL_QT_STATIC "Build Mapbox GL Qt bindings staticly" OFF) option(MBGL_QT_LIBRARY_ONLY "Build only libraries" OFF) +option(MBGL_QT_INSIDE_QT_PLUGIN "Build qmapboxgl as OBJECT library, so it can be bundled into separate single plugin lib." OFF) +option(MBGL_WITH_QT_HEADLESS "Build Mapbox GL Qt with headless support" ON) option(MBGL_QT_WITH_INTERNAL_SQLITE "Build Mapbox GL Qt bindings with internal sqlite" $>) find_package(QT NAMES Qt6 Qt5 COMPONENTS Core REQUIRED) find_package(Qt${QT_VERSION_MAJOR} COMPONENTS Gui Network - OpenGL - Widgets REQUIRED) if (Qt6_FOUND) find_package(Qt${QT_VERSION_MAJOR}OpenGLWidgets REQUIRED) endif() +if(MBGL_WITH_QT_HEADLESS OR NOT MBGL_QT_LIBRARY_ONLY) + find_package(Qt${QT_VERSION_MAJOR} + COMPONENTS OpenGL + Widgets + REQUIRED) +endif() + if(NOT MBGL_QT_WITH_INTERNAL_SQLITE) find_package(Qt${QT_VERSION_MAJOR}Sql REQUIRED) else() @@ -95,7 +101,6 @@ target_sources( ${PROJECT_SOURCE_DIR}/platform/qt/src/async_task_impl.hpp ${PROJECT_SOURCE_DIR}/platform/qt/src/number_format.cpp ${PROJECT_SOURCE_DIR}/platform/qt/src/gl_functions.cpp - ${PROJECT_SOURCE_DIR}/platform/qt/src/headless_backend_qt.cpp $<$:${PROJECT_SOURCE_DIR}/platform/qt/src/http_file_source.cpp> $<$:${PROJECT_SOURCE_DIR}/platform/qt/src/http_file_source.hpp> $<$:${PROJECT_SOURCE_DIR}/platform/qt/src/http_request.cpp> @@ -113,6 +118,19 @@ target_sources( ${PROJECT_SOURCE_DIR}/platform/qt/src/utf.cpp ) +if(MBGL_WITH_QT_HEADLESS OR NOT MBGL_QT_LIBRARY_ONLY) + target_sources( + mbgl-core + PRIVATE + ${PROJECT_SOURCE_DIR}/platform/qt/src/headless_backend_qt.cpp + ) + target_link_libraries( + mbgl-core + PRIVATE + Qt${QT_VERSION_MAJOR}::OpenGL + ) +endif() + target_compile_definitions( mbgl-core PRIVATE QT_IMAGE_DECODERS @@ -124,18 +142,8 @@ target_include_directories( PRIVATE ${PROJECT_SOURCE_DIR}/platform/default/include ) +include(GNUInstallDirs) include(${PROJECT_SOURCE_DIR}/vendor/nunicode.cmake) -if(MBGL_QT_STATIC) - install(TARGETS mbgl-vendor-csscolorparser - ARCHIVE DESTINATION lib - ) - install(TARGETS mbgl-vendor-nunicode - ARCHIVE DESTINATION lib - ) - install(TARGETS mbgl-vendor-parsedate - ARCHIVE DESTINATION lib - ) -endif() set_property(TARGET mbgl-core PROPERTY AUTOMOC ON) @@ -146,18 +154,11 @@ target_link_libraries( Qt${QT_VERSION_MAJOR}::Core Qt${QT_VERSION_MAJOR}::Gui Qt${QT_VERSION_MAJOR}::Network - Qt${QT_VERSION_MAJOR}::OpenGL $,mbgl-vendor-sqlite,Qt${QT_VERSION_MAJOR}::Sql> $<$:$,mbgl-vendor-icu,ICU::uc>> mbgl-vendor-nunicode ) -if(MBGL_QT_STATIC) - install(TARGETS mbgl-core - ARCHIVE DESTINATION lib - ) -endif() - set(qmapboxgl_headers ${PROJECT_SOURCE_DIR}/platform/qt/include/QMapbox ${PROJECT_SOURCE_DIR}/platform/qt/include/QMapboxGL @@ -166,7 +167,11 @@ set(qmapboxgl_headers ) if(MBGL_QT_STATIC) - add_library(qmapboxgl STATIC) + if(MBGL_QT_INSIDE_QT_PLUGIN) + add_library(qmapboxgl OBJECT) + else() + add_library(qmapboxgl STATIC) + endif() else() add_library(qmapboxgl SHARED) endif() @@ -195,6 +200,7 @@ target_sources( set_target_properties( qmapboxgl PROPERTIES AUTOMOC ON + EXPORT_NAME QMapboxGL OUTPUT_NAME QMapboxGL VERSION ${MBGL_QT_VERSION} SOVERSION ${MBGL_QT_VERSION_COMPATIBILITY} @@ -208,59 +214,75 @@ if (NOT MBGL_QT_STATIC) ) endif() +include(CMakePackageConfigHelpers) +set(CMAKECONFIG_INSTALL_DIR ${CMAKE_INSTALL_LIBDIR}/cmake/QMapboxGL/) + +configure_package_config_file( + "platform/qt/QMapboxGLConfig.cmake.in" + "${CMAKE_CURRENT_BINARY_DIR}/QMapboxGLConfig.cmake" + INSTALL_DESTINATION ${CMAKECONFIG_INSTALL_DIR} + PATH_VARS CMAKE_INSTALL_PREFIX CMAKE_INSTALL_INCLUDEDIR + CMAKE_INSTALL_LIBDIR NO_CHECK_REQUIRED_COMPONENTS_MACRO) + +write_basic_package_version_file(${CMAKE_CURRENT_BINARY_DIR}/QMapboxGLConfigVersion.cmake + VERSION ${MBGL_QT_VERSION} + COMPATIBILITY AnyNewerVersion) + +install(EXPORT QMapboxGLTargets + DESTINATION ${CMAKECONFIG_INSTALL_DIR} + COMPONENT development) + +install(FILES + "${CMAKE_CURRENT_BINARY_DIR}/QMapboxGLConfig.cmake" + "${CMAKE_CURRENT_BINARY_DIR}/QMapboxGLConfigVersion.cmake" + DESTINATION ${CMAKECONFIG_INSTALL_DIR} + COMPONENT development) + +install( + DIRECTORY include/mbgl + DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}" + COMPONENT development +) + # FIXME: Because of rapidjson conversion target_include_directories( qmapboxgl - PRIVATE ${PROJECT_SOURCE_DIR}/src -) - -target_include_directories( - qmapboxgl - PUBLIC ${PROJECT_SOURCE_DIR}/platform/qt/include + PRIVATE + ${PROJECT_SOURCE_DIR}/src + ${PROJECT_SOURCE_DIR}/platform/qt/include + INTERFACE + "$" ) target_compile_definitions( qmapboxgl PRIVATE - $,QT_MAPBOXGL_STATIC,QT_BUILD_MAPBOXGL_LIB> + QT_BUILD_MAPBOXGL_LIB ) target_link_libraries( qmapboxgl - PRIVATE + PUBLIC Qt${QT_VERSION_MAJOR}::Core Qt${QT_VERSION_MAJOR}::Gui - mbgl-compiler-options - mbgl-core + Qt${QT_VERSION_MAJOR}::Network + PRIVATE + "$" + "$" + "$" + "$" + "$" ) -if(MSVC) - if(MBGL_QT_STATIC) - set_target_properties(mbgl-vendor-csscolorparser PROPERTIES COMPILE_PDB_NAME mbgl-vendor-csscolorparserd) - set_target_properties(mbgl-vendor-nunicode PROPERTIES COMPILE_PDB_NAME mbgl-vendor-nunicoded) - set_target_properties(mbgl-vendor-parsedate PROPERTIES COMPILE_PDB_NAME mbgl-vendor-parsedated) - - set_target_properties(mbgl-core PROPERTIES COMPILE_PDB_NAME mbgl-cored) - set_target_properties(qmapboxgl PROPERTIES COMPILE_PDB_NAME QMapboxGLd) - - install(FILES $/mbgl-vendor-csscolorparserd.pdb CONFIGURATIONS "Debug" DESTINATION lib) - install(FILES $/mbgl-vendor-nunicoded.pdb CONFIGURATIONS "Debug" DESTINATION lib) - install(FILES $/mbgl-vendor-parsedated.pdb CONFIGURATIONS "Debug" DESTINATION lib) - - install(FILES $/mbgl-cored.pdb CONFIGURATIONS "Debug" DESTINATION lib) - install(FILES $/QMapboxGLd.pdb CONFIGURATIONS "Debug" DESTINATION lib) - else() - install(FILES $ CONFIGURATIONS "Debug" DESTINATION bin) - endif() -endif() - install(TARGETS qmapboxgl - RUNTIME DESTINATION bin - FRAMEWORK DESTINATION lib - LIBRARY DESTINATION lib - ARCHIVE DESTINATION lib - INCLUDES DESTINATION include - PUBLIC_HEADER DESTINATION include + EXPORT QMapboxGLTargets + # Explicit set of DESTINATION is needed for older CMake versions. + RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}" + FRAMEWORK DESTINATION "${CMAKE_INSTALL_LIBDIR}" + LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}" + ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}" + INCLUDES DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}" + PUBLIC_HEADER DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}" ) if(NOT MBGL_QT_LIBRARY_ONLY) @@ -292,6 +314,11 @@ if(NOT MBGL_QT_LIBRARY_ONLY) qmapboxgl ) + target_include_directories( + mbgl-qt + PUBLIC ${PROJECT_SOURCE_DIR}/platform/qt/include + ) + add_executable( mbgl-test-runner ${PROJECT_SOURCE_DIR}/platform/qt/test/main.cpp diff --git a/platform/qt/src/qmapboxgl.cpp b/platform/qt/src/qmapboxgl.cpp index 8b566080b..328c50976 100644 --- a/platform/qt/src/qmapboxgl.cpp +++ b/platform/qt/src/qmapboxgl.cpp @@ -459,6 +459,43 @@ void QMapboxGLSettings::setResourceTransform(const std::function > QMapboxGLSettings::defaultStyles(){ + QVector> styles; + for (const auto &style : tileServerOptionsInternal()->defaultStyles()) { + styles.append(QPair( + QString::fromStdString(style.getUrl()), QString::fromStdString(style.getName()))); + } + return styles; +} + mbgl::TileServerOptions *QMapboxGLSettings::tileServerOptionsInternal() const { return m_tileServerOptionsInternal; } diff --git a/vendor/csscolorparser.cmake b/vendor/csscolorparser.cmake index 28038ff98..1cb8605ed 100644 --- a/vendor/csscolorparser.cmake +++ b/vendor/csscolorparser.cmake @@ -2,8 +2,14 @@ if(TARGET mbgl-vendor-csscolorparser) return() endif() -add_library( - mbgl-vendor-csscolorparser STATIC +if(MBGL_QT_STATIC) + add_library(mbgl-vendor-csscolorparser OBJECT) +else() + add_library(mbgl-vendor-csscolorparser STATIC) +endif() + +target_sources( + mbgl-vendor-csscolorparser PRIVATE ${CMAKE_CURRENT_LIST_DIR}/csscolorparser/csscolorparser.cpp ) diff --git a/vendor/nunicode.cmake b/vendor/nunicode.cmake index 40dcd7513..b21581b44 100644 --- a/vendor/nunicode.cmake +++ b/vendor/nunicode.cmake @@ -2,8 +2,14 @@ if(TARGET mbgl-vendor-nunicode) return() endif() -add_library( - mbgl-vendor-nunicode STATIC +if(MBGL_QT_STATIC) + add_library(mbgl-vendor-nunicode OBJECT) +else() + add_library(mbgl-vendor-nunicode STATIC) +endif() + +target_sources( + mbgl-vendor-nunicode PRIVATE ${CMAKE_CURRENT_LIST_DIR}/nunicode/src/libnu/ducet.c ${CMAKE_CURRENT_LIST_DIR}/nunicode/src/libnu/strcoll.c ${CMAKE_CURRENT_LIST_DIR}/nunicode/src/libnu/strings.c diff --git a/vendor/parsedate.cmake b/vendor/parsedate.cmake index fe1163ce0..5bd94410c 100644 --- a/vendor/parsedate.cmake +++ b/vendor/parsedate.cmake @@ -2,8 +2,14 @@ if(TARGET mbgl-vendor-parsedate) return() endif() -add_library( - mbgl-vendor-parsedate STATIC +if(MBGL_QT_STATIC) + add_library(mbgl-vendor-parsedate OBJECT) +else() + add_library(mbgl-vendor-parsedate STATIC) +endif() + +target_sources( + mbgl-vendor-parsedate PRIVATE ${CMAKE_CURRENT_LIST_DIR}/parsedate/parsedate.cpp )