Skip to content

Commit

Permalink
Merge pull request #15 from nschloe/new-pybind11
Browse files Browse the repository at this point in the history
New pybind11
  • Loading branch information
nschloe authored May 27, 2020
2 parents 6854674 + 48b5182 commit 2364f4d
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 15 deletions.
88 changes: 77 additions & 11 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,33 +1,97 @@
import sys

import setuptools
from setuptools import Extension, find_packages, setup
from setuptools.command.build_ext import build_ext


# https://github.com/pybind/python_example/
class get_pybind_include:
def __init__(self, user=False):
self.user = user

def __str__(self):
import pybind11

return pybind11.get_include(self.user)
return pybind11.get_include()


ext_modules = [
Extension(
"_accupy",
["src/pybind11.cpp"],
language="c++",
include_dirs=[
"/usr/include/eigen3/",
get_pybind_include(),
get_pybind_include(user=True),
],
include_dirs=["/usr/include/eigen3/", get_pybind_include()],
)
]


# cf http://bugs.python.org/issue26689
def has_flag(compiler, flagname):
"""Return a boolean indicating whether a flag name is supported on
the specified compiler.
"""
import tempfile
import os

with tempfile.NamedTemporaryFile("w", suffix=".cpp", delete=False) as f:
f.write("int main (int argc, char **argv) { return 0; }")
fname = f.name
try:
compiler.compile([fname], extra_postargs=[flagname])
except setuptools.distutils.errors.CompileError:
return False
finally:
try:
os.remove(fname)
except OSError:
pass
return True


def cpp_flag(compiler):
flags = ["-std=c++17", "-std=c++14", "-std=c++11"]

for flag in flags:
if has_flag(compiler, flag):
return flag

raise RuntimeError("Unsupported compiler -- at least C++11 support is needed!")


class BuildExt(build_ext):
c_opts = {
"msvc": ["/EHsc"],
"unix": [],
}
l_opts = {
"msvc": [],
"unix": [],
}

if sys.platform == "darwin":
darwin_opts = ["-stdlib=libc++", "-mmacosx-version-min=10.7"]
c_opts["unix"] += darwin_opts
l_opts["unix"] += darwin_opts

def build_extensions(self):
ct = self.compiler.compiler_type
opts = self.c_opts.get(ct, [])
link_opts = self.l_opts.get(ct, [])
if ct == "unix":
opts.append(cpp_flag(self.compiler))
if has_flag(self.compiler, "-fvisibility=hidden"):
opts.append("-fvisibility=hidden")

for ext in self.extensions:
ext.define_macros = [
("VERSION_INFO", '"{}"'.format(self.distribution.get_version()))
]
ext.extra_compile_args = opts
ext.extra_link_args = link_opts
build_ext.build_extensions(self)


setup(
name="accupy",
version="0.3.0",
version="0.3.1",
packages=find_packages(),
ext_modules=ext_modules,
url="https://github.com/nschloe/accupy",
Expand All @@ -38,9 +102,11 @@ def __str__(self):
"importlib_metadata",
"mpmath",
"numpy",
"pybind11 >= 2.2",
"pybind11 >= 2.5.0",
"pyfma",
],
setup_requires=["pybind11 >= 2.5.0"],
cmdclass={"build_ext": BuildExt},
python_requires=">=3.6",
description="Accurate sums and dot products for Python",
long_description=open("README.md").read(),
Expand Down
2 changes: 0 additions & 2 deletions test/test_dot.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@ def test_speed_comparison1(n_range=None):
xlabel="n",
logx=True,
logy=True,
automatic_order=False,
)
plt.gca().set_aspect(0.2)
lgd = plt.legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.0)
Expand Down Expand Up @@ -126,7 +125,6 @@ def test_speed_comparison2(n_range=None):
xlabel="n",
logx=True,
logy=True,
automatic_order=False,
)
plt.gca().set_aspect(0.2)
lgd = plt.legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.0)
Expand Down
2 changes: 0 additions & 2 deletions test/test_sums.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,6 @@ def test_speed_comparison1(n_range=None):
xlabel="n",
logx=True,
logy=True,
automatic_order=False,
)
plt.gca().set_aspect(0.5)
lgd = plt.legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.0)
Expand Down Expand Up @@ -155,7 +154,6 @@ def test_speed_comparison2(n_range=None):
xlabel="n",
logx=True,
logy=True,
automatic_order=False,
)
plt.gca().set_aspect(0.5)
lgd = plt.legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.0)
Expand Down

0 comments on commit 2364f4d

Please sign in to comment.