From d4474be921b654699ceafb6fb28297c4090c0c76 Mon Sep 17 00:00:00 2001 From: Haolan Date: Tue, 9 Sep 2025 17:45:02 +0800 Subject: [PATCH] chore: fix some wrong commets --- internal/crosscompile/compile/compile.go | 8 +++++++- internal/crosscompile/compile/compile_test.go | 6 +++--- internal/crosscompile/compile/libc/newlibesp.go | 2 ++ internal/crosscompile/compile/libc/picolibc.go | 1 + internal/crosscompile/crosscompile.go | 4 ++++ internal/crosscompile/crosscompile_test.go | 8 ++++---- internal/crosscompile/libc.go | 4 +++- 7 files changed, 24 insertions(+), 9 deletions(-) diff --git a/internal/crosscompile/compile/compile.go b/internal/crosscompile/compile/compile.go index 01136d8e..acfec5b3 100644 --- a/internal/crosscompile/compile/compile.go +++ b/internal/crosscompile/compile/compile.go @@ -27,12 +27,16 @@ type CompileGroup struct { LDFlags []string // Linker flags } +// IsCompiled checks if the compile group has already been compiled by verifying +// if the output archive file exists in the specified directory func (g CompileGroup) IsCompiled(outputDir string) bool { archive := filepath.Join(outputDir, filepath.Base(g.OutputFileName)) _, err := os.Stat(archive) return err == nil } +// Compile compiles all source files in the group into a static library archive +// If the archive already exists, compilation is skipped func (g CompileGroup) Compile( outputDir string, options CompileOptions, ) (err error) { @@ -101,11 +105,13 @@ type CompileConfig struct { type LibConfig struct { Url string - Name string // compile name (e.g., "picolibc", "musl", "glibc") + Name string // Library name (e.g., "picolibc", "musl", "glibc") Version string ArchiveSrcDir string } +// String returns a string representation of the library configuration +// in the format "name-version" func (cfg LibConfig) String() string { return fmt.Sprintf("%s-%s", cfg.Name, cfg.Version) } diff --git a/internal/crosscompile/compile/compile_test.go b/internal/crosscompile/compile/compile_test.go index f5f5ba7d..eb37254f 100644 --- a/internal/crosscompile/compile/compile_test.go +++ b/internal/crosscompile/compile/compile_test.go @@ -11,8 +11,8 @@ import ( "github.com/goplus/llgo/xtool/nm" ) -func TestIsCompile(t *testing.T) { - t.Run("IsCompile Not Exists", func(t *testing.T) { +func TestIsCompiled(t *testing.T) { + t.Run("IsCompiled Not Exists", func(t *testing.T) { cfg := CompileConfig{ Groups: []CompileGroup{ { @@ -25,7 +25,7 @@ func TestIsCompile(t *testing.T) { t.Errorf("unexpected result: should false") } }) - t.Run("IsCompile Exists", func(t *testing.T) { + t.Run("IsCompiled Exists", func(t *testing.T) { tmpFile, err := os.CreateTemp("", "test*.a") if err != nil { t.Error(err) diff --git a/internal/crosscompile/compile/libc/newlibesp.go b/internal/crosscompile/compile/libc/newlibesp.go index a2e32149..cb55f495 100644 --- a/internal/crosscompile/compile/libc/newlibesp.go +++ b/internal/crosscompile/compile/libc/newlibesp.go @@ -21,10 +21,12 @@ var _libcCCFlags = []string{ "-ffreestanding", } +// withDefaultCCFlags appends default C compiler flags to the provided flags func withDefaultCCFlags(ccflags []string) []string { return append(ccflags, _libcCCFlags...) } +// GetNewlibESP32Config returns the configuration for downloading and building newlib for ESP32 func GetNewlibESP32Config() compile.LibConfig { return compile.LibConfig{ Url: "https://github.com/goplus/newlib/archive/refs/tags/esp-4.3.0_20250211-patch3.tar.gz", diff --git a/internal/crosscompile/compile/libc/picolibc.go b/internal/crosscompile/compile/libc/picolibc.go index 7a6d4b3f..680c1ed7 100644 --- a/internal/crosscompile/compile/libc/picolibc.go +++ b/internal/crosscompile/compile/libc/picolibc.go @@ -7,6 +7,7 @@ import ( "github.com/goplus/llgo/internal/crosscompile/compile" ) +// GetPicolibcConfig returns the configuration for downloading and building picolibc func GetPicolibcConfig() compile.LibConfig { return compile.LibConfig{ Name: "picolibc", diff --git a/internal/crosscompile/crosscompile.go b/internal/crosscompile/crosscompile.go index 7c2d1dd4..025f6293 100644 --- a/internal/crosscompile/crosscompile.go +++ b/internal/crosscompile/crosscompile.go @@ -167,10 +167,14 @@ func getESPClangPlatform(goos, goarch string) string { return "" } +// ldFlagsFromFileName extracts the library name from a filename for use in linker flags +// For example, "libmath.a" becomes "math" for use with "-lmath" func ldFlagsFromFileName(fileName string) string { return strings.TrimPrefix(strings.TrimSuffix(fileName, ".a"), "lib") } +// compileWithConfig compiles libraries according to the provided configuration +// and returns the necessary linker flags for linking against the compiled libraries func compileWithConfig( compileConfig compile.CompileConfig, outputDir string, options compile.CompileOptions, diff --git a/internal/crosscompile/crosscompile_test.go b/internal/crosscompile/crosscompile_test.go index 9856133c..6bf0fcf3 100644 --- a/internal/crosscompile/crosscompile_test.go +++ b/internal/crosscompile/crosscompile_test.go @@ -37,10 +37,10 @@ func TestUseCrossCompileSDK(t *testing.T) { name: "Same Platform", goos: runtime.GOOS, goarch: runtime.GOARCH, - expectSDK: true, // Changed: now we expect flags even for same platform - expectCCFlags: true, // Changed: CCFLAGS will contain sysroot - expectCFlags: false, // Changed: CFLAGS will not contain include paths - expectLDFlags: false, // Changed: LDFLAGS will not contain library paths + expectSDK: true, // We expect flags even for same platform + expectCCFlags: true, // CCFLAGS will contain sysroot + expectCFlags: false, // CFLAGS will not contain include paths + expectLDFlags: false, // LDFLAGS will not contain library paths }, { name: "WASM Target", diff --git a/internal/crosscompile/libc.go b/internal/crosscompile/libc.go index fb255419..b7feefc3 100644 --- a/internal/crosscompile/libc.go +++ b/internal/crosscompile/libc.go @@ -12,7 +12,7 @@ import ( // for testing, in testing env, we use fake path, it will cause downloading failure var needSkipDownload = false -// GetCompileConfigByName retrieves libc compilation configuration by name +// getLibcCompileConfigByName retrieves libc compilation configuration by name // Returns compilation file lists and corresponding cflags func getLibcCompileConfigByName(baseDir, libcName, target, mcpu string) (outputDir string, cfg compile.CompileConfig, err error) { if libcName == "" { @@ -47,6 +47,8 @@ func getLibcCompileConfigByName(baseDir, libcName, target, mcpu string) (outputD return libcDir, compileConfig, nil } +// getRTCompileConfigByName retrieves runtime library compilation configuration by name +// Returns compilation file lists and corresponding flags for the specified runtime library func getRTCompileConfigByName(baseDir, rtName, target string) (outputDir string, cfg compile.CompileConfig, err error) { if rtName == "" { err = fmt.Errorf("rt name cannot be empty")