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

(Fixes cross compilation) Default to using glib-compile-resources bin before using glib_build_tools crate #1223

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
5 changes: 4 additions & 1 deletion crates/rnote-ui/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,10 @@ url = { workspace = true }

[build-dependencies]
anyhow = { workspace = true }
glib-build-tools = { workspace = true }
glib-build-tools = { workspace = true, optional = true }

[features]
use_glib_build_tools = ["glib-build-tools"]
Copy link
Owner

@flxzt flxzt Nov 2, 2024

Choose a reason for hiding this comment

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

It should be mentioned here that this feature is enabled progammatically in the build.rs file and not supposed to be used by the user


[target.'cfg(windows)'.build-dependencies]
winresource = { workspace = true }
75 changes: 67 additions & 8 deletions crates/rnote-ui/build.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
fn main() -> anyhow::Result<()> {
compile_gresources()?;
let is_cross_compiling = detect_cross_compilation();

if !is_cross_compiling {
println!("cargo:rustc-cfg=feature=\"use_glib_build_tools\"");
}

compile_gresources(is_cross_compiling)?;

#[cfg(windows)]
compile_icon_winres()?;
Expand All @@ -18,11 +24,64 @@ fn compile_icon_winres() -> anyhow::Result<()> {
.context("Failed to compile winresource resource")
}

fn compile_gresources() -> anyhow::Result<()> {
glib_build_tools::compile_resources(
&["data"],
"data/resources.gresource.xml",
"compiled.gresource",
);
Ok(())
fn detect_cross_compilation() -> bool {
let host = std::env::var("HOST").unwrap_or_default();
let target = std::env::var("TARGET").unwrap_or_default();
host != target
}

fn compile_gresources(is_cross_compiling: bool) -> anyhow::Result<()> {
use std::env;
use std::path::PathBuf;
use std::process::Command;

let out_dir = env::var("OUT_DIR").expect("OUT_DIR not set");
let output_path = PathBuf::from(&out_dir).join("compiled.gresource");

// First, try using the system's glib-compile-resources
let system_result = Command::new("glib-compile-resources")
.args(&[
"--sourcedir=data",
"data/resources.gresource.xml",
&format!("--target={}", output_path.display()),
])
.status();

match system_result {
Ok(status) if status.success() => return Ok(()),
Ok(_) => println!("glib-compile-resources command failed, trying fallback method..."),
Err(e) if e.kind() == std::io::ErrorKind::NotFound => {
println!("glib-compile-resources not found, trying fallback method...")
}
Err(e) => println!(
"Error executing glib-compile-resources: {}, trying fallback method...",
e
),
}

// If cross-compiling, don't use glib_build_tools
if is_cross_compiling {
return Err(anyhow::anyhow!(
"Failed to compile gresources: system glib-compile-resources failed and we're cross-compiling. \
Please ensure you have glib development tools installed on your target system."
));
}

// If not cross-compiling and system command fails, fall back to glib_build_tools if available
#[cfg(feature = "use_glib_build_tools")]
{
println!("Attempting to use glib_build_tools::compile_resources...");
glib_build_tools::compile_resources(
&["data"],
"data/resources.gresource.xml",
output_path.to_str().unwrap(),
);
Ok(())
}

#[cfg(not(feature = "use_glib_build_tools"))]
Err(anyhow::anyhow!(
"Failed to compile gresources: system glib-compile-resources failed and glib_build_tools is not available. \
Please ensure you have glib development tools installed on your system."
))
}