From 1b3889ebc937d7fd69ace2b8a7e16d42e5bd7372 Mon Sep 17 00:00:00 2001 From: Haolan Date: Fri, 29 Aug 2025 19:25:09 +0800 Subject: [PATCH] feat: add target name design --- internal/crosscompile/compile/libc/newlibesp.go | 9 +++++---- internal/crosscompile/compile/libc/picolibc.go | 5 +++-- internal/crosscompile/compile/rtlib/compiler_rt.go | 5 +++-- internal/crosscompile/crosscompile.go | 5 ++--- internal/crosscompile/fetch.go | 13 ++++--------- internal/crosscompile/libc.go | 10 +++++----- 6 files changed, 22 insertions(+), 25 deletions(-) diff --git a/internal/crosscompile/compile/libc/newlibesp.go b/internal/crosscompile/compile/libc/newlibesp.go index 7beb902c..b310c618 100644 --- a/internal/crosscompile/compile/libc/newlibesp.go +++ b/internal/crosscompile/compile/libc/newlibesp.go @@ -1,13 +1,14 @@ package libc import ( + "fmt" "path/filepath" "github.com/goplus/llgo/internal/crosscompile/compile" ) // getNewlibESP32Config returns configuration for newlib esp32 -func GetNewlibESP32Config(baseDir, arch string) *compile.CompileConfig { +func GetNewlibESP32Config(baseDir, target string) *compile.CompileConfig { libcDir := filepath.Join(baseDir, "newlib", "libc") return &compile.CompileConfig{ @@ -15,7 +16,7 @@ func GetNewlibESP32Config(baseDir, arch string) *compile.CompileConfig { Name: "newlib-esp32", Groups: []compile.CompileGroup{ { - OutputFileName: "libcrt0.a", + OutputFileName: fmt.Sprintf("libcrt0-%s.a", target), Files: []string{ filepath.Join(baseDir, "libgloss", "xtensa", "clibrary_init.c"), filepath.Join(baseDir, "libgloss", "xtensa", "syscalls.c"), @@ -39,7 +40,7 @@ func GetNewlibESP32Config(baseDir, arch string) *compile.CompileConfig { }, }, { - OutputFileName: "libgloss.a", + OutputFileName: fmt.Sprintf("libgloss-%s.a", target), Files: []string{ filepath.Join(baseDir, "libgloss", "libnosys", "chown.c"), filepath.Join(baseDir, "libgloss", "libnosys", "close.c"), @@ -236,7 +237,7 @@ func GetNewlibESP32Config(baseDir, arch string) *compile.CompileConfig { }, }, { - OutputFileName: "libc.a", + OutputFileName: fmt.Sprintf("libc-%s.a", target), Files: []string{ filepath.Join(libcDir, "argz", "argz_add.c"), filepath.Join(libcDir, "argz", "argz_add_sep.c"), diff --git a/internal/crosscompile/compile/libc/picolibc.go b/internal/crosscompile/compile/libc/picolibc.go index 319fbad2..a7efa2da 100644 --- a/internal/crosscompile/compile/libc/picolibc.go +++ b/internal/crosscompile/compile/libc/picolibc.go @@ -1,6 +1,7 @@ package libc import ( + "fmt" "os" "path/filepath" @@ -8,7 +9,7 @@ import ( ) // getPicolibcConfig returns configuration for picolibc -func GetPicolibcConfig(baseDir string) *compile.CompileConfig { +func GetPicolibcConfig(baseDir, target string) *compile.CompileConfig { libcIncludeDir := filepath.Join(baseDir, "libc", "include") libmIncludeDir := filepath.Join(baseDir, "libm", "common") localeIncludeDir := filepath.Join(baseDir, "libc", "locale") @@ -23,7 +24,7 @@ func GetPicolibcConfig(baseDir string) *compile.CompileConfig { Name: "picolibc", Groups: []compile.CompileGroup{ { - OutputFileName: "libc.a", + OutputFileName: fmt.Sprintf("libc-%s.a", target), Files: []string{ filepath.Join(baseDir, "libc", "string", "bcmp.c"), filepath.Join(baseDir, "libc", "string", "bcopy.c"), diff --git a/internal/crosscompile/compile/rtlib/compiler_rt.go b/internal/crosscompile/compile/rtlib/compiler_rt.go index 6de99715..d8926918 100644 --- a/internal/crosscompile/compile/rtlib/compiler_rt.go +++ b/internal/crosscompile/compile/rtlib/compiler_rt.go @@ -1,18 +1,19 @@ package rtlib import ( + "fmt" "path/filepath" "github.com/goplus/llgo/internal/crosscompile/compile" ) -func GetCompilerRTConfig(baseDir, arch string) *compile.CompileConfig { +func GetCompilerRTConfig(baseDir, target string) *compile.CompileConfig { return &compile.CompileConfig{ Url: "https://github.com/goplus/compiler-rt/archive/refs/tags/v0.1.0.tar.gz", ArchiveSrcDir: "compiler-rt-0.1.0", Groups: []compile.CompileGroup{ { - OutputFileName: "libclang_builtins.a", + OutputFileName: fmt.Sprintf("libclang_builtins-%s.a", target), Files: []string{ filepath.Join(baseDir, "lib", "builtins", "xtensa/ieee754_sqrtf.S"), filepath.Join(baseDir, "lib", "builtins", "absvdi2.c"), diff --git a/internal/crosscompile/crosscompile.go b/internal/crosscompile/crosscompile.go index fbfa8935..13e69de3 100644 --- a/internal/crosscompile/crosscompile.go +++ b/internal/crosscompile/crosscompile.go @@ -227,7 +227,6 @@ func getOrCompileWithConfig( exportCCFlags, exportLDFlags []string, ) (ldflags []string, err error) { if err = checkDownloadAndExtractLib( - compileConfig, compileConfig.Url, outputDir, compileConfig.ArchiveSrcDir, ); err != nil { @@ -613,7 +612,7 @@ func useTarget(targetName string) (export Export, err error) { baseDir := filepath.Join(cacheRoot(), "crosscompile") outputDir := filepath.Join(baseDir, config.Libc) - compileConfig, err = getLibcCompileConfigByName(baseDir, config.Libc) + compileConfig, err = getLibcCompileConfigByName(baseDir, config.Libc, config.LLVMTarget) if err != nil { return } @@ -632,7 +631,7 @@ func useTarget(targetName string) (export Export, err error) { baseDir := filepath.Join(cacheRoot(), "crosscompile") outputDir := filepath.Join(baseDir, config.RTLib) - compileConfig, err = getRTCompileConfigByName(baseDir, config.RTLib) + compileConfig, err = getRTCompileConfigByName(baseDir, config.RTLib, config.LLVMTarget) if err != nil { return } diff --git a/internal/crosscompile/fetch.go b/internal/crosscompile/fetch.go index 4c21ff5f..40db05f5 100644 --- a/internal/crosscompile/fetch.go +++ b/internal/crosscompile/fetch.go @@ -13,8 +13,6 @@ import ( "path/filepath" "strings" "syscall" - - "github.com/goplus/llgo/internal/crosscompile/compile" ) // checkDownloadAndExtractWasiSDK downloads and extracts WASI SDK @@ -82,9 +80,9 @@ func checkDownloadAndExtractESPClang(platformSuffix, dir string) error { return nil } -func checkDownloadAndExtractLib(cfg *compile.CompileConfig, url, dstDir, internalArchiveSrcDir string) error { +func checkDownloadAndExtractLib(url, dstDir, internalArchiveSrcDir string) error { // Check if already exists - if cfg.IsCompiled(dstDir) { + if _, err := os.Stat(dstDir); err == nil { return nil } @@ -97,7 +95,7 @@ func checkDownloadAndExtractLib(cfg *compile.CompileConfig, url, dstDir, interna defer releaseLock(lockFile) // Double-check after acquiring lock - if cfg.IsCompiled(dstDir) { + if _, err := os.Stat(dstDir); err == nil { return nil } fmt.Fprintf(os.Stderr, "%s not found in LLGO_ROOT or cache, will download and compile.\n", dstDir) @@ -117,10 +115,7 @@ func checkDownloadAndExtractLib(cfg *compile.CompileConfig, url, dstDir, interna srcDir = filepath.Join(tempExtractDir, internalArchiveSrcDir) } - os.RemoveAll(dstDir) - if err := os.Rename(srcDir, dstDir); err != nil { - return fmt.Errorf("failed to rename libc directory: %w", err) - } + os.Rename(srcDir, dstDir) return nil } diff --git a/internal/crosscompile/libc.go b/internal/crosscompile/libc.go index 8bd96e85..f3d27f4d 100644 --- a/internal/crosscompile/libc.go +++ b/internal/crosscompile/libc.go @@ -11,7 +11,7 @@ import ( // GetCompileConfigByName retrieves libc compilation configuration by name // Returns compilation file lists and corresponding cflags -func getLibcCompileConfigByName(baseDir, libcName string) (*compile.CompileConfig, error) { +func getLibcCompileConfigByName(baseDir, libcName, target string) (*compile.CompileConfig, error) { if libcName == "" { return nil, fmt.Errorf("libc name cannot be empty") } @@ -19,15 +19,15 @@ func getLibcCompileConfigByName(baseDir, libcName string) (*compile.CompileConfi switch libcName { case "picolibc": - return libc.GetPicolibcConfig(libcDir), nil + return libc.GetPicolibcConfig(libcDir, target), nil case "newlib-esp32": - return libc.GetNewlibESP32Config(libcDir, "xtensa"), nil + return libc.GetNewlibESP32Config(libcDir, target), nil default: return nil, fmt.Errorf("unsupported libc: %s", libcName) } } -func getRTCompileConfigByName(baseDir, rtName string) (*compile.CompileConfig, error) { +func getRTCompileConfigByName(baseDir, rtName, target string) (*compile.CompileConfig, error) { if rtName == "" { return nil, fmt.Errorf("rt name cannot be empty") } @@ -35,7 +35,7 @@ func getRTCompileConfigByName(baseDir, rtName string) (*compile.CompileConfig, e switch rtName { case "compiler-rt": - return rtlib.GetCompilerRTConfig(rtDir, "xtensa"), nil + return rtlib.GetCompilerRTConfig(rtDir, target), nil default: return nil, fmt.Errorf("unsupported rt: %s", rtName) }