fix cross compilation
This commit is contained in:
@@ -339,11 +339,21 @@ func useTarget(targetName string) (export Export, err error) {
|
|||||||
target = llvm.GetTargetTriple(config.GOOS, config.GOARCH)
|
target = llvm.GetTargetTriple(config.GOOS, config.GOARCH)
|
||||||
}
|
}
|
||||||
|
|
||||||
ccflags = append(ccflags, "-Wno-override-module", "--target="+config.LLVMTarget)
|
cflags := []string{"-Wno-override-module"}
|
||||||
|
if config.LLVMTarget != "" {
|
||||||
|
cflags = append(cflags, "--target="+config.LLVMTarget)
|
||||||
|
ccflags = append(ccflags, "--target="+config.LLVMTarget)
|
||||||
|
}
|
||||||
|
cflags = append(cflags, config.CFlags...)
|
||||||
|
|
||||||
// Inspired by tinygo
|
// Inspired by tinygo
|
||||||
cpu := config.CPU
|
cpu := config.CPU
|
||||||
if cpu != "" {
|
if cpu != "" {
|
||||||
|
if config.Linker == "ld.lld" {
|
||||||
|
ldflags = append(ldflags, "-mllvm", "-mcpu="+cpu)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Always add CPU to CCFLAGS for proper compilation
|
||||||
if strings.HasPrefix(target, "i386") || strings.HasPrefix(target, "x86_64") {
|
if strings.HasPrefix(target, "i386") || strings.HasPrefix(target, "x86_64") {
|
||||||
ccflags = append(ccflags, "-march="+cpu)
|
ccflags = append(ccflags, "-march="+cpu)
|
||||||
} else if strings.HasPrefix(target, "avr") {
|
} else if strings.HasPrefix(target, "avr") {
|
||||||
@@ -351,10 +361,6 @@ func useTarget(targetName string) (export Export, err error) {
|
|||||||
} else {
|
} else {
|
||||||
ccflags = append(ccflags, "-mcpu="+cpu)
|
ccflags = append(ccflags, "-mcpu="+cpu)
|
||||||
}
|
}
|
||||||
// Only add -mllvm flags for non-WebAssembly linkers
|
|
||||||
if config.Linker == "ld.lld" {
|
|
||||||
ldflags = append(ldflags, "-mllvm", "-mcpu="+cpu)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Handle Features
|
// Handle Features
|
||||||
@@ -375,7 +381,7 @@ func useTarget(targetName string) (export Export, err error) {
|
|||||||
ldflags = append(ldflags, "-L", env.LLGoROOT()) // search targets/*.ld
|
ldflags = append(ldflags, "-L", env.LLGoROOT()) // search targets/*.ld
|
||||||
|
|
||||||
// Combine with config flags
|
// Combine with config flags
|
||||||
export.CFLAGS = config.CFlags
|
export.CFLAGS = cflags
|
||||||
export.CCFLAGS = ccflags
|
export.CCFLAGS = ccflags
|
||||||
export.LDFLAGS = append(ldflags, config.LDFlags...)
|
export.LDFLAGS = append(ldflags, config.LDFlags...)
|
||||||
|
|
||||||
@@ -390,44 +396,3 @@ func Use(goos, goarch string, wasiThreads bool, targetName string) (export Expor
|
|||||||
}
|
}
|
||||||
return use(goos, goarch, wasiThreads)
|
return use(goos, goarch, wasiThreads)
|
||||||
}
|
}
|
||||||
|
|
||||||
// filterCompatibleLDFlags filters out linker flags that are incompatible with clang/lld
|
|
||||||
func filterCompatibleLDFlags(ldflags []string) []string {
|
|
||||||
if len(ldflags) == 0 {
|
|
||||||
return ldflags
|
|
||||||
}
|
|
||||||
|
|
||||||
var filtered []string
|
|
||||||
|
|
||||||
incompatiblePrefixes := []string{
|
|
||||||
"--defsym=", // Use -Wl,--defsym= instead
|
|
||||||
"-T", // Linker script, needs special handling
|
|
||||||
}
|
|
||||||
|
|
||||||
i := 0
|
|
||||||
for i < len(ldflags) {
|
|
||||||
flag := ldflags[i]
|
|
||||||
|
|
||||||
// Check incompatible prefixes
|
|
||||||
skip := false
|
|
||||||
for _, prefix := range incompatiblePrefixes {
|
|
||||||
if strings.HasPrefix(flag, prefix) {
|
|
||||||
skip = true
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if skip {
|
|
||||||
// Skip -T and its argument if separate
|
|
||||||
if flag == "-T" && i+1 < len(ldflags) {
|
|
||||||
i += 2 // Skip both -T and the script path
|
|
||||||
} else {
|
|
||||||
i++
|
|
||||||
}
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
filtered = append(filtered, flag)
|
|
||||||
i++
|
|
||||||
}
|
|
||||||
|
|
||||||
return filtered
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -230,9 +230,21 @@ func TestUseTarget(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if CPU is in CCFLAGS
|
// Check if CPU is in LDFLAGS (for ld.lld linker) or CCFLAGS (for other cases)
|
||||||
if tc.expectCPU != "" {
|
if tc.expectCPU != "" {
|
||||||
found := false
|
found := false
|
||||||
|
// First check LDFLAGS for -mllvm -mcpu= pattern
|
||||||
|
for i, flag := range export.LDFLAGS {
|
||||||
|
if flag == "-mllvm" && i+1 < len(export.LDFLAGS) {
|
||||||
|
nextFlag := export.LDFLAGS[i+1]
|
||||||
|
if nextFlag == "-mcpu="+tc.expectCPU {
|
||||||
|
found = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// If not found in LDFLAGS, check CCFLAGS for direct CPU flags
|
||||||
|
if !found {
|
||||||
expectedFlags := []string{"-mmcu=" + tc.expectCPU, "-mcpu=" + tc.expectCPU}
|
expectedFlags := []string{"-mmcu=" + tc.expectCPU, "-mcpu=" + tc.expectCPU}
|
||||||
for _, flag := range export.CCFLAGS {
|
for _, flag := range export.CCFLAGS {
|
||||||
for _, expectedFlag := range expectedFlags {
|
for _, expectedFlag := range expectedFlags {
|
||||||
@@ -242,8 +254,9 @@ func TestUseTarget(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
if !found {
|
if !found {
|
||||||
t.Errorf("Expected CPU %s in CCFLAGS, got %v", tc.expectCPU, export.CCFLAGS)
|
t.Errorf("Expected CPU %s in LDFLAGS or CCFLAGS, got LDFLAGS=%v, CCFLAGS=%v", tc.expectCPU, export.LDFLAGS, export.CCFLAGS)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -277,59 +290,3 @@ func TestUseWithTarget(t *testing.T) {
|
|||||||
t.Error("Expected LDFLAGS to be set for native build")
|
t.Error("Expected LDFLAGS to be set for native build")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestFilterCompatibleLDFlags(t *testing.T) {
|
|
||||||
testCases := []struct {
|
|
||||||
name string
|
|
||||||
input []string
|
|
||||||
expected []string
|
|
||||||
}{
|
|
||||||
{
|
|
||||||
name: "Empty flags",
|
|
||||||
input: []string{},
|
|
||||||
expected: []string{},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "Compatible flags only",
|
|
||||||
input: []string{"-lm", "-lpthread"},
|
|
||||||
expected: []string{"-lm", "-lpthread"},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "Incompatible flags filtered",
|
|
||||||
input: []string{"--gc-sections", "-lm", "--emit-relocs", "-lpthread"},
|
|
||||||
expected: []string{"--gc-sections", "-lm", "--emit-relocs", "-lpthread"},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "Defsym flags filtered",
|
|
||||||
input: []string{"--defsym=_stack_size=512", "-lm", "--defsym=_bootloader_size=512"},
|
|
||||||
expected: []string{"-lm"},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "Linker script flags filtered",
|
|
||||||
input: []string{"-T", "script.ld", "-lm"},
|
|
||||||
expected: []string{"-lm"},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "Mixed compatible and incompatible",
|
|
||||||
input: []string{"-lm", "--gc-sections", "--defsym=test=1", "-lpthread", "--no-demangle"},
|
|
||||||
expected: []string{"-lm", "--gc-sections", "-lpthread", "--no-demangle"},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, tc := range testCases {
|
|
||||||
t.Run(tc.name, func(t *testing.T) {
|
|
||||||
result := filterCompatibleLDFlags(tc.input)
|
|
||||||
|
|
||||||
if len(result) != len(tc.expected) {
|
|
||||||
t.Errorf("Expected %d flags, got %d: %v", len(tc.expected), len(result), result)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
for i, expected := range tc.expected {
|
|
||||||
if result[i] != expected {
|
|
||||||
t.Errorf("Expected flag[%d] = %s, got %s", i, expected, result[i])
|
|
||||||
}
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
Reference in New Issue
Block a user