CMake: Add qml app deployment auto test

Add a cmake test that builds two user qml modules and a qml app,
installs the built binaries, in a shared Qt build deploys the
runtime dependencies and then runs the app to confirm that it works.

With a static Qt, the installation of the runtime dependencies is
skipped, but the app should still run, because all the modules plugins
and resources are statically linked in.

The test is expected to pass only when targeting Windows and macOS
(both when using shared and static Qt).

For the static case to work, we explicitly need to add a qrc:///
qml import path, to ensure that user module qmldir files are found.

Pick-to: 6.3
Task-number: QTBUG-98545
Change-Id: Id2abbd669f22f30b4e639bba0b4e14b792e08714
Reviewed-by: Jörg Bornemann <joerg.bornemann@qt.io>
Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
This commit is contained in:
Alexandru Croitor 2022-02-14 18:21:36 +01:00
parent 5568183f58
commit c5483d1e53
11 changed files with 518 additions and 0 deletions

View File

@ -86,4 +86,25 @@ if(TARGET Qt::Quick)
)
endif()
endif()
set(deploy_args
test_qml_app_deployment
BINARY "${CMAKE_CTEST_COMMAND}"
BINARY_ARGS "-V"
# Need to explicitly specify a writable install prefix.
BUILD_OPTIONS
-DCMAKE_INSTALL_PREFIX=${CMAKE_CURRENT_BINARY_DIR}/test_qml_app_deployment_installed
)
# For now, the test should only pass on Windows and macOS shared and static builds and fail on
# other platforms, because there is no support for runtime dependency deployment
# on those platforms.
# With static builds the runtime dependencies are just skipped, but the test should
# still pass.
if((WIN32 OR (APPLE AND NOT IOS)))
_qt_internal_test_expect_pass(${deploy_args})
else()
_qt_internal_test_expect_fail(${deploy_args})
endif()
endif()

View File

@ -0,0 +1,87 @@
cmake_minimum_required(VERSION 3.16)
project(deployment_api)
enable_testing()
find_package(Qt6 COMPONENTS REQUIRED Quick Test QuickTestUtilsPrivate)
qt6_standard_project_setup()
function(create_test_executable target)
cmake_parse_arguments(arg "" "" "" ${ARGN})
qt_add_executable(${target} main.cpp)
qt_add_qml_module(${target}
URI EntryModule
VERSION 1.0
QML_FILES main.qml
NO_RESOURCE_TARGET_PATH
)
set_target_properties(${target} PROPERTIES
# We explicitly don't set WIN32_EXECUTABLE to ensure we see errors from stderr when
# something fails and not having to use DebugView.
MACOSX_BUNDLE TRUE
)
target_link_libraries(${target} PRIVATE
Qt::Quick
Qt::Test
Qt::QuickPrivate
Qt::QuickTestUtilsPrivate
)
install(TARGETS ${target}
BUNDLE DESTINATION .
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
)
# Need to manually install the backing libraries on Windows because windeployqt does not do it.
# On macOS, macdeployqt takes care of installing the backing library because it is detected
# as a dependency of the qml plugin, which is specified via the -executable option
# of macdeployqt.
# Note we install all the binaries and backing libraries before calling the deploy tool.
if(WIN32)
install(TARGETS EllipseShape FunkyShape
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
)
endif()
qt_generate_deploy_qml_app_script(
TARGET ${target}
FILENAME_VARIABLE deploy_script
NO_UNSUPPORTED_PLATFORM_ERROR
# Just to ensure that running the test from the build dir also works
MACOS_BUNDLE_POST_BUILD
)
install(SCRIPT ${deploy_script})
if(APPLE AND NOT IOS)
set(installed_app_location "${CMAKE_INSTALL_PREFIX}/${target}.app/Contents/MacOS/${target}")
elseif(WIN32)
set(installed_app_location "${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_BINDIR}/${target}.exe")
endif()
# There's no nice way to get the location of an installed binary, so we need to construct
# the binary install path by hand, somewhat similar to how it's done in
# the implementation of qt_deploy_runtime_dependencies.
# On unsupported deployment platforms, either the install_ test will fail not finding
# the location of the app (because we do not set a installed_app_location value)
# or the run_deployed_ test will fail because we didn't deploy the runtime dependencies.
# When support for additional platforms is added, these locations will have to be augmented.
add_test(install_${target} "${CMAKE_COMMAND}" --install .)
set_tests_properties(install_${target} PROPERTIES FIXTURES_SETUP deploy_step)
add_test(NAME run_deployed_${target}
COMMAND "${installed_app_location}"
# Make sure that we don't use the default working directory which is
# CMAKE_CURRENT_BINARY_DIR because on Windows the loader might pick up dlls
# from the working directory instead of the installed app dir, if the dll is
# missing in the app dir.
WORKING_DIRECTORY "${CMAKE_INSTALL_PREFIX}")
endfunction()
# Create the backing targets before the app, so that the backing library
# install commands don't error out.
add_subdirectory(Shapes/FunkyShape)
add_subdirectory(Shapes/EllipseShape)
create_test_executable(UserQmlApp)

