Home > other >  Qt Webassembly via conan package manager
Qt Webassembly via conan package manager

Time:02-10

I have a really simple Qt (QML) project :

CMakeLists.txt :

cmake_minimum_required(VERSION 3.12.2)

project(testtttt VERSION 0.1 LANGUAGES CXX)

set(CMAKE_INCLUDE_CURRENT_DIR ON)

set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

find_package(QT NAMES Qt6 COMPONENTS Core Quick REQUIRED)
find_package(Qt${QT_VERSION_MAJOR} COMPONENTS Core Quick REQUIRED)

set(PROJECT_SOURCES
        main.cpp
        qml.qrc
)

qt_add_executable(testtttt
        MANUAL_FINALIZATION
        ${PROJECT_SOURCES}
)

target_link_libraries(testtttt PRIVATE Qt${QT_VERSION_MAJOR}::Core Qt${QT_VERSION_MAJOR}::Quick)

set_target_properties(testtttt PROPERTIES
    MACOSX_BUNDLE_GUI_IDENTIFIER my.example.com
    MACOSX_BUNDLE_BUNDLE_VERSION ${PROJECT_VERSION}
    MACOSX_BUNDLE_SHORT_VERSION_STRING ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}
)

qt_import_qml_plugins(testtttt)
qt_finalize_executable(testtttt)

conanfile.txt :

[requires]
qtbase/6.2.2@qt/everywhere
qtdeclarative/6.2.2@qt/everywhere

main.cpp :

#include <QGuiApplication>
#include <QQmlApplicationEngine>


int main(int argc, char *argv[])
{
    QGuiApplication app(argc, argv);

    QQmlApplicationEngine engine;
    const QUrl url(QStringLiteral("qrc:/main.qml"));
    QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
                     &app, [url](QObject *obj, const QUrl &objUrl) {
        if (!obj && url == objUrl)
            QCoreApplication::exit(-1);
    }, Qt::QueuedConnection);
    engine.load(url);

    return app.exec();
}

main.qml :

import QtQuick
import QtQuick.Window

Window {
    width: 640
    height: 480
    visible: true
    title: qsTr("Hello World")

    Rectangle {
        anchors.fill: parent
        color: "red"

        Text {
            anchors.centerIn: parent
            text: "Hello World!"
        }
    }
}

qml.qrc :

<RCC>
    <qresource prefix="/">
        <file>main.qml</file>
    </qresource>
</RCC>

As you can see, it is downloading the official Qt conan packages, from the Qt conan server. (So it does NOT use the conan packages from the conan-center-index server.)

I can successfully build and run it under my macOS machine using this script :

build_and_run.sh :

set -e

conan install qtbuildprofiles/6.2.2@qt/everywhere -r qt

conan install -u conanfile.txt --profile=macos-universal-clang --build=never --update --generator=virtualenv -r qt
source activate.sh

mkdir -p build
cd build
echo "cmake ../ -G Ninja -DCMAKE_BUILD_TYPE=Release"
cmake ../ -G Ninja -DCMAKE_BUILD_TYPE=Release
VERBOSE=1 cmake --build . --config Release --clean-first
cd ..

#Run the built app :
./build/testtttt

I am trying to build it for WebAssembly as well, using this script :

buildWasm.sh :

set -e

conan install qtbuildprofiles/6.2.2@qt/everywhere -r qt

mkdir -p wasmBuildMachine
cd wasmBuildMachine
conan install -u ../conanfile.txt --profile=../macos-universal-clang --build=never --update --generator=virtualenv -r qt
source activate.sh
QT_HOST_PATH=$(echo "$Qt6_DIR" | sed "s#/lib/cmake/Qt6##g")
echo "QT_HOST_PATH : $QT_HOST_PATH"
source deactivate.sh
cd ..

conan install -u conanfile.txt --profile=macos-webassembly-x86_64-clang --build=never --update --generator=virtualenv -r qt
source activate.sh

QT_CMAKE=$(echo "$Qt6_DIR" | sed "s#/lib/cmake/Qt6##g")
QT_CMAKE ="/bin/qt-cmake"
echo "QT_CMAKE : $QT_CMAKE"

mkdir -p buildWasm
cd buildWasm
echo "$QT_CMAKE ../ -G Ninja -DCMAKE_BUILD_TYPE=Release -DQT_HOST_PATH=$QT_HOST_PATH"
$QT_CMAKE ../ -G Ninja -DCMAKE_BUILD_TYPE=Release -DQT_HOST_PATH=$QT_HOST_PATH
VERBOSE=1 cmake --build . --config Release --clean-first

The build fails with this error :

-- Could NOT find Qt6Quick (missing: Qt6Quick_DIR)
CMake Error at CMakeLists.txt:15 (find_package):
Found package configuration file:

/Users/myUser/.conan/data/qtbase/6.2.2/qt/everywhere/package/5f6084cf36d2dec9c03d119ccdb11027ce3fa5ba/lib/cmake/Qt6/Qt6Config.cmake

but it set Qt6_FOUND to FALSE so package "Qt6" is considered to be NOT
FOUND. Reason given by package:

Failed to find Qt component "Quick".

Expected Config file at
"/Users/myUser/.conan/data/qtbase/6.2.2/qt/everywhere/package/5f6084cf36d2dec9c03d119ccdb11027ce3fa5ba/lib/cmake/Qt6Quick/Qt6QuickConfig.cmake"
does NOT exist

...and indeed : QtQuick is in the qtdeclarative conan folder (/Users/myUser/.conan/data/qtbase/6.2.2/qt/everywhere/package/...), and not in the qtbase folder.

How could I fix this issue?

I am following these documentation pages :

https://www.qt.io/blog/installing-qt-via-conan-package-manager

https://wiki.qt.io/Using_Conan_for_Qt6

https://www.qt.io/blog/qt-6-build-system

To simplify things : my project can be downloaded from here as well (every sources that I shared above) :

https://wetransfer.com/downloads/e8717b634b2eff069134b4407f77243c20220202190736/81e9433c52a22081915a7d5d144e2ef420220202190748/a8ec17

UPDATE : I just tried this widget application : Qt/Examples/Qt-6.2.1/widgets/richtext/syntaxhighlighter/ .

It works well, using the exact same conanfile.txt and buildWasm.sh that I shared above.

Here is a screenshot as well about this Example application, that I compiled for WebAssembly, running in Chrome : Screenshot 2022-02-04 at 16.32.21.png

So it seems like that the issue is with the QtQuick module... Possible solution ideas are very well welcomed! I tried many things, but nothing worked so far...

CodePudding user response:

It turned out that this is a bug on Qt's side : https://bugreports.qt.io/browse/QTBUG-94524 ( Introduce a QT_ADDITIONAL_HOST_PACKAGES_PREFIX_PATH variable ) .

The same script should work, once that fix will be deployed.

  • Related