Skip to content

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
61 changes: 61 additions & 0 deletions glib-build-tools/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Copy link
Member

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 of OUT_DIR, see https://doc.rust-lang.org/cargo/reference/environment-variables.html#environment-variables-cargo-sets-for-build-scripts


println!(
"cargo:rustc-env=GSETTINGS_SCHEMA_DIR={}",
Copy link
Member

Choose a reason for hiding this comment

The 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
Copy link
Author

Choose a reason for hiding this comment

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

https://docs.gtk.org/gio/overview.html#running-gio-applications

GSETTINGS_SCHEMA_DIR. This variable can be set to the names of directories to consider when looking for compiled schemas for GSettings, in addition to the glib-2.0/schemas subdirectories of the XDG system data directories. To specify multiple directories, use G_SEARCHPATH_SEPARATOR_S as a separator.

Copy link
Member

Choose a reason for hiding this comment

The 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

Copy link
Author

Choose a reason for hiding this comment

The 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 glib-2.0/schemas directory within one of the directories listed in XDG_DATA_DIRS.

I'm trying to implement a similar approach to compile_resources. After reading through the GLib source code, I couldn't find anything—unless I'm missing something.

Copy link
Author

Choose a reason for hiding this comment

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

Copy link
Member

Choose a reason for hiding this comment

The 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);
}
}
Loading