View File

@ -0,0 +1,10 @@
qt_add_qml_module(EllipseShape
URI Shapes.EllipseShape
VERSION 1.0
SOURCES ellipseitem.cpp ellipseitem.h
)
target_link_libraries(EllipseShape
PRIVATE
Qt::Quick
)

View File

@ -0,0 +1,49 @@
/****************************************************************************
**
** Copyright (C) 2022 The Qt Company Ltd.
** Contact: http://www.qt.io/licensing/
**
** This file is part of the test suite of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:GPL-EXCEPT$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include "ellipseitem.h"
#include <QPainter>
EllipseItem::EllipseItem(QQuickItem *parent)
: QQuickPaintedItem(parent)
{
}
EllipseItem::~EllipseItem()
{
}
void EllipseItem::paint(QPainter *painter)
{
const qreal halfPenWidth = qMax(painter->pen().width() / 2.0, 1.0);
QRectF rect = boundingRect();
rect.adjust(halfPenWidth, halfPenWidth, -halfPenWidth, -halfPenWidth);
painter->drawEllipse(rect);
}

View File

@ -0,0 +1,45 @@
/****************************************************************************
**
** Copyright (C) 2022 The Qt Company Ltd.
** Contact: http://www.qt.io/licensing/
**
** This file is part of the test suite of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:GPL-EXCEPT$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/
#ifndef ELLIPSEITEM_H
#define ELLIPSEITEM_H
#include <QQuickPaintedItem>
class EllipseItem : public QQuickPaintedItem
{
Q_OBJECT
Q_DISABLE_COPY(EllipseItem)
QML_NAMED_ELEMENT(EllipseItemCpp)
public:
EllipseItem(QQuickItem *parent = nullptr);
~EllipseItem();
void paint(QPainter *painter);
};
#endif // ELLIPSEITEM_H

View File

@ -0,0 +1,10 @@
qt_add_qml_module(FunkyShape
URI Shapes.FunkyShape
VERSION 1.0
QML_FILES FunkyItemQml.qml
SOURCES funkyitem.cpp funkyitem.h
)
target_link_libraries(FunkyShape
PRIVATE
Qt::Quick
)

View File

@ -0,0 +1,76 @@
/****************************************************************************
**
** Copyright (C) 2022 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the examples of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:BSD$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** BSD License Usage
** Alternatively, you may use this file under the terms of the BSD license
** as follows:
**
** "Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions are
** met:
** * Redistributions of source code must retain the above copyright
** notice, this list of conditions and the following disclaimer.
** * Redistributions in binary form must reproduce the above copyright
** notice, this list of conditions and the following disclaimer in
** the documentation and/or other materials provided with the
** distribution.
** * Neither the name of The Qt Company Ltd nor the names of its
** contributors may be used to endorse or promote products derived
** from this software without specific prior written permission.
**
**
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
**
** $QT_END_LICENSE$
**
****************************************************************************/
import QtQuick
import QtQuick.Shapes
Shape {
width: 200
height: 150
anchors.centerIn: parent
ShapePath {
strokeWidth: 4
strokeColor: "red"
fillGradient: LinearGradient {
x1: 20; y1: 20
x2: 180; y2: 130
GradientStop { position: 0; color: "blue" }
GradientStop { position: 0.2; color: "green" }
GradientStop { position: 0.4; color: "red" }
GradientStop { position: 0.6; color: "yellow" }
GradientStop { position: 1; color: "cyan" }
}
strokeStyle: ShapePath.DashLine
dashPattern: [ 1, 4 ]
startX: 20; startY: 20
PathLine { x: 180; y: 130 }
PathLine { x: 20; y: 130 }
PathLine { x: 20; y: 20 }
}
}

View File

@ -0,0 +1,49 @@
/****************************************************************************
**
** Copyright (C) 2022 The Qt Company Ltd.
** Contact: http://www.qt.io/licensing/
**
** This file is part of the test suite of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:GPL-EXCEPT$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include "funkyitem.h"
#include <QPainter>
FunkyItem::FunkyItem(QQuickItem *parent)
: QQuickPaintedItem(parent)
{
}
FunkyItem::~FunkyItem()
{
}
void FunkyItem::paint(QPainter *painter)
{
const qreal halfPenWidth = qMax(painter->pen().width() / 2.0, 1.0);
QRectF rect = boundingRect();
rect.adjust(halfPenWidth, halfPenWidth, -halfPenWidth, -halfPenWidth);
painter->drawEllipse(rect);
}

View File

@ -0,0 +1,45 @@
/****************************************************************************
**
** Copyright (C) 2022 The Qt Company Ltd.
** Contact: http://www.qt.io/licensing/
**
** This file is part of the test suite of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:GPL-EXCEPT$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/
#ifndef FunkyItem_H
#define FunkyItem_H
#include <QQuickPaintedItem>
class FunkyItem : public QQuickPaintedItem
{
Q_OBJECT
Q_DISABLE_COPY(FunkyItem)
QML_NAMED_ELEMENT(FunkyItemCpp)
public:
FunkyItem(QQuickItem *parent = nullptr);
~FunkyItem();
void paint(QPainter *painter);
};
#endif // FunkyItem_H

View File

@ -0,0 +1,56 @@
/****************************************************************************
**
** Copyright (C) 2022 The Qt Company Ltd.
** Contact: http://www.qt.io/licensing/
**
** This file is part of the test suite of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:GPL-EXCEPT$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include <QtQml/QQmlEngine>
#include <QtQuick/QQuickView>
#include <QtTest/QTest>
#include <QtQuickTestUtils/private/viewtestutils_p.h>
class test_qml_app_deployment : public QObject
{
Q_OBJECT
private slots:
void canRun();
};
void test_qml_app_deployment::canRun()
{
QQuickView view;
#ifdef QT_STATIC
// Need to add qrc:/// as an import path when using Qt static builds,
// to ensure that user module qmldir files are found from the embedded resources
// and not from the filesystem.
view.engine()->addImportPath(QLatin1String("qrc:///"));
#endif
QVERIFY(QQuickTest::showView(view, QUrl("qrc:///main.qml")));
}
QTEST_MAIN(test_qml_app_deployment)
#include "main.moc"

View File

@ -0,0 +1,70 @@
/****************************************************************************
**
** Copyright (C) 2022 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the examples of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:BSD$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** BSD License Usage
** Alternatively, you may use this file under the terms of the BSD license
** as follows:
**
** "Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions are
** met:
** * Redistributions of source code must retain the above copyright
** notice, this list of conditions and the following disclaimer.
** * Redistributions in binary form must reproduce the above copyright
** notice, this list of conditions and the following disclaimer in
** the documentation and/or other materials provided with the
** distribution.
** * Neither the name of The Qt Company Ltd nor the names of its
** contributors may be used to endorse or promote products derived
** from this software without specific prior written permission.
**
**
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
**
** $QT_END_LICENSE$
**
****************************************************************************/
import QtQuick
import Shapes.EllipseShape
import Shapes.FunkyShape
Item {
width: 640; height: 480
visible: true
Item {
anchors.fill: parent
EllipseItemCpp {
anchors.fill: parent
}
FunkyItemCpp {
anchors.fill: parent
}
FunkyItemQml {
anchors.fill: parent
}
}
}