zed/crates/gpui2/build.rs

135 lines
4.1 KiB
Rust
Raw Normal View History

2023-09-23 18:10:33 +03:00
use std::{
env,
path::{Path, PathBuf},
process::{self, Command},
};
2023-09-20 05:55:13 +03:00
use cbindgen::Config;
2023-09-20 05:55:13 +03:00
fn main() {
generate_dispatch_bindings();
2023-09-23 18:10:33 +03:00
let header_path = generate_shader_bindings();
compile_metal_shaders(&header_path);
2023-09-20 05:55:13 +03:00
}
fn generate_dispatch_bindings() {
println!("cargo:rustc-link-lib=framework=System");
println!("cargo:rerun-if-changed=src/platform/mac/dispatch.h");
let bindings = bindgen::Builder::default()
.header("src/platform/mac/dispatch.h")
.allowlist_var("_dispatch_main_q")
2023-10-04 20:53:29 +03:00
.allowlist_var("DISPATCH_QUEUE_PRIORITY_DEFAULT")
.allowlist_function("dispatch_get_global_queue")
2023-09-20 05:55:13 +03:00
.allowlist_function("dispatch_async_f")
2023-10-22 20:56:25 +03:00
.allowlist_function("dispatch_after_f")
.allowlist_function("dispatch_time")
2023-09-20 05:55:13 +03:00
.parse_callbacks(Box::new(bindgen::CargoCallbacks))
.layout_tests(false)
.generate()
.expect("unable to generate bindings");
let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());
bindings
.write_to_file(out_path.join("dispatch_sys.rs"))
.expect("couldn't write dispatch bindings");
}
2023-09-23 18:10:33 +03:00
fn generate_shader_bindings() -> PathBuf {
let output_path = PathBuf::from(env::var("OUT_DIR").unwrap()).join("scene.h");
let crate_dir = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap());
let mut config = Config::default();
config.include_guard = Some("SCENE_H".into());
config.language = cbindgen::Language::C;
config.export.include.extend([
"Bounds".into(),
"Corners".into(),
"Edges".into(),
"Size".into(),
"Pixels".into(),
"PointF".into(),
"Hsla".into(),
2023-10-09 19:50:42 +03:00
"ContentMask".into(),
2023-10-03 20:16:42 +03:00
"Uniforms".into(),
"AtlasTile".into(),
2023-10-10 14:01:35 +03:00
"PathRasterizationInputIndex".into(),
"PathVertex_ScaledPixels".into(),
2023-10-05 16:30:47 +03:00
"ShadowInputIndex".into(),
"Shadow".into(),
2023-10-06 16:34:37 +03:00
"QuadInputIndex".into(),
"Underline".into(),
"UnderlineInputIndex".into(),
"Quad".into(),
2023-10-04 13:41:21 +03:00
"SpriteInputIndex".into(),
2023-10-03 18:53:08 +03:00
"MonochromeSprite".into(),
2023-10-04 13:41:21 +03:00
"PolychromeSprite".into(),
2023-10-10 14:01:35 +03:00
"PathSprite".into(),
]);
config.no_includes = true;
config.enumeration.prefix_with_name = true;
2023-09-23 18:10:33 +03:00
cbindgen::Builder::new()
.with_src(crate_dir.join("src/scene.rs"))
.with_src(crate_dir.join("src/geometry.rs"))
.with_src(crate_dir.join("src/color.rs"))
2023-10-04 05:04:17 +03:00
.with_src(crate_dir.join("src/window.rs"))
2023-10-03 18:53:08 +03:00
.with_src(crate_dir.join("src/platform.rs"))
.with_src(crate_dir.join("src/platform/mac/metal_renderer.rs"))
.with_config(config)
2023-09-23 18:10:33 +03:00
.generate()
.expect("Unable to generate bindings")
.write_to_file(&output_path);
2023-10-04 05:04:17 +03:00
2023-09-23 18:10:33 +03:00
output_path
}
fn compile_metal_shaders(header_path: &Path) {
let shader_path = "./src/platform/mac/shaders.metal";
let air_output_path = PathBuf::from(env::var("OUT_DIR").unwrap()).join("shaders.air");
let metallib_output_path = PathBuf::from(env::var("OUT_DIR").unwrap()).join("shaders.metallib");
println!("cargo:rerun-if-changed={}", header_path.display());
println!("cargo:rerun-if-changed={}", shader_path);
let output = Command::new("xcrun")
.args([
"-sdk",
"macosx",
"metal",
"-gline-tables-only",
"-mmacosx-version-min=10.15.7",
"-MO",
"-c",
shader_path,
"-include",
&header_path.to_str().unwrap(),
"-o",
])
.arg(&air_output_path)
.output()
.unwrap();
if !output.status.success() {
eprintln!(
"metal shader compilation failed:\n{}",
String::from_utf8_lossy(&output.stderr)
);
process::exit(1);
}
let output = Command::new("xcrun")
.args(["-sdk", "macosx", "metallib"])
.arg(air_output_path)
.arg("-o")
.arg(metallib_output_path)
.output()
.unwrap();
if !output.status.success() {
eprintln!(
"metallib compilation failed:\n{}",
String::from_utf8_lossy(&output.stderr)
);
process::exit(1);
}
}