Skip to content

Commit

Permalink
More robust way of checking whether the compiler is clang
Browse files Browse the repository at this point in the history
  • Loading branch information
jobovy committed Jul 2, 2024
1 parent 8d7355c commit 999a98e
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,18 @@ def compiler_has_flag(compiler, flagname):
return True


# Test whether the compiler is clang, allowing for the fact that it's name might be gcc...
def compiler_is_clang(compiler):
# Test whether the compiler is clang by running the compiler with the --version flag and checking whether the output contains "clang"
try:
output = subprocess.check_output(
[compiler.compiler[0], "--version"], stderr=subprocess.STDOUT
)
except (OSError, subprocess.CalledProcessError):
return False
return b"clang" in output


# Now need to subclass BuildExt to access the compiler used and check flags
class BuildExt(build_ext):
def build_extensions(self):
Expand All @@ -246,7 +258,8 @@ def build_extensions(self):
for flag in ext.extra_compile_args:
if compiler_has_flag(self.compiler, flag):
extra_compile_args.append(flag)
elif "clang" in self.compiler.compiler[0] and flag == "-fopenmp":
elif compiler_is_clang(self.compiler) and flag == "-fopenmp":
print("Here")
# clang does not support -fopenmp, but does support -Xclang -fopenmp
extra_compile_args.append("-Xclang")
extra_compile_args.append("-fopenmp")
Expand Down

0 comments on commit 999a98e

Please sign in to comment.