Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cxx-qt-build: Export .qmltypes and qmldir for use with external tooling #982

Draft
wants to merge 20 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
aaad741
WIP: qt-build-utils: add resources to volatile workaround
ahayzen-kdab Jun 17, 2024
9a05ead
WIP: Add cxxqt_import_qml_module to CMake
LeonMatthes Jun 17, 2024
a78e677
Add cxxqt_import_crate
LeonMatthesKDAB Jun 18, 2024
814003c
CMake: Use cxxqt_import_ functions everywhere
LeonMatthesKDAB Jun 18, 2024
99f0bc0
CMake: Add fake target to build required obj file
LeonMatthesKDAB Jun 18, 2024
0269983
cxx-qt-build: Do not use -bins suffix of -link-arg
LeonMatthesKDAB Jun 18, 2024
7d1edee
build-utils: Only reference cachegen if it exists
LeonMatthesKDAB Jun 18, 2024
76f1eeb
Enable -x in scripts/check_cargo_build_rerun.sh
LeonMatthesKDAB Jun 18, 2024
9aa28be
cmake: Automatically add a Qt5 compatibility target
LeonMatthesKDAB Jun 19, 2024
b6d6ad3
fix: Close the std_types_qt5 file before compiling
LeonMatthesKDAB Jun 19, 2024
b118006
Remove loop error that would accidentally add unwanted files to the init
LeonMatthesKDAB Jun 19, 2024
f30e3bc
cxx-qt-build: Refactor `build` method
LeonMatthesKDAB Jun 20, 2024
17e0e76
Cxx-Qt-build: compile object file with -arg-bins
LeonMatthesKDAB Jun 20, 2024
91689e5
cxx-qt-build: specify -l to the staticlib manually
LeonMatthesKDAB Jun 20, 2024
a4939a1
qt-build-utils: Refactor to use std::fs::write
LeonMatthesKDAB Jun 20, 2024
5451dae
cxx-qt-build: Allow custom initializers in opts
LeonMatthesKDAB Jun 20, 2024
213902a
Revert "fix: CI: Force use of Rust 1.77 (#957)"
LeonMatthesKDAB Jun 20, 2024
1757f09
clippy: Allow missing_safety_doc in nested_qobjects
LeonMatthesKDAB Jun 21, 2024
193bb66
cxx-qt-build: Include Qt5 support in initializers
LeonMatthesKDAB Jun 21, 2024
c9d2c5d
Export qmltypes and qmldir for each plugin
LeonMatthesKDAB Jun 21, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 1 addition & 6 deletions .github/workflows/github-cxx-qt-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -193,13 +193,8 @@ jobs:
# Ensure clippy and rustfmt is installed, they should come from github runner
#
# Note we still need rustfmt for the cxx-qt-gen tests
#
# TODO: Remove the `rustup default 1.77` selection.
# This is a workaround for Github Actions, which cannot currently compile CXX-Qt with Rust 1.78.
#
# See: https://github.com/KDAB/cxx-qt/issues/958
- name: "Install Rust toolchain"
run: rustup default 1.77 && rustup component add clippy rustfmt
run: rustup component add clippy rustfmt

- name: "Rust tools cache"
uses: actions/cache@v4
Expand Down
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

include(CompilerCaching)
include(CxxQt)

# Enable extra Qt definitions for all projects
add_compile_definitions(
Expand Down
118 changes: 118 additions & 0 deletions cmake/CxxQt.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
# SPDX-FileCopyrightText: 2024 Klarälvdalens Datakonsult AB, a KDAB Group company <[email protected]>
# SPDX-FileContributor: Andrew Hayzen <[email protected]>
# SPDX-FileContributor: Leon Matthes <[email protected]>
#
# SPDX-License-Identifier: MIT OR Apache-2.0

find_package(Corrosion QUIET)
if(NOT Corrosion_FOUND)
include(FetchContent)
FetchContent_Declare(
Corrosion
GIT_REPOSITORY https://github.com/corrosion-rs/corrosion.git
GIT_TAG v0.5.0
)

FetchContent_MakeAvailable(Corrosion)
endif()

function(cxxqt_import_crate)
cmake_parse_arguments(IMPORT_CRATE "" "CXXQT_EXPORT_DIR;QMAKE" "" ${ARGN})

corrosion_import_crate(IMPORTED_CRATES __cxxqt_imported_crates ${IMPORT_CRATE_UNPARSED_ARGUMENTS})

message(STATUS "CXX-Qt Found crate(s): ${__cxxqt_imported_crates}")

if (NOT DEFINED IMPORT_CRATE_CXXQT_EXPORT_DIR)
set(IMPORT_CRATE_CXXQT_EXPORT_DIR "${CMAKE_CURRENT_BINARY_DIR}/cxxqt/")
endif()
message(VERBOSE "CXX-Qt EXPORT_DIR: ${IMPORT_CRATE_CXXQT_EXPORT_DIR}")

if (NOT DEFINED IMPORT_CRATE_QMAKE)
get_target_property(QMAKE Qt::qmake IMPORTED_LOCATION)
if (NOT QMAKE STREQUAL "QMAKE-NOTFOUND")
set(IMPORT_CRATE_QMAKE "${QMAKE}")
else()
message(FATAL_ERROR "cxxqt_import_crate: QMAKE is not defined and could not be queried from the Qt::qmake target!\nPlease use the QMAKE argument to specify the path to the qmake executable or use find_package(Qt) before calling this function.")
endif()
endif()

foreach(CRATE ${__cxxqt_imported_crates})
corrosion_set_env_vars(${CRATE}
"CXXQT_EXPORT_DIR=${IMPORT_CRATE_CXXQT_EXPORT_DIR}"
"QMAKE=${IMPORT_CRATE_QMAKE}"
$<$<BOOL:${CMAKE_RUSTC_WRAPPER}>:RUSTC_WRAPPER=${CMAKE_RUSTC_WRAPPER}>)

file(MAKE_DIRECTORY "${IMPORT_CRATE_CXXQT_EXPORT_DIR}/include/${CRATE}")
target_include_directories(${CRATE} INTERFACE "${IMPORT_CRATE_CXXQT_EXPORT_DIR}/include/${CRATE}")

set_target_properties(${CRATE}
PROPERTIES
CXXQT_EXPORT_DIR "${IMPORT_CRATE_CXXQT_EXPORT_DIR}")

# When using the Ninja generator, we need to provide **some** way to generate the object file
# Unfortunately I'm not able to tell corrosion that this obj file is indeed a byproduct, so
# create a fake target for it.
# This target doesn't need to do anything, because the file should already exist after building the crate.
add_custom_target(${CRATE}_mock_initializers
COMMAND ${CMAKE_COMMAND} -E true
DEPENDS ${CRATE}
BYPRODUCTS "${IMPORT_CRATE_CXXQT_EXPORT_DIR}/initializers.o")

add_library(${CRATE}_initializers OBJECT IMPORTED)
set_target_properties(${CRATE}_initializers
PROPERTIES
IMPORTED_OBJECTS "${IMPORT_CRATE_CXXQT_EXPORT_DIR}/initializers.o")
# Note that we need to link using TARGET_OBJECTS, so that the object files are included **transitively**, otherwise
# Only the linker flags from the object library would be included, but not the actual object files.
# See also the "Linking Object Libraries" and "Linking Object Libraries via $<TARGET_OBJECTS>" sections:
# https://cmake.org/cmake/help/latest/command/target_link_libraries.html
target_link_libraries(${CRATE} INTERFACE ${CRATE}_initializers $<TARGET_OBJECTS:${CRATE}_initializers>)

message(VERBOSE "CXX-Qt Expects QML plugin: ${QML_MODULE_URI} in directory: ${QML_MODULE_PLUGIN_DIR}")
endforeach()

endfunction()


function(cxxqt_import_qml_module target)
cmake_parse_arguments(QML_MODULE "" "URI;SOURCE_CRATE" "" ${ARGN})

if (NOT DEFINED QML_MODULE_URI)
message(FATAL_ERROR "cxxqt_import_qml_module: URI must be specified!")
endif()

if (NOT DEFINED QML_MODULE_SOURCE_CRATE)
message(FATAL_ERROR "cxxqt_import_qml_module: SOURCE_CRATE must be specified!")
endif()

get_target_property(QML_MODULE_EXPORT_DIR ${QML_MODULE_SOURCE_CRATE} CXXQT_EXPORT_DIR)
get_target_property(QML_MODULE_CRATE_TYPE ${QML_MODULE_SOURCE_CRATE} TYPE)

if (${QML_MODULE_EXPORT_DIR} STREQUAL "QML_MODULE_EXPORT_DIR-NOTFOUND")
message(FATAL_ERROR "cxxqt_import_qml_module: SOURCE_CRATE must be a valid target that has been imported with cxxqt_import_crate!")
endif()

# Note: This needs to match the URI conversion in cxx-qt-build
string(REPLACE "." "_" plugin_name ${QML_MODULE_URI})
set(QML_MODULE_PLUGIN_DIR "${QML_MODULE_EXPORT_DIR}/plugins/${plugin_name}")
file(MAKE_DIRECTORY ${QML_MODULE_PLUGIN_DIR})

# QML plugin - init target
# When using the Ninja generator, we need to provide **some** way to generate the object file
# Unfortunately I'm not able to tell corrosion that this obj file is indeed a byproduct, so
# create a fake target for it.
# This target doesn't need to do anything, because the file should already exist after building the crate.
add_custom_target(${target}_mock_obj_output
COMMAND ${CMAKE_COMMAND} -E true
DEPENDS ${QML_MODULE_SOURCE_CRATE}
BYPRODUCTS "${QML_MODULE_PLUGIN_DIR}/plugin_init.o")

add_library(${target} OBJECT IMPORTED)
set_target_properties(${target}
PROPERTIES
IMPORTED_OBJECTS "${QML_MODULE_PLUGIN_DIR}/plugin_init.o")
target_link_libraries(${target} INTERFACE ${QML_MODULE_SOURCE_CRATE})

message(VERBOSE "CXX-Qt Expects QML plugin: ${QML_MODULE_URI} in directory: ${QML_MODULE_PLUGIN_DIR}")
endfunction()
Loading