Skip to content

Commit a925e02

Browse files
committed
use new corrosion_generate_headers function
1 parent c8e5c58 commit a925e02

File tree

37 files changed

+81
-109
lines changed

37 files changed

+81
-109
lines changed

Cargo.toml

+4
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,7 @@ members = [
2222
"tests/basic_cxx_qt/rust",
2323
"tests/qt_types_standalone/rust",
2424
]
25+
26+
[patch.crates-io]
27+
cxx = { git = "https://github.com/Be-ing/cxx.git", branch = "cxxbuild_output_var" }
28+
cxx-build = { git = "https://github.com/Be-ing/cxx.git", branch = "cxxbuild_output_var" }

book/src/concepts/build_systems.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,5 @@ CXX-Qt can be integrated into existing CMake projects or built with only cargo.
1212
* [CMake Integration](../getting-started/5-cmake-integration.md)
1313
* [Cargo Integration](../getting-started/6-cargo-executable.md)
1414

15-
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,
15+
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,
1616
as documented in [CMake integration](../getting-started/5-cmake-integration.md). However, using C++ build systems besides CMake with CXX-Qt is untested.

crates/cxx-qt-build/src/lib.rs

+5-6
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ fn generate_cxxqt_cpp_files(
221221
/// running `cargo build`. Otherwise [CxxQtBuilder] prefers the newer version by default.
222222
///
223223
/// To use [CxxQtBuilder] for a library to link with a C++ application, specify a directory to output
224-
/// cxx-qt's autogenerated headers by having the C++ build system set the `CXXQT_EXPORT_DIR`
224+
/// cxx-qt's autogenerated headers by having the C++ build system set the `GENERATED_HEADER_DIR`
225225
/// environment variable before calling `cargo build`. Then, add the same directory path to the C++
226226
/// include paths. Also, set the `QMAKE` environment variable to the path of the `qmake` executable
227227
/// for the Qt installation found by the C++ build system. This ensures that the C++ build system and
@@ -350,12 +350,11 @@ impl CxxQtBuilder {
350350

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

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

crates/cxx-qt-gen/src/generator/rust/mod.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ impl GeneratedRustBlocks {
4848

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

5354
syn::parse2(quote! {
5455
unsafe extern "C++" {

crates/cxx-qt-gen/src/writer/cpp/header.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -129,12 +129,13 @@ pub fn write_cpp_header(generated: &GeneratedCppBlocks) -> String {
129129
}}
130130
131131
{forward_declare}
132-
#include "cxx-qt-gen/{cxx_file_stem}.cxx.h"
132+
#include "{crate_name}/{cxx_file_stem}.cxx.h"
133133
134134
{qobjects}
135135
"#,
136136
cxx_file_stem = generated.cxx_file_stem,
137137
forward_declare = forward_declare(generated).join("\n"),
138+
crate_name = std::env::var("CARGO_PKG_NAME").unwrap(),
138139
qobjects = qobjects_header(generated).join("\n"),
139140
}
140141
}

crates/cxx-qt-gen/src/writer/cpp/source.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,11 @@ fn qobjects_source(generated: &GeneratedCppBlocks) -> Vec<String> {
8080
/// For a given GeneratedCppBlocks write this into a C++ source
8181
pub fn write_cpp_source(generated: &GeneratedCppBlocks) -> String {
8282
formatdoc! {r#"
83-
#include "cxx-qt-gen/{cxx_file_stem}.cxxqt.h"
83+
#include "{crate_name}/{cxx_file_stem}.cxxqt.h"
8484
8585
{qobjects}
8686
"#,
87+
crate_name = std::env::var("CARGO_PKG_NAME").unwrap(),
8788
cxx_file_stem = generated.cxx_file_stem,
8889
qobjects = qobjects_source(generated).join("\n"),
8990
}

examples/cargo_without_cmake/CMakeLists.txt

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ if(NOT Corrosion_FOUND)
2626
include(FetchContent)
2727
FetchContent_Declare(
2828
Corrosion
29-
GIT_REPOSITORY https://github.com/corrosion-rs/corrosion.git
30-
GIT_TAG v0.2.1
29+
GIT_REPOSITORY https://github.com/Be-ing/corrosion.git
30+
GIT_TAG code_generators
3131
)
3232

3333
FetchContent_MakeAvailable(Corrosion)

examples/cargo_without_cmake/src/cpp/run.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
#include <QtGui/QGuiApplication>
1212
#include <QtQml/QQmlApplicationEngine>
1313

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

1616
// Include the C++ code generated by rcc from the .qrc file
1717
#include "qml.qrc.cpp"

examples/demo_threading/CMakeLists.txt

+4-8
Original file line numberDiff line numberDiff line change
@@ -27,21 +27,17 @@ if(NOT Corrosion_FOUND)
2727
include(FetchContent)
2828
FetchContent_Declare(
2929
Corrosion
30-
GIT_REPOSITORY https://github.com/corrosion-rs/corrosion.git
31-
GIT_TAG v0.2.1
30+
GIT_REPOSITORY https://github.com/Be-ing/corrosion.git
31+
GIT_TAG code_generators
3232
)
3333

3434
FetchContent_MakeAvailable(Corrosion)
3535
endif()
3636

3737
set(CRATE demo-threading)
3838
corrosion_import_crate(MANIFEST_PATH rust/Cargo.toml CRATES ${CRATE})
39-
set(CXXQT_EXPORT_DIR "${CMAKE_CURRENT_BINARY_DIR}/cxxqt")
40-
corrosion_set_env_vars(${CRATE}
41-
"CXXQT_EXPORT_DIR=${CXXQT_EXPORT_DIR}"
42-
"QMAKE=${QMAKE}"
43-
)
44-
target_include_directories(${CRATE} INTERFACE "${CXXQT_EXPORT_DIR}/${CRATE}")
39+
corrosion_generate_headers(${CRATE})
40+
corrosion_set_env_vars(${CRATE} "QMAKE=${QMAKE}")
4541
target_link_libraries(${CRATE} INTERFACE
4642
Qt::Core
4743
Qt::Gui

examples/demo_threading/cpp/helpers/energyusageproxymodel.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
#include <QAbstractListModel>
1313
#include <QVector>
1414

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

1717
class EnergyUsageProxyModel : public QAbstractListModel
1818
{

examples/demo_threading/cpp/main.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
#include <QtGui/QGuiApplication>
88
#include <QtQml/QQmlApplicationEngine>
99

10-
#include "cxx-qt-gen/energy_usage.cxxqt.h"
10+
#include "demo-threading/energy_usage.cxxqt.h"
1111
#include "helpers/energyusageproxymodel.h"
1212
#include "helpers/sensor.h"
1313

examples/qml_extension_plugin/plugin/CMakeLists.txt

+4-8
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,17 @@ if(NOT Corrosion_FOUND)
1010
include(FetchContent)
1111
FetchContent_Declare(
1212
Corrosion
13-
GIT_REPOSITORY https://github.com/corrosion-rs/corrosion.git
14-
GIT_TAG v0.2.1
13+
GIT_REPOSITORY https://github.com/Be-ing/corrosion.git
14+
GIT_TAG code_generators
1515
)
1616

1717
FetchContent_MakeAvailable(Corrosion)
1818
endif()
1919

2020
set(CRATE qml-extension-plugin)
2121
corrosion_import_crate(MANIFEST_PATH rust/Cargo.toml CRATES ${CRATE})
22-
set(CXXQT_EXPORT_DIR "${CMAKE_CURRENT_BINARY_DIR}/cxxqt")
23-
corrosion_set_env_vars(${CRATE}
24-
"CXXQT_EXPORT_DIR=${CXXQT_EXPORT_DIR}"
25-
"QMAKE=${QMAKE}"
26-
)
27-
target_include_directories(${CRATE} INTERFACE "${CXXQT_EXPORT_DIR}/${CRATE}")
22+
corrosion_generate_headers(${CRATE})
23+
corrosion_set_env_vars(${CRATE} "QMAKE=${QMAKE}")
2824

2925
set(QML_IMPORT_DIR ${CMAKE_CURRENT_BINARY_DIR}/../qml)
3026
set(PLUGIN_OUTPUT_DIR ${QML_IMPORT_DIR}/com/kdab/cxx_qt/demo)

examples/qml_extension_plugin/plugin/cpp/plugin.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
#include <QQmlEngine>
1010
#include <QQmlExtensionPlugin>
1111

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

1414
class CoreQmlpluginPlugin : public QQmlExtensionPlugin
1515
{

examples/qml_features/CMakeLists.txt

+4-8
Original file line numberDiff line numberDiff line change
@@ -28,21 +28,17 @@ if(NOT Corrosion_FOUND)
2828
include(FetchContent)
2929
FetchContent_Declare(
3030
Corrosion
31-
GIT_REPOSITORY https://github.com/corrosion-rs/corrosion.git
32-
GIT_TAG v0.2.1
31+
GIT_REPOSITORY https://github.com/Be-ing/corrosion.git
32+
GIT_TAG code_generators
3333
)
3434

3535
FetchContent_MakeAvailable(Corrosion)
3636
endif()
3737

3838
set(CRATE qml-features)
3939
corrosion_import_crate(MANIFEST_PATH rust/Cargo.toml CRATES ${CRATE})
40-
set(CXXQT_EXPORT_DIR "${CMAKE_CURRENT_BINARY_DIR}/cxxqt")
41-
corrosion_set_env_vars(${CRATE}
42-
"CXXQT_EXPORT_DIR=${CXXQT_EXPORT_DIR}"
43-
"QMAKE=${QMAKE}"
44-
)
45-
target_include_directories(${CRATE} INTERFACE "${CXXQT_EXPORT_DIR}/${CRATE}")
40+
corrosion_generate_headers(${CRATE})
41+
corrosion_set_env_vars(${CRATE} "QMAKE=${QMAKE}")
4642
target_link_libraries(${CRATE} INTERFACE
4743
Qt::Core
4844
Qt::Gui

examples/qml_features/cpp/main.cpp

+7-7
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@
88
#include <QtGui/QGuiApplication>
99
#include <QtQml/QQmlApplicationEngine>
1010

11-
#include "cxx-qt-gen/custom_base_class.cxxqt.h"
12-
#include "cxx-qt-gen/rust_invokables.cxxqt.h"
13-
#include "cxx-qt-gen/rust_properties.cxxqt.h"
14-
#include "cxx-qt-gen/rust_signals.cxxqt.h"
15-
#include "cxx-qt-gen/serialisation.cxxqt.h"
16-
#include "cxx-qt-gen/threading_website.cxxqt.h"
17-
#include "cxx-qt-gen/types.cxxqt.h"
11+
#include "qml-features/custom_base_class.cxxqt.h"
12+
#include "qml-features/rust_invokables.cxxqt.h"
13+
#include "qml-features/rust_properties.cxxqt.h"
14+
#include "qml-features/rust_signals.cxxqt.h"
15+
#include "qml-features/serialisation.cxxqt.h"
16+
#include "qml-features/threading_website.cxxqt.h"
17+
#include "qml-features/types.cxxqt.h"
1818

1919
int
2020
main(int argc, char* argv[])

examples/qml_features/tests/main.cpp

+7-7
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@
88
#include <QtQml/QQmlEngine>
99
#include <QtQuickTest/quicktest.h>
1010

11-
#include "cxx-qt-gen/custom_base_class.cxxqt.h"
12-
#include "cxx-qt-gen/rust_invokables.cxxqt.h"
13-
#include "cxx-qt-gen/rust_properties.cxxqt.h"
14-
#include "cxx-qt-gen/rust_signals.cxxqt.h"
15-
#include "cxx-qt-gen/serialisation.cxxqt.h"
16-
#include "cxx-qt-gen/threading_website.cxxqt.h"
17-
#include "cxx-qt-gen/types.cxxqt.h"
11+
#include "qml-features/custom_base_class.cxxqt.h"
12+
#include "qml-features/rust_invokables.cxxqt.h"
13+
#include "qml-features/rust_properties.cxxqt.h"
14+
#include "qml-features/rust_signals.cxxqt.h"
15+
#include "qml-features/serialisation.cxxqt.h"
16+
#include "qml-features/threading_website.cxxqt.h"
17+
#include "qml-features/types.cxxqt.h"
1818

1919
class Setup : public QObject
2020
{

examples/qml_minimal/CMakeLists.txt

+7-17
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ if(NOT Corrosion_FOUND)
3636
include(FetchContent)
3737
FetchContent_Declare(
3838
Corrosion
39-
GIT_REPOSITORY https://github.com/corrosion-rs/corrosion.git
40-
GIT_TAG v0.2.1
39+
GIT_REPOSITORY https://github.com/Be-ing/corrosion.git
40+
GIT_TAG code_generators
4141
)
4242

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

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

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

6959
# Link the Rust INTERFACE library target to Qt. Do this on the library target
7060
# rather than the main executable. This way, CMake targets besides the main

examples/qml_minimal/cpp/main.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
#include <QtQml/QQmlApplicationEngine>
1212

1313
// ANCHOR: book_cpp_include
14-
#include "cxx-qt-gen/my_object.cxxqt.h"
14+
#include "qml-minimal/my_object.cxxqt.h"
1515
// ANCHOR_END: book_cpp_include
1616

1717
int

examples/qml_minimal/tests/myobject/tst_myobject.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
#include <QtQml/QQmlEngine>
1010
#include <QtQuickTest/quicktest.h>
1111

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

1414
class Setup : public QObject
1515
{

tests/basic_cxx_only/CMakeLists.txt

+2-6
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,8 @@ get_target_property(QMAKE Qt::qmake IMPORTED_LOCATION)
2727

2828
set(CRATE basic-cxx-only)
2929
corrosion_import_crate(MANIFEST_PATH rust/Cargo.toml CRATES ${CRATE})
30-
set(CXXQT_EXPORT_DIR "${CMAKE_CURRENT_BINARY_DIR}/cxxqt")
31-
corrosion_set_env_vars(${CRATE}
32-
"CXXQT_EXPORT_DIR=${CXXQT_EXPORT_DIR}"
33-
"QMAKE=${QMAKE}"
34-
)
35-
target_include_directories(${CRATE} INTERFACE "${CXXQT_EXPORT_DIR}/${CRATE}")
30+
corrosion_generate_headers(${CRATE})
31+
corrosion_set_env_vars(${CRATE} "QMAKE=${QMAKE}")
3632
target_link_libraries(${CRATE} INTERFACE
3733
Qt::Core
3834
Qt::Gui

tests/basic_cxx_only/cpp/main.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
// SPDX-License-Identifier: MIT OR Apache-2.0
88
#include <QtTest/QTest>
99

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

1212
int hidden_num = 100;
1313

tests/basic_cxx_qt/CMakeLists.txt

+2-6
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,8 @@ get_target_property(QMAKE Qt::qmake IMPORTED_LOCATION)
2626

2727
set(CRATE basic-cxx-qt)
2828
corrosion_import_crate(MANIFEST_PATH rust/Cargo.toml CRATES ${CRATE})
29-
set(CXXQT_EXPORT_DIR "${CMAKE_CURRENT_BINARY_DIR}/cxxqt")
30-
corrosion_set_env_vars(${CRATE}
31-
"CXXQT_EXPORT_DIR=${CXXQT_EXPORT_DIR}"
32-
"QMAKE=${QMAKE}"
33-
)
34-
target_include_directories(${CRATE} INTERFACE "${CXXQT_EXPORT_DIR}/${CRATE}")
29+
corrosion_generate_headers(${CRATE})
30+
corrosion_set_env_vars(${CRATE} "QMAKE=${QMAKE}")
3531
target_link_libraries(${CRATE} INTERFACE
3632
Qt::Core
3733
Qt::Gui

tests/basic_cxx_qt/cpp/main.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@
99
#include <QtTest/QSignalSpy>
1010
#include <QtTest/QTest>
1111

12-
#include "cxx-qt-gen/empty.cxxqt.h"
13-
#include "cxx-qt-gen/my_data.cxxqt.h"
14-
#include "cxx-qt-gen/my_object.cxxqt.h"
15-
#include "cxx-qt-gen/my_types.cxxqt.h"
12+
#include "basic-cxx-qt/empty.cxxqt.h"
13+
#include "basic-cxx-qt/my_data.cxxqt.h"
14+
#include "basic-cxx-qt/my_object.cxxqt.h"
15+
#include "basic-cxx-qt/my_types.cxxqt.h"
1616

1717
class CxxQtTest : public QObject
1818
{

tests/qt_types_standalone/CMakeLists.txt

+2-6
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,8 @@ get_target_property(QMAKE Qt::qmake IMPORTED_LOCATION)
2727

2828
set(CRATE qt-types-standalone)
2929
corrosion_import_crate(MANIFEST_PATH rust/Cargo.toml CRATES ${CRATE})
30-
set(CXXQT_EXPORT_DIR "${CMAKE_CURRENT_BINARY_DIR}/cxxqt")
31-
corrosion_set_env_vars(${CRATE}
32-
"CXXQT_EXPORT_DIR=${CXXQT_EXPORT_DIR}"
33-
"QMAKE=${QMAKE}"
34-
)
35-
target_include_directories(${CRATE} INTERFACE "${CXXQT_EXPORT_DIR}/${CRATE}")
30+
corrosion_generate_headers(${CRATE})
31+
corrosion_set_env_vars(${CRATE} "QMAKE=${QMAKE}")
3632
target_link_libraries(${CRATE} INTERFACE
3733
Qt::Core
3834
Qt::Gui

tests/qt_types_standalone/cpp/qcolor.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
#include <QtGui/QColor>
1010
#include <QtTest/QTest>
1111

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

1414
class QColorTest : public QObject
1515
{

tests/qt_types_standalone/cpp/qdate.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
#include <QtCore/QDate>
1010
#include <QtTest/QTest>
1111

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

1414
class QDateTest : public QObject
1515
{

tests/qt_types_standalone/cpp/qdatetime.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
#include <QtCore/QDateTime>
1010
#include <QtTest/QTest>
1111

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

1414
class QDateTimeTest : public QObject
1515
{

0 commit comments

Comments
 (0)