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

Use method calls to initialize dependencies #1169

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

LeonMatthesKDAB
Copy link
Collaborator

This speeds up rebuilds, as we only need to link to upstream
dependencies initializers, not build their initializers every time.

It should also allow us to manually initialize certain libraries down
the line with something similar to Q_INIT_RESOURCE, etc.

Closes #1166

Comment on lines 948 to 944
// Build static initializers into a library that we link with whole-archive
// Then use object files just to trigger the initial call.
init_builder
.clone()
.file(initializers_file)
.link_lib_modifier("+whole-archive")
.compile(&format!("{}-initializers", crate_name()));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is the whole-archive for the cargo build or cmake build or both ?

and then why is this needed ? can these initialiser functions not be public exported functions that are built into a library. Then when linking the library the object file that is also links makes use of the public functions preventing them being optimised out ?

.map(|init_fun| {
(
// declaration
format!("extern \"C\" int {init_fun}();"),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

::std::int32_t ?

})
.unzip();
let init_function = format!(
"{declarations}\nextern \"C\" int {init_fun}() {{\n{calls}\nreturn 42;\n}}",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

::std::int32_t ?

.compile(&format!("{}-initializers", crate_name()));

let init_call = format!(
"extern \"C\" int {init_fun}();\nstatic const int do_{init_fun} = {init_fun}();",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

::std::int32_t ?

@@ -11,163 +11,102 @@
#include <cxx-qt-lib/qset.h>
#include <cxx-qt-lib/qvector.h>

static const int register_QHash_i32_QByteArray =
extern "C" int
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

::std::int32_t ?

qRegisterMetaType<::QVector_u64>("QVector_u64");

return 42;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do you return 42 ? Seems to be a magic value that isn't used anywhere ? Can you not use void ? If you do need to return something to indicate success why not bool or true/false ? If this is a magic value then can we have a static const in cxx-qt or exposed from rust somewhere ?

/// The build system must ensure that if the .cpp file is built into a static library, either
/// the `+whole-archive` flag is used, or the initializer function is called by the
/// application.
pub fn qrc(&mut self, input_file: &impl AsRef<Path>) -> (PathBuf, String) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wonder if in the refactor we should have a type for a ProcessedFile or something that then has input path, source, output path, any initialiser function etc wrapped up.

@LeonMatthesKDAB LeonMatthesKDAB force-pushed the 1166-call-initializers branch 3 times, most recently from fc2ed2a to 9b66808 Compare January 29, 2025 13:18
Copy link

codecov bot commented Jan 29, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 100.00%. Comparing base (cdc5ef9) to head (3dba56b).

Additional details and impacted files
@@            Coverage Diff            @@
##              main     #1169   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           71        71           
  Lines        11967     11967           
=========================================
  Hits         11967     11967           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@LeonMatthesKDAB LeonMatthesKDAB force-pushed the 1166-call-initializers branch 7 times, most recently from 40e7f38 to b76d33d Compare January 30, 2025 14:08
This speeds up rebuilds, as we only need to link to upstream
dependencies initializers, not build their initializers every time.

It should also allow us to manually initialize certain libraries down
the line with something similar to Q_INIT_RESOURCE, etc.

Closes KDAB#1166
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

CXX-Qt-build: Refactor initializers to just call downstream dependencies
2 participants