Skip to content

Commit

Permalink
Add 'gravelspoon' script that can be used in a meson.build file to ob…
Browse files Browse the repository at this point in the history
…tain the NumPy include files.
  • Loading branch information
mjziebarth committed Dec 29, 2023
1 parent 1fe3e2e commit cb145aa
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 2 deletions.
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,20 @@ setup(name='mypackage',
)
```

### Finding the NumPy include directory
Mebuex ships with a Python script to discover the system NumPy includes when
building in the isolated build environment. The `gravelspoon` command that
helps break out of the build prison can be used in a `meson.build` file as
follows:
```python
incpath_np = run_command(
gravelspoon
)

incdir_np = include_directories([incpath_np])
```


## Install
Mebuex can be installed with Pip:
```bash
Expand All @@ -57,6 +71,11 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).


### [1.2.0] - 2023-12-29
#### Added
- Add the `gravelspoon` command to break out of the setuptools build isolation
and find the system NumPy includes.

### [1.1.7] - 2023-08-20
#### Added
- Print error message stderr when re-raising assumed compilation error.
Expand Down
8 changes: 8 additions & 0 deletions mebuex/scripts/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Command line scripts.
#
# Author: Malte J. Ziebarth ([email protected])
#
# Copyright 2023, Malte J. Ziebarth
# SPDX-License-Identifier: MIT

from .gravelspoon import numpy_get_include
64 changes: 64 additions & 0 deletions mebuex/scripts/gravelspoon.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# Breaking out of the setuptools build isolation to use the system's
# NumPy includes.
#
# Author: Malte J. Ziebarth ([email protected])
#
# Copyright 2023, Malte J. Ziebarth
# SPDX-License-Identifier: MIT


def numpy_get_include():
"""
This function tries to find the NumPy include directory,
potentially within a build isolation, and prints the include
path to standard out.
"""
try:
# In case things work out of the box:
import numpy
print(numpy.get_include())

except ImportError:
import subprocess
import sys
from pathlib import Path
from os import rename, symlink
np_incl_byte = subprocess.check_output(
[sys.executable,'-c','import os;os.chdir("..");import numpy;'
'print(numpy.get_include())'],
env={}
)

np_include = np_incl_byte.decode().strip()

# Make numpy available in the isolated environment this code is running
# in. Do this by symlinking the previously discovered system NumPy
# package into a site-package directory found in the isolated
# environment Python path.
found_site_packages = False
success = False
for path in sys.path[::-1]:
p = Path(path)
if 'site-packages' in path:
found_site_packages = True
p = Path(path)
if not p.exists():
continue
is_dir = (p / "numpy").is_dir()
if is_dir:
rename((p / "numpy").resolve(), (p / "numpyold").resolve())
symlink(Path(np_include).parent.parent.resolve(),
(p / "numpy").resolve())
success = True
break

if not success:
msg = "Could not link the NumPy package to the isolated " \
"site-packages."
if found_site_packages:
msg += " Found site-packages but did not exist."
else:
msg += " Found no site-pacakges."
raise RuntimeError(msg)

print(np_include)
7 changes: 5 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ requires = ["setuptools>=61"]

[project]
name = "mebuex"
version = "1.1.7"
version = "1.2.0"
authors = [
{name = "Malte J. Ziebarth", email = "[email protected]"},
]
Expand All @@ -28,5 +28,8 @@ license = {text="MIT"}
"Homepage" = "https://github.com/mjziebarth/Mebuex"
"Bug Tracker" = "https://github.com/mjziebarth/Mebuex/issues"

[project.scripts]
gravelspoon = "mebuex.scripts:numpy_get_include"

[tool.setuptools]
packages = ["mebuex"]
packages = ["mebuex","mebuex.scripts"]

0 comments on commit cb145aa

Please sign in to comment.