feat: support libc version

This commit is contained in:
Haolan
2025-09-04 19:08:48 +08:00
parent b1d95bb73c
commit 95bd495d7b
10 changed files with 503 additions and 135 deletions

View File

@@ -9,27 +9,20 @@ func TestGetPicolibcConfig(t *testing.T) {
baseDir := "/test/base"
target := "test-target"
config := GetPicolibcConfig(baseDir, target)
if config.Name != "picolibc" {
t.Errorf("Expected Name 'picolibc', got '%s'", config.Name)
}
if config.ArchiveSrcDir != "picolibc-main" {
t.Errorf("Expected ArchiveSrcDir 'picolibc-main', got '%s'", config.ArchiveSrcDir)
}
config := GetPicolibcCompileConfig(baseDir, target)
// Test LibcCFlags
if len(config.LibcCFlags) != 2 {
t.Errorf("Expected 2 LibcCFlags, got %d", len(config.LibcCFlags))
if len(config.ExportCFlags) != 2 {
t.Errorf("Expected 2 LibcCFlags, got %d", len(config.ExportCFlags))
} else {
expected := "-I" + baseDir
if config.LibcCFlags[0] != expected {
t.Errorf("Expected LibcCFlags[0] to be '%s', got '%s'", expected, config.LibcCFlags[0])
if config.ExportCFlags[0] != expected {
t.Errorf("Expected LibcCFlags[0] to be '%s', got '%s'", expected, config.ExportCFlags[0])
}
expected = "-isystem" + filepath.Join(baseDir, "newlib", "libc", "include")
if config.LibcCFlags[1] != expected {
t.Errorf("Expected LibcCFlags[1] to be '%s', got '%s'", expected, config.LibcCFlags[1])
if config.ExportCFlags[1] != expected {
t.Errorf("Expected LibcCFlags[1] to be '%s', got '%s'", expected, config.ExportCFlags[1])
}
}
@@ -113,22 +106,22 @@ func TestGetPicolibcConfig(t *testing.T) {
func TestGetPicolibcConfig_EdgeCases(t *testing.T) {
t.Run("EmptyBaseDir", func(t *testing.T) {
config := GetPicolibcConfig("", "test-target")
config := GetPicolibcCompileConfig("", "test-target")
// Check that paths are constructed correctly even with empty baseDir
expected := "-I"
if config.LibcCFlags[0] != expected {
t.Errorf("Expected LibcCFlags[0] to be '%s', got '%s'", expected, config.LibcCFlags[0])
if config.ExportCFlags[0] != expected {
t.Errorf("Expected LibcCFlags[0] to be '%s', got '%s'", expected, config.ExportCFlags[0])
}
expected = "-isystem" + filepath.Join("", "newlib", "libc", "include")
if config.LibcCFlags[1] != expected {
t.Errorf("Expected LibcCFlags[1] to be '%s', got '%s'", expected, config.LibcCFlags[1])
if config.ExportCFlags[1] != expected {
t.Errorf("Expected LibcCFlags[1] to be '%s', got '%s'", expected, config.ExportCFlags[1])
}
})
t.Run("EmptyTarget", func(t *testing.T) {
config := GetPicolibcConfig("/test/base", "")
config := GetPicolibcCompileConfig("/test/base", "")
// Check output file name formatting
expectedOutput := "libc-.a"
@@ -144,17 +137,6 @@ func TestGetNewlibESP32ConfigRISCV(t *testing.T) {
config := getNewlibESP32ConfigRISCV(baseDir, target)
// Test basic configuration
if config.Url != _newlibUrl {
t.Errorf("Expected URL '%s', got '%s'", _newlibUrl, config.Url)
}
if config.Name != "newlib-esp32" {
t.Errorf("Expected Name 'newlib-esp32', got '%s'", config.Name)
}
if config.ArchiveSrcDir != _archiveInternalSrcDir {
t.Errorf("Expected ArchiveSrcDir '%s', got '%s'", _archiveInternalSrcDir, config.ArchiveSrcDir)
}
// Test LibcCFlags
libcDir := filepath.Join(baseDir, "newlib", "libc")
expectedCFlags := []string{
@@ -162,12 +144,12 @@ func TestGetNewlibESP32ConfigRISCV(t *testing.T) {
"-I" + filepath.Join(baseDir, "newlib"),
"-I" + libcDir,
}
if len(config.LibcCFlags) != len(expectedCFlags) {
t.Errorf("Expected %d LibcCFlags, got %d", len(expectedCFlags), len(config.LibcCFlags))
if len(config.ExportCFlags) != len(expectedCFlags) {
t.Errorf("Expected %d LibcCFlags, got %d", len(expectedCFlags), len(config.ExportCFlags))
} else {
for i, expected := range expectedCFlags {
if config.LibcCFlags[i] != expected {
t.Errorf("LibcCFlags[%d] mismatch. Expected '%s', got '%s'", i, expected, config.LibcCFlags[i])
if config.ExportCFlags[i] != expected {
t.Errorf("LibcCFlags[%d] mismatch. Expected '%s', got '%s'", i, expected, config.ExportCFlags[i])
}
}
}
@@ -288,17 +270,6 @@ func TestGetNewlibESP32ConfigXtensa(t *testing.T) {
config := getNewlibESP32ConfigXtensa(baseDir, target)
// Test basic configuration
if config.Url != _newlibUrl {
t.Errorf("Expected URL '%s', got '%s'", _newlibUrl, config.Url)
}
if config.Name != "newlib-esp32" {
t.Errorf("Expected Name 'newlib-esp32', got '%s'", config.Name)
}
if config.ArchiveSrcDir != _archiveInternalSrcDir {
t.Errorf("Expected ArchiveSrcDir '%s', got '%s'", _archiveInternalSrcDir, config.ArchiveSrcDir)
}
// Test LibcCFlags
libcDir := filepath.Join(baseDir, "newlib", "libc")
expectedCFlags := []string{
@@ -306,12 +277,12 @@ func TestGetNewlibESP32ConfigXtensa(t *testing.T) {
"-I" + filepath.Join(baseDir, "newlib"),
"-I" + libcDir,
}
if len(config.LibcCFlags) != len(expectedCFlags) {
t.Errorf("Expected %d LibcCFlags, got %d", len(expectedCFlags), len(config.LibcCFlags))
if len(config.ExportCFlags) != len(expectedCFlags) {
t.Errorf("Expected %d LibcCFlags, got %d", len(expectedCFlags), len(config.ExportCFlags))
} else {
for i, expected := range expectedCFlags {
if config.LibcCFlags[i] != expected {
t.Errorf("LibcCFlags[%d] mismatch. Expected '%s', got '%s'", i, expected, config.LibcCFlags[i])
if config.ExportCFlags[i] != expected {
t.Errorf("LibcCFlags[%d] mismatch. Expected '%s', got '%s'", i, expected, config.ExportCFlags[i])
}
}
}
@@ -407,8 +378,8 @@ func TestEdgeCases(t *testing.T) {
// Check that paths are constructed correctly
expected := "-isystem" + filepath.Join(libcDir, "include")
if config.LibcCFlags[0] != expected {
t.Errorf("Expected LibcCFlags[0] to be '%s', got '%s'", expected, config.LibcCFlags[0])
if config.ExportCFlags[0] != expected {
t.Errorf("Expected LibcCFlags[0] to be '%s', got '%s'", expected, config.ExportCFlags[0])
}
})
@@ -428,8 +399,8 @@ func TestEdgeCases(t *testing.T) {
// Check that paths are constructed correctly
expected := "-I" + filepath.Join(libcDir, "include")
if config.LibcCFlags[0] != expected {
t.Errorf("Expected LibcCFlags[0] to be '%s', got '%s'", expected, config.LibcCFlags[0])
if config.ExportCFlags[0] != expected {
t.Errorf("Expected LibcCFlags[0] to be '%s', got '%s'", expected, config.ExportCFlags[0])
}
})

View File

@@ -21,16 +21,20 @@ var _libcCCFlags = []string{
"-ffreestanding",
}
const (
_newlibUrl = "https://github.com/goplus/newlib/archive/refs/tags/v0.2.0.tar.gz"
_archiveInternalSrcDir = "newlib-0.2.0"
)
func withDefaultCCFlags(ccflags []string) []string {
return append(ccflags, _libcCCFlags...)
}
func getNewlibESP32ConfigRISCV(baseDir, target string) *compile.CompileConfig {
func GetNewlibESP32Config() compile.LibConfig {
return compile.LibConfig{
Url: "https://github.com/goplus/newlib/archive/refs/tags/v0.3.0.tar.gz",
Name: "newlib-esp32",
Version: "v0.3.0",
ArchiveSrcDir: "newlib-0.3.0",
}
}
func getNewlibESP32ConfigRISCV(baseDir, target string) compile.CompileConfig {
libcDir := filepath.Join(baseDir, "newlib", "libc")
libcIncludeDir := []string{
@@ -39,11 +43,8 @@ func getNewlibESP32ConfigRISCV(baseDir, target string) *compile.CompileConfig {
"-I" + libcDir,
}
return &compile.CompileConfig{
Url: _newlibUrl,
Name: "newlib-esp32",
LibcCFlags: libcIncludeDir,
ArchiveSrcDir: _archiveInternalSrcDir,
return compile.CompileConfig{
ExportCFlags: libcIncludeDir,
Groups: []compile.CompileGroup{
{
OutputFileName: fmt.Sprintf("libcrt0-%s.a", target),
@@ -1092,7 +1093,7 @@ func getNewlibESP32ConfigRISCV(baseDir, target string) *compile.CompileConfig {
}
}
func getNewlibESP32ConfigXtensa(baseDir, target string) *compile.CompileConfig {
func getNewlibESP32ConfigXtensa(baseDir, target string) compile.CompileConfig {
libcDir := filepath.Join(baseDir, "newlib", "libc")
libcIncludeDir := []string{
@@ -1101,11 +1102,8 @@ func getNewlibESP32ConfigXtensa(baseDir, target string) *compile.CompileConfig {
"-I" + libcDir,
}
return &compile.CompileConfig{
Url: _newlibUrl,
Name: "newlib-esp32",
ArchiveSrcDir: _archiveInternalSrcDir,
LibcCFlags: libcIncludeDir,
return compile.CompileConfig{
ExportCFlags: libcIncludeDir,
Groups: []compile.CompileGroup{
{
OutputFileName: fmt.Sprintf("libcrt0-%s.a", target),
@@ -2075,7 +2073,7 @@ func getNewlibESP32ConfigXtensa(baseDir, target string) *compile.CompileConfig {
}
// getNewlibESP32Config returns configuration for newlib esp32
func GetNewlibESP32Config(baseDir, target, mcpu string) *compile.CompileConfig {
func GetNewlibESP32CompileConfig(baseDir, target, mcpu string) compile.CompileConfig {
if strings.Contains(target, "riscv32") {
return getNewlibESP32ConfigRISCV(baseDir, target)
}

View File

@@ -7,16 +7,22 @@ import (
"github.com/goplus/llgo/internal/crosscompile/compile"
)
// getPicolibcConfig returns configuration for picolibc
func GetPicolibcConfig(baseDir, target string) *compile.CompileConfig {
return &compile.CompileConfig{
Url: "https://github.com/goplus/picolibc/archive/refs/heads/main.zip",
Name: "picolibc",
LibcCFlags: []string{
func GetPicolibcConfig() compile.LibConfig {
return compile.LibConfig{
Name: "picolibc",
Version: "v0.1.0",
Url: "https://github.com/goplus/picolibc/archive/refs/heads/main.zip",
ArchiveSrcDir: "picolibc-main",
}
}
// GetPicolibcCompileConfig returns configuration for picolibc
func GetPicolibcCompileConfig(baseDir, target string) compile.CompileConfig {
return compile.CompileConfig{
ExportCFlags: []string{
"-I" + baseDir,
"-isystem" + filepath.Join(baseDir, "newlib", "libc", "include"),
},
Groups: []compile.CompileGroup{
{
OutputFileName: fmt.Sprintf("libc-%s.a", target),
@@ -158,6 +164,5 @@ func GetPicolibcConfig(baseDir, target string) *compile.CompileConfig {
CCFlags: _libcCCFlags,
},
},
ArchiveSrcDir: "picolibc-main",
}
}