1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// Take a look at the license at the top of the repository in the LICENSE file.

#![doc = include_str!("../README.md")]

use std::env;
use std::path::Path;
use std::process::Command;

// rustdoc-stripper-ignore-next
/// Call to run `glib-compile-resources` to generate compiled gresources to embed
/// in binary with [`gio::resources_register_include`]. `target` is relative to `OUT_DIR`.
///
/// ```no_run
/// glib_build_tools::compile_resources(
///     "resources",
///     "resources/resources.gresource.xml",
///     "compiled.gresource",
/// );
/// ```
pub fn compile_resources<P: AsRef<Path>>(source_dir: P, gresource: &str, target: &str) {
    let out_dir = env::var("OUT_DIR").unwrap();

    let status = Command::new("glib-compile-resources")
        .arg("--sourcedir")
        .arg(source_dir.as_ref())
        .arg("--target")
        .arg(&format!("{out_dir}/{target}"))
        .arg(gresource)
        .status()
        .unwrap();

    assert!(
        status.success(),
        "glib-compile-resources failed with exit status {status}",
    );

    println!("cargo:rerun-if-changed={gresource}");
    let output = Command::new("glib-compile-resources")
        .arg("--sourcedir")
        .arg(source_dir.as_ref())
        .arg("--generate-dependencies")
        .arg(gresource)
        .output()
        .unwrap()
        .stdout;
    let output = String::from_utf8(output).unwrap();
    for dep in output.split_whitespace() {
        println!("cargo:rerun-if-changed={dep}");
    }
}