soc/integration/export: Rewrite and fix zicsr RISC-V workaround.

- Improve regex to be more robust (Was not working with binutils from Sifive).
- Improve readability.
- Only do it for RISC-V.
- Add comments.
This commit is contained in:
Florent Kermarrec 2022-06-02 18:39:38 +02:00
parent 9e38dc70a4
commit bafae4d449
1 changed files with 19 additions and 6 deletions

View File

@ -75,7 +75,7 @@ def get_cpu_mak(cpu, compile_software):
for i in range(len(triple)):
t = triple[i]
# Use native toolchain if host and target platforms are the same.
if t == 'riscv64-unknown-elf' and p == 'linux-riscv64':
if t == "riscv64-unknown-elf" and p == "linux-riscv64":
r = '--native--'
break
if which(t+"-gcc"):
@ -89,12 +89,25 @@ def get_cpu_mak(cpu, compile_software):
msg += "- " + triple[i] + "\n"
raise OSError(msg)
return r
selected_triple = select_triple(triple)
if not clang:
binutils_version = re.match("GNU ar \(GNU Binutils\) (.+)", os.popen(selected_triple + "-ar -V").read()).group(1).split(".")
if int(binutils_version[1]) >= 2 and int(binutils_version[2]) >= 37 and (not re.search("zicsr", flags)):
flags = re.compile("-march=([^ ]+)").sub("-march=\\1_zicsr", flags)
# RISC-V's march zicsr workaround (for binutils >= 2.37).
def get_binutils_version():
version = 0
for i, l in enumerate(os.popen(selected_triple + "-ar -V")):
# Version is last float reported in first line.
if i == 0:
version = float(re.findall("\d+\.\d+", l)[-1])
return version
def apply_riscv_zicsr_march_workaround(flags):
# Append _zicsr to march when binutils >= 2.37 and zicsr is not present.
if (get_binutils_version() >= 2.37) and ("zicsr" not in flags):
flags = re.compile("-march=([^ ]+)").sub("-march=\\1_zicsr", flags)
return flags
if (not clang) and ("riscv" in selected_triple):
flags = apply_riscv_zicsr_march_workaround(flags)
# Return informations.
return [