build.rs (2395B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this 3 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ 4 5 use std::env; 6 use std::path::Path; 7 use std::process::{exit, Command}; 8 use std::sync::LazyLock; 9 use walkdir::WalkDir; 10 11 #[cfg(feature = "gecko")] 12 mod build_gecko; 13 14 #[cfg(not(feature = "gecko"))] 15 mod build_gecko { 16 pub fn generate() {} 17 } 18 19 pub static PYTHON: LazyLock<String> = LazyLock::new(|| { 20 env::var("PYTHON3").ok().unwrap_or_else(|| { 21 let candidates = if cfg!(windows) { 22 ["python3.exe"] 23 } else { 24 ["python3"] 25 }; 26 for &name in &candidates { 27 if Command::new(name) 28 .arg("--version") 29 .output() 30 .ok() 31 .map_or(false, |out| out.status.success()) 32 { 33 return name.to_owned(); 34 } 35 } 36 panic!( 37 "Can't find python (tried {})! Try fixing PATH or setting the PYTHON3 env var", 38 candidates.join(", ") 39 ) 40 }) 41 }); 42 43 fn generate_properties(engine: &str) { 44 for entry in WalkDir::new("properties") { 45 let entry = entry.unwrap(); 46 match entry.path().extension().and_then(|e| e.to_str()) { 47 Some("mako") | Some("rs") | Some("py") | Some("zip") => { 48 println!("cargo:rerun-if-changed={}", entry.path().display()); 49 }, 50 _ => {}, 51 } 52 } 53 54 let script = Path::new(&env::var_os("CARGO_MANIFEST_DIR").unwrap()) 55 .join("properties") 56 .join("build.py"); 57 58 let status = Command::new(&*PYTHON) 59 .arg(&script) 60 .arg(engine) 61 .arg("style-crate") 62 .status() 63 .unwrap(); 64 if !status.success() { 65 exit(1) 66 } 67 } 68 69 fn main() { 70 let gecko = cfg!(feature = "gecko"); 71 let servo = cfg!(feature = "servo"); 72 let engine = match (gecko, servo) { 73 (true, false) => "gecko", 74 (false, true) => "servo", 75 _ => panic!( 76 "\n\n\ 77 The style crate requires enabling one of its 'servo' or 'gecko' feature flags. \ 78 \n\n" 79 ), 80 }; 81 println!("cargo:rerun-if-changed=build.rs"); 82 println!("cargo:out_dir={}", env::var("OUT_DIR").unwrap()); 83 generate_properties(engine); 84 build_gecko::generate(); 85 }