|
| 1 | +//! `cargo gpu build`, analogous to `cargo build` |
| 2 | +
|
| 3 | +use std::io::Write as _; |
| 4 | + |
| 5 | +use clap::Parser; |
| 6 | +use spirv_builder_cli::{Linkage, ShaderModule}; |
| 7 | + |
| 8 | +use crate::{install::Install, target_spec_dir}; |
| 9 | + |
| 10 | +/// `cargo build` subcommands |
| 11 | +#[derive(Parser, Debug)] |
| 12 | +pub struct Build { |
| 13 | + /// Install the `rust-gpu` compiler and components |
| 14 | + #[clap(flatten)] |
| 15 | + install: Install, |
| 16 | + |
| 17 | + /// Directory containing the shader crate to compile. |
| 18 | + #[clap(long, default_value = "./")] |
| 19 | + pub shader_crate: std::path::PathBuf, |
| 20 | + |
| 21 | + /// Shader target. |
| 22 | + #[clap(long, default_value = "spirv-unknown-vulkan1.2")] |
| 23 | + shader_target: String, |
| 24 | + |
| 25 | + /// Set cargo default-features. |
| 26 | + #[clap(long)] |
| 27 | + no_default_features: bool, |
| 28 | + |
| 29 | + /// Set cargo features. |
| 30 | + #[clap(long)] |
| 31 | + features: Vec<String>, |
| 32 | + |
| 33 | + /// Path to the output directory for the compiled shaders. |
| 34 | + #[clap(long, short, default_value = "./")] |
| 35 | + pub output_dir: std::path::PathBuf, |
| 36 | +} |
| 37 | + |
| 38 | +impl Build { |
| 39 | + /// Entrypoint |
| 40 | + pub fn run(&mut self) { |
| 41 | + let (dylib_path, spirv_builder_cli_path) = self.install.run(); |
| 42 | + |
| 43 | + // Ensure the shader output dir exists |
| 44 | + log::debug!("ensuring output-dir '{}' exists", self.output_dir.display()); |
| 45 | + std::fs::create_dir_all(&self.output_dir).unwrap(); |
| 46 | + self.output_dir = self.output_dir.canonicalize().unwrap(); |
| 47 | + |
| 48 | + // Ensure the shader crate exists |
| 49 | + self.shader_crate = self.shader_crate.canonicalize().unwrap(); |
| 50 | + assert!( |
| 51 | + self.shader_crate.exists(), |
| 52 | + "shader crate '{}' does not exist. (Current dir is '{}')", |
| 53 | + self.shader_crate.display(), |
| 54 | + std::env::current_dir().unwrap().display() |
| 55 | + ); |
| 56 | + |
| 57 | + let spirv_builder_args = spirv_builder_cli::Args { |
| 58 | + dylib_path, |
| 59 | + shader_crate: self.shader_crate.clone(), |
| 60 | + shader_target: self.shader_target.clone(), |
| 61 | + path_to_target_spec: target_spec_dir().join(format!("{}.json", self.shader_target)), |
| 62 | + no_default_features: self.no_default_features, |
| 63 | + features: self.features.clone(), |
| 64 | + output_dir: self.output_dir.clone(), |
| 65 | + }; |
| 66 | + |
| 67 | + // UNWRAP: safe because we know this always serializes |
| 68 | + let arg = serde_json::to_string_pretty(&spirv_builder_args).unwrap(); |
| 69 | + log::info!("using spirv-builder-cli arg: {arg}"); |
| 70 | + |
| 71 | + // Call spirv-builder-cli to compile the shaders. |
| 72 | + let output = std::process::Command::new(spirv_builder_cli_path) |
| 73 | + .arg(arg) |
| 74 | + .stdout(std::process::Stdio::inherit()) |
| 75 | + .stderr(std::process::Stdio::inherit()) |
| 76 | + .output() |
| 77 | + .unwrap(); |
| 78 | + assert!(output.status.success(), "build failed"); |
| 79 | + |
| 80 | + let spirv_manifest = self.output_dir.join("spirv-manifest.json"); |
| 81 | + if spirv_manifest.is_file() { |
| 82 | + log::debug!( |
| 83 | + "successfully built shaders, raw manifest is at '{}'", |
| 84 | + spirv_manifest.display() |
| 85 | + ); |
| 86 | + } else { |
| 87 | + log::error!("missing raw manifest '{}'", spirv_manifest.display()); |
| 88 | + panic!("missing raw manifest"); |
| 89 | + } |
| 90 | + |
| 91 | + let shaders: Vec<ShaderModule> = |
| 92 | + serde_json::from_reader(std::fs::File::open(&spirv_manifest).unwrap()).unwrap(); |
| 93 | + |
| 94 | + let mut linkage: Vec<_> = shaders |
| 95 | + .into_iter() |
| 96 | + .map( |
| 97 | + |ShaderModule { |
| 98 | + entry, |
| 99 | + path: filepath, |
| 100 | + }| { |
| 101 | + use relative_path::PathExt as _; |
| 102 | + let path = self.output_dir.join(filepath.file_name().unwrap()); |
| 103 | + std::fs::copy(&filepath, &path).unwrap(); |
| 104 | + let path_relative_to_shader_crate = |
| 105 | + path.relative_to(&self.shader_crate).unwrap().to_path(""); |
| 106 | + Linkage::new(entry, path_relative_to_shader_crate) |
| 107 | + }, |
| 108 | + ) |
| 109 | + .collect(); |
| 110 | + |
| 111 | + // Write the shader manifest json file |
| 112 | + let manifest_path = self.output_dir.join("manifest.json"); |
| 113 | + // Sort the contents so the output is deterministic |
| 114 | + linkage.sort(); |
| 115 | + // UNWRAP: safe because we know this always serializes |
| 116 | + let json = serde_json::to_string_pretty(&linkage).unwrap(); |
| 117 | + let mut file = std::fs::File::create(&manifest_path).unwrap_or_else(|error| { |
| 118 | + log::error!( |
| 119 | + "could not create shader manifest file '{}': {error}", |
| 120 | + manifest_path.display(), |
| 121 | + ); |
| 122 | + panic!("{error}") |
| 123 | + }); |
| 124 | + file.write_all(json.as_bytes()).unwrap_or_else(|error| { |
| 125 | + log::error!( |
| 126 | + "could not write shader manifest file '{}': {error}", |
| 127 | + manifest_path.display(), |
| 128 | + ); |
| 129 | + panic!("{error}") |
| 130 | + }); |
| 131 | + |
| 132 | + log::info!("wrote manifest to '{}'", manifest_path.display()); |
| 133 | + |
| 134 | + if spirv_manifest.is_file() { |
| 135 | + log::debug!( |
| 136 | + "removing spirv-manifest.json file '{}'", |
| 137 | + spirv_manifest.display() |
| 138 | + ); |
| 139 | + std::fs::remove_file(spirv_manifest).unwrap(); |
| 140 | + } |
| 141 | + } |
| 142 | +} |
0 commit comments