aboutsummaryrefslogtreecommitdiffstats
path: root/examples/qml/tutorials/extending-qml
diff options
context:
space:
mode:
authorAlexandru Croitor <alexandru.croitor@qt.io>2021-08-25 16:20:34 +0200
committerCraig Scott <craig.scott@qt.io>2021-08-31 14:51:25 +1000
commit9cbf4d2cc85723dadadadb5fe1ade9719dfeb2a6 (patch)
treeef785f5c4e16027f6707f83378fe677a10a283e1 /examples/qml/tutorials/extending-qml
parent784af37650a77b30de01776262c1fe6727075615 (diff)
CMake: qmake: Fix chapter6-plugins example to build in more configs
Before this change, I tested the following configurations / scenarios (1) shared Qt + qmake + macOS bundle -> works (2) shared Qt + CMake + macOS bundle -> broken (3) static Qt + qmake + macOS bundle -> broken (4) static Qt + CMake + macOS bundle -> works (2) was broken because the shared qml plugin is not located in the expected location (see below) (3) was broken because qmake needs a lot of error-prone boilerplate to ensure static qml plugin building and linking works. The change fixes the example to build and run successfully in case (2). Tested the following scenarios on macOS with the change shared Qt + qmake + macOS bundle -> works shared Qt + CMake + macOS bundle -> works shared Qt + qmake + no bundle -> works shared Qt + CMake + no bundle -> works static Qt + qmake -> still broken static Qt + CMake -> works To make shared qml plugins be found in a macOS bundle, we need to copy the qmldir and plugin under the bundle's PlugIns subfolder, because the application adds that as the expected qml import path for macOS. This basically mimics what the qmake project does with QMAKE_BUNDLE_DATA. The change also cleans up the CMake projects a bit so they don't link PUBLIC-ly against dependencies, install the plugin under a more sensible location and don't needlessly setup package finding and other boilerplate that's done in the parent project. The change also cleans up the qmake projects to adjust to not pollute the macos bundle dir with static plugin files. Amends 9e1d2a0eb15487f8f2acb4d91b281568897fb4e1 Pick-to: 6.2 Change-Id: I45bb699a67adf598587350a03f778291fad3f850 Reviewed-by: Craig Scott <craig.scott@qt.io> Reviewed-by: Ulf Hermann <ulf.hermann@qt.io>
Diffstat (limited to 'examples/qml/tutorials/extending-qml')
-rw-r--r--examples/qml/tutorials/extending-qml/chapter6-plugins/CMakeLists.txt2
-rw-r--r--examples/qml/tutorials/extending-qml/chapter6-plugins/Charts/CMakeLists.txt52
-rw-r--r--examples/qml/tutorials/extending-qml/chapter6-plugins/app.pro4
3 files changed, 28 insertions, 30 deletions
diff --git a/examples/qml/tutorials/extending-qml/chapter6-plugins/CMakeLists.txt b/examples/qml/tutorials/extending-qml/chapter6-plugins/CMakeLists.txt
index cf3d218c57..dc0b20dc21 100644
--- a/examples/qml/tutorials/extending-qml/chapter6-plugins/CMakeLists.txt
+++ b/examples/qml/tutorials/extending-qml/chapter6-plugins/CMakeLists.txt
@@ -25,7 +25,7 @@ set_target_properties(chapter6-plugins PROPERTIES
WIN32_EXECUTABLE TRUE
MACOSX_BUNDLE TRUE
)
-target_link_libraries(chapter6-plugins PUBLIC
+target_link_libraries(chapter6-plugins PRIVATE
Qt::Qml
Qt::Quick
)
diff --git a/examples/qml/tutorials/extending-qml/chapter6-plugins/Charts/CMakeLists.txt b/examples/qml/tutorials/extending-qml/chapter6-plugins/Charts/CMakeLists.txt
index 15f6dcc2c2..28efcaace8 100644
--- a/examples/qml/tutorials/extending-qml/chapter6-plugins/Charts/CMakeLists.txt
+++ b/examples/qml/tutorials/extending-qml/chapter6-plugins/Charts/CMakeLists.txt
@@ -1,25 +1,3 @@
-# Generated from import.pro.
-
-cmake_minimum_required(VERSION 3.16)
-project(chartsplugin LANGUAGES CXX)
-
-set(CMAKE_INCLUDE_CURRENT_DIR ON)
-
-set(CMAKE_AUTOMOC ON)
-set(CMAKE_AUTORCC ON)
-set(CMAKE_AUTOUIC ON)
-
-if(NOT DEFINED INSTALL_EXAMPLESDIR)
- set(INSTALL_EXAMPLESDIR "examples")
-endif()
-
-set(INSTALL_EXAMPLEDIR "${INSTALL_EXAMPLESDIR}/qml/tutorials/extending-qml/chapter6-plugins/Charts")
-
-find_package(Qt6 COMPONENTS Core)
-find_package(Qt6 COMPONENTS Gui)
-find_package(Qt6 COMPONENTS Qml)
-find_package(Qt6 COMPONENTS Quick)
-
qt6_add_qml_module(chartsplugin
VERSION 1.0
URI "Charts"
@@ -30,17 +8,37 @@ target_sources(chartsplugin PRIVATE
piechart.cpp piechart.h
pieslice.cpp pieslice.h
)
-set_target_properties(chartsplugin PROPERTIES
- WIN32_EXECUTABLE TRUE
- MACOSX_BUNDLE TRUE
-)
-target_link_libraries(chartsplugin PUBLIC
+
+target_link_libraries(chartsplugin PRIVATE
Qt::Core
Qt::Gui
Qt::Qml
Qt::Quick
)
+if(QT6_IS_SHARED_LIBS_BUILD AND APPLE)
+ get_target_property(is_bundle chapter6-plugins MACOSX_BUNDLE)
+ if(is_bundle)
+ # The application's main.cpp adds an explicit QML import path to look for qml modules under
+ # a PlugIns subdirectory in a macOS bundle.
+ # Copy the qmldir and shared library qml plugin.
+
+ set(charts_dir "$<TARGET_FILE_DIR:chartsplugin>")
+ set(chars_qmldir_file "${charts_dir}/qmldir")
+ set(app_dir "$<TARGET_FILE_DIR:chapter6-plugins>")
+ set(bundle_charts_dir "${app_dir}/../PlugIns/Charts")
+
+ add_custom_command(TARGET chartsplugin POST_BUILD
+ COMMAND ${CMAKE_COMMAND} -E make_directory ${bundle_charts_dir}
+ COMMAND ${CMAKE_COMMAND} -E copy_if_different
+ $<TARGET_FILE:chartsplugin> ${bundle_charts_dir}
+ COMMAND ${CMAKE_COMMAND} -E copy_if_different
+ ${chars_qmldir_file} ${bundle_charts_dir}
+ )
+ endif()
+endif()
+
+set(INSTALL_EXAMPLEDIR "${INSTALL_EXAMPLEDIR}/Charts")
install(TARGETS chartsplugin
RUNTIME DESTINATION "${INSTALL_EXAMPLEDIR}"
BUNDLE DESTINATION "${INSTALL_EXAMPLEDIR}"
diff --git a/examples/qml/tutorials/extending-qml/chapter6-plugins/app.pro b/examples/qml/tutorials/extending-qml/chapter6-plugins/app.pro
index b340981e42..8f53075a7d 100644
--- a/examples/qml/tutorials/extending-qml/chapter6-plugins/app.pro
+++ b/examples/qml/tutorials/extending-qml/chapter6-plugins/app.pro
@@ -3,9 +3,9 @@ QT += qml quick
# Ensure that the application will see the import path for the Charts module:
# * On Windows, do not build into a debug/release subdirectory.
-# * On OS X, add the plugin files into the bundle.
+# * On macOS, add the plugin files into the bundle, only in a shared build though
win32: DESTDIR = ./
-osx {
+macos:!qtConfig(static) {
charts.files = $$OUT_PWD/Charts
charts.path = Contents/PlugIns
QMAKE_BUNDLE_DATA += charts