target_qualification.rs (8824B)
1 use std::{collections::BTreeSet, fs, path::Path, process::Command}; 2 3 use serde::Deserialize; 4 5 #[derive(Debug, Deserialize)] 6 struct TargetMatrix { 7 schema_version: u32, 8 msrv_toolchain: String, 9 current_toolchain: String, 10 operating_system: Vec<OperatingSystem>, 11 } 12 13 #[derive(Debug, Deserialize, PartialEq, Eq)] 14 struct OperatingSystem { 15 name: String, 16 target: String, 17 cross_compiler: Option<String>, 18 cross_cflags: Option<String>, 19 } 20 21 #[derive(Debug, Deserialize)] 22 struct Catalog { 23 package: Vec<CatalogPackage>, 24 } 25 26 #[derive(Debug, Deserialize)] 27 struct CatalogPackage { 28 name: String, 29 state: String, 30 groups: Vec<String>, 31 } 32 33 pub fn run(workspace_root: &Path) -> Result<(), String> { 34 let (matrix, packages) = load(workspace_root)?; 35 for toolchain in [&matrix.msrv_toolchain, &matrix.current_toolchain] 36 .into_iter() 37 .collect::<BTreeSet<_>>() 38 { 39 run_command( 40 workspace_root, 41 "rustup", 42 &[ 43 "run", 44 toolchain, 45 "cargo", 46 "check", 47 "--workspace", 48 "--all-targets", 49 "--locked", 50 ], 51 )?; 52 } 53 for operating_system in matrix.operating_system { 54 for package in &packages { 55 run_target_command( 56 workspace_root, 57 &operating_system, 58 &[ 59 "check", 60 "-p", 61 package, 62 "--lib", 63 "--target", 64 &operating_system.target, 65 "--locked", 66 ], 67 )?; 68 } 69 } 70 Ok(()) 71 } 72 73 fn load(workspace_root: &Path) -> Result<(TargetMatrix, Vec<String>), String> { 74 let matrix_path = workspace_root.join("contracts/releases/target_matrix.toml"); 75 let matrix = toml::from_str::<TargetMatrix>(&read(&matrix_path)?) 76 .map_err(|error| format!("failed to parse {}: {error}", matrix_path.display()))?; 77 validate(&matrix)?; 78 79 let catalog_path = workspace_root.join("contracts/crates/catalog.v2.toml"); 80 let catalog = toml::from_str::<Catalog>(&read(&catalog_path)?) 81 .map_err(|error| format!("failed to parse {}: {error}", catalog_path.display()))?; 82 let mut packages = catalog 83 .package 84 .into_iter() 85 .filter(|package| { 86 package.state == "active" && package.groups.iter().any(|group| group == "public_native") 87 }) 88 .map(|package| package.name) 89 .collect::<Vec<_>>(); 90 packages.sort(); 91 let expected = crate::catalog::public_packages(workspace_root)?; 92 if packages.iter().collect::<std::collections::BTreeSet<_>>() != expected.iter().collect() { 93 return Err(format!( 94 "target qualification must match current public allocation, found {} packages", 95 packages.len() 96 )); 97 } 98 Ok((matrix, packages)) 99 } 100 101 fn read(path: &Path) -> Result<String, String> { 102 fs::read_to_string(path).map_err(|error| format!("failed to read {}: {error}", path.display())) 103 } 104 105 fn validate(matrix: &TargetMatrix) -> Result<(), String> { 106 if matrix.schema_version != 1 { 107 return Err(format!( 108 "target matrix schema_version must be 1, found {}", 109 matrix.schema_version 110 )); 111 } 112 if matrix.msrv_toolchain != crate::architecture::STABLE_TOOLCHAIN 113 || matrix.current_toolchain != crate::architecture::STABLE_TOOLCHAIN 114 { 115 return Err(format!( 116 "target matrix must select exact current/MSRV compiler {}", 117 crate::architecture::STABLE_TOOLCHAIN 118 )); 119 } 120 let expected = [ 121 ( 122 "linux", 123 "x86_64-unknown-linux-gnu", 124 Some("zig cc"), 125 Some("-target x86_64-linux-gnu"), 126 ), 127 ("macos", "aarch64-apple-darwin", None, None), 128 ]; 129 if matrix.operating_system.len() != expected.len() 130 || expected.iter().any(|(name, target, compiler, cflags)| { 131 !matrix.operating_system.iter().any(|entry| { 132 entry.name == *name 133 && entry.target == *target 134 && entry.cross_compiler.as_deref() == *compiler 135 && entry.cross_cflags.as_deref() == *cflags 136 }) 137 }) 138 { 139 return Err( 140 "target matrix must contain the exact Linux x86_64 and macOS aarch64 triples" 141 .to_owned(), 142 ); 143 } 144 Ok(()) 145 } 146 147 fn run_target_command( 148 workspace_root: &Path, 149 operating_system: &OperatingSystem, 150 args: &[&str], 151 ) -> Result<(), String> { 152 eprintln!("cargo {}", args.join(" ")); 153 let mut command = Command::new("cargo"); 154 command.args(args).current_dir(workspace_root); 155 let target_key = operating_system.target.replace('-', "_"); 156 if let Some(compiler) = operating_system.cross_compiler.as_deref() { 157 command.env(format!("CC_{target_key}"), compiler); 158 } 159 if let Some(cflags) = operating_system.cross_cflags.as_deref() { 160 command.env(format!("CFLAGS_{target_key}"), cflags); 161 } 162 let status = command 163 .status() 164 .map_err(|error| format!("failed to start cargo: {error}"))?; 165 if status.success() { 166 Ok(()) 167 } else { 168 Err(format!( 169 "target qualification failed: cargo {}", 170 args.join(" ") 171 )) 172 } 173 } 174 175 fn run_command(workspace_root: &Path, program: &str, args: &[&str]) -> Result<(), String> { 176 eprintln!("{program} {}", args.join(" ")); 177 let status = Command::new(program) 178 .args(args) 179 .current_dir(workspace_root) 180 .status() 181 .map_err(|error| format!("failed to start {program}: {error}"))?; 182 if status.success() { 183 Ok(()) 184 } else { 185 Err(format!( 186 "target qualification failed: {program} {}", 187 args.join(" ") 188 )) 189 } 190 } 191 192 #[cfg(test)] 193 mod tests { 194 use super::{OperatingSystem, TargetMatrix, load, validate}; 195 196 #[test] 197 fn current_contract_selects_exact_toolchains_targets_and_packages() { 198 let root = std::path::Path::new(env!("CARGO_MANIFEST_DIR")) 199 .parent() 200 .and_then(std::path::Path::parent) 201 .expect("workspace root"); 202 let (matrix, packages) = load(root).expect("target matrix"); 203 assert_eq!(matrix.msrv_toolchain, "1.99.0"); 204 assert_eq!(matrix.current_toolchain, "1.99.0"); 205 assert_eq!(matrix.operating_system.len(), 2); 206 assert_eq!( 207 matrix 208 .operating_system 209 .iter() 210 .map(|entry| (entry.name.as_str(), entry.target.as_str())) 211 .collect::<Vec<_>>(), 212 [ 213 ("linux", "x86_64-unknown-linux-gnu"), 214 ("macos", "aarch64-apple-darwin"), 215 ] 216 ); 217 assert_eq!(packages.len(), 22); 218 } 219 220 #[test] 221 fn unsupported_production_targets_are_rejected() { 222 let mut matrix = TargetMatrix { 223 schema_version: 1, 224 msrv_toolchain: "1.99.0".to_owned(), 225 current_toolchain: "1.99.0".to_owned(), 226 operating_system: vec![ 227 OperatingSystem { 228 name: "linux".to_owned(), 229 target: "x86_64-unknown-linux-gnu".to_owned(), 230 cross_compiler: Some("zig cc".to_owned()), 231 cross_cflags: Some("-target x86_64-linux-gnu".to_owned()), 232 }, 233 OperatingSystem { 234 name: "macos".to_owned(), 235 target: "aarch64-apple-darwin".to_owned(), 236 cross_compiler: None, 237 cross_cflags: None, 238 }, 239 ], 240 }; 241 for toolchain in ["stable", "1.97.1"] { 242 matrix.current_toolchain = toolchain.to_owned(); 243 assert!(validate(&matrix).is_err(), "accepted {toolchain}"); 244 } 245 matrix.current_toolchain = "1.99.0".to_owned(); 246 for toolchain in ["stable", "1.97.1", "1.98.0", "1.100.0"] { 247 matrix.msrv_toolchain = toolchain.to_owned(); 248 assert!(validate(&matrix).is_err(), "accepted minimum {toolchain}"); 249 } 250 matrix.msrv_toolchain = "1.99.0".to_owned(); 251 for (name, target) in [ 252 ("windows", "x86_64-pc-windows-gnu"), 253 ("macos", "x86_64-apple-darwin"), 254 ("linux", "aarch64-unknown-linux-gnu"), 255 ] { 256 matrix.operating_system.push(OperatingSystem { 257 name: name.to_owned(), 258 target: target.to_owned(), 259 cross_compiler: None, 260 cross_cflags: None, 261 }); 262 assert!(validate(&matrix).is_err(), "accepted {target}"); 263 matrix.operating_system.pop(); 264 } 265 } 266 }