|
| 1 | +//! Generate sysconfig mappings for supported python-build-standalone *nix platforms. |
| 2 | +use anstream::println; |
| 3 | +use anyhow::{Result, bail}; |
| 4 | +use pretty_assertions::StrComparison; |
| 5 | +use serde::Deserialize; |
| 6 | +use std::collections::BTreeMap; |
| 7 | +use std::fmt::Write; |
| 8 | +use std::path::PathBuf; |
| 9 | + |
| 10 | +use crate::ROOT_DIR; |
| 11 | +use crate::generate_all::Mode; |
| 12 | + |
| 13 | +/// Contains current supported targets |
| 14 | +const TARGETS_YML_URL: &str = "https://raw.githubusercontent.com/astral-sh/python-build-standalone/refs/tags/20250529/cpython-unix/targets.yml"; |
| 15 | + |
| 16 | +#[derive(clap::Args)] |
| 17 | +pub(crate) struct Args { |
| 18 | + #[arg(long, default_value_t, value_enum)] |
| 19 | + pub(crate) mode: Mode, |
| 20 | +} |
| 21 | + |
| 22 | +#[derive(Debug, Deserialize)] |
| 23 | +struct TargetConfig { |
| 24 | + host_cc: Option<String>, |
| 25 | + host_cxx: Option<String>, |
| 26 | + target_cc: Option<String>, |
| 27 | + target_cxx: Option<String>, |
| 28 | +} |
| 29 | + |
| 30 | +pub(crate) async fn main(args: &Args) -> Result<()> { |
| 31 | + let reference_string = generate().await?; |
| 32 | + let filename = "generated_mappings.rs"; |
| 33 | + let reference_path = PathBuf::from(ROOT_DIR) |
| 34 | + .join("crates") |
| 35 | + .join("uv-python") |
| 36 | + .join("src") |
| 37 | + .join("sysconfig") |
| 38 | + .join(filename); |
| 39 | + |
| 40 | + match args.mode { |
| 41 | + Mode::DryRun => { |
| 42 | + println!("{reference_string}"); |
| 43 | + } |
| 44 | + Mode::Check => match fs_err::read_to_string(reference_path) { |
| 45 | + Ok(current) => { |
| 46 | + if current == reference_string { |
| 47 | + println!("Up-to-date: {filename}"); |
| 48 | + } else { |
| 49 | + let comparison = StrComparison::new(¤t, &reference_string); |
| 50 | + bail!( |
| 51 | + "{filename} changed, please run `cargo dev generate-sysconfig-metadata`:\n{comparison}" |
| 52 | + ); |
| 53 | + } |
| 54 | + } |
| 55 | + Err(err) if err.kind() == std::io::ErrorKind::NotFound => { |
| 56 | + bail!("{filename} not found, please run `cargo dev generate-sysconfig-metadata`"); |
| 57 | + } |
| 58 | + Err(err) => { |
| 59 | + bail!( |
| 60 | + "{filename} changed, please run `cargo dev generate-sysconfig-metadata`:\n{err}" |
| 61 | + ); |
| 62 | + } |
| 63 | + }, |
| 64 | + Mode::Write => match fs_err::read_to_string(&reference_path) { |
| 65 | + Ok(current) => { |
| 66 | + if current == reference_string { |
| 67 | + println!("Up-to-date: {filename}"); |
| 68 | + } else { |
| 69 | + println!("Updating: {filename}"); |
| 70 | + fs_err::write(reference_path, reference_string.as_bytes())?; |
| 71 | + } |
| 72 | + } |
| 73 | + Err(err) if err.kind() == std::io::ErrorKind::NotFound => { |
| 74 | + println!("Updating: {filename}"); |
| 75 | + fs_err::write(reference_path, reference_string.as_bytes())?; |
| 76 | + } |
| 77 | + Err(err) => { |
| 78 | + bail!( |
| 79 | + "{filename} changed, please run `cargo dev generate-sysconfig-metadata`:\n{err}" |
| 80 | + ); |
| 81 | + } |
| 82 | + }, |
| 83 | + } |
| 84 | + |
| 85 | + Ok(()) |
| 86 | +} |
| 87 | + |
| 88 | +async fn generate() -> Result<String> { |
| 89 | + println!("Downloading python-build-standalone cpython-unix/targets.yml ..."); |
| 90 | + let body = reqwest::get(TARGETS_YML_URL).await?.text().await?; |
| 91 | + |
| 92 | + let parsed: BTreeMap<String, TargetConfig> = serde_yaml::from_str(&body)?; |
| 93 | + |
| 94 | + let mut replacements: BTreeMap<&str, BTreeMap<String, String>> = BTreeMap::new(); |
| 95 | + |
| 96 | + for targets_config in parsed.values() { |
| 97 | + for sysconfig_cc_entry in ["CC", "LDSHARED", "BLDSHARED", "LINKCC"] { |
| 98 | + if let Some(ref from_cc) = targets_config.host_cc { |
| 99 | + replacements |
| 100 | + .entry(sysconfig_cc_entry) |
| 101 | + .or_default() |
| 102 | + .insert(from_cc.to_string(), "cc".to_string()); |
| 103 | + } |
| 104 | + if let Some(ref from_cc) = targets_config.target_cc { |
| 105 | + replacements |
| 106 | + .entry(sysconfig_cc_entry) |
| 107 | + .or_default() |
| 108 | + .insert(from_cc.to_string(), "cc".to_string()); |
| 109 | + } |
| 110 | + } |
| 111 | + for sysconfig_cxx_entry in ["CXX", "LDCXXSHARED"] { |
| 112 | + if let Some(ref from_cxx) = targets_config.host_cxx { |
| 113 | + replacements |
| 114 | + .entry(sysconfig_cxx_entry) |
| 115 | + .or_default() |
| 116 | + .insert(from_cxx.to_string(), "c++".to_string()); |
| 117 | + } |
| 118 | + if let Some(ref from_cxx) = targets_config.target_cxx { |
| 119 | + replacements |
| 120 | + .entry(sysconfig_cxx_entry) |
| 121 | + .or_default() |
| 122 | + .insert(from_cxx.to_string(), "c++".to_string()); |
| 123 | + } |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + let mut output = String::new(); |
| 128 | + |
| 129 | + // Opening statements |
| 130 | + output.push_str("//! DO NOT EDIT\n"); |
| 131 | + output.push_str("//!\n"); |
| 132 | + output.push_str("//! Generated with `cargo run dev generate-sysconfig-metadata`\n"); |
| 133 | + output.push_str("//! Targets from <https://github.com/astral-sh/python-build-standalone/blob/20250529/cpython-unix/targets.yml>\n"); |
| 134 | + output.push_str("//!\n"); |
| 135 | + |
| 136 | + // Disable clippy/fmt |
| 137 | + output.push_str("#![allow(clippy::all)]\n"); |
| 138 | + output.push_str("#![cfg_attr(any(), rustfmt::skip)]\n\n"); |
| 139 | + |
| 140 | + // Begin main code |
| 141 | + output.push_str("use std::collections::BTreeMap;\n"); |
| 142 | + output.push_str("use std::sync::LazyLock;\n\n"); |
| 143 | + output.push_str("use crate::sysconfig::replacements::{ReplacementEntry, ReplacementMode};\n\n"); |
| 144 | + |
| 145 | + output.push_str( |
| 146 | + "/// Mapping for sysconfig keys to lookup and replace with the appropriate entry.\n", |
| 147 | + ); |
| 148 | + output.push_str("pub(crate) static DEFAULT_VARIABLE_UPDATES: LazyLock<BTreeMap<String, Vec<ReplacementEntry>>> = LazyLock::new(|| {\n"); |
| 149 | + output.push_str(" BTreeMap::from_iter([\n"); |
| 150 | + |
| 151 | + // Add Replacement Entries for CC, CXX, etc. |
| 152 | + for (key, entries) in &replacements { |
| 153 | + writeln!(output, " (\"{key}\".to_string(), vec![")?; |
| 154 | + for (from, to) in entries { |
| 155 | + writeln!( |
| 156 | + output, |
| 157 | + " ReplacementEntry {{ mode: ReplacementMode::Partial {{ from: \"{from}\".to_string() }}, to: \"{to}\".to_string() }}," |
| 158 | + )?; |
| 159 | + } |
| 160 | + writeln!(output, " ]),")?; |
| 161 | + } |
| 162 | + |
| 163 | + // Add AR case last |
| 164 | + output.push_str(" (\"AR\".to_string(), vec![\n"); |
| 165 | + output.push_str(" ReplacementEntry {\n"); |
| 166 | + output.push_str(" mode: ReplacementMode::Full,\n"); |
| 167 | + output.push_str(" to: \"ar\".to_string(),\n"); |
| 168 | + output.push_str(" },\n"); |
| 169 | + output.push_str(" ]),\n"); |
| 170 | + |
| 171 | + // Closing |
| 172 | + output.push_str(" ])\n});\n"); |
| 173 | + |
| 174 | + Ok(output) |
| 175 | +} |
| 176 | + |
| 177 | +#[cfg(test)] |
| 178 | +mod tests { |
| 179 | + use std::env; |
| 180 | + |
| 181 | + use anyhow::Result; |
| 182 | + |
| 183 | + use uv_static::EnvVars; |
| 184 | + |
| 185 | + use crate::generate_all::Mode; |
| 186 | + |
| 187 | + use super::{Args, main}; |
| 188 | + |
| 189 | + #[tokio::test] |
| 190 | + async fn test_generate_sysconfig_mappings() -> Result<()> { |
| 191 | + let mode = if env::var(EnvVars::UV_UPDATE_SCHEMA).as_deref() == Ok("1") { |
| 192 | + Mode::Write |
| 193 | + } else { |
| 194 | + Mode::Check |
| 195 | + }; |
| 196 | + main(&Args { mode }).await |
| 197 | + } |
| 198 | +} |
0 commit comments