Skip to content

Commit

Permalink
BLD: migrate to the mesonpy build backend
Browse files Browse the repository at this point in the history
  • Loading branch information
neutrinoceros committed Feb 17, 2025
1 parent cd48afc commit 7330948
Show file tree
Hide file tree
Showing 9 changed files with 178 additions and 88 deletions.
6 changes: 6 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# exclude files that shouldn't be included in source distributions
.* export-ignore
tests/pytest_mpl_baseline/** export-ignore
_typos.toml export-ignore
uv.lock export-ignore
src/gpgi/_lib.py export-ignore
7 changes: 4 additions & 3 deletions .github/workflows/cd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ on:
pull_request:
paths:
- pyproject.toml
- setup.py
- MANIFEST.in
- meson.*
- .github/workflows/cd.yml
workflow_dispatch:

Expand Down Expand Up @@ -46,7 +45,9 @@ jobs:
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
- uses: astral-sh/setup-uv@4db96194c378173c656ce18a155ffc14a9fc4355 # v5.2.2
- name: Build sdist
run: uv build --sdist
run: |
uv sync --only-dev
uv build --sdist
- name: Upload sdist
uses: actions/upload-artifact@65c4c4a1ddee5b72f698fdd19549f0f0fb45cf08 # v4.6.0
with:
Expand Down
19 changes: 12 additions & 7 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ jobs:
- uses: astral-sh/setup-uv@4db96194c378173c656ce18a155ffc14a9fc4355 # v5.2.2
- name: Check build
run: |
uvx check-manifest
uv sync --only-dev
uv build
Expand Down Expand Up @@ -61,12 +60,14 @@ jobs:
with:
python-version: ${{ matrix.python-version }}

# TODO: add --no-editable
- run: |
uv sync --only-dev
uv sync --group covcheck
env:
GPGI_PY_LIB: ${{ matrix.marker == 'PY_LIB' }}
- run: uv sync --group covcheck --no-install-project
- if: ${{ matrix.marker != 'PY_LIB' }}
# using editable mode for simplicity regarding coverage analysis
run: uv pip install --editable . --no-deps
- if: ${{ matrix.marker == 'PY_LIB' }}
# disabling caching here to avoid shadowing the pure Python module
# with a remnant .so from a previous run
run: uv pip install --editable . --no-deps -Csetup-args='-Dpylib=true' --no-cache
- name: Test
run: uv run --no-sync coverage run --parallel-mode -m pytest --color=yes

Expand Down Expand Up @@ -253,6 +254,10 @@ jobs:
echo "PYTHON_GIL=0" >> $GITHUB_ENV
echo
- name: Build
# ninja is a system level requirement to meson-python
# it is automatically obtained from PyPI if lacking at the system level
# when building a package in isolation, which is why it's not listed
# under our build-system requirements.
run: |
uv venv
uv lock --upgrade --prerelease=allow --no-build
Expand Down
13 changes: 0 additions & 13 deletions MANIFEST.in

This file was deleted.

77 changes: 77 additions & 0 deletions meson.build
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
project('gpgi', 'c', 'cython', meson_version: '>=1.5.0')

# detect missing dependencies early
py = import('python').find_installation(pure: false)
cy = meson.get_compiler('cython')
cython = find_program(cy.cmd_array()[0])

numpy_compile_flags = [
# keep in sync with pyproject.toml
'-DNPY_TARGET_VERSION=NPY_1_25_API_VERSION',
'-DNPY_NO_DEPRECATED_API=NPY_1_25_API_VERSION',
]

# copied from PyWavelets
# https://github.com/PyWavelets/pywt/blob/becef5486174c77727ea2a60e6392744b2cc1d4a/pywt/_extensions/meson.build#L13
# When building against numpy 1.x is dropped, this can be simplified: the else branch
# becomes unreachable, so `required: false` can be omitted.

_numpy_dep = dependency('numpy', required: false)
if _numpy_dep.found()
np_dep = declare_dependency(dependencies: _numpy_dep, compile_args: numpy_compile_flags)
else
# For cross-compilation it is often not possible to run the Python interpreter
# in order to retrieve numpy's include directory. It can be specified in the
# cross file instead:
# [properties]
# numpy-include-dir = /abspath/to/host-pythons/site-packages/numpy/core/include
#
# This uses the path as is, and avoids running the interpreter.
incdir_numpy = meson.get_external_property('numpy-include-dir', 'not-given')
if incdir_numpy == 'not-given'
incdir_numpy = run_command(py,
[
'-c',
'''import os
import numpy as np
try:
incdir = os.path.relpath(np.get_include())
except Exception:
incdir = np.get_include()
print(incdir)
'''
],
check: true
).stdout().strip()
endif
inc_np = include_directories(incdir_numpy)
np_dep = declare_dependency(include_directories: inc_np, compile_args: numpy_compile_flags)
endif

if get_option('pylib')
py.install_sources('src/gpgi/_lib.py', subdir: 'gpgi')
else
py.extension_module(
'_lib',
'src/gpgi/_lib.pyx',
subdir: 'gpgi',
install: true,
dependencies : [np_dep],
c_args: numpy_compile_flags,
)
endif

# meson doesn't have an equivalent to setuptools.packages.find
# at the time of writing, so, listing every module explicitly here
py.install_sources(
'src/gpgi/__init__.py',
'src/gpgi/_boundaries.py',
'src/gpgi/_data_types.py',
'src/gpgi/_lib.pyi',
'src/gpgi/_load.py',
'src/gpgi/_spatial_data.py',
'src/gpgi/_typing.py',
'src/gpgi/py.typed',
'src/gpgi/typing.py',
subdir: 'gpgi'
)
6 changes: 6 additions & 0 deletions meson.options
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
option(
'pylib',
type : 'boolean',
value : false,
description : 'build as a pure Python library',
)
25 changes: 10 additions & 15 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
[build-system]
# keep in sync with dependency-groups
requires = [
"setuptools>=61.2",
"meson-python>=0.17.1",
"Cython>=3.0",
"numpy>=1.25.0",
]
build-backend = "setuptools.build_meta"
build-backend = "mesonpy"

[project]
name = "gpgi"
Expand All @@ -22,8 +22,8 @@ classifiers = [
]
requires-python = ">=3.11"
dependencies = [
# keep in sync with NPY_TARGET_VERSION (setup.py)
# keep in sync with build-time requirements (dev group)
# keep in sync with NPY_TARGET_VERSION (meson.build)
"numpy>=1.25, <3",
]

Expand Down Expand Up @@ -67,25 +67,21 @@ typecheck = [
# This could be simplified/reunifed depending on what happens on the following
# upstream issue
# https://github.com/astral-sh/uv/issues/7052
#
# ninja is a system level requirement to meson-python
# it is automatically obtained from PyPI if lacking at the system level
# when building a package in isolation, which is why it's not listed
# under build-system requires.
dev = [
"setuptools>=61.2",
"meson-python>=0.17.1",
"ninja>=1.11.1.3",
"Cython>=3.0.12",
"numpy>=1.25.0, <3", # keep in sync with runtime requirement
]

[tool.uv]
no-build-isolation-package = ["gpgi"]

[tool.setuptools]
license-files = [
"LICENSE",
]
include-package-data = false

[tool.setuptools.packages.find]
where = ["src"]
namespaces = false

[tool.coverage.run]
source = [
"src/gpgi",
Expand Down Expand Up @@ -138,7 +134,6 @@ convention = "numpy"

[tool.ruff.lint.per-file-ignores]
"tests/**" = ["D"]
"setup.py" = ["D"]
"_backports.py" = ["D"]

[tool.mypy]
Expand Down
39 changes: 0 additions & 39 deletions setup.py

This file was deleted.

74 changes: 63 additions & 11 deletions uv.lock

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

0 comments on commit 7330948

Please sign in to comment.