feat(crosscompile): extend Export struct and add target-based configuration

- Add LLVMTarget, CPU, Features, BuildTags fields to Export struct
- Implement UseTarget() function for target name-based configuration loading
- Add UseWithTarget() function combining target and goos/goarch fallback
- Include comprehensive unit tests for target integration
- Support 206+ embedded platform configurations with inheritance

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Li Jie
2025-07-26 12:05:19 +10:00
parent 0c11c93b3a
commit 5424b53b62
2 changed files with 306 additions and 4 deletions

View File

@@ -6,6 +6,7 @@ package crosscompile
import (
"os"
"runtime"
"slices"
"testing"
)
@@ -154,3 +155,181 @@ func TestUseCrossCompileSDK(t *testing.T) {
})
}
}
func TestUseTarget(t *testing.T) {
// Test cases for target-based configuration
testCases := []struct {
name string
targetName string
expectError bool
expectLLVM string
expectCPU string
}{
{
name: "WASI Target",
targetName: "wasi",
expectError: false,
expectLLVM: "",
expectCPU: "generic",
},
{
name: "RP2040 Target",
targetName: "rp2040",
expectError: false,
expectLLVM: "thumbv6m-unknown-unknown-eabi",
expectCPU: "cortex-m0plus",
},
{
name: "Cortex-M Target",
targetName: "cortex-m",
expectError: false,
expectLLVM: "",
expectCPU: "",
},
{
name: "Arduino Target (with filtered flags)",
targetName: "arduino",
expectError: false,
expectLLVM: "avr",
expectCPU: "atmega328p",
},
{
name: "Nonexistent Target",
targetName: "nonexistent-target",
expectError: true,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
export, err := useTarget(tc.targetName)
if tc.expectError {
if err == nil {
t.Errorf("Expected error for target %s, but got none", tc.targetName)
}
return
}
if err != nil {
t.Fatalf("Unexpected error for target %s: %v", tc.targetName, err)
}
// Check if LLVM target is in CCFLAGS
if tc.expectLLVM != "" {
found := false
expectedFlag := "--target=" + tc.expectLLVM
for _, flag := range export.CCFLAGS {
if flag == expectedFlag {
found = true
break
}
}
if !found {
t.Errorf("Expected LLVM target %s in CCFLAGS, got %v", expectedFlag, export.CCFLAGS)
}
}
// Check if CPU is in CCFLAGS
if tc.expectCPU != "" {
found := false
expectedFlags := []string{"-mmcu=" + tc.expectCPU, "-mcpu=" + tc.expectCPU}
for _, flag := range export.CCFLAGS {
for _, expectedFlag := range expectedFlags {
if flag == expectedFlag {
found = true
break
}
}
}
if !found {
t.Errorf("Expected CPU %s in CCFLAGS, got %v", tc.expectCPU, export.CCFLAGS)
}
}
t.Logf("Target %s: BuildTags=%v, CFlags=%v, CCFlags=%v, LDFlags=%v",
tc.targetName, export.BuildTags, export.CFLAGS, export.CCFLAGS, export.LDFLAGS)
})
}
}
func TestUseWithTarget(t *testing.T) {
// Test target-based configuration takes precedence
export, err := UseWithTarget("linux", "amd64", false, "wasi")
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
// Check if LLVM target is in CCFLAGS
found := slices.Contains(export.CCFLAGS, "-mcpu=generic")
if !found {
t.Errorf("Expected CPU generic in CCFLAGS, got %v", export.CCFLAGS)
}
// Test fallback to goos/goarch when no target specified
export, err = UseWithTarget(runtime.GOOS, runtime.GOARCH, false, "")
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
// Should use native configuration (only check for macOS since that's where tests run)
if runtime.GOOS == "darwin" && len(export.LDFLAGS) == 0 {
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])
}
}
})
}
}