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

Refactor build script and expose build functions for crate dependents #19

Merged
merged 9 commits into from
Oct 25, 2024

Conversation

dastansam
Copy link
Contributor

@dastansam dastansam commented Aug 2, 2024

This PR tries to address the issue #12 and also do a little bit of refactoring of build script:

  • this crate's build script was refactored to save and pass the crate's manifest path as a path to shipped icons
  • function is exposed under build-utils feature flag for crate dependents to bundle icons and save icon names in the constant.
  • docs updated

@dastansam dastansam force-pushed the refactor-crate-build-script branch 2 times, most recently from 32cf9f1 to 168626c Compare August 2, 2024 13:53
@dastansam dastansam changed the title Extract build specific logic to build crate, refactor crate Refactor build script Aug 2, 2024
@dastansam dastansam changed the title Refactor build script Refactor build script and expose build functions for crate dependents Aug 2, 2024
@dastansam dastansam force-pushed the refactor-crate-build-script branch from 168626c to 07c9fb1 Compare August 2, 2024 15:05
@dastansam dastansam marked this pull request as ready for review August 5, 2024 13:59
Copy link
Contributor

@nazar-pc nazar-pc left a comment

Choose a reason for hiding this comment

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

I expected the whole icons.toml to be replaced with either a section in Cargo.toml or direct code when calling build script. A separate file in the root of the project is annoying.

src/lib.rs Outdated
pub fn initialize_icons() {
gio::resources_register_include!("resources.gresource").unwrap();
#[macro_export]
macro_rules! initialize_icons {
Copy link
Contributor

Choose a reason for hiding this comment

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

Why does this need to be a macro? Looks like a function should work just like before. If you really want to make sure compiler removes it when not necessary add #[inline] to the function definition.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

gio::resources_register_include!("resources.gresource") tries to load the gresource but since I am moving the resource bundling to the application's build script from this crate, it won't find the resources.gresource in the OUT_DIR of this crate

Copy link
Contributor

Choose a reason for hiding this comment

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

Then specify an absolute path there instead of relative

Copy link
Contributor Author

Choose a reason for hiding this comment

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

gio::resources_register_include! tries to read OUT_DIR env variable which is not set in the relm4-icons crate because it doesn't have build script anymore. Or am I missing sth?

Copy link
Contributor

Choose a reason for hiding this comment

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

All the macro does is calling a hidden function internally. You do not need either macro or that function strictly speaking, just copy these 3 lines and call them with whatever arguments you may need:
https://github.com/gtk-rs/gtk-rs-core/blob/e1ad60d1c8b38634f638f50ec2b36cdc62462daf/gio/src/resource.rs#L35-L37

Copy link
Contributor Author

Choose a reason for hiding this comment

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

converted it back to function, copied those 3 lines. but it's loading the gresource in runtime now. is there other way to to make it compile time and include it in the binary?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, to call include_bytes! macro just like above code does to store contents of a file in a constant

Copy link
Contributor

Choose a reason for hiding this comment

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

You provide contents of the file as an argument rather than its path in that case

Cargo.toml Outdated Show resolved Hide resolved
Cargo.toml Outdated Show resolved Hide resolved
src/build_utils.rs Outdated Show resolved Hide resolved
@dastansam dastansam requested a review from nazar-pc August 8, 2024 21:51
src/lib.rs Outdated
pub fn initialize_icons() {
gio::resources_register_include!("resources.gresource").unwrap();
#[macro_export]
macro_rules! initialize_icons {
Copy link
Contributor

Choose a reason for hiding this comment

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

Then specify an absolute path there instead of relative

build_icons/README.md Outdated Show resolved Hide resolved
build_icons/src/lib.rs Outdated Show resolved Hide resolved
build_icons/src/lib.rs Outdated Show resolved Hide resolved
build_icons/README.md Outdated Show resolved Hide resolved
Copy link
Member

@AaronErhardt AaronErhardt left a comment

Choose a reason for hiding this comment

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

From my side there are no additional objections that would prevent merging. Once @nazar-pc is satisfied as well, I suggest we merge the PR and publish a new alpha release that can be tested and gradually improved.

Also thanks for the continuous work on this PR!

@dastansam dastansam force-pushed the refactor-crate-build-script branch from 91adb0e to 133b001 Compare August 27, 2024 12:55
README.md Outdated Show resolved Hide resolved
build_icons/build.rs Outdated Show resolved Hide resolved
@dastansam dastansam requested a review from nazar-pc August 28, 2024 13:52
Copy link
Contributor

@nazar-pc nazar-pc left a comment

Choose a reason for hiding this comment

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

Almost there, also please test this if you can, I think if you did you'd see that it didn't actually work properly yet.

src/lib.rs Outdated
Comment on lines 36 to 39
let display = gdk::Display::default().unwrap();
let theme = gtk::IconTheme::for_display(&display);
theme.add_resource_path("/org/gtkrs/icons/");
theme.add_resource_path("/org/gtkrs/icons/scalable/actions/");
Copy link
Contributor

Choose a reason for hiding this comment

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

This does not look like an equivalent change. It only previously applied if app_id was not specified, but now it applies unconditionally despite the fact that app ID is required.

Looking at generation code, I think you'll want to store prefix in a constant and then call initialize_icons with it. Maybe @AaronErhardt can clarify, but I think it should not be necessary to have both resource paths, just the prefix used in generation code should be sufficient.

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, the code in question is more a hack than an actual documented feature of GTK. It is not strictly harmful to call it unconditionally, but if the user has specified an app ID, the path of the icons can be adjusted such that it doesn't need this hack in the first place (that's why the if was there). We might as well deprecate or remove this code path entirely instad of relying on it.


// Create file that contains the icon names as constants
let constants = format!(
"
/// Path to the shipped icons.
pub const SHIPPED_ICONS_PATH: &str = \"{}/icons\";",
icons_path.display()
manifest_path
Copy link
Contributor

Choose a reason for hiding this comment

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

This will work, but consider proper path handling, including when it has ":

    std::fs::write(
        std::path::Path::new(&out_dir).join(CONSTANTS_FILE),
        manifest_path.join("icons').canonicalize().to_str().unwrap(),
    )
    .unwrap();

Storing OsString would be even more valid, but it requires platform-specific code to decode back on read, this way it will simply fail on non-utf8 characters at build time.

@nazar-pc
Copy link
Contributor

@dastansam one more push, please 🙏

@dastansam dastansam requested a review from nazar-pc October 13, 2024 13:12
Copy link
Contributor

@nazar-pc nazar-pc left a comment

Choose a reason for hiding this comment

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

@AaronErhardt this is almost ready, please take another look when you have a chance

Comment on lines 13 to 19
pub const SHIPPED_ICONS_PATH: &str = \"{}\";",
std::path::Path::new(&manifest_path)
.join("icons")
.canonicalize()
.unwrap()
.to_str()
.unwrap()
Copy link
Contributor

Choose a reason for hiding this comment

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

I'll post the same code snippet again:

    std::fs::write(
        std::path::Path::new(&out_dir).join(CONSTANTS_FILE),
        manifest_path.join("icons').canonicalize().to_str().unwrap(),
    )
    .unwrap();

Note that it stored the path in the file, not in the string inside of the file. Why? Because your version will result in corrupted file if path contains ".

src/lib.rs Outdated Show resolved Hide resolved
@dastansam dastansam force-pushed the refactor-crate-build-script branch from 2d136c0 to 4120f43 Compare October 15, 2024 08:36
@dastansam dastansam requested a review from nazar-pc October 15, 2024 08:36
Copy link
Contributor

@nazar-pc nazar-pc left a comment

Choose a reason for hiding this comment

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

Looks fine to me assuming it works

@AaronErhardt AaronErhardt merged commit c86eda6 into Relm4:main Oct 25, 2024
5 checks passed
@AaronErhardt
Copy link
Member

Thanks for all the work! Let's test the code and work towards a new stable release :)

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.

3 participants