Skip to content

Commit

Permalink
Merge pull request #8 from ACEnglish/master
Browse files Browse the repository at this point in the history
pyproject.toml fixes pip installation
  • Loading branch information
kcleal authored May 24, 2023
2 parents b6328dc + f9928ea commit a40979c
Show file tree
Hide file tree
Showing 6 changed files with 152 additions and 142 deletions.
File renamed without changes.
4 changes: 2 additions & 2 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ Alignment of pattern and text strings can be performed by accessing WFA2-lib fun

.. code-block:: python
from pywfa.align import WavefrontAligner
from pywfa import WavefrontAligner
pattern = "TCTTTACTCGCGCGTTGGAGAAATACAATAGT"
text = "TCTATACTGCGCGTTTGGAGAAATAAAATAGT"
Expand Down Expand Up @@ -115,7 +115,7 @@ To configure the `WaveFrontAligner`, options can be provided during initializati

.. code-block:: python
from pywfa.align import WavefrontAligner
from pywfa import WavefrontAligner
a = WavefrontAligner(scope="score",
distance="affine2p",
Expand Down
116 changes: 116 additions & 0 deletions _custom_build.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
import os
import glob
import shutil
import sysconfig
import setuptools
from subprocess import run
from distutils import ccompiler
from setuptools import Extension
from Cython.Build import cythonize
from setuptools.command.build_py import build_py as _build_py

###########
# Helpers #
###########
debug = False

cy_options = {
'annotate': False,
'compiler_directives': {
'profile': debug,
'linetrace': debug,
'boundscheck': debug,
'wraparound': debug,
'nonecheck': debug,
'initializedcheck': debug,
'language_level': 3
}
}

cfg_vars = sysconfig.get_config_vars()
for key, value in cfg_vars.items():
if type(value) == str:
cfg_vars[key] = value.replace("-Wstrict-prototypes", "")

def has_flag(compiler, flagname):
"""Return a boolean indicating whether a flag name is supported on
the specified compiler.
"""
import tempfile
with tempfile.NamedTemporaryFile('w', suffix='.c') as f:
f.write('int main (int argc, char **argv) { return 0; }')
try:
compiler.compile([f.name], extra_postargs=[flagname])
except setuptools.distutils.errors.CompileError:
return False
return True

def get_extra_args(flags):
compiler = ccompiler.new_compiler()
extra_compile_args = []
for f in flags:
if has_flag(compiler, f):
extra_compile_args.append(f)
return extra_compile_args

##################
# WFA2_lib build #
##################
extras = ["-Wno-unused-function", "-Wno-unused-result", '-Wno-ignored-qualifiers', "-Wno-deprecated-declarations"]
extras_pywfa = get_extra_args(extras)

root = os.path.abspath(os.path.dirname(__file__))
wfa = os.path.join(root, "pywfa/WFA2_lib")
libraries = [f"{wfa}/lib"]
library_dirs = [f"{wfa}/lib"]
include_dirs = [".", root, wfa, f"{wfa}/lib", f"{wfa}/utils", f"{wfa}/wavefront", f"{wfa}/bindings/cpp", f"{wfa}/system",
f"{wfa}/alignment", f"{root}/pywfa"]

print("Libs", libraries)
print("Library dirs", library_dirs)
print("Include dirs", include_dirs)

# Can't get openmp to behave. Whenever its on (1), the build fails.
# this has happened on multiple OS with/without `libomp-dev`
# compiler = ccompiler.new_compiler()
omp = 0 #1 if has_flag(compiler, "-fopenmp") else 0
ret = run(f"cd pywfa/WFA2_lib; make clean all BUILD_WFA_PARALLEL={omp} BUILD_MINIMAL=1; cd ../../", shell=True)
if ret.returncode != 0:
print("Unable to build WFA2_lib")
print(ret)
exit(ret.returncode)

##################
# bindings build #
##################
m_ext_module = cythonize(Extension("pywfa.align",
["pywfa/align.pyx"],
libraries=libraries,
library_dirs=library_dirs,
include_dirs=include_dirs,
extra_compile_args=extras_pywfa,
language="c",
extra_objects=[f"{wfa}/lib/libwfa.a"]
),
**cy_options)

shared = glob.glob(f"{root}/build/lib*/pywfa/*.so") + glob.glob(f"{root}/build/lib*/pywfa/*.dll")
[shutil.copy(i, f"{root}/pywfa") for i in shared]

###################
# Basic build_ext #
###################
# Thanks to https://stackoverflow.com/questions/73800736/pyproject-toml-and-cython-extension-module
class build_py(_build_py):
def run(self):
self.run_command("build_ext")
return super().run()

def initialize_options(self):
super().initialize_options()
if self.distribution.ext_modules == None:
self.distribution.ext_modules = []

self.distribution.ext_modules.extend(
m_ext_module
)
28 changes: 28 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
[build-system]
requires = ["setuptools>=61.0", "cython"]
build-backend = "setuptools.build_meta"

[project]
name="pywfa"
version='0.4.1'
description="Align sequences using WFA2-lib"
requires-python='>=3.7'
readme = "README.rst"
authors = [
{ name = "Kez Cleal", email = "clealk@cardiff.ac.uk" }
]
classifiers = [
"Programming Language :: Python :: 3",
"License :: OSI Approved :: MIT License",
"Operating System :: OS Independent",
]

[tool.setuptools]
py-modules = ["_custom_build"]
packages=["pywfa", "pywfa.tests", "pywfa.WFA2_lib"]

[tool.setuptools.cmdclass]
build_py = "_custom_build.build_py"

[project.urls]
Repository="https://github.com/kcleal/pywfa"
9 changes: 6 additions & 3 deletions pywfa/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
from __future__ import absolute_import
from .align import WavefrontAligner, clip_cigartuples, cigartuples_to_str, elide_mismatches_from_cigar

from pywfa.align import (
WavefrontAligner,
clip_cigartuples,
cigartuples_to_str,
elide_mismatches_from_cigar,
)
137 changes: 0 additions & 137 deletions setup.py

This file was deleted.

0 comments on commit a40979c

Please sign in to comment.