Skip to content

Commit 1fadd0e

Browse files
committed
builder: use target-specific -march for RISC-V library compilation
The -march flag for compiling C libraries (compiler-rt, picolibc) was hardcoded to rv32imac for all riscv32 targets. This is incorrect for targets that lack the atomic extension, such as ESP32-C3 (rv32imc) and TKey (rv32iczmmul). Extract the -march value from the target's cflags when available, falling back to the previous defaults (rv32imac, rv64gc) for targets that don't override it. Fixes #5114 Signed-off-by: deadprogram <ron@hybridgroup.com>
1 parent eba6e7a commit 1fadd0e

1 file changed

Lines changed: 16 additions & 2 deletions

File tree

builder/library.go

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,9 +167,9 @@ func (l *Library) load(config *compileopts.Config, tmpdir string) (job *compileJ
167167
// double.
168168
args = append(args, "-mdouble=64")
169169
case "riscv32":
170-
args = append(args, "-march=rv32imac", "-fforce-enable-int128")
170+
args = append(args, "-march="+riscvMarch(config, "rv32imac"), "-fforce-enable-int128")
171171
case "riscv64":
172-
args = append(args, "-march=rv64gc")
172+
args = append(args, "-march="+riscvMarch(config, "rv64gc"))
173173
case "mips":
174174
args = append(args, "-fno-pic")
175175
}
@@ -298,3 +298,17 @@ func (l *Library) load(config *compileopts.Config, tmpdir string) (job *compileJ
298298
once.Do(unlock)
299299
}, nil
300300
}
301+
302+
// riscvMarch returns the -march value for RISC-V library compilation.
303+
// It extracts the value from the target's cflags if present, otherwise
304+
// falls back to the provided default. This ensures libraries are compiled
305+
// with the correct ISA extensions for each target (e.g. rv32imc for
306+
// ESP32-C3 which lacks the atomic extension).
307+
func riscvMarch(config *compileopts.Config, defaultMarch string) string {
308+
for _, flag := range config.Target.CFlags {
309+
if strings.HasPrefix(flag, "-march=") {
310+
return flag[len("-march="):]
311+
}
312+
}
313+
return defaultMarch
314+
}

0 commit comments

Comments
 (0)