Skip to content

Commit 2bb9abe

Browse files
setup: add compiler flags when appropriate
1 parent 22129b8 commit 2bb9abe

File tree

1 file changed

+51
-3
lines changed

1 file changed

+51
-3
lines changed

setup.py

Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,17 @@
1111

1212
import glob
1313
import os
14+
import platform
1415
import sys
1516

1617
import pkgconfig
17-
from pybind11.setup_helpers import ParallelCompile, Pybind11Extension, build_ext, naive_recompile
18+
from pybind11.setup_helpers import (
19+
ParallelCompile,
20+
Pybind11Extension,
21+
build_ext,
22+
has_flag,
23+
naive_recompile,
24+
)
1825
from setuptools import setup
1926

2027
ParallelCompile("NPY_NUM_BUILD_JOBS", needs_recompile=naive_recompile).install()
@@ -41,6 +48,47 @@
4148
print("Library directories args are:")
4249
print(libsemigroups_info["library_dirs"])
4350

51+
52+
def get_arch():
53+
"""Simple function to return the architecture, namely, x86 or not"""
54+
arch = platform.machine().lower()
55+
if arch in {"x86_64", "amd64", "i386", "i686"}:
56+
return "x86"
57+
if arch in {"arm64", "aarch64", "armv7l", "armv6l"}:
58+
return "arm"
59+
return arch
60+
61+
62+
class LibsemigroupsBuildExt(build_ext):
63+
"""Class conditionally add compile flags"""
64+
65+
def build_extensions(self):
66+
compiler = self.compiler
67+
68+
if has_flag(compiler, "-mavx"):
69+
print("Compiler supports '-mavx' flag, adding it to 'extra_compile_args'")
70+
for ext in self.extensions:
71+
ext.extra_compile_args += ["-mavx"]
72+
else:
73+
print("Compiler does not support '-mavx' flag, not adding it to 'extra_compile_args'")
74+
if get_arch() == "arm" and (
75+
any(x.startswith("gcc") for x in compiler.compiler)
76+
or any(x.startswith("g++") for x in compiler.compiler_cxx)
77+
):
78+
print(
79+
"Compiler is gcc, and architecture is arm, adding '-fpermissive' to "
80+
"'extra_compile_args'"
81+
)
82+
for ext in self.extensions:
83+
ext.extra_compile_args += ["-fpermissive"]
84+
85+
for ext in self.extensions:
86+
print(f"'extra_compile_args' for '{ext.name}' are:")
87+
print(ext.extra_compile_args)
88+
89+
super().build_extensions()
90+
91+
4492
ext_modules = [
4593
Pybind11Extension(
4694
"_libsemigroups_pybind11",
@@ -49,8 +97,8 @@
4997
library_dirs=libsemigroups_info["library_dirs"],
5098
language="c++",
5199
libraries=["semigroups"],
52-
extra_compile_args=["-fpermissive", "-flax-vector-conversions"],
100+
extra_compile_args=["-flax-vector-conversions"],
53101
)
54102
]
55103

56-
setup(ext_modules=ext_modules, cmdclass={"build_ext": build_ext})
104+
setup(ext_modules=ext_modules, cmdclass={"build_ext": LibsemigroupsBuildExt})

0 commit comments

Comments
 (0)