Skip to content

Commit

Permalink
[qt] Make WASM build run without crashes with latest Qt (#1709)
Browse files Browse the repository at this point in the history
  • Loading branch information
ntadej authored Oct 14, 2023
1 parent a3e3290 commit 39f0685
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 34 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/qt-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,8 @@ jobs:
-DCMAKE_INSTALL_PREFIX="../install" \
-DMLN_WITH_QT=ON \
-DMLN_QT_DEPLOYMENT=ON \
-DMLN_QT_WITH_INTERNAL_ICU=ON
-DMLN_QT_WITH_INTERNAL_ICU=ON \
-DMLN_QT_WITH_INTERNAL_SQLITE=ON
ninja
ninja install
Expand Down
66 changes: 40 additions & 26 deletions platform/qt/qt.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -136,17 +136,42 @@ endif()

target_link_libraries(
mbgl-core
PUBLIC
$<BUILD_INTERFACE:mbgl-vendor-parsedate>
$<BUILD_INTERFACE:mbgl-vendor-nunicode>
$<BUILD_INTERFACE:mbgl-vendor-csscolorparser>
$<$<NOT:$<OR:$<PLATFORM_ID:Windows>,$<PLATFORM_ID:Emscripten>>>:z>
$<IF:$<BOOL:${MLN_QT_WITH_INTERNAL_SQLITE}>,$<BUILD_INTERFACE:mbgl-vendor-sqlite>,Qt${QT_VERSION_MAJOR}::Sql>
PRIVATE
$<$<PLATFORM_ID:Linux>:${CMAKE_THREAD_LIBS_INIT}>
$<$<NOT:$<OR:$<PLATFORM_ID:Windows>,$<PLATFORM_ID:Emscripten>>>:z>
Qt${QT_VERSION_MAJOR}::Core
Qt${QT_VERSION_MAJOR}::Gui
Qt${QT_VERSION_MAJOR}::Network
$<IF:$<BOOL:${MLN_QT_WITH_INTERNAL_SQLITE}>,mbgl-vendor-sqlite,Qt${QT_VERSION_MAJOR}::Sql>
$<$<PLATFORM_ID:Linux>:$<IF:$<BOOL:${MLN_QT_WITH_INTERNAL_ICU}>,mbgl-vendor-icu,ICU::uc>>
mbgl-vendor-nunicode
)

# Interface library for object linking
add_library(mbgl-core-interface INTERFACE)
target_link_libraries(
mbgl-core-interface
INTERFACE
$<BUILD_INTERFACE:mbgl-core> $<TARGET_OBJECTS:mbgl-core>
$<BUILD_INTERFACE:mbgl-vendor-parsedate> $<TARGET_OBJECTS:mbgl-vendor-parsedate>
$<BUILD_INTERFACE:mbgl-vendor-nunicode> $<TARGET_OBJECTS:mbgl-vendor-nunicode>
$<BUILD_INTERFACE:mbgl-vendor-csscolorparser> $<TARGET_OBJECTS:mbgl-vendor-csscolorparser>
$<$<BOOL:${MLN_QT_WITH_INTERNAL_SQLITE}>:$<BUILD_INTERFACE:mbgl-vendor-sqlite>>
$<$<BOOL:${MLN_QT_WITH_INTERNAL_SQLITE}>:$<TARGET_OBJECTS:mbgl-vendor-sqlite>>
)
export(TARGETS mbgl-core-interface FILE MapboxCoreTargets.cmake APPEND)

if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
if (MLN_QT_WITH_INTERNAL_ICU)
target_link_libraries(mbgl-core PUBLIC $<BUILD_INTERFACE:mbgl-vendor-icu>)
target_link_libraries(mbgl-core-interface INTERFACE $<BUILD_INTERFACE:mbgl-vendor-icu> $<TARGET_OBJECTS:mbgl-vendor-icu>)
else()
target_link_libraries(mbgl-core PUBLIC ICU::uc)
endif()
endif()

set(qmaplibregl_headers
${PROJECT_SOURCE_DIR}/platform/qt/include/QMapLibreGL/QMapLibreGL
${PROJECT_SOURCE_DIR}/platform/qt/include/QMapLibreGL/export.hpp
Expand Down Expand Up @@ -279,20 +304,8 @@ target_link_libraries(
Qt${QT_VERSION_MAJOR}::Network
PRIVATE
$<BUILD_INTERFACE:mbgl-compiler-options>
$<BUILD_INTERFACE:mbgl-core>
$<BUILD_INTERFACE:mbgl-vendor-parsedate>
$<BUILD_INTERFACE:mbgl-vendor-nunicode>
$<BUILD_INTERFACE:mbgl-vendor-csscolorparser>
$<BUILD_INTERFACE:mbgl-core-interface>
)
# Do not use generator expressions for cleaner output
if (MLN_QT_STATIC AND NOT MLN_QT_INSIDE_PLUGIN)
target_link_libraries(
qmaplibregl
PUBLIC
$<$<NOT:$<BOOL:${MLN_QT_WITH_INTERNAL_SQLITE}>>:Qt${QT_VERSION_MAJOR}::Sql>
$<$<NOT:$<OR:$<PLATFORM_ID:Windows>,$<PLATFORM_ID:Emscripten>>>:z>
)
endif()

if (MLN_QT_STATIC OR MLN_QT_INSIDE_PLUGIN)
# Don't add import/export into public header because we don't build shared library.
Expand All @@ -317,16 +330,16 @@ install(TARGETS qmaplibregl
)

if(NOT MLN_QT_LIBRARY_ONLY)
add_executable(
mbgl-qt
set(mbglqt_sources
${PROJECT_SOURCE_DIR}/platform/qt/app/main.cpp
${PROJECT_SOURCE_DIR}/platform/qt/app/mapwindow.cpp
${PROJECT_SOURCE_DIR}/platform/qt/app/mapwindow.hpp
${PROJECT_SOURCE_DIR}/platform/qt/resources/common.qrc
)

if(CMAKE_SYSTEM_NAME STREQUAL "Emscripten")
set(CMAKE_EXECUTABLE_SUFFIX ".html")
if (COMMAND qt_add_executable)
qt_add_executable(mbgl-qt ${mbglqt_sources})
else()
add_executable(mbgl-qt ${mbglqt_sources})
endif()

# Qt public API should keep compatibility with old compilers for legacy systems
Expand All @@ -347,7 +360,10 @@ if(NOT MLN_QT_LIBRARY_ONLY)
mbgl-qt
PRIVATE ${PROJECT_SOURCE_DIR}/platform/qt/include
)
endif()

# tests
if(NOT MLN_QT_LIBRARY_ONLY AND NOT CMAKE_SYSTEM_NAME STREQUAL "Emscripten")
add_executable(
mbgl-test-runner
${PROJECT_SOURCE_DIR}/platform/qt/test/main.cpp
Expand Down Expand Up @@ -383,6 +399,8 @@ if(NOT MLN_QT_LIBRARY_ONLY)
PRIVATE -Wl,--whole-archive mbgl-test -Wl,--no-whole-archive
)
endif()

add_test(NAME mbgl-test-runner COMMAND mbgl-test-runner WORKING_DIRECTORY ${PROJECT_SOURCE_DIR})
endif()

find_program(MLN_QDOC NAMES qdoc)
Expand All @@ -399,7 +417,3 @@ if(MLN_QDOC)
${CMAKE_BINARY_DIR}/docs
)
endif()

if(NOT MLN_QT_LIBRARY_ONLY)
add_test(NAME mbgl-test-runner COMMAND mbgl-test-runner WORKING_DIRECTORY ${PROJECT_SOURCE_DIR})
endif()
3 changes: 3 additions & 0 deletions platform/qt/src/mbgl/http_request.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ QNetworkRequest HTTPRequest::networkRequest() const {
QNetworkRequest req = QNetworkRequest(requestUrl());
req.setAttribute(QNetworkRequest::CacheLoadControlAttribute, QNetworkRequest::PreferCache);

// User-Agent can not be set on WASM
#ifndef Q_OS_WASM
static const QByteArray agent =
!m_context->getClientOptions().name().empty()
? QString("%1/%2 (%3) MapLibreGL/%4 (Qt %5)")
Expand All @@ -45,6 +47,7 @@ QNetworkRequest HTTPRequest::networkRequest() const {
.toLatin1()
: QString("MapLibreGL/%1 (Qt %2)").arg(version::revision).arg(QT_VERSION_STR).toLatin1();
req.setRawHeader("User-Agent", agent);
#endif

if (m_resource.priorEtag) {
const auto etag = m_resource.priorEtag;
Expand Down
2 changes: 1 addition & 1 deletion vendor/csscolorparser.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ if(TARGET mbgl-vendor-csscolorparser)
return()
endif()

if(MLN_WITH_QT AND ${CMAKE_VERSION} VERSION_GREATER_EQUAL "3.12.0")
if(MLN_WITH_QT)
add_library(mbgl-vendor-csscolorparser OBJECT)
else()
add_library(mbgl-vendor-csscolorparser STATIC)
Expand Down
10 changes: 8 additions & 2 deletions vendor/icu.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,14 @@ if(TARGET mbgl-vendor-icu)
return()
endif()

add_library(
mbgl-vendor-icu STATIC
if(MLN_WITH_QT)
add_library(mbgl-vendor-icu OBJECT)
else()
add_library(mbgl-vendor-icu STATIC)
endif()

target_sources(
mbgl-vendor-icu PRIVATE
${CMAKE_CURRENT_LIST_DIR}/icu/src/cmemory.cpp
${CMAKE_CURRENT_LIST_DIR}/icu/src/cstring.cpp
${CMAKE_CURRENT_LIST_DIR}/icu/src/ubidi.cpp
Expand Down
2 changes: 1 addition & 1 deletion vendor/nunicode.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ if(TARGET mbgl-vendor-nunicode)
return()
endif()

if(MLN_WITH_QT AND ${CMAKE_VERSION} VERSION_GREATER_EQUAL "3.12.0")
if(MLN_WITH_QT)
add_library(mbgl-vendor-nunicode OBJECT)
else()
add_library(mbgl-vendor-nunicode STATIC)
Expand Down
2 changes: 1 addition & 1 deletion vendor/parsedate.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ if(TARGET mbgl-vendor-parsedate)
return()
endif()

if(MLN_WITH_QT AND ${CMAKE_VERSION} VERSION_GREATER_EQUAL "3.12.0")
if(MLN_WITH_QT)
add_library(mbgl-vendor-parsedate OBJECT)
else()
add_library(mbgl-vendor-parsedate STATIC)
Expand Down
14 changes: 12 additions & 2 deletions vendor/sqlite.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,14 @@ if(TARGET mbgl-vendor-sqlite)
return()
endif()

add_library(
mbgl-vendor-sqlite STATIC
if(MLN_WITH_QT)
add_library(mbgl-vendor-sqlite OBJECT)
else()
add_library(mbgl-vendor-sqlite STATIC)
endif()

target_sources(
mbgl-vendor-sqlite PRIVATE
${CMAKE_CURRENT_LIST_DIR}/sqlite/src/sqlite3.c
)

Expand Down Expand Up @@ -32,3 +38,7 @@ export(TARGETS
mbgl-vendor-sqlite
APPEND FILE MapboxCoreTargets.cmake
)

if(CMAKE_SYSTEM_NAME STREQUAL "Emscripten")
set_target_properties(mbgl-vendor-sqlite PROPERTIES COMPILE_FLAGS "-pthread")
endif()

0 comments on commit 39f0685

Please sign in to comment.