Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 34 additions & 32 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Modules/cpython-sys/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@ edition = "2024"

[build-dependencies]
bindgen = "0.72.1"
prettyplease = "0.2.37"
shlex = "1.3"
syn = { version = "2.0.110", features = ["full", "parsing"] }
56 changes: 14 additions & 42 deletions Modules/cpython-sys/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -324,52 +324,24 @@ fn patch_windows_imported_pointer_globals(bindings: String, dll_name: &str) -> S
// The fix: annotate pointer-valued extern statics with `raw-dylib` on
// Windows so Rust generates the import thunk itself and handles the IAT
// indirection correctly — two loads, matching `__declspec(dllimport)`.
let lines: Vec<_> = bindings.lines().collect();
let mut patched = String::with_capacity(bindings.len());
let mut index = 0;

while index < lines.len() {
if lines[index] == "unsafe extern \"C\" {"
&& lines
.get(index + 1)
.and_then(|l| parse_pointer_static_decl(l))
.is_some()
&& lines.get(index + 2).is_some_and(|l| l.trim() == "}")
{
patched.push_str(&format!(
"#[cfg_attr(windows, link(name = \"{dll_name}\", kind = \"raw-dylib\"))]\n"
));
// Keep the original extern block unchanged.
for i in index..index + 3 {
patched.push_str(lines[i]);
patched.push('\n');
}
index += 3;
let mut file = syn::parse_file(&bindings).expect("bindgen emitted invalid Rust");

for item in &mut file.items {
let syn::Item::ForeignMod(foreign_mod) = item else {
continue;
};
let [syn::ForeignItem::Static(static_item)] = foreign_mod.items.as_slice() else {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it expected that these extern blocks have a single item only?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, bindgen will emit one extern block per global, e.g. (taken from a generated c_api.rs):

unsafe extern "C" {
    pub static mut PyLong_Type: PyTypeObject;
}
unsafe extern "C" {
    pub static mut PyBool_Type: PyTypeObject;
}

I'll add a comment because this is somewhat load bearing on how bindgen generates extern data from C.

continue;
};
if !matches!(*static_item.ty, syn::Type::Ptr(_)) {
continue;
}

patched.push_str(lines[index]);
patched.push('\n');
index += 1;
}

patched
}

fn parse_pointer_static_decl(line: &str) -> Option<(&str, bool, &str)> {
let mut decl = line.trim().strip_prefix("pub static ")?;
let is_mut = decl.starts_with("mut ");
if is_mut {
decl = decl.strip_prefix("mut ")?;
}

let (name, ty) = decl.split_once(':')?;
let ty = ty.trim().strip_suffix(';')?;
if !ty.starts_with('*') {
return None;
foreign_mod.attrs.push(syn::parse_quote!(
#[cfg_attr(windows, link(name = #dll_name, kind = "raw-dylib"))]
));
}

Some((name.trim(), is_mut, ty))
prettyplease::unparse(&file)
}

fn add_target_clang_args(
Expand Down
Loading