glib_build_tools/lib.rs
1// Take a look at the license at the top of the repository in the LICENSE file.
2
3#![doc = include_str!("../README.md")]
4
5use std::{env, path::Path, process::Command};
6
7// rustdoc-stripper-ignore-next
8/// Call to run `glib-compile-resources` to generate compiled gresources to embed
9/// in binary with [`gio::resources_register_include`]. `target` is relative to `OUT_DIR`.
10///
11/// ```no_run
12/// glib_build_tools::compile_resources(
13/// &["resources"],
14/// "resources/resources.gresource.xml",
15/// "compiled.gresource",
16/// );
17/// ```
18pub fn compile_resources<P: AsRef<Path>>(source_dirs: &[P], gresource: &str, target: &str) {
19 let out_dir = env::var("OUT_DIR").unwrap();
20 let out_dir = Path::new(&out_dir);
21
22 let mut command = Command::new("glib-compile-resources");
23
24 for source_dir in source_dirs {
25 command.arg("--sourcedir").arg(source_dir.as_ref());
26 }
27
28 let output = command
29 .arg("--target")
30 .arg(out_dir.join(target))
31 .arg(gresource)
32 .output()
33 .unwrap();
34
35 assert!(
36 output.status.success(),
37 "glib-compile-resources failed with exit status {} and stderr:\n{}",
38 output.status,
39 String::from_utf8_lossy(&output.stderr)
40 );
41
42 println!("cargo:rerun-if-changed={gresource}");
43 let mut command = Command::new("glib-compile-resources");
44
45 for source_dir in source_dirs {
46 command.arg("--sourcedir").arg(source_dir.as_ref());
47 }
48
49 let output = command
50 .arg("--generate-dependencies")
51 .arg(gresource)
52 .output()
53 .unwrap()
54 .stdout;
55 let output = String::from_utf8(output).unwrap();
56 for dep in output.split_whitespace() {
57 println!("cargo:rerun-if-changed={dep}");
58 }
59}