Skip to content

use new corrosion_add_target_generated_headers function #341

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,7 @@ members = [
"tests/basic_cxx_qt/rust",
"tests/qt_types_standalone/rust",
]

[patch.crates-io]
cxx = { git = "https://github.com/Be-ing/cxx.git", branch = "cxxbuild_output_var" }
cxx-build = { git = "https://github.com/Be-ing/cxx.git", branch = "cxxbuild_output_var" }
2 changes: 1 addition & 1 deletion book/src/concepts/build_systems.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ CXX-Qt can be integrated into existing CMake projects or built with only cargo.
* [CMake Integration](../getting-started/5-cmake-integration.md)
* [Cargo Integration](../getting-started/6-cargo-executable.md)

CXX-Qt could work with any C++ build system so long as the `QMAKE` and `CXXQT_EXPORT_DIR` environment variables are set before calling Cargo,
CXX-Qt could work with any C++ build system so long as the `QMAKE` and `GENERATED_HEADER_DIR` environment variables are set before calling Cargo,
as documented in [CMake integration](../getting-started/5-cmake-integration.md). However, using C++ build systems besides CMake with CXX-Qt is untested.
11 changes: 5 additions & 6 deletions crates/cxx-qt-build/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ fn generate_cxxqt_cpp_files(
/// running `cargo build`. Otherwise [CxxQtBuilder] prefers the newer version by default.
///
/// To use [CxxQtBuilder] for a library to link with a C++ application, specify a directory to output
/// cxx-qt's autogenerated headers by having the C++ build system set the `CXXQT_EXPORT_DIR`
/// cxx-qt's autogenerated headers by having the C++ build system set the `GENERATED_HEADER_DIR`
/// environment variable before calling `cargo build`. Then, add the same directory path to the C++
/// include paths. Also, set the `QMAKE` environment variable to the path of the `qmake` executable
/// for the Qt installation found by the C++ build system. This ensures that the C++ build system and
Expand Down Expand Up @@ -350,12 +350,11 @@ impl CxxQtBuilder {

// The include directory needs to be namespaced by crate name when exporting for a C++ build system,
// but for using cargo build without a C++ build system, OUT_DIR is already namespaced by crate name.
let header_root = match env::var("CXXQT_EXPORT_DIR") {
Ok(export_dir) => format!("{}/{}", export_dir, env::var("CARGO_PKG_NAME").unwrap()),
Err(_) => env::var("OUT_DIR").unwrap(),
};
let header_root =
env::var("GENERATED_HEADER_DIR").unwrap_or_else(|_| env::var("OUT_DIR").unwrap());
let generated_header_dir =
format!("{}/{}", header_root, env::var("CARGO_PKG_NAME").unwrap());
self.cc_builder.include(&header_root);
let generated_header_dir = format!("{}/cxx-qt-gen", header_root);

cxx_qt_lib_headers::write_headers(&format!("{}/cxx-qt-lib", header_root));

Expand Down
3 changes: 2 additions & 1 deletion crates/cxx-qt-gen/src/generator/rust/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ impl GeneratedRustBlocks {

/// Generate the include line for this parsed block
fn generate_include(parser: &Parser) -> Result<Item> {
let import_path = format!("cxx-qt-gen/{}.cxxqt.h", parser.cxx_file_stem);
let crate_name = std::env::var("CARGO_PKG_NAME").unwrap();
let import_path = format!("{}/{}.cxxqt.h", crate_name, parser.cxx_file_stem);

syn::parse2(quote! {
unsafe extern "C++" {
Expand Down
3 changes: 2 additions & 1 deletion crates/cxx-qt-gen/src/writer/cpp/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,12 +129,13 @@ pub fn write_cpp_header(generated: &GeneratedCppBlocks) -> String {
}}

{forward_declare}
#include "cxx-qt-gen/{cxx_file_stem}.cxx.h"
#include "{crate_name}/{cxx_file_stem}.cxx.h"

{qobjects}
"#,
cxx_file_stem = generated.cxx_file_stem,
forward_declare = forward_declare(generated).join("\n"),
crate_name = std::env::var("CARGO_PKG_NAME").unwrap(),
qobjects = qobjects_header(generated).join("\n"),
}
}
Expand Down
3 changes: 2 additions & 1 deletion crates/cxx-qt-gen/src/writer/cpp/source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,11 @@ fn qobjects_source(generated: &GeneratedCppBlocks) -> Vec<String> {
/// For a given GeneratedCppBlocks write this into a C++ source
pub fn write_cpp_source(generated: &GeneratedCppBlocks) -> String {
formatdoc! {r#"
#include "cxx-qt-gen/{cxx_file_stem}.cxxqt.h"
#include "{crate_name}/{cxx_file_stem}.cxxqt.h"

{qobjects}
"#,
crate_name = std::env::var("CARGO_PKG_NAME").unwrap(),
cxx_file_stem = generated.cxx_file_stem,
qobjects = qobjects_source(generated).join("\n"),
}
Expand Down
4 changes: 2 additions & 2 deletions examples/cargo_without_cmake/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ if(NOT Corrosion_FOUND)
include(FetchContent)
FetchContent_Declare(
Corrosion
GIT_REPOSITORY https://github.com/corrosion-rs/corrosion.git
GIT_TAG v0.2.1
GIT_REPOSITORY https://github.com/Be-ing/corrosion.git
GIT_TAG code_generators
)

FetchContent_MakeAvailable(Corrosion)
Expand Down
2 changes: 1 addition & 1 deletion examples/cargo_without_cmake/src/cpp/run.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#include <QtGui/QGuiApplication>
#include <QtQml/QQmlApplicationEngine>

#include "cxx-qt-gen/my_object.cxxqt.h"
#include "qml-minimal-no-cmake/my_object.cxxqt.h"

// Include the C++ code generated by rcc from the .qrc file
#include "qml.qrc.cpp"
Expand Down
12 changes: 4 additions & 8 deletions examples/demo_threading/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,17 @@ if(NOT Corrosion_FOUND)
include(FetchContent)
FetchContent_Declare(
Corrosion
GIT_REPOSITORY https://github.com/corrosion-rs/corrosion.git
GIT_TAG v0.2.1
GIT_REPOSITORY https://github.com/Be-ing/corrosion.git
GIT_TAG code_generators
)

FetchContent_MakeAvailable(Corrosion)
endif()

set(CRATE demo-threading)
corrosion_import_crate(MANIFEST_PATH rust/Cargo.toml CRATES ${CRATE})
set(CXXQT_EXPORT_DIR "${CMAKE_CURRENT_BINARY_DIR}/cxxqt")
corrosion_set_env_vars(${CRATE}
"CXXQT_EXPORT_DIR=${CXXQT_EXPORT_DIR}"
"QMAKE=${QMAKE}"
)
target_include_directories(${CRATE} INTERFACE "${CXXQT_EXPORT_DIR}/${CRATE}")
corrosion_add_target_generated_headers(${CRATE})
corrosion_set_env_vars(${CRATE} "QMAKE=${QMAKE}")
target_link_libraries(${CRATE} INTERFACE
Qt::Core
Qt::Gui
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#include <QAbstractListModel>
#include <QVector>

#include "cxx-qt-gen/energy_usage.cxxqt.h"
#include "demo-threading/energy_usage.cxxqt.h"

class EnergyUsageProxyModel : public QAbstractListModel
{
Expand Down
2 changes: 1 addition & 1 deletion examples/demo_threading/cpp/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include <QtGui/QGuiApplication>
#include <QtQml/QQmlApplicationEngine>

#include "cxx-qt-gen/energy_usage.cxxqt.h"
#include "demo-threading/energy_usage.cxxqt.h"
#include "helpers/energyusageproxymodel.h"
#include "helpers/sensor.h"

Expand Down
12 changes: 4 additions & 8 deletions examples/qml_extension_plugin/plugin/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,17 @@ if(NOT Corrosion_FOUND)
include(FetchContent)
FetchContent_Declare(
Corrosion
GIT_REPOSITORY https://github.com/corrosion-rs/corrosion.git
GIT_TAG v0.2.1
GIT_REPOSITORY https://github.com/Be-ing/corrosion.git
GIT_TAG code_generators
)

FetchContent_MakeAvailable(Corrosion)
endif()

set(CRATE qml-extension-plugin)
corrosion_import_crate(MANIFEST_PATH rust/Cargo.toml CRATES ${CRATE})
set(CXXQT_EXPORT_DIR "${CMAKE_CURRENT_BINARY_DIR}/cxxqt")
corrosion_set_env_vars(${CRATE}
"CXXQT_EXPORT_DIR=${CXXQT_EXPORT_DIR}"
"QMAKE=${QMAKE}"
)
target_include_directories(${CRATE} INTERFACE "${CXXQT_EXPORT_DIR}/${CRATE}")
corrosion_add_target_generated_headers(${CRATE})
corrosion_set_env_vars(${CRATE} "QMAKE=${QMAKE}")

set(QML_IMPORT_DIR ${CMAKE_CURRENT_BINARY_DIR}/../qml)
set(PLUGIN_OUTPUT_DIR ${QML_IMPORT_DIR}/com/kdab/cxx_qt/demo)
Expand Down
2 changes: 1 addition & 1 deletion examples/qml_extension_plugin/plugin/cpp/plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#include <QQmlEngine>
#include <QQmlExtensionPlugin>

#include "cxx-qt-gen/my_object.cxxqt.h"
#include "qml-extension-plugin/my_object.cxxqt.h"

class CoreQmlpluginPlugin : public QQmlExtensionPlugin
{
Expand Down
12 changes: 4 additions & 8 deletions examples/qml_features/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,17 @@ if(NOT Corrosion_FOUND)
include(FetchContent)
FetchContent_Declare(
Corrosion
GIT_REPOSITORY https://github.com/corrosion-rs/corrosion.git
GIT_TAG v0.2.1
GIT_REPOSITORY https://github.com/Be-ing/corrosion.git
GIT_TAG code_generators
)

FetchContent_MakeAvailable(Corrosion)
endif()

set(CRATE qml-features)
corrosion_import_crate(MANIFEST_PATH rust/Cargo.toml CRATES ${CRATE})
set(CXXQT_EXPORT_DIR "${CMAKE_CURRENT_BINARY_DIR}/cxxqt")
corrosion_set_env_vars(${CRATE}
"CXXQT_EXPORT_DIR=${CXXQT_EXPORT_DIR}"
"QMAKE=${QMAKE}"
)
target_include_directories(${CRATE} INTERFACE "${CXXQT_EXPORT_DIR}/${CRATE}")
corrosion_add_target_generated_headers(${CRATE})
corrosion_set_env_vars(${CRATE} "QMAKE=${QMAKE}")
target_link_libraries(${CRATE} INTERFACE
Qt::Core
Qt::Gui
Expand Down
14 changes: 7 additions & 7 deletions examples/qml_features/cpp/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@
#include <QtGui/QGuiApplication>
#include <QtQml/QQmlApplicationEngine>

#include "cxx-qt-gen/custom_base_class.cxxqt.h"
#include "cxx-qt-gen/rust_invokables.cxxqt.h"
#include "cxx-qt-gen/rust_properties.cxxqt.h"
#include "cxx-qt-gen/rust_signals.cxxqt.h"
#include "cxx-qt-gen/serialisation.cxxqt.h"
#include "cxx-qt-gen/threading_website.cxxqt.h"
#include "cxx-qt-gen/types.cxxqt.h"
#include "qml-features/custom_base_class.cxxqt.h"
#include "qml-features/rust_invokables.cxxqt.h"
#include "qml-features/rust_properties.cxxqt.h"
#include "qml-features/rust_signals.cxxqt.h"
#include "qml-features/serialisation.cxxqt.h"
#include "qml-features/threading_website.cxxqt.h"
#include "qml-features/types.cxxqt.h"

int
main(int argc, char* argv[])
Expand Down
14 changes: 7 additions & 7 deletions examples/qml_features/tests/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@
#include <QtQml/QQmlEngine>
#include <QtQuickTest/quicktest.h>

#include "cxx-qt-gen/custom_base_class.cxxqt.h"
#include "cxx-qt-gen/rust_invokables.cxxqt.h"
#include "cxx-qt-gen/rust_properties.cxxqt.h"
#include "cxx-qt-gen/rust_signals.cxxqt.h"
#include "cxx-qt-gen/serialisation.cxxqt.h"
#include "cxx-qt-gen/threading_website.cxxqt.h"
#include "cxx-qt-gen/types.cxxqt.h"
#include "qml-features/custom_base_class.cxxqt.h"
#include "qml-features/rust_invokables.cxxqt.h"
#include "qml-features/rust_properties.cxxqt.h"
#include "qml-features/rust_signals.cxxqt.h"
#include "qml-features/serialisation.cxxqt.h"
#include "qml-features/threading_website.cxxqt.h"
#include "qml-features/types.cxxqt.h"

class Setup : public QObject
{
Expand Down
24 changes: 7 additions & 17 deletions examples/qml_minimal/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ if(NOT Corrosion_FOUND)
include(FetchContent)
FetchContent_Declare(
Corrosion
GIT_REPOSITORY https://github.com/corrosion-rs/corrosion.git
GIT_TAG v0.2.1
GIT_REPOSITORY https://github.com/Be-ing/corrosion.git
GIT_TAG code_generators
)

FetchContent_MakeAvailable(Corrosion)
Expand All @@ -49,22 +49,12 @@ set(CRATE qml-minimal)
# Corrosion creates a CMake target with the same name as the crate.
corrosion_import_crate(MANIFEST_PATH rust/Cargo.toml CRATES ${CRATE})

# The Rust library's build script needs to be told where to output the
# generated headers so CMake can find them. To do this, tell Corrosion
# to set the CXXQT_EXPORT_DIR environment variable when calling `cargo build`.
# Also, set the QMAKE environment variable to ensure the Rust library uses
# the same installation of Qt as CMake.
set(CXXQT_EXPORT_DIR "${CMAKE_CURRENT_BINARY_DIR}/cxxqt")
corrosion_set_env_vars(${CRATE}
"CXXQT_EXPORT_DIR=${CXXQT_EXPORT_DIR}"
"QMAKE=${QMAKE}"
)
# Add the generated headers to the include path
corrosion_add_target_generated_headers(${CRATE})

# Include the headers generated by the Rust library's build script. Each
# crate gets its own subdirectory under CXXQT_EXPORT_DIR. This allows you
# to include headers generated by multiple crates without risk of one crate
# overwriting another's files.
target_include_directories(${CRATE} INTERFACE "${CXXQT_EXPORT_DIR}/${CRATE}")
# Set the QMAKE environment variable to ensure the Rust library uses
# the same installation of Qt as CMake.
corrosion_set_env_vars(${CRATE} "QMAKE=${QMAKE}")

# Link the Rust INTERFACE library target to Qt. Do this on the library target
# rather than the main executable. This way, CMake targets besides the main
Expand Down
2 changes: 1 addition & 1 deletion examples/qml_minimal/cpp/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#include <QtQml/QQmlApplicationEngine>

// ANCHOR: book_cpp_include
#include "cxx-qt-gen/my_object.cxxqt.h"
#include "qml-minimal/my_object.cxxqt.h"
// ANCHOR_END: book_cpp_include

int
Expand Down
2 changes: 1 addition & 1 deletion examples/qml_minimal/tests/myobject/tst_myobject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#include <QtQml/QQmlEngine>
#include <QtQuickTest/quicktest.h>

#include "cxx-qt-gen/my_object.cxxqt.h"
#include "qml-minimal/my_object.cxxqt.h"

class Setup : public QObject
{
Expand Down
8 changes: 2 additions & 6 deletions tests/basic_cxx_only/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,8 @@ get_target_property(QMAKE Qt::qmake IMPORTED_LOCATION)

set(CRATE basic-cxx-only)
corrosion_import_crate(MANIFEST_PATH rust/Cargo.toml CRATES ${CRATE})
set(CXXQT_EXPORT_DIR "${CMAKE_CURRENT_BINARY_DIR}/cxxqt")
corrosion_set_env_vars(${CRATE}
"CXXQT_EXPORT_DIR=${CXXQT_EXPORT_DIR}"
"QMAKE=${QMAKE}"
)
target_include_directories(${CRATE} INTERFACE "${CXXQT_EXPORT_DIR}/${CRATE}")
corrosion_add_target_generated_headers(${CRATE})
corrosion_set_env_vars(${CRATE} "QMAKE=${QMAKE}")
target_link_libraries(${CRATE} INTERFACE
Qt::Core
Qt::Gui
Expand Down
2 changes: 1 addition & 1 deletion tests/basic_cxx_only/cpp/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
// SPDX-License-Identifier: MIT OR Apache-2.0
#include <QtTest/QTest>

#include "cxx-qt-gen/ffi.cxx.h"
#include "basic-cxx-only/ffi.cxx.h"

int hidden_num = 100;

Expand Down
8 changes: 2 additions & 6 deletions tests/basic_cxx_qt/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,8 @@ get_target_property(QMAKE Qt::qmake IMPORTED_LOCATION)

set(CRATE basic-cxx-qt)
corrosion_import_crate(MANIFEST_PATH rust/Cargo.toml CRATES ${CRATE})
set(CXXQT_EXPORT_DIR "${CMAKE_CURRENT_BINARY_DIR}/cxxqt")
corrosion_set_env_vars(${CRATE}
"CXXQT_EXPORT_DIR=${CXXQT_EXPORT_DIR}"
"QMAKE=${QMAKE}"
)
target_include_directories(${CRATE} INTERFACE "${CXXQT_EXPORT_DIR}/${CRATE}")
corrosion_add_target_generated_headers(${CRATE})
corrosion_set_env_vars(${CRATE} "QMAKE=${QMAKE}")
target_link_libraries(${CRATE} INTERFACE
Qt::Core
Qt::Gui
Expand Down
8 changes: 4 additions & 4 deletions tests/basic_cxx_qt/cpp/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
#include <QtTest/QSignalSpy>
#include <QtTest/QTest>

#include "cxx-qt-gen/empty.cxxqt.h"
#include "cxx-qt-gen/my_data.cxxqt.h"
#include "cxx-qt-gen/my_object.cxxqt.h"
#include "cxx-qt-gen/my_types.cxxqt.h"
#include "basic-cxx-qt/empty.cxxqt.h"
#include "basic-cxx-qt/my_data.cxxqt.h"
#include "basic-cxx-qt/my_object.cxxqt.h"
#include "basic-cxx-qt/my_types.cxxqt.h"

class CxxQtTest : public QObject
{
Expand Down
8 changes: 2 additions & 6 deletions tests/qt_types_standalone/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,8 @@ get_target_property(QMAKE Qt::qmake IMPORTED_LOCATION)

set(CRATE qt-types-standalone)
corrosion_import_crate(MANIFEST_PATH rust/Cargo.toml CRATES ${CRATE})
set(CXXQT_EXPORT_DIR "${CMAKE_CURRENT_BINARY_DIR}/cxxqt")
corrosion_set_env_vars(${CRATE}
"CXXQT_EXPORT_DIR=${CXXQT_EXPORT_DIR}"
"QMAKE=${QMAKE}"
)
target_include_directories(${CRATE} INTERFACE "${CXXQT_EXPORT_DIR}/${CRATE}")
corrosion_add_target_generated_headers(${CRATE})
corrosion_set_env_vars(${CRATE} "QMAKE=${QMAKE}")
target_link_libraries(${CRATE} INTERFACE
Qt::Core
Qt::Gui
Expand Down
2 changes: 1 addition & 1 deletion tests/qt_types_standalone/cpp/qcolor.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#include <QtGui/QColor>
#include <QtTest/QTest>

#include "cxx-qt-gen/qcolor_cxx.cxx.h"
#include "qt-types-standalone/qcolor_cxx.cxx.h"

class QColorTest : public QObject
{
Expand Down
2 changes: 1 addition & 1 deletion tests/qt_types_standalone/cpp/qdate.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#include <QtCore/QDate>
#include <QtTest/QTest>

#include "cxx-qt-gen/qdate_cxx.cxx.h"
#include "qt-types-standalone/qdate_cxx.cxx.h"

class QDateTest : public QObject
{
Expand Down
2 changes: 1 addition & 1 deletion tests/qt_types_standalone/cpp/qdatetime.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#include <QtCore/QDateTime>
#include <QtTest/QTest>

#include "cxx-qt-gen/qdatetime_cxx.cxx.h"
#include "qt-types-standalone/qdatetime_cxx.cxx.h"

class QDateTimeTest : public QObject
{
Expand Down
Loading