|  | 
| 2 | 2 | #![allow(clippy::unwrap_used, reason = "this is basically a test")] | 
| 3 | 3 | //! `cargo gpu build`, analogous to `cargo build` | 
| 4 | 4 | 
 | 
| 5 |  | -use spirv_builder::SpirvBuilder; | 
| 6 |  | -use std::path::PathBuf; | 
| 7 |  | - | 
| 8 |  | -#[cfg(feature = "watch")] | 
| 9 | 5 | use anyhow::Context as _; | 
| 10 |  | -#[cfg(feature = "watch")] | 
| 11 |  | -use spirv_builder::{CompileResult, ModuleResult}; | 
| 12 |  | -#[cfg(feature = "watch")] | 
|  | 6 | +use spirv_builder::{CompileResult, ModuleResult, SpirvBuilder}; | 
| 13 | 7 | use std::io::Write as _; | 
|  | 8 | +use std::path::PathBuf; | 
| 14 | 9 | 
 | 
| 15 | 10 | use crate::install::Install; | 
| 16 |  | -use crate::lockfile::LockfileMismatchHandler; | 
| 17 |  | - | 
| 18 |  | -#[cfg(feature = "watch")] | 
| 19 | 11 | use crate::linkage::Linkage; | 
|  | 12 | +use crate::lockfile::LockfileMismatchHandler; | 
| 20 | 13 | 
 | 
| 21 | 14 | /// Args for just a build | 
| 22 | 15 | #[derive(Debug, Clone, serde::Deserialize, serde::Serialize)] | 
| @@ -102,32 +95,51 @@ impl Build { | 
| 102 | 95 |         ); | 
| 103 | 96 | 
 | 
| 104 | 97 |         #[cfg(feature = "watch")] | 
| 105 |  | -        if self.build.watch { | 
|  | 98 | +        let watching = self.build.watch; | 
|  | 99 | +        #[cfg(not(feature = "watch"))] | 
|  | 100 | +        let watching = false; | 
|  | 101 | +        if watching { | 
|  | 102 | +            return self.watch(); | 
|  | 103 | +        } | 
|  | 104 | + | 
|  | 105 | +        self.build() | 
|  | 106 | +    } | 
|  | 107 | + | 
|  | 108 | +    /// Builds shader crate locally using [`SpirvBuilder`]. | 
|  | 109 | +    fn build(&self) -> anyhow::Result<()> { | 
|  | 110 | +        crate::user_output!( | 
|  | 111 | +            "Compiling shaders at {}...\n", | 
|  | 112 | +            self.install.shader_crate.display() | 
|  | 113 | +        ); | 
|  | 114 | +        let result = self.build.spirv_builder.build()?; | 
|  | 115 | +        self.parse_compilation_result(&result)?; | 
|  | 116 | +        Ok(()) | 
|  | 117 | +    } | 
|  | 118 | + | 
|  | 119 | +    /// Watches shader crate for changes using [`SpirvBuilder`] | 
|  | 120 | +    /// or panics depending on presence of `watch` feature. | 
|  | 121 | +    fn watch(&self) -> anyhow::Result<()> { | 
|  | 122 | +        #[cfg(feature = "watch")] | 
|  | 123 | +        { | 
| 106 | 124 |             let this = self.clone(); | 
| 107 | 125 |             self.build | 
| 108 | 126 |                 .spirv_builder | 
| 109 | 127 |                 .watch(move |result, accept| { | 
| 110 |  | -                    let result1 = this.parse_compilation_result(&result); | 
|  | 128 | +                    let parse_result = this.parse_compilation_result(&result); | 
| 111 | 129 |                     if let Some(accept) = accept { | 
| 112 |  | -                        accept.submit(result1); | 
|  | 130 | +                        accept.submit(parse_result); | 
| 113 | 131 |                     } | 
| 114 | 132 |                 })? | 
| 115 | 133 |                 .context("unreachable")??; | 
| 116 | 134 |             std::thread::park(); | 
| 117 |  | -        } else { | 
| 118 |  | -            crate::user_output!( | 
| 119 |  | -                "Compiling shaders at {}...\n", | 
| 120 |  | -                self.install.shader_crate.display() | 
| 121 |  | -            ); | 
| 122 |  | -            let result = self.build.spirv_builder.build()?; | 
| 123 |  | -            self.parse_compilation_result(&result)?; | 
|  | 135 | +            Ok(()) | 
| 124 | 136 |         } | 
| 125 | 137 | 
 | 
| 126 |  | -        Ok(()) | 
|  | 138 | +        #[cfg(not(feature = "watch"))] | 
|  | 139 | +        unreachable!("cannot watch without the `watch` feature") | 
| 127 | 140 |     } | 
| 128 | 141 | 
 | 
| 129 |  | -    /// Parses compilation result from `SpirvBuilder` and writes it out to a file | 
| 130 |  | -    #[cfg(feature = "watch")] | 
|  | 142 | +    /// Parses compilation result from [`SpirvBuilder`] and writes it out to a file | 
| 131 | 143 |     fn parse_compilation_result(&self, result: &CompileResult) -> anyhow::Result<()> { | 
| 132 | 144 |         let shaders = match &result.module { | 
| 133 | 145 |             ModuleResult::MultiModule(modules) => { | 
|  | 
0 commit comments