Skip to content

Commit d06d53c

Browse files
setup: add compiler flags when appropriate
1 parent d742e20 commit d06d53c

File tree

1 file changed

+53
-3
lines changed

1 file changed

+53
-3
lines changed

setup.py

Lines changed: 53 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,49 @@
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+
# pylint: disable=too-few-public-methods
64+
"""Class conditionally add compile flags"""
65+
66+
def build_extensions(self):
67+
"""Adds compile flags before calling build_extensions in build_ext"""
68+
compiler = self.compiler
69+
70+
if has_flag(compiler, "-mavx"):
71+
print("Compiler supports '-mavx' flag, adding it to 'extra_compile_args'")
72+
for ext in self.extensions:
73+
ext.extra_compile_args += ["-mavx"]
74+
else:
75+
print("Compiler does not support '-mavx' flag, not adding it to 'extra_compile_args'")
76+
if get_arch() == "arm" and (
77+
any(x.startswith("gcc") for x in compiler.compiler)
78+
or any(x.startswith("g++") for x in compiler.compiler_cxx)
79+
):
80+
print(
81+
"Compiler is gcc, and architecture is arm, adding '-fpermissive' to "
82+
"'extra_compile_args'"
83+
)
84+
for ext in self.extensions:
85+
ext.extra_compile_args += ["-fpermissive"]
86+
87+
for ext in self.extensions:
88+
print(f"'extra_compile_args' for '{ext.name}' are:")
89+
print(ext.extra_compile_args)
90+
91+
super().build_extensions()
92+
93+
4494
ext_modules = [
4595
Pybind11Extension(
4696
"_libsemigroups_pybind11",
@@ -49,8 +99,8 @@
4999
library_dirs=libsemigroups_info["library_dirs"],
50100
language="c++",
51101
libraries=["semigroups"],
52-
extra_compile_args=["-fpermissive", "-flax-vector-conversions"],
102+
extra_compile_args=["-flax-vector-conversions"],
53103
)
54104
]
55105

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

0 commit comments

Comments
 (0)