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