-
Notifications
You must be signed in to change notification settings - Fork 8
/
detect_flags.py
84 lines (73 loc) · 2.76 KB
/
detect_flags.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import platform
import os
import subprocess
import re
# https://stackoverflow.com/a/377028
def which(program):
def is_exe(fpath):
return os.path.isfile(fpath) and os.access(fpath, os.X_OK)
fpath, _ = os.path.split(program)
if fpath:
if is_exe(program):
return program
else:
for path in os.environ.get("PATH", "").split(os.pathsep):
exe_file = os.path.join(path, program)
if is_exe(exe_file):
return exe_file
return None
def get_compile_info_from_compiler(compiler: str, arch_string: str):
info = ""
if "clang" in compiler:
ps = subprocess.Popen(("clang", "-E", "-", arch_string, "-###"), stdout=subprocess.PIPE, stderr=subprocess.PIPE)
info = ps.stderr.read().decode("utf-8")
return(re.findall(r'"-target-feature" "\+([^\"]+)"', info))
elif "g++" in compiler:
ps = subprocess.Popen(("g++", "-E", "-", arch_string, "-###"), stdout=subprocess.PIPE, stderr=subprocess.PIPE)
info = ps.stderr.read().decode("utf-8")
return(re.findall(r' -m((?!no-)[^\s]+)', info))
def get_compile_info_from_lscpu():
if which("lscpu"):
my_env = os.environ.copy()
my_env["LANG"] = "en"
ps = subprocess.Popen(("lscpu"), env=my_env, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
info2 = ps.stderr.read()
info = ps.stdout.read()
lines = info.decode("utf-8").split("\n")
regex = re.compile(r"\s*Flags:\s*(?P<flags>(\w+\s)*(\w+))")
flags_set = set()
for line in lines:
if line.lstrip().lower().startswith("flags"):
for _, match in enumerate(regex.finditer(line)):
for flag in match.group("flags").split(" "):
flags_set.add(flag)
return flags_set
else:
print("No LSCPU")
return set()
def get_platform():
if "Darwin" in platform.system():
return "Apple"
else:
return platform.system()
arch = platform.uname()[4]
# if "x86" in arch:
# print(f"x86 CPU on {get_platform()}")
# arch_string = "-march=native"
# elif "aarch" in arch or "Apple" in get_platform():
# print("ARM CPU or Apple System")
flags = set()
if which("lscpu"):
flags = get_compile_info_from_lscpu()
else:
arch_string = ""
if "Apple" in get_platform() or "aarch" in arch:
arch_string = "-mcpu=native"
else:
arch_string = "-march=native"
print("Debug:", arch, get_platform())
gcc_flags = get_compile_info_from_compiler("g++", "-march=native") if which("g++") else set()
clang_flags = get_compile_info_from_compiler("clang", arch_string) if which("clang") else set()
flags.update(gcc_flags)
flags.update(clang_flags)
print(' '.join(sorted(flags)))