diff --git a/litex/soc/integration/export.py b/litex/soc/integration/export.py index 6176f4b5b..f0646bbb7 100644 --- a/litex/soc/integration/export.py +++ b/litex/soc/integration/export.py @@ -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 [