Skip to content

Commit

Permalink
Add QT extension
Browse files Browse the repository at this point in the history
  • Loading branch information
Jarred Wilson committed Sep 6, 2023
1 parent 93a9d81 commit 3b07cab
Show file tree
Hide file tree
Showing 8 changed files with 460 additions and 2 deletions.
2 changes: 1 addition & 1 deletion extensions/desktop/command-chain/desktop-launch
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/bin/bash

set -- "${SNAP}/gnome-platform/command-chain/desktop-launch" "$@"
set -- "${SNAP_DESKTOP_RUNTIME}/command-chain/desktop-launch" "$@"
# shellcheck source=/dev/null
source "${SNAP}/snap/command-chain/run"
2 changes: 1 addition & 1 deletion extensions/desktop/command-chain/hooks-configure-fonts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/bin/bash

set -- "${SNAP}/gnome-platform/command-chain/hooks-configure-fonts" "$@"
set -- "${SNAP_DESKTOP_RUNTIME}/command-chain/hooks-configure-desktop" "$@"
# shellcheck source=/dev/null
source "${SNAP}/snap/command-chain/run"
1 change: 1 addition & 0 deletions schema/snapcraft.json
Original file line number Diff line number Diff line change
Expand Up @@ -914,6 +914,7 @@
"gnome-3-34",
"gnome-3-38",
"kde-neon",
"qt-framework",
"ros1-noetic",
"ros2-foxy"
]
Expand Down
220 changes: 220 additions & 0 deletions snapcraft/extensions/qt_framework.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,220 @@
# -*- Mode:Python; indent-tabs-mode:nil; tab-width:4 -*-
#
# Copyright 2022 Canonical Ltd.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 3 as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

"""Generic QT Framework extension to support core22 and onwards."""
import dataclasses
import functools
from typing import Any, Dict, List, Optional, Tuple

from overrides import overrides

from .extension import Extension, get_extensions_data_dir, prepend_to_env

_SDK_SNAP = {"core22": "qt-framework-sdk"}


@dataclasses.dataclass
class ExtensionInfo:
"""Content/SDK build information."""

cmake_args: list


@dataclasses.dataclass
class QTSnaps:
"""A structure of QT related snaps."""

sdk: str
content: str
builtin: bool = True


class QTFramework(Extension):
r"""The QT Framework extension.
This extension makes it easy to assemble QT based applications.
It configures each application with the following plugs:
\b
- Common Icon Themes.
- Common Sound Themes.
- The QT Frameworks runtime libraries and utilities.
For easier desktop integration, it also configures each application
entry with these additional plugs:
\b
- desktop (https://snapcraft.io/docs/desktop-interface)
- desktop-legacy (https://snapcraft.io/docs/desktop-legacy-interface)
- opengl (https://snapcraft.io/docs/opengl-interface)
- wayland (https://snapcraft.io/docs/wayland-interface)
- x11 (https://snapcraft.io/docs/x11-interface)
"""

@staticmethod
@overrides
def get_supported_bases() -> Tuple[str, ...]:
return ("core22",)

@staticmethod
@overrides
def get_supported_confinement() -> Tuple[str, ...]:
return "strict", "devmode"

@staticmethod
@overrides
def is_experimental(base: Optional[str]) -> bool:
return False

@overrides
def get_app_snippet(self) -> Dict[str, Any]:
return {
"command-chain": ["snap/command-chain/desktop-launch"],
"plugs": ["desktop", "desktop-legacy", "opengl", "wayland", "x11"],
"environment": {
"QT_PLUGIN_PATH": "$SNAP/qt-framework/opt/qt5-15/plugins:$SNAP/usr/lib/$SNAPCRAFT_ARCH_TRIPLET/qt5/plugins",
},
}

@functools.cached_property
def qt_snaps(self) -> QTSnaps:
"""Return the QT related snaps to use to construct the environment."""
base = self.yaml_data["base"]
sdk_snap = _SDK_SNAP[base]

build_snaps: List[str] = []
for part in self.yaml_data["parts"].values():
build_snaps.extend(part.get("build-snaps", []))

builtin = True

for snap in build_snaps:
if sdk_snap == snap.split("/")[0]:
builtin = False
break

# The same except the trailing -sd
content = sdk_snap[:-4]

return QTSnaps(sdk=sdk_snap, content=content, builtin=builtin)

@functools.cached_property
def ext_info(self) -> ExtensionInfo:
"""Return the extension info cmake_args, provider, content, build_snaps."""
prefix_root = f"/snap/{self.qt_snaps.sdk}/current/opt"
versions = ["qt6-5", "qt6-4", "qt6-2", "qt5-15"]
prefix_path = "-DCMAKE_PREFIX_PATH="

for version in versions:
prefix_path += f"{prefix_root}/{version}"

cmake_args = [
f"-DCMAKE_FIND_ROOT_PATH=/snap/{self.qt_snaps.sdk}/current",
prefix_path,
"-DZLIB_INCLUDE_DIR=/lib/x86_64-linux-gnu",
]

return ExtensionInfo(cmake_args=cmake_args)

@overrides
def get_root_snippet(self) -> Dict[str, Any]:
return {
"assumes": ["snapd2.43"], # for 'snapctl is-connected'
"compression": "lzo",
"plugs": {
"desktop": {"mount-host-font-cache": False},
"icon-themes": {
"interface": "content",
"target": "$SNAP/data-dir/icons",
"default-provider": "gtk-common-themes",
},
"sound-themes": {
"interface": "content",
"target": "$SNAP/data-dir/sounds",
"default-provider": "gtk-common-themes",
},
self.qt_snaps.content: {
"interface": "content",
"default-provider": self.qt_snaps.content,
"target": "$SNAP/qt-framework",
},
},
"environment": {"SNAP_DESKTOP_RUNTIME": "$SNAP/qt-framework"},
"hooks": {
"configure": {
"plugs": ["desktop"],
"command-chain": ["snap/command-chain/hooks-configure-fonts"],
}
},
"layout": {
"/usr/share/X11": {"symlink": "$SNAP/qt-framework/usr/share/X11"}
},
}

@overrides
def get_part_snippet(self, *, plugin_name: str) -> Dict[str, Any]:
sdk_snap = self.qt_snaps.sdk
cmake_args = self.ext_info.cmake_args

return {
"build-environment": [
{
"PATH": prepend_to_env(
"PATH", [f"/snap/{sdk_snap}/current/usr/bin"]
),
},
{
"XDG_DATA_DIRS": prepend_to_env(
"XDG_DATA_DIRS",
[
f"$CRAFT_STAGE/usr/share:/snap/{sdk_snap}/current/usr/share",
"/usr/share",
],
),
},
{
"SNAPCRAFT_CMAKE_ARGS": prepend_to_env(
"SNAPCRAFT_CMAKE_ARGS", cmake_args
),
},
],
"build-packages": [
"libgl1-mesa-dev",
],
}

@overrides
def get_parts_snippet(self) -> Dict[str, Any]:
source = get_extensions_data_dir() / "desktop" / "command-chain"

if self.qt_snaps.builtin:
return {
"qt-framework/sdk": {
"source": str(source),
"plugin": "make",
"make-parameters": [f"PLATFORM_PLUG={self.qt_snaps.content}"],
"build-snaps": [self.qt_snaps.sdk],
},
}

return {
"qt-framework/sdk": {
"source": str(source),
"plugin": "make",
"make-parameters": [f"PLATFORM_PLUG={self.qt_snaps.content}"],
},
}
2 changes: 2 additions & 0 deletions snapcraft/extensions/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

from .gnome import GNOME
from .kde_neon import KDENeon
from .qt_framework import QTFramework
from .ros2_humble import ROS2HumbleExtension

if TYPE_CHECKING:
Expand All @@ -33,6 +34,7 @@
"gnome": GNOME,
"ros2-humble": ROS2HumbleExtension,
"kde-neon": KDENeon,
"qt-framework": QTFramework,
}


Expand Down
2 changes: 2 additions & 0 deletions tests/unit/commands/test_list_extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ def test_command(emitter, command):
gnome-3-34 core18
gnome-3-38 core20
kde-neon core18, core20, core22
qt-framework core22
ros1-noetic core20
ros2-foxy core20
ros2-humble core22"""
Expand All @@ -68,6 +69,7 @@ def test_command_extension_dups(emitter, command):
gnome-3-34 core18
gnome-3-38 core20
kde-neon core18, core20, core22
qt-framework core22
ros1-noetic core20
ros2-foxy core20
ros2-humble core22"""
Expand Down
Loading

0 comments on commit 3b07cab

Please sign in to comment.