From 2f29eac694353bc94cc7c187f9874827e8d8de87 Mon Sep 17 00:00:00 2001 From: Shahrad Elahi Date: Mon, 19 May 2025 09:56:11 +0100 Subject: [PATCH 1/2] build-tools: Implement `compile_schemas` helper for compiling GSettings schemas It generates the `gschemas.compiled` file by copying all `.gschema.xml` files from the specified directories into the `glib-2.0/schemas` cache directory and then running `glib-compile-schemas --strict` to compile them. --- glib-build-tools/src/lib.rs | 77 ++++++++++++++++++++++++++++++++++++- 1 file changed, 76 insertions(+), 1 deletion(-) diff --git a/glib-build-tools/src/lib.rs b/glib-build-tools/src/lib.rs index 1d063bb24ad5..2f728cd0a009 100644 --- a/glib-build-tools/src/lib.rs +++ b/glib-build-tools/src/lib.rs @@ -2,7 +2,12 @@ #![doc = include_str!("../README.md")] -use std::{env, path::Path, process::Command}; +use gio::glib; +use std::{ + env, + path::{Path, PathBuf}, + process::Command, +}; // rustdoc-stripper-ignore-next /// Call to run `glib-compile-resources` to generate compiled gresources to embed @@ -57,3 +62,73 @@ pub fn compile_resources>(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 from specified directories and store `gschemas.compiled` into `glib-2.0` schema cache +/// directory. +/// +/// If `target_dir` is `None`, the default schema cache directory is used: +/// - Unix: `$HOME/.local/share/glib-2.0/schemas/` +/// - Windows: `C:/ProgramData/glib-2.0/schemas/` +/// +/// ```no_run +/// glib_build_tools::compile_schemas( +/// &["schemas"], +/// None +/// ); +/// ``` +pub fn compile_schemas(schemas_dir: &[&str], target_dir: Option<&str>) { + let target_dir = match target_dir { + Some(base) => PathBuf::from(base), + None => { + let prefix = if cfg!(windows) { + PathBuf::from("C:/ProgramData") + } else { + glib::user_data_dir() + }; + + Path::new(&prefix).join("glib-2.0").join("schemas") + } + }; + + // Ensure target_dir exists + std::fs::create_dir_all(&target_dir).expect("Failed to create target directory"); + + // 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); + } +} From 02b7f71a4c829687c0c7e4d864ef1f544ae51ff3 Mon Sep 17 00:00:00 2001 From: Shahrad Elahi Date: Fri, 23 May 2025 13:09:11 +0100 Subject: [PATCH 2/2] fix --- glib-build-tools/src/lib.rs | 40 ++++++++++++------------------------- 1 file changed, 13 insertions(+), 27 deletions(-) diff --git a/glib-build-tools/src/lib.rs b/glib-build-tools/src/lib.rs index 2f728cd0a009..43545a76bdc9 100644 --- a/glib-build-tools/src/lib.rs +++ b/glib-build-tools/src/lib.rs @@ -2,12 +2,7 @@ #![doc = include_str!("../README.md")] -use gio::glib; -use std::{ - env, - path::{Path, PathBuf}, - process::Command, -}; +use std::{env, path::Path, process::Command}; // rustdoc-stripper-ignore-next /// Call to run `glib-compile-resources` to generate compiled gresources to embed @@ -65,36 +60,27 @@ pub fn compile_resources>(source_dirs: &[P], gresource: &str, tar // rustdoc-stripper-ignore-next /// Call to run `glib-compile-schemas` to generate compiled gschemas from `.gschema.xml` schemas -/// files from specified directories and store `gschemas.compiled` into `glib-2.0` schema cache -/// directory. -/// -/// If `target_dir` is `None`, the default schema cache directory is used: -/// - Unix: `$HOME/.local/share/glib-2.0/schemas/` -/// - Windows: `C:/ProgramData/glib-2.0/schemas/` +/// files. /// /// ```no_run /// glib_build_tools::compile_schemas( -/// &["schemas"], -/// None +/// &["schemas"] /// ); /// ``` -pub fn compile_schemas(schemas_dir: &[&str], target_dir: Option<&str>) { - let target_dir = match target_dir { - Some(base) => PathBuf::from(base), - None => { - let prefix = if cfg!(windows) { - PathBuf::from("C:/ProgramData") - } else { - glib::user_data_dir() - }; - - Path::new(&prefix).join("glib-2.0").join("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={}", + target_dir.to_str().unwrap() + ); + // 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)