| 1 |
from setuptools import setup, Extension, find_packages |
| 2 |
from setuptools.command.build_ext import build_ext |
| 3 |
import sys |
| 4 |
import setuptools |
| 5 |
import ffc |
| 6 |
|
| 7 |
__version__ = '0.0.1' |
| 8 |
|
| 9 |
|
| 10 |
class get_pybind_include(object): |
| 11 |
"""Helper class to determine the pybind11 include path |
| 12 |
|
| 13 |
The purpose of this class is to postpone importing pybind11 |
| 14 |
until it is actually installed, so that the ``get_include()`` |
| 15 |
method can be invoked. """ |
| 16 |
|
| 17 |
def __init__(self, user=False): |
| 18 |
self.user = user |
| 19 |
|
| 20 |
def __str__(self): |
| 21 |
import pybind11 |
| 22 |
return pybind11.get_include(self.user) |
| 23 |
|
| 24 |
|
| 25 |
ext_modules = [ |
| 26 |
Extension('ffc_factory', |
| 27 |
['interface.cpp', |
| 28 |
'dofmap.cpp', |
| 29 |
'finite_element.cpp', |
| 30 |
'finite_element_bindings.cpp'], |
| 31 |
include_dirs=[ |
| 32 |
# Path to pybind11 headers |
| 33 |
ffc.get_include_path(), |
| 34 |
get_pybind_include(), |
| 35 |
get_pybind_include(user=True) |
| 36 |
], |
| 37 |
language='c++'), |
| 38 |
] |
| 39 |
|
| 40 |
|
| 41 |
# As of Python 3.6, CCompiler has a `has_flag` method. |
| 42 |
# cf http://bugs.python.org/issue26689 |
| 43 |
def has_flag(compiler, flagname): |
| 44 |
"""Return a boolean indicating whether a flag name is supported on |
| 45 |
the specified compiler. |
| 46 |
""" |
| 47 |
import tempfile |
| 48 |
with tempfile.NamedTemporaryFile('w', suffix='.cpp') as f: |
| 49 |
f.write('int main (int argc, char **argv) { return 0; }') |
| 50 |
try: |
| 51 |
compiler.compile([f.name], extra_postargs=[flagname]) |
| 52 |
except setuptools.distutils.errors.CompileError: |
| 53 |
return False |
| 54 |
return True |
| 55 |
|
| 56 |
|
| 57 |
def cpp_flag(compiler): |
| 58 |
"""Return the -std=c++[11/14] compiler flag. |
| 59 |
|
| 60 |
The c++14 is prefered over c++11 (when it is available). |
| 61 |
""" |
| 62 |
if has_flag(compiler, '-std=c++14'): |
| 63 |
return '-std=c++14' |
| 64 |
elif has_flag(compiler, '-std=c++11'): |
| 65 |
return '-std=c++11' |
| 66 |
else: |
| 67 |
raise RuntimeError('Unsupported compiler -- at least C++11 support ' |
| 68 |
'is needed!') |
| 69 |
|
| 70 |
|
| 71 |
class BuildExt(build_ext): |
| 72 |
"""A custom build extension for adding compiler-specific options.""" |
| 73 |
c_opts = { |
| 74 |
'msvc': ['/EHsc'], |
| 75 |
'unix': [], |
| 76 |
} |
| 77 |
|
| 78 |
if sys.platform == 'darwin': |
| 79 |
c_opts['unix'] += ['-stdlib=libc++', '-mmacosx-version-min=10.7'] |
| 80 |
|
| 81 |
def build_extensions(self): |
| 82 |
ct = self.compiler.compiler_type |
| 83 |
opts = self.c_opts.get(ct, []) |
| 84 |
if ct == 'unix': |
| 85 |
opts.append('-DVERSION_INFO="%s"' % self.distribution.get_version()) |
| 86 |
opts.append(cpp_flag(self.compiler)) |
| 87 |
if has_flag(self.compiler, '-fvisibility=default'): |
| 88 |
opts.append('-fvisibility=default') |
| 89 |
elif ct == 'msvc': |
| 90 |
opts.append('/DVERSION_INFO=\\"%s\\"' % self.distribution.get_version()) |
| 91 |
for ext in self.extensions: |
| 92 |
ext.extra_compile_args = opts |
| 93 |
build_ext.build_extensions(self) |
| 94 |
|
| 95 |
|
| 96 |
setup(name='fenics-ffc-factory', |
| 97 |
version=__version__, |
| 98 |
author='FEniCS Project', |
| 99 |
author_email='[email protected]', |
| 100 |
url='https://fenicsproject.org', |
| 101 |
description='Python wrappers for UFC using pybind11', |
| 102 |
long_description='', |
| 103 |
packages=find_packages(), |
| 104 |
ext_modules=ext_modules, |
| 105 |
setup_requires=['pybind11>=1.7', |
| 106 |
'fenics-ffc'], |
| 107 |
cmdclass={'build_ext': BuildExt}, |
| 108 |
zip_safe=False,) |