Skip to content

Commit

Permalink
Patch additional sysconfig values such as clang at install time (#9916)
Browse files Browse the repository at this point in the history
## Summary

Minor follow up to #9905 to patch
`clang` with `cc`.

Implements the replacements used in
[sysconfigpatcher](https://github.com/bluss/sysconfigpatcher/blob/main/src/sysconfigpatcher.py#L54),
namely

```python
DEFAULT_VARIABLE_UPDATES = {
    "CC": WordReplace("clang", "cc"),
    "CXX": WordReplace("clang++", "c++"),
    "BLDSHARED": WordReplace("clang", "cc"),
    "LDSHARED": WordReplace("clang", "cc"),
    "LDCXXSHARED": WordReplace("clang++", "c++"),
    "LINKCC": WordReplace("clang", "cc"),
    "AR": "ar",
}
```

## Test Plan

Added an additional test. Tested local python installs.

Related traces
```
TRACE Updated `AR` from `/tools/clang-linux64/bin/llvm-ar` to `ar`
TRACE Updated `CC` from `clang -pthread` to `cc -pthread`
TRACE Updated `CXX` from `clang++ -pthread` to `c++ -pthread`
TRACE Updated `BLDSHARED` from `clang -pthread -shared -L/tools/deps/lib` to `cc -pthread -shared -L/tools/deps/lib`
TRACE Updated `LDSHARED` from `clang -pthread -shared -L/tools/deps/lib` to `cc -pthread -shared -L/tools/deps/lib`
TRACE Updated `LDCXXSHARED` from `clang++ -pthread -shared` to `c++ -pthread -shared`
TRACE Updated `LINKCC` from `clang -pthread` to `cc -pthread
```

## Pending Discussion Items

#9905 (comment)
  • Loading branch information
samypr100 authored Dec 17, 2024
1 parent 6dfe177 commit e730ef1
Showing 1 changed file with 73 additions and 11 deletions.
84 changes: 73 additions & 11 deletions crates/uv-python/src/sysconfig/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ mod parser;
/// Replacement mode for sysconfig values.
#[derive(Debug)]
enum ReplacementMode {
Partial { from: String },
Full,
}

Expand All @@ -52,8 +53,13 @@ struct ReplacementEntry {

impl ReplacementEntry {
/// Patches a sysconfig value either partially (replacing a specific word) or fully.
fn patch(&self, _entry: &str) -> String {
fn patch(&self, entry: &str) -> String {
match &self.mode {
ReplacementMode::Partial { from } => entry
.split_whitespace()
.map(|word| if word == from { &self.to } else { word })
.collect::<Vec<_>>()
.join(" "),
ReplacementMode::Full => self.to.clone(),
}
}
Expand All @@ -62,13 +68,69 @@ impl ReplacementEntry {
/// Mapping for sysconfig keys to lookup and replace with the appropriate entry.
static DEFAULT_VARIABLE_UPDATES: LazyLock<BTreeMap<String, ReplacementEntry>> =
LazyLock::new(|| {
BTreeMap::from_iter([(
"AR".to_string(),
ReplacementEntry {
mode: ReplacementMode::Full,
to: "ar".to_string(),
},
)])
BTreeMap::from_iter([
(
"CC".to_string(),
ReplacementEntry {
mode: ReplacementMode::Partial {
from: "clang".to_string(),
},
to: "cc".to_string(),
},
),
(
"CXX".to_string(),
ReplacementEntry {
mode: ReplacementMode::Partial {
from: "clang++".to_string(),
},
to: "c++".to_string(),
},
),
(
"BLDSHARED".to_string(),
ReplacementEntry {
mode: ReplacementMode::Partial {
from: "clang".to_string(),
},
to: "cc".to_string(),
},
),
(
"LDSHARED".to_string(),
ReplacementEntry {
mode: ReplacementMode::Partial {
from: "clang".to_string(),
},
to: "cc".to_string(),
},
),
(
"LDCXXSHARED".to_string(),
ReplacementEntry {
mode: ReplacementMode::Partial {
from: "clang++".to_string(),
},
to: "c++".to_string(),
},
),
(
"LINKCC".to_string(),
ReplacementEntry {
mode: ReplacementMode::Partial {
from: "clang".to_string(),
},
to: "cc".to_string(),
},
),
(
"AR".to_string(),
ReplacementEntry {
mode: ReplacementMode::Full,
to: "ar".to_string(),
},
),
])
});

/// Update the `sysconfig` data in a Python installation.
Expand Down Expand Up @@ -292,8 +354,8 @@ mod tests {
# system configuration generated and used by the sysconfig module
build_time_vars = {
"AR": "ar",
"CC": "clang -pthread",
"CXX": "clang++ -pthread",
"CC": "cc -pthread",
"CXX": "c++ -pthread",
"PYTHON_BUILD_STANDALONE": 1
}
"###);
Expand All @@ -316,7 +378,7 @@ mod tests {
insta::assert_snapshot!(data.to_string_pretty()?, @r###"
# system configuration generated and used by the sysconfig module
build_time_vars = {
"BLDSHARED": "clang -bundle -undefined dynamic_lookup -arch arm64",
"BLDSHARED": "cc -bundle -undefined dynamic_lookup -arch arm64",
"PYTHON_BUILD_STANDALONE": 1
}
"###);
Expand Down

0 comments on commit e730ef1

Please sign in to comment.