build.rs

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