Skip to content

Commit fa35c15

Browse files
setup: add compiler flags when appropriate
1 parent b594dfe commit fa35c15

1 file changed

Lines changed: 48 additions & 3 deletions

File tree

setup.py

Lines changed: 48 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+
naive_recompile,
23+
has_flag,
24+
)
1825
from setuptools import setup
1926

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

51+
52+
def get_arch():
53+
arch = platform.machine().lower()
54+
if arch in ("x86_64", "amd64", "i386", "i686"):
55+
return "x86"
56+
if arch in ("arm64", "aarch64", "armv7l", "armv6l"):
57+
return "arm"
58+
return arch
59+
60+
61+
class LibsemigroupsBuildExt(build_ext):
62+
def build_extensions(self):
63+
compiler = self.compiler
64+
65+
if has_flag(compiler, "-mavx"):
66+
print("Compiler supports '-mavx' flag, adding it to 'extra_compile_args'")
67+
for ext in self.extensions:
68+
ext.extra_compile_args += ["-mavx"]
69+
else:
70+
print("Compiler does not support '-mavx' flag, not adding it to 'extra_compile_args'")
71+
if get_arch() == "arm" and (
72+
any(x.startswith("gcc") for x in compiler.compiler)
73+
or any(x.startswith("g++") for x in compiler.compiler_cxx)
74+
):
75+
print(
76+
"Compiler is gcc, and architecture is arm, adding '-fpermissive' to "
77+
"'extra_compile_args'"
78+
)
79+
for ext in self.extensions:
80+
ext.extra_compile_args += ["-fpermissive"]
81+
82+
for ext in self.extensions:
83+
print(f"'extra_compile_args' for '{ext.name}' are:")
84+
print(ext.extra_compile_args)
85+
86+
super().build_extensions()
87+
88+
4489
ext_modules = [
4590
Pybind11Extension(
4691
"_libsemigroups_pybind11",
@@ -49,8 +94,8 @@
4994
library_dirs=libsemigroups_info["library_dirs"],
5095
language="c++",
5196
libraries=["semigroups"],
52-
extra_compile_args=["-fpermissive", "-flax-vector-conversions"],
97+
extra_compile_args=["-flax-vector-conversions"],
5398
)
5499
]
55100

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

0 commit comments

Comments
 (0)