Skip to content

Commit 3dc0283

Browse files
authored
Make Rust extensions work on macOS (#12)
1 parent 81d46e3 commit 3dc0283

File tree

4 files changed

+23
-3
lines changed

4 files changed

+23
-3
lines changed

Modules/cpython-sys/build.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ fn main() {
88
.header("wrapper.h")
99
.clang_arg(format!("-I{}", srcdir.as_os_str().to_str().unwrap()))
1010
.clang_arg(format!("-I{}/Include", srcdir.as_os_str().to_str().unwrap()))
11+
.clang_arg(format!("-I{}/Include/internal", srcdir.as_os_str().to_str().unwrap()))
1112
.allowlist_function("Py.*")
1213
.allowlist_function("_Py.*")
1314
.allowlist_type("Py.*")
@@ -24,4 +25,4 @@ fn main() {
2425
bindings
2526
.write_to_file(out_path.join("bindings.rs"))
2627
.expect("Couldn't write bindings!");
27-
}
28+
}

Modules/makesetup

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ case $makepre in
8383
'') makepre=Makefile.pre;;
8484
esac
8585

86+
UNAME_SYSTEM=`uname -s 2>/dev/null || echo unknown`
87+
8688
# Newline for sed i and a commands
8789
NL='\
8890
'
@@ -289,7 +291,14 @@ sed -e 's/[ ]*#.*//' -e '/^[ ]*$/d' |
289291
echo "$rule" >>$rulesf
290292
for mod in $mods
291293
do
292-
custom_ldflags="-Wl,--defsym=PyInit_$mod=PyInit_$mod"
294+
case $UNAME_SYSTEM in
295+
Darwin*)
296+
custom_ldflags="$custom_ldflags -Wl,-u,_PyInit_$mod"
297+
;;
298+
*)
299+
custom_ldflags="$custom_ldflags -Wl,--defsym=PyInit_$mod=PyInit_$mod"
300+
;;
301+
esac
293302
done
294303
fi
295304
case $doconfig in

Python/remote_debug.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ extern "C" {
2929

3030
#include "pyconfig.h"
3131
#include "internal/pycore_ceval.h"
32+
#include "internal/pycore_debug_offsets.h"
3233

3334
#ifdef __linux__
3435
# include <elf.h>

Tools/build/regen-rust-wrapper-h.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@
33
from pathlib import Path
44

55
ROOT = Path(__file__).resolve().parents[2]
6-
INCLUDE = ROOT / "Include"
76
WRAPPER_H = ROOT / "Modules" / "cpython-sys" / "wrapper.h"
7+
SKIP_PREFIXES = ("cpython/",)
8+
SKIP_EXACT = {
9+
"internal/pycore_crossinterp_data_registry.h",
10+
}
811

912
def normalize_path(header: str) -> str:
1013
return re.sub(r'(:?\.\/)(:?Include\/)?', '', header)
@@ -18,7 +21,13 @@ def main(output: str = WRAPPER_H) -> None:
1821
f.write("#include \"Modules/expat/expat.h\"\n")
1922
for header in headers.split():
2023
normalized_path = normalize_path(header)
24+
if normalized_path.startswith(SKIP_PREFIXES):
25+
continue
26+
if normalized_path in SKIP_EXACT:
27+
continue
2128
f.write(f"#include \"{normalized_path}\"\n")
29+
if normalized_path == "Python/remote_debug.h":
30+
f.write("#undef UNUSED\n")
2231

2332
if __name__ == "__main__":
2433
import sys

0 commit comments

Comments
 (0)