CLI tool for generating C++ sources at CMake configure stage #146
-
Requirements
SolutionCLI ToolAs suggested in #106 we should use a CLI tool similar to
This could look like cxxqtbuilder --header --cpp-namespace-prefix=cxx_qt src/mod/object.rs > path/to/build/gen/object.h
cxxqtbuilder --cpp-namespace-prefix=cxx_qt src/mod/object.rs > path/to/build/gen/object.cpp It also should have an option for generating cxx-qt-lib sources cxxqtbuilder --generate-cxx-qt-lib-sources --out-dir path/to/build/lib/ CMake configureThis tool can then be wrapped by a CMake helper cxx_qt_gen(
GENERATED_SOURCES
src/mod/object.rs
src/mod/another.rs
OUTPUT_DIRECTORY path/to/output/directory
)
add_executable(
appbinary
src/main.cpp
src/qml.qrc
${GENERATED_SOURCES}
) OUTPUT_DIRECTORY would be optional and default to (a subdirectory of) CMAKE_CURRENT_BINARY_DIR. There could be an optional NAMESPACE argument for the C++ namespace too. CMake BuildFor the building part we may be able to use https://github.com/corrosion-rs/corrosion to build and link the Rust crate to our Qt binary. As this would be a typical build now. Problems / Further discussions
|
Beta Was this translation helpful? Give feedback.
Replies: 8 comments 39 replies
-
An alternative CLI could be simpler:
This would generate both |
Beta Was this translation helpful? Give feedback.
-
How about combining these into one CMake function?
OUTPUT_DIRECTORY would be optional and default to (a subdirectory of) CMAKE_CURRENT_BINARY_DIR. There could be an optional NAMESPACE argument for the C++ namespace too. |
Beta Was this translation helpful? Give feedback.
-
I think it would be ideal to have the namespace specified within the source code. If you want to change the namespace, it would be odd to have to change the build system to do that. |
Beta Was this translation helpful? Give feedback.
-
Could we turn cxx-qt-lib into a separate library installed along with the cxxqtbuilder tool? IIUC the generated code for cxx-qt-lib isn't changing per application. I'm not sure if this is feasible or desirable though. |
Beta Was this translation helpful? Give feedback.
-
I also wonder if we should drop the generation stuff for QQmlExtensionPlugin and just require you to do it manually, as it's just a simple C++ file + qmldir file 🤔 |
Beta Was this translation helpful? Give feedback.
-
Just a quick thought that came to mind to potentially make implementing this easier: Technically, the only thing that we really need to do at configure time is figure out which files are relevant for the build. I haven't thought this through entirely, especially with regards to the @Be-ing As it seems you'll be the one implementing this, also feel free to ignore my ramblings :) Just something that popped into my head a few days ago. |
Beta Was this translation helpful? Give feedback.
-
Now that I have implemented the C++ code generation with the CLI tool in #161, I realize that building the C++ code that cxx generates from cxx-qt-lib as a library is a prerequisite for using the CLI tool in the build process. Currently, CxxQtBuilder invoked from build.rs is used to dump the C++ source code generated from cxx-qt-lib, which is embedded into the cxx-qt-build Rust library, to the Cargo target directory. This is quite hacky and inefficient. This process builds that generated C++ code repeatedly, once for each example/test. This would not scale well to a large application using many cxx-qt crates. I don't want to replicate this when integrating the CLI tool into the build process. I hacked up a quick proof-of-concept CMakeLists.txt to build the C++ code generated from cxx-qt-lib and link it to Qt. This worked fine. I propose moving to this process for building this repo:
The C++ files generated from cxx-qt-lib would be packaged together with a CMakeLists.txt, the cxx.h header, a CMake config file for |
Beta Was this translation helpful? Give feedback.
-
Follow up on this for posterity: I successfully implemented this in #173, then realized it wasn't a great idea after all due to the practicalities of distributing a Rust executable, a C++ library, and a CMake module which all have to have exactly the same version. Instead, in #174 and #218 I refactored cxx-qt-build so that it not only generates C++ code, but also compiles it, links it with the Rust, and links Qt. This greatly simplified the CMake integration; we don't need our own custom CMake module anymore and instead only need a few calls to Corrosion. This also allows much easier integration with other C++ build systems. All the C++ build system has to do is call |
Beta Was this translation helpful? Give feedback.
Follow up on this for posterity: I successfully implemented this in #173, then realized it wasn't a great idea after all due to the practicalities of distributing a Rust executable, a C++ library, and a CMake module which all have to have exactly the same version. Instead, in #174 and #218 I refactored cxx-qt-build so that it not only generates C++ code, but also compiles it, links it with the Rust, and links Qt. This greatly simplified the CMake integration; we don't need our own custom CMake module anymore and instead only need a few calls to Corrosion. This also allows much easier integration with other C++ build systems. All the C++ build system has to do is call
cargo build
with the …