build.rs

  1use std::{
  2    env,
  3    path::{Path, PathBuf},
  4    process::{self, Command},
  5};
  6
  7use cbindgen::Config;
  8
  9fn main() {
 10    generate_dispatch_bindings();
 11    let header_path = generate_shader_bindings();
 12    compile_metal_shaders(&header_path);
 13}
 14
 15fn generate_dispatch_bindings() {
 16    println!("cargo:rustc-link-lib=framework=System");
 17    println!("cargo:rerun-if-changed=src/platform/mac/dispatch.h");
 18
 19    let bindings = bindgen::Builder::default()
 20        .header("src/platform/mac/dispatch.h")
 21        .allowlist_var("_dispatch_main_q")
 22        .allowlist_var("DISPATCH_QUEUE_PRIORITY_DEFAULT")
 23        .allowlist_var("DISPATCH_TIME_NOW")
 24        .allowlist_function("dispatch_get_global_queue")
 25        .allowlist_function("dispatch_async_f")
 26        .allowlist_function("dispatch_after_f")
 27        .allowlist_function("dispatch_time")
 28        .parse_callbacks(Box::new(bindgen::CargoCallbacks))
 29        .layout_tests(false)
 30        .generate()
 31        .expect("unable to generate bindings");
 32
 33    let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());
 34    bindings
 35        .write_to_file(out_path.join("dispatch_sys.rs"))
 36        .expect("couldn't write dispatch bindings");
 37}
 38
 39fn generate_shader_bindings() -> PathBuf {
 40    let output_path = PathBuf::from(env::var("OUT_DIR").unwrap()).join("scene.h");
 41    let crate_dir = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap());
 42    let mut config = Config::default();
 43    config.include_guard = Some("SCENE_H".into());
 44    config.language = cbindgen::Language::C;
 45    config.export.include.extend([
 46        "Bounds".into(),
 47        "Corners".into(),
 48        "Edges".into(),
 49        "Size".into(),
 50        "Pixels".into(),
 51        "PointF".into(),
 52        "Hsla".into(),
 53        "ContentMask".into(),
 54        "Uniforms".into(),
 55        "AtlasTile".into(),
 56        "PathRasterizationInputIndex".into(),
 57        "PathVertex_ScaledPixels".into(),
 58        "ShadowInputIndex".into(),
 59        "Shadow".into(),
 60        "QuadInputIndex".into(),
 61        "Underline".into(),
 62        "UnderlineInputIndex".into(),
 63        "Quad".into(),
 64        "SpriteInputIndex".into(),
 65        "MonochromeSprite".into(),
 66        "PolychromeSprite".into(),
 67        "PathSprite".into(),
 68        "SurfaceInputIndex".into(),
 69        "SurfaceBounds".into(),
 70    ]);
 71    config.no_includes = true;
 72    config.enumeration.prefix_with_name = true;
 73    cbindgen::Builder::new()
 74        .with_src(crate_dir.join("src/scene.rs"))
 75        .with_src(crate_dir.join("src/geometry.rs"))
 76        .with_src(crate_dir.join("src/color.rs"))
 77        .with_src(crate_dir.join("src/window.rs"))
 78        .with_src(crate_dir.join("src/platform.rs"))
 79        .with_src(crate_dir.join("src/platform/mac/metal_renderer.rs"))
 80        .with_config(config)
 81        .generate()
 82        .expect("Unable to generate bindings")
 83        .write_to_file(&output_path);
 84
 85    output_path
 86}
 87
 88fn compile_metal_shaders(header_path: &Path) {
 89    let shader_path = "./src/platform/mac/shaders.metal";
 90    let air_output_path = PathBuf::from(env::var("OUT_DIR").unwrap()).join("shaders.air");
 91    let metallib_output_path = PathBuf::from(env::var("OUT_DIR").unwrap()).join("shaders.metallib");
 92
 93    println!("cargo:rerun-if-changed={}", header_path.display());
 94    println!("cargo:rerun-if-changed={}", shader_path);
 95
 96    let output = Command::new("xcrun")
 97        .args([
 98            "-sdk",
 99            "macosx",
100            "metal",
101            "-gline-tables-only",
102            "-mmacosx-version-min=10.15.7",
103            "-MO",
104            "-c",
105            shader_path,
106            "-include",
107            &header_path.to_str().unwrap(),
108            "-o",
109        ])
110        .arg(&air_output_path)
111        .output()
112        .unwrap();
113
114    if !output.status.success() {
115        eprintln!(
116            "metal shader compilation failed:\n{}",
117            String::from_utf8_lossy(&output.stderr)
118        );
119        process::exit(1);
120    }
121
122    let output = Command::new("xcrun")
123        .args(["-sdk", "macosx", "metallib"])
124        .arg(air_output_path)
125        .arg("-o")
126        .arg(metallib_output_path)
127        .output()
128        .unwrap();
129
130    if !output.status.success() {
131        eprintln!(
132            "metallib compilation failed:\n{}",
133            String::from_utf8_lossy(&output.stderr)
134        );
135        process::exit(1);
136    }
137}