Skip to content

Commit

Permalink
Merge pull request #241 from tschoonj/meson-python
Browse files Browse the repository at this point in the history
Use meson-python for building pip installable packages
  • Loading branch information
tschoonj authored Jul 27, 2024
2 parents 7ee1206 + a8aad3b commit ae8b16f
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 44 deletions.
4 changes: 4 additions & 0 deletions .bumpversion.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,7 @@ replace = version = '{new_version}'
[bumpversion:file:xraylib.spec]
search = Version: {current_version}
replace = Version: {new_version}

[bumpversion:file:pyproject.toml]
search = version = "{current_version}"
replace = version = "{new_version}"
71 changes: 71 additions & 0 deletions .github/workflows/build-publish-pypi.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
name: Build and upload to PyPI

on:
workflow_dispatch:
pull_request:
push:
branches:
- master
tags:
- v*

jobs:
build_wheels:
name: Build wheels on ${{ matrix.os }}
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
# macos-13 is an intel runner, macos-14 is apple silicon
os: [ubuntu-latest, windows-latest, macos-13, macos-14]

steps:
- uses: actions/checkout@v4

# https://github.com/actions/runner-images/issues/3342#issuecomment-2206538745
- name: Install swig on macOS
if: startsWith(matrix.os, 'macos')
run: brew install swig
- name: Build wheels
uses: pypa/cibuildwheel@v2.19.2

- uses: actions/upload-artifact@v4
with:
name: cibw-wheels-${{ matrix.os }}-${{ strategy.job-index }}
path: ./wheelhouse/*.whl
retention-days: 1

build_sdist:
name: Build source distribution
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- name: Build sdist
run: pipx run build --sdist

- uses: actions/upload-artifact@v4
with:
name: cibw-sdist
path: dist/*.tar.gz
retention-days: 1

upload_pypi:
name: Upload to pypi
needs: [build_wheels, build_sdist]
runs-on: ubuntu-latest
environment: pypi
permissions:
id-token: write
if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags/v')
steps:
- uses: actions/download-artifact@v4
with:
# unpacks all CIBW artifacts into dist/
pattern: cibw-*
path: dist
merge-multiple: true

- uses: pypa/gh-action-pypi-publish@release/v1
# with:
# To test: repository-url: https://test.pypi.org/legacy/
35 changes: 3 additions & 32 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,33 +11,6 @@ on:
- master

jobs:
pip:
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest, macos-latest]
python-version: ['3.9', '3.10', '3.11', '3.12']
name: Pip install with Python ${{ matrix.python-version }} on ${{ matrix.os }}
steps:
- uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}
# https://github.com/actions/runner-images/issues/3342#issuecomment-2206538745
- name: Install swig on macOS
run: brew install swig
if: ${{ matrix.os == 'macos-latest' }}
- name: Install xraylib with pip
run: |
python -m pip install -U numpy pip
python -m pip install . -vv
- name: Test importing xraylib
run: |
python -c "import xraylib"
python -c "import xraylib_np"
visual-studio:
timeout-minutes: 60
runs-on: windows-latest
Expand All @@ -61,7 +34,7 @@ jobs:
- uses: ilammy/msvc-dev-cmd@v1
- name: Build with meson
run: |
meson setup meson-build --buildtype=release -Dpython=python -Dpython-bindings=enabled -Dpython-numpy-bindings=enabled
meson setup meson-build --buildtype=release -Dpython-bindings=enabled -Dpython-numpy-bindings=enabled
meson compile -C meson-build
meson test -C meson-build || (cat meson-build\meson-logs\testlog.txt)
# ninja -v -C meson-build dist
Expand Down Expand Up @@ -221,7 +194,7 @@ jobs:
if: matrix.buildsystem == 'meson'
run: |
set -ex
meson setup meson-build -Dpython=/opt/homebrew/bin/python3 -Dpython-bindings=enabled -Dpython-numpy-bindings=enabled
meson setup meson-build -Dpython-bindings=enabled -Dpython-numpy-bindings=enabled
meson compile -C meson-build
meson test -C meson-build
meson dist -C meson-build
Expand Down Expand Up @@ -298,14 +271,13 @@ jobs:
TARBALL=$(ls *.tar.gz)
tar xfz $TARBALL
cd ${TARBALL%.tar.gz}
meson setup meson-build -Dpython-bindings=enabled -Dpython-numpy-bindings=enabled -Dpython=${XRL_PYTHON3}
meson setup meson-build -Dpython-bindings=enabled -Dpython-numpy-bindings=enabled
meson compile -C meson-build
set +ex
env:
CC: ${{matrix.cc}}
CXX: ${{matrix.cxx}}
LDFLAGS: ${{matrix.ldflags}}
XRL_PYTHON3: /opt/homebrew/bin/python3

linux:
timeout-minutes: 60
Expand All @@ -315,7 +287,6 @@ jobs:
image:
- ubuntu:jammy
- debian:bookworm
- rockylinux:8
- rockylinux:9
- fedora:latest
cc:
Expand Down
4 changes: 2 additions & 2 deletions meson.build
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
project('xraylib',
'c',
meson_version: '>= 0.56.0',
meson_version: '>= 0.60.0',
version: '4.1.4',
license: 'BSD',
default_options: ['cpp_std=c++11',]
Expand Down Expand Up @@ -104,7 +104,7 @@ subdir('cplusplus')

if not (get_option('python-bindings').disabled() and get_option('python-numpy-bindings').disabled())
pymod = import('python')
python = pymod.find_installation(get_option('python'), required: false, modules: ['numpy',])
python = pymod.find_installation(required: false, modules: ['numpy',])

if not python.found() or python.language_version().version_compare('< 3.6')
if get_option('python-bindings').enabled() or get_option('python-numpy-bindings').enabled()
Expand Down
1 change: 0 additions & 1 deletion meson_options.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,3 @@ option('fortran-bindings', type: 'feature', value: 'auto', description: 'Build F
option('python-bindings', type: 'feature', value: 'auto', description: 'Build classic Python bindings')
option('python-numpy-bindings', type: 'feature', value: 'auto', description: 'Build numpy Python bindings')
option('swig', type : 'string', value : 'swig', description: 'Path to swig executable')
option('python', type : 'string', value : 'python3', description: 'Python interpreter to compile bindings for')
28 changes: 20 additions & 8 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
[project]
name = "xraylib"
authors = [
{name="Tom Schoonjans", email="Tom.Schoonjans@gmail.com"}
]
description = "pip-installable xraylib"
requires-python = "~=3.7"
requires-python = "~=3.8"
readme = "README.md"
classifiers = [
"Intended Audience :: Developers",
Expand All @@ -14,20 +15,31 @@ classifiers = [
dependencies = [
"numpy"
]
version = "4.1.4"

[project.urls]
repository = "https://github.com/tschoonj/xraylib"
documentation = "https://github.com/tschoonj/xraylib"

[build-system]
requires = ["mesonpep517", "ninja", "Cython", "numpy"]
build-backend = "mesonpep517.buildapi"
build-backend = 'mesonpy'
requires = ['meson-python', 'Cython', 'ninja', 'numpy']

[tool.mesonpep517.metadata]
meson-python-option-name = "python"
meson-options = [
[tool.meson-python.args]
setup = [
"-Ddefault_library=static",
"-Dpython-bindings=enabled",
"-Dpython-numpy-bindings=enabled"
"-Dpython-numpy-bindings=enabled",
"-Dfortran-bindings=disabled",
"--vsenv"
]
summary = "pip-installable xraylib"
install = ['--tags=python-runtime']

[tool.cibuildwheel]
skip = ["*-win32", "*-manylinux_i686", "pp*", "*musllinux*"]
build-verbosity = 1
test-command = [
'python -c "import xraylib"',
'python -c "import xraylib_np"',
]
test-skip = "cp38-macosx_*:arm64"
6 changes: 5 additions & 1 deletion python/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import numpy
print(numpy.get_include())
'''

rv = run_command(get_option('python'), '-c', numpy_include_script, check: false)
rv = run_command(python, '-c', numpy_include_script, check: false)
if rv.returncode() != 0
error('Could not retrieve numpy include location. Please check that numpy has been installed.')
endif
Expand Down Expand Up @@ -43,13 +43,16 @@ if not get_option('python-bindings').disabled()
],
install_dir: [false, python.get_install_dir(pure: false)],
install: true,
install_tag: [false, 'python-runtime']

)
xraylib_ext = python.extension_module('_xraylib', xraylib_wrap_c[0],
dependencies : [python_dep, xraylib_lib_dep],
include_directories: extra_include_dirs,
install: true,
install_dir: python.get_install_dir(pure: false),
c_args: core_c_args + ['-I' + numpy_header_location] + python_error_flags,
install_tag: ['python-runtime']
)
subdir('tests')
endif
Expand Down Expand Up @@ -83,6 +86,7 @@ if not get_option('python-numpy-bindings').disabled()
install: true,
install_dir: python.get_install_dir(pure: false),
c_args: core_c_args + ['-I' + numpy_header_location] + python_error_flags,
install_tag: ['python-runtime'],
)

endif
Expand Down

0 comments on commit ae8b16f

Please sign in to comment.