-
-
Notifications
You must be signed in to change notification settings - Fork 127
Implement glib_build_tools::compile_schemas
#1721
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -57,3 +57,64 @@ pub fn compile_resources<P: AsRef<Path>>(source_dirs: &[P], gresource: &str, tar | |
println!("cargo:rerun-if-changed={dep}"); | ||
} | ||
} | ||
|
||
// rustdoc-stripper-ignore-next | ||
/// Call to run `glib-compile-schemas` to generate compiled gschemas from `.gschema.xml` schemas | ||
/// files. | ||
/// | ||
/// ```no_run | ||
/// glib_build_tools::compile_schemas( | ||
/// &["schemas"] | ||
/// ); | ||
/// ``` | ||
pub fn compile_schemas(schemas_dir: &[&str]) { | ||
let out_dir = env::var("OUT_DIR").unwrap(); | ||
let out_dir = Path::new(&out_dir); | ||
|
||
let target_dir = out_dir.join("gschemas"); | ||
|
||
// Ensure target_dir exists | ||
std::fs::create_dir_all(&target_dir).expect("Failed to create target directory"); | ||
|
||
println!( | ||
"cargo:rustc-env=GSETTINGS_SCHEMA_DIR={}", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How would this be used? |
||
target_dir.to_str().unwrap() | ||
); | ||
Comment on lines
+79
to
+82
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. https://docs.gtk.org/gio/overview.html#running-gio-applications
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. But this is passing the environment variable to rustc, it doesn't make your application run with it There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can run the application this way, but of course, the compiled schema binary must be installed in the I'm trying to implement a similar approach to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @sdroege Here is a demo: https://github.com/shahradelahi/todo-gtk-rs There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think this makes sense really. The correct way for all this is to install the schemas in the right location and the environment variable is meant for uninstalled usage or special setups. cargo has no mechanism for installing anything but an executable, so the usual solution for this is to put another, proper build system around cargo. E.g. having a Makefile (which hopefully does things correctly enough), or using meson or cmake. I don't think what you're trying to achieve here (installing schemas) is possible with cargo in its current form but would need more features in cargo first. |
||
|
||
// Recursively copy all files with .gschema.xml extension from schema_dir to target_dir | ||
for schema_dir in schemas_dir { | ||
let entries = Path::new(schema_dir) | ||
.read_dir() | ||
.expect("Failed to read schema directory") | ||
.flatten(); | ||
|
||
for entry in entries { | ||
let path = entry.path(); | ||
let file_name = path.file_name().unwrap().to_str().unwrap(); | ||
|
||
if path.is_file() && file_name.ends_with(".gschema.xml") { | ||
let target_path = target_dir.join(path.file_name().unwrap()); | ||
std::fs::copy(&path, &target_path).expect("Failed to copy schema file"); | ||
} | ||
} | ||
} | ||
|
||
let mut command = Command::new("glib-compile-schemas"); | ||
command.arg("--strict"); | ||
command.arg(target_dir); | ||
|
||
let output = command | ||
.output() | ||
.expect("Failed to execute glib-compile-schemas"); | ||
|
||
assert!( | ||
output.status.success(), | ||
"glib-compile-schemas failed with exit status {} and stderr:\n{}", | ||
output.status, | ||
String::from_utf8_lossy(&output.stderr) | ||
); | ||
|
||
for schema_dir in schemas_dir { | ||
println!("cargo:rerun-if-changed={}", schema_dir); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Installing things as part of
build.rs
is an anti-pattern and unexpected. You should not place (or modify) anything outside ofOUT_DIR
, see https://doc.rust-lang.org/cargo/reference/environment-variables.html#environment-variables-cargo-sets-for-build-scripts