Skip to content

Commit da8e82c

Browse files
authored
macos dynamic lib (#58)
1 parent 870ffb9 commit da8e82c

4 files changed

Lines changed: 31 additions & 19 deletions

File tree

bindings/python/README.md

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
High-performance SGP4 satellite orbit propagation for Python, powered by Zig with SIMD acceleration.
44

5+
**Supported platforms:** macOS and Linux
6+
**Requires:** Python 3.12+
7+
58
## Quick Start
69

710
```python
@@ -66,15 +69,9 @@ sgp4.propagate_into(times, positions, velocities)
6669

6770
## Building
6871

69-
Requires [Zig](https://ziglang.org/) and Python 3.10+.
72+
Requires [Zig](https://ziglang.org/) and Python 3.12+.
7073

7174
```bash
72-
zig build python-bindings \
73-
-Dpython-include=$(python -c "import sysconfig; print(sysconfig.get_path('include'))") \
74-
-Dpython-lib=python3.12 \
75-
-Dpython-lib-path=$(python -c "import sysconfig; print(sysconfig.get_config_var('LIBDIR'))") \
76-
-Doptimize=ReleaseFast
77-
78-
cp zig-out/bindings/python/astroz/lib_astroz.so bindings/python/astroz/_astroz.so
79-
pip install -e bindings/python
75+
cd bindings/python
76+
pip install -e .
8077
```

bindings/python/pyproject.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ name = "astroz"
77
version = "0.3.0"
88
description = "Python bindings for astroz - high-performance astrodynamics library"
99
readme = "README.md"
10-
requires-python = ">=3.8"
10+
requires-python = ">=3.12"
1111
license = {text = "MIT"}
1212
keywords = ["astrodynamics", "orbital-mechanics", "sgp4", "tle", "satellite"]
1313
classifiers = [
@@ -17,9 +17,10 @@ classifiers = [
1717
"Programming Language :: Python :: 3",
1818
"Topic :: Scientific/Engineering :: Astronomy",
1919
]
20+
dependencies = ["numpy"]
2021

2122
[project.optional-dependencies]
22-
dev = ["numpy", "sgp4"] # For benchmarks and testing
23+
dev = ["sgp4"] # For benchmarks and testing
2324

2425
[project.urls]
2526
Repository = "https://github.com/ATTron/astroz"

bindings/python/setup.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,16 @@ def build_extension(self, ext):
3535
"build",
3636
"python-bindings",
3737
f"-Dpython-include={python_include}",
38-
f"-Dpython-lib={python_lib}",
3938
"-Doptimize=ReleaseFast",
4039
]
4140

42-
# Add library path if it exists
43-
if python_lib_dir and Path(python_lib_dir).exists():
44-
cmd.append(f"-Dpython-lib-path={python_lib_dir}")
41+
# On non-macOS platforms, link against Python library
42+
# On macOS, Python extensions should NOT link against the Python library -
43+
# symbols resolve at runtime when loaded by the interpreter
44+
if sys.platform != "darwin":
45+
cmd.append(f"-Dpython-lib={python_lib}")
46+
if python_lib_dir and Path(python_lib_dir).exists():
47+
cmd.append(f"-Dpython-lib-path={python_lib_dir}")
4548

4649
print(f"Building with: {' '.join(cmd)}")
4750
subprocess.check_call(cmd, cwd=project_root)

build.zig

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -143,12 +143,18 @@ pub fn build(b: *std.Build) void {
143143
python_mod.addIncludePath(.{ .cwd_relative = "/usr/include/python3" });
144144
}
145145

146-
if (python_lib_path) |path| {
147-
python_mod.addLibraryPath(.{ .cwd_relative = path });
146+
const is_macos = target.result.os.tag == .macos;
147+
148+
// On macOS, don't link against Python library - symbols resolve at load time
149+
// when the extension is loaded by the Python interpreter. This is the standard
150+
// approach used by all major Python extension build systems.
151+
if (!is_macos) {
152+
if (python_lib_path) |path| {
153+
python_mod.addLibraryPath(.{ .cwd_relative = path });
154+
}
155+
python_mod.linkSystemLibrary(python_lib_name orelse "python3.12", .{});
148156
}
149157

150-
// Link Python library and libc through the module
151-
python_mod.linkSystemLibrary(python_lib_name orelse "python3.12", .{});
152158
python_mod.link_libc = true;
153159

154160
const python_lib = b.addLibrary(.{
@@ -158,6 +164,11 @@ pub fn build(b: *std.Build) void {
158164
.use_llvm = use_llvm,
159165
});
160166

167+
// On macOS, allow undefined symbols - they resolve when loaded by Python
168+
if (is_macos) {
169+
python_lib.linker_allow_shlib_undefined = true;
170+
}
171+
161172
const python_install = b.addInstallArtifact(python_lib, .{
162173
.dest_dir = .{ .override = .{ .custom = "bindings/python/astroz" } },
163174
});

0 commit comments

Comments
 (0)