From 9e035b386f4e3dca982a4acd06fc36705448f0f6 Mon Sep 17 00:00:00 2001 From: Chris Wright Date: Sat, 5 Feb 2022 18:34:30 +0000 Subject: [PATCH 1/6] Build mappy via Makefile and libminimap2.a --- pyproject.toml | 2 ++ setup.py | 51 +++++++++++++++++++++++++++++--------------------- 2 files changed, 32 insertions(+), 21 deletions(-) create mode 100644 pyproject.toml diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 00000000..bc2be673 --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,2 @@ +[build-system] +requires = ["setuptools", "wheel", "Cython"] diff --git a/setup.py b/setup.py index e6766fa3..0130ec96 100644 --- a/setup.py +++ b/setup.py @@ -1,26 +1,36 @@ try: from setuptools import setup, Extension + from setuptools.command.build_ext import build_ext except ImportError: from distutils.core import setup from distutils.extension import Extension + from distutils.command.build_ext import build_ext -import sys, platform +import sys, platform, subprocess -sys.path.append('python') - -extra_compile_args = ['-DHAVE_KALLOC'] -include_dirs = ["."] - -if platform.machine() in ["aarch64", "arm64"]: - include_dirs.append("sse2neon/") - extra_compile_args.extend(['-ftree-vectorize', '-DKSW_SSE2_ONLY', '-D__SSE2__']) -else: - extra_compile_args.append('-msse4.1') # WARNING: ancient x86_64 CPUs don't have SSE4 def readme(): with open('python/README.rst') as f: return f.read() + +class LibMM2Build(build_ext): + # Uses Makefile to build library, avoids duplicating logic + # determining which objects to compile but does require + # end users to have Make (since precompiled wheels are not + # distributed on PyPI). + def run(self): + def compile_libminimap2(*args, **kwargs): + cmd = ['make', 'libminimap2.a'] + list(args) + subprocess.check_call(cmd) + if platform.machine() in ["aarch64", "arm64"]: + options = ["arm_neon=1", "aarch64=1"] + self.execute( + compile_libminimap2, options, + 'Compiling libminimap2 using Makefile') + build_ext.run(self) + + setup( name = 'mappy', version = '2.24', @@ -32,16 +42,15 @@ def readme(): license = 'MIT', keywords = 'sequence-alignment', scripts = ['python/minimap2.py'], - ext_modules = [Extension('mappy', - sources = ['python/mappy.pyx', 'align.c', 'bseq.c', 'lchain.c', 'seed.c', 'format.c', 'hit.c', 'index.c', 'pe.c', 'options.c', - 'ksw2_extd2_sse.c', 'ksw2_exts2_sse.c', 'ksw2_extz2_sse.c', 'ksw2_ll_sse.c', - 'kalloc.c', 'kthread.c', 'map.c', 'misc.c', 'sdust.c', 'sketch.c', 'esterr.c', 'splitidx.c'], - depends = ['minimap.h', 'bseq.h', 'kalloc.h', 'kdq.h', 'khash.h', 'kseq.h', 'ksort.h', - 'ksw2.h', 'kthread.h', 'kvec.h', 'mmpriv.h', 'sdust.h', - 'python/cmappy.h', 'python/cmappy.pxd'], - extra_compile_args = extra_compile_args, - include_dirs = include_dirs, - libraries = ['z', 'm', 'pthread'])], + cmdclass = {'build_ext': LibMM2Build}, + ext_modules = [ + Extension( + 'mappy', + sources = ['python/mappy.pyx'], + depends = ['python/cmappy.h', 'python/cmappy.pxd'], + include_dirs = ['.'], + extra_objects = ['libminimap2.a'], + libraries = ['z', 'm', 'pthread'])], classifiers = [ 'Development Status :: 5 - Production/Stable', 'License :: OSI Approved :: MIT License', From f5cefd81738e52fdac5e312c0e182f0b2bd75bcd Mon Sep 17 00:00:00 2001 From: Chris Wright Date: Fri, 11 Feb 2022 18:10:45 +0000 Subject: [PATCH 2/6] fix undefined variable --- setup.py | 1 + 1 file changed, 1 insertion(+) diff --git a/setup.py b/setup.py index 0130ec96..ec867add 100644 --- a/setup.py +++ b/setup.py @@ -23,6 +23,7 @@ def run(self): def compile_libminimap2(*args, **kwargs): cmd = ['make', 'libminimap2.a'] + list(args) subprocess.check_call(cmd) + options = [] if platform.machine() in ["aarch64", "arm64"]: options = ["arm_neon=1", "aarch64=1"] self.execute( From 1951fe908ae83efd4f459e060d29b21254d54993 Mon Sep 17 00:00:00 2001 From: Chris Wright Date: Mon, 14 Feb 2022 11:00:01 +0000 Subject: [PATCH 3/6] Fix indentation --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index ec867add..ce4d79c2 100644 --- a/setup.py +++ b/setup.py @@ -23,7 +23,7 @@ def run(self): def compile_libminimap2(*args, **kwargs): cmd = ['make', 'libminimap2.a'] + list(args) subprocess.check_call(cmd) - options = [] + options = [] if platform.machine() in ["aarch64", "arm64"]: options = ["arm_neon=1", "aarch64=1"] self.execute( From ea3cde8c4113aee77d31fc2437335f90e97934b1 Mon Sep 17 00:00:00 2001 From: Chris Wright Date: Wed, 14 Aug 2024 15:42:22 +0100 Subject: [PATCH 4/6] Add all C sources to MANIFEST for source distribution --- MANIFEST.in | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/MANIFEST.in b/MANIFEST.in index 3df1d198..ea2262ce 100644 --- a/MANIFEST.in +++ b/MANIFEST.in @@ -1,7 +1,6 @@ include *.h +include *.c include Makefile -include ksw2_dispatch.c -include main.c include README.md include sse2neon/emmintrin.h include python/cmappy.h From 7f241a49e6558a33c3915e607460cf5ea1c597e9 Mon Sep 17 00:00:00 2001 From: Chris Wright Date: Thu, 15 Aug 2024 09:23:43 +0100 Subject: [PATCH 5/6] Add fPIC to python build --- Makefile | 7 +++++++ setup.py | 2 +- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 17b13b64..939bc0b7 100644 --- a/Makefile +++ b/Makefile @@ -38,6 +38,13 @@ ifneq ($(tsan),) LIBS+=-fsanitize=thread -ldl endif +ifneq ($(python),) + # this allows us to build libminimap2.a here and then link to + # it in the python extension build, rather than doing + CFLAGS+=-fPIC +endif + + .PHONY:all extra clean depend .SUFFIXES:.c .o diff --git a/setup.py b/setup.py index bbca2f45..6e199da3 100644 --- a/setup.py +++ b/setup.py @@ -21,7 +21,7 @@ class LibMM2Build(build_ext): # distributed on PyPI). def run(self): def compile_libminimap2(*args, **kwargs): - cmd = ['make', 'libminimap2.a'] + list(args) + cmd = ['make', 'libminimap2.a'] + list(args) + ["python=1"] subprocess.check_call(cmd) options = [] if platform.machine() in ["aarch64", "arm64"]: From c38e52e548b5711dd42e4a3df08e4afe9fd4be8d Mon Sep 17 00:00:00 2001 From: Chris Wright Date: Thu, 15 Aug 2024 09:39:03 +0100 Subject: [PATCH 6/6] Add github actions for python wheels and sdist --- .github/workflows/python.yml | 48 ++++++++++++++++++++++++++++++++++++ setup.py | 8 +++--- 2 files changed, 52 insertions(+), 4 deletions(-) create mode 100644 .github/workflows/python.yml diff --git a/.github/workflows/python.yml b/.github/workflows/python.yml new file mode 100644 index 00000000..acbd6248 --- /dev/null +++ b/.github/workflows/python.yml @@ -0,0 +1,48 @@ +name: Build + +on: [push, pull_request] + +# doing as cibuildwheel docs says leads to frankenstein arm builds on macos +# so lets be more explicit, no linux arm build as not available on github + +jobs: + build_wheels: + name: Wheel ${{ matrix.python }}-${{ matrix.buildplat[1] }} + runs-on: ${{ matrix.buildplat[0] }} + strategy: + fail-fast: false + matrix: + buildplat: + - [ubuntu-latest, manylinux_x86_64, auto] + - [macos-13, macosx_x86_64, x86_64] + - [macos-14, macosx_arm64, arm64] + python: ["cp38", "cp39", "cp310", "cp311", "cp312"] + + steps: + - uses: actions/checkout@v4 + + - name: Build wheels + uses: pypa/cibuildwheel@v2.20.0 + env: + CIBW_ARCHS: ${{ matrix.buildplat[2] }} + CIBW_BUILD: ${{ matrix.python }}-${{ matrix.buildplat[1] }} + + - uses: actions/upload-artifact@v4 + with: + name: cibw-wheels-${{ matrix.python }}-${{ matrix.buildplat[1] }} + path: ./wheelhouse/*.whl + + + build-sdist: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - uses: actions/setup-python@v5 + with: + python-version: '3.10' + - name: Build sdist + run: python setup.py sdist + - uses: actions/upload-artifact@v4 + with: + name: sdist + path: dist diff --git a/setup.py b/setup.py index 6e199da3..80c89543 100644 --- a/setup.py +++ b/setup.py @@ -21,14 +21,14 @@ class LibMM2Build(build_ext): # distributed on PyPI). def run(self): def compile_libminimap2(*args, **kwargs): - cmd = ['make', 'libminimap2.a'] + list(args) + ["python=1"] + cmd = ['make', 'libminimap2.a'] + list(args) subprocess.check_call(cmd) - options = [] + options = ["python=1"] if platform.machine() in ["aarch64", "arm64"]: - options = ["arm_neon=1", "aarch64=1"] + options += ["arm_neon=1", "aarch64=1"] self.execute( compile_libminimap2, options, - 'Compiling libminimap2 using Makefile') + f'Compiling libminimap2 using Makefile with options: {options}') build_ext.run(self)