1#![cfg_attr(any(not(target_os = "macos"), feature = "macos-blade"), allow(unused))]
2
3//TODO: consider generating shader code for WGSL
4//TODO: deprecate "runtime-shaders" and "macos-blade"
5
6fn main() {
7 #[cfg(target_os = "macos")]
8 macos::build();
9}
10
11#[cfg(target_os = "macos")]
12mod macos {
13 use std::{
14 env,
15 path::{Path, PathBuf},
16 };
17
18 use cbindgen::Config;
19
20 pub(super) fn build() {
21 generate_dispatch_bindings();
22 #[cfg(not(feature = "macos-blade"))]
23 {
24 let header_path = generate_shader_bindings();
25
26 #[cfg(feature = "runtime_shaders")]
27 emit_stitched_shaders(&header_path);
28 #[cfg(not(feature = "runtime_shaders"))]
29 compile_metal_shaders(&header_path);
30 }
31 }
32
33 fn generate_dispatch_bindings() {
34 println!("cargo:rustc-link-lib=framework=System");
35 println!("cargo:rerun-if-changed=src/platform/mac/dispatch.h");
36
37 let bindings = bindgen::Builder::default()
38 .header("src/platform/mac/dispatch.h")
39 .allowlist_var("_dispatch_main_q")
40 .allowlist_var("_dispatch_source_type_data_add")
41 .allowlist_var("DISPATCH_QUEUE_PRIORITY_DEFAULT")
42 .allowlist_var("DISPATCH_QUEUE_PRIORITY_HIGH")
43 .allowlist_var("DISPATCH_TIME_NOW")
44 .allowlist_function("dispatch_get_global_queue")
45 .allowlist_function("dispatch_async_f")
46 .allowlist_function("dispatch_after_f")
47 .allowlist_function("dispatch_time")
48 .allowlist_function("dispatch_source_merge_data")
49 .allowlist_function("dispatch_source_create")
50 .allowlist_function("dispatch_source_set_event_handler_f")
51 .allowlist_function("dispatch_resume")
52 .allowlist_function("dispatch_suspend")
53 .allowlist_function("dispatch_source_cancel")
54 .allowlist_function("dispatch_set_context")
55 .parse_callbacks(Box::new(bindgen::CargoCallbacks))
56 .layout_tests(false)
57 .generate()
58 .expect("unable to generate bindings");
59
60 let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());
61 bindings
62 .write_to_file(out_path.join("dispatch_sys.rs"))
63 .expect("couldn't write dispatch bindings");
64 }
65
66 fn generate_shader_bindings() -> PathBuf {
67 let output_path = PathBuf::from(env::var("OUT_DIR").unwrap()).join("scene.h");
68 let crate_dir = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap());
69 let mut config = Config::default();
70 config.include_guard = Some("SCENE_H".into());
71 config.language = cbindgen::Language::C;
72 config.export.include.extend([
73 "Bounds".into(),
74 "Corners".into(),
75 "Edges".into(),
76 "Size".into(),
77 "Pixels".into(),
78 "PointF".into(),
79 "Hsla".into(),
80 "ContentMask".into(),
81 "Uniforms".into(),
82 "AtlasTile".into(),
83 "PathRasterizationInputIndex".into(),
84 "PathVertex_ScaledPixels".into(),
85 "ShadowInputIndex".into(),
86 "Shadow".into(),
87 "QuadInputIndex".into(),
88 "Underline".into(),
89 "UnderlineInputIndex".into(),
90 "Quad".into(),
91 "SpriteInputIndex".into(),
92 "MonochromeSprite".into(),
93 "PolychromeSprite".into(),
94 "PathSprite".into(),
95 "SurfaceInputIndex".into(),
96 "SurfaceBounds".into(),
97 "TransformationMatrix".into(),
98 ]);
99 config.no_includes = true;
100 config.enumeration.prefix_with_name = true;
101
102 let mut builder = cbindgen::Builder::new();
103
104 let src_paths = [
105 crate_dir.join("src/scene.rs"),
106 crate_dir.join("src/geometry.rs"),
107 crate_dir.join("src/color.rs"),
108 crate_dir.join("src/window.rs"),
109 crate_dir.join("src/platform.rs"),
110 crate_dir.join("src/platform/mac/metal_renderer.rs"),
111 ];
112 for src_path in src_paths {
113 println!("cargo:rerun-if-changed={}", src_path.display());
114 builder = builder.with_src(src_path);
115 }
116
117 builder
118 .with_config(config)
119 .generate()
120 .expect("Unable to generate bindings")
121 .write_to_file(&output_path);
122
123 output_path
124 }
125
126 /// To enable runtime compilation, we need to "stitch" the shaders file with the generated header
127 /// so that it is self-contained.
128 #[cfg(feature = "runtime_shaders")]
129 fn emit_stitched_shaders(header_path: &Path) {
130 use std::str::FromStr;
131 fn stitch_header(header: &Path, shader_path: &Path) -> std::io::Result<PathBuf> {
132 let header_contents = std::fs::read_to_string(header)?;
133 let shader_contents = std::fs::read_to_string(shader_path)?;
134 let stitched_contents = format!("{header_contents}\n{shader_contents}");
135 let out_path =
136 PathBuf::from(env::var("OUT_DIR").unwrap()).join("stitched_shaders.metal");
137 std::fs::write(&out_path, stitched_contents)?;
138 Ok(out_path)
139 }
140 let shader_source_path = "./src/platform/mac/shaders.metal";
141 let shader_path = PathBuf::from_str(shader_source_path).unwrap();
142 stitch_header(header_path, &shader_path).unwrap();
143 println!("cargo:rerun-if-changed={}", &shader_source_path);
144 }
145
146 #[cfg(not(feature = "runtime_shaders"))]
147 fn compile_metal_shaders(header_path: &Path) {
148 use std::process::{self, Command};
149 let shader_path = "./src/platform/mac/shaders.metal";
150 let air_output_path = PathBuf::from(env::var("OUT_DIR").unwrap()).join("shaders.air");
151 let metallib_output_path =
152 PathBuf::from(env::var("OUT_DIR").unwrap()).join("shaders.metallib");
153 println!("cargo:rerun-if-changed={}", shader_path);
154
155 let output = Command::new("xcrun")
156 .args([
157 "-sdk",
158 "macosx",
159 "metal",
160 "-gline-tables-only",
161 "-mmacosx-version-min=10.15.7",
162 "-MO",
163 "-c",
164 shader_path,
165 "-include",
166 &header_path.to_str().unwrap(),
167 "-o",
168 ])
169 .arg(&air_output_path)
170 .output()
171 .unwrap();
172
173 if !output.status.success() {
174 eprintln!(
175 "metal shader compilation failed:\n{}",
176 String::from_utf8_lossy(&output.stderr)
177 );
178 process::exit(1);
179 }
180
181 let output = Command::new("xcrun")
182 .args(["-sdk", "macosx", "metallib"])
183 .arg(air_output_path)
184 .arg("-o")
185 .arg(metallib_output_path)
186 .output()
187 .unwrap();
188
189 if !output.status.success() {
190 eprintln!(
191 "metallib compilation failed:\n{}",
192 String::from_utf8_lossy(&output.stderr)
193 );
194 process::exit(1);
195 }
196 }
197}