diff --git a/.github/actions/setup-deps/action.yml b/.github/actions/setup-deps/action.yml index 7a0669c8..8d877e97 100644 --- a/.github/actions/setup-deps/action.yml +++ b/.github/actions/setup-deps/action.yml @@ -34,7 +34,7 @@ runs: echo "deb http://apt.llvm.org/$(lsb_release -cs)/ llvm-toolchain-$(lsb_release -cs)-${{inputs.llvm-version}} main" | sudo tee /etc/apt/sources.list.d/llvm.list wget -O - https://apt.llvm.org/llvm-snapshot.gpg.key | sudo apt-key add - sudo apt-get update - sudo apt-get install -y llvm-${{inputs.llvm-version}}-dev clang-${{inputs.llvm-version}} libclang-${{inputs.llvm-version}}-dev lld-${{inputs.llvm-version}} libunwind-${{inputs.llvm-version}}-dev libc++-${{inputs.llvm-version}}-dev pkg-config libgc-dev libssl-dev zlib1g-dev libffi-dev libcjson-dev libuv1-dev + sudo apt-get install -y llvm-${{inputs.llvm-version}}-dev clang-${{inputs.llvm-version}} libclang-${{inputs.llvm-version}}-dev lld-${{inputs.llvm-version}} libunwind-${{inputs.llvm-version}}-dev libc-dev libc++-${{inputs.llvm-version}}-dev pkg-config libgc-dev libssl-dev zlib1g-dev libffi-dev libcjson-dev libuv1-dev echo "PATH=/usr/lib/llvm-${{inputs.llvm-version}}/bin:$PATH" >> $GITHUB_ENV # Install optional deps for demos. diff --git a/internal/crosscompile/compile/compile.go b/internal/crosscompile/compile/compile.go index 20f72e2c..9699ac1d 100644 --- a/internal/crosscompile/compile/compile.go +++ b/internal/crosscompile/compile/compile.go @@ -11,6 +11,14 @@ import ( "github.com/goplus/llgo/internal/clang" ) +type CompileOptions struct { + CC string // Compiler to use + Linker string + CCFLAGS []string + CFLAGS []string + LDFLAGS []string +} + type CompileGroup struct { OutputFileName string Files []string // List of source files to compile @@ -25,7 +33,9 @@ func (g CompileGroup) IsCompiled(outputDir string) bool { return !os.IsNotExist(err) } -func (g CompileGroup) Compile(outputDir, cc, linkerName string, extraCCFlags, extraLDFlags []string) (err error) { +func (g CompileGroup) Compile( + outputDir string, options CompileOptions, +) (err error) { if g.IsCompiled(outputDir) { return } @@ -35,15 +45,17 @@ func (g CompileGroup) Compile(outputDir, cc, linkerName string, extraCCFlags, ex } defer os.RemoveAll(tmpCompileDir) - compileLDFlags := append(slices.Clone(extraLDFlags), g.LDFlags...) - compileCCFlags := append(slices.Clone(extraCCFlags), g.CCFlags...) - cfg := clang.NewConfig(cc, compileCCFlags, g.CFlags, compileLDFlags, linkerName) + compileLDFlags := append(slices.Clone(options.LDFLAGS), g.LDFlags...) + compileCCFlags := append(slices.Clone(options.CCFLAGS), g.CCFlags...) + compileCFFlags := append(slices.Clone(options.CFLAGS), g.CFlags...) + + cfg := clang.NewConfig(options.CC, compileCCFlags, compileCFFlags, compileLDFlags, options.Linker) var objFiles []string compiler := clang.NewCompiler(cfg) - compiler.Verbose = false + compiler.Verbose = true archive := filepath.Join(outputDir, g.OutputFileName) fmt.Fprintf(os.Stderr, "Start to compile group %s to %s...\n", g.OutputFileName, archive) @@ -70,7 +82,7 @@ func (g CompileGroup) Compile(outputDir, cc, linkerName string, extraCCFlags, ex args := []string{"rcs", archive} args = append(args, objFiles...) - ccDir := filepath.Dir(cc) + ccDir := filepath.Dir(options.CC) llvmAr := filepath.Join(ccDir, "llvm-ar") cmd := exec.Command(llvmAr, args...) @@ -87,13 +99,5 @@ type CompileConfig struct { Name string // compile name (e.g., "picolibc", "musl", "glibc") Groups []CompileGroup ArchiveSrcDir string -} - -func (c CompileConfig) IsCompiled(outputDir string) bool { - for _, group := range c.Groups { - if !group.IsCompiled(outputDir) { - return false - } - } - return true + LibcCFlags []string } diff --git a/internal/crosscompile/compile/compile_test.go b/internal/crosscompile/compile/compile_test.go index 9995c555..74039a9d 100644 --- a/internal/crosscompile/compile/compile_test.go +++ b/internal/crosscompile/compile/compile_test.go @@ -2,6 +2,7 @@ package compile import ( "os" + "path/filepath" "strings" "testing" @@ -18,12 +19,12 @@ func TestIsCompile(t *testing.T) { }, } - if cfg.IsCompiled(".") || cfg.Groups[0].IsCompiled(".") { + if cfg.Groups[0].IsCompiled(".") { t.Errorf("unexpected result: should false") } }) t.Run("IsCompile Exists", func(t *testing.T) { - tmpFile, err := os.CreateTemp(".", "test*.a") + tmpFile, err := os.CreateTemp("", "test*.a") if err != nil { t.Error(err) return @@ -37,7 +38,7 @@ func TestIsCompile(t *testing.T) { }, } - if !cfg.IsCompiled(".") && !cfg.Groups[0].IsCompiled(".") { + if cfg.Groups[0].IsCompiled(filepath.Dir(tmpFile.Name())) { t.Errorf("unexpected result: should true") } }) @@ -54,7 +55,10 @@ func TestCompile(t *testing.T) { group := CompileGroup{ OutputFileName: tmpFile.Name(), } - err = group.Compile(".", "clang", "lld", nil, nil) + err = group.Compile(".", CompileOptions{ + CC: "clang", + Linker: "lld", + }) if err != nil { t.Errorf("unexpected result: should nil") } @@ -74,7 +78,10 @@ func TestCompile(t *testing.T) { group := CompileGroup{ OutputFileName: "nop.a", } - err = group.Compile(".", "clang", "lld", nil, nil) + err = group.Compile(".", CompileOptions{ + CC: "clang", + Linker: "lld", + }) if err == nil { t.Errorf("unexpected result: should not nil") } @@ -104,11 +111,18 @@ func TestCompile(t *testing.T) { OutputFileName: "nop.a", Files: []string{tmpFile.Name()}, } - err = group.Compile(".", "clang", "lld", []string{"-nostdinc"}, nil) + err = group.Compile(".", CompileOptions{ + CC: "clang", + Linker: "lld", + CCFLAGS: []string{"-nostdinc"}, + }) if err == nil { t.Errorf("unexpected result: should not nil") } - err = group.Compile(".", "clang", "lld", nil, nil) + err = group.Compile(".", CompileOptions{ + CC: "clang", + Linker: "lld", + }) if err != nil { t.Errorf("unexpected result: should not nil") } diff --git a/internal/crosscompile/compile/libc/newlibesp.go b/internal/crosscompile/compile/libc/newlibesp.go index 86e5651a..23d7930d 100644 --- a/internal/crosscompile/compile/libc/newlibesp.go +++ b/internal/crosscompile/compile/libc/newlibesp.go @@ -8,12 +8,42 @@ import ( "github.com/goplus/llgo/internal/crosscompile/compile" ) +var _libcLDFlags = []string{ + "-nostdlib", + "-ffunction-sections", + "-fdata-sections", +} + +var _libcCCFlags = []string{ + "-Oz", + "-fno-builtin", + // freestanding tells compiler this is a baremental project + "-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 { libcDir := filepath.Join(baseDir, "newlib", "libc") + libcIncludeDir := []string{ + "-isystem" + filepath.Join(libcDir, "include"), + "-I" + filepath.Join(baseDir, "newlib"), + "-I" + libcDir, + } + return &compile.CompileConfig{ - Url: "https://github.com/goplus/newlib/archive/refs/tags/v0.2.0.tar.gz", - Name: "newlib-esp32", + Url: _newlibUrl, + Name: "newlib-esp32", + LibcCFlags: libcIncludeDir, + ArchiveSrcDir: _archiveInternalSrcDir, Groups: []compile.CompileGroup{ { OutputFileName: fmt.Sprintf("libcrt0-%s.a", target), @@ -30,10 +60,8 @@ func getNewlibESP32ConfigRISCV(baseDir, target string) *compile.CompileConfig { "-I" + filepath.Join(baseDir, "libgloss", "riscv"), "-I" + filepath.Join(baseDir, "libgloss", "riscv", "esp"), }, - LDFlags: []string{"-nostdlib"}, - CCFlags: []string{ - "-Oz", - }, + LDFlags: _libcLDFlags, + CCFlags: _libcCCFlags, }, { OutputFileName: fmt.Sprintf("libgloss-%s.a", target), @@ -298,10 +326,8 @@ func getNewlibESP32ConfigRISCV(baseDir, target string) *compile.CompileConfig { "-I" + filepath.Join(baseDir, "libgloss", "riscv"), "-I" + filepath.Join(baseDir, "libgloss", "riscv", "esp"), }, - LDFlags: []string{"-nostdlib"}, - CCFlags: []string{ - "-Oz", - }, + LDFlags: _libcLDFlags, + CCFlags: _libcCCFlags, }, { OutputFileName: fmt.Sprintf("libc-%s.a", target), @@ -1054,27 +1080,32 @@ func getNewlibESP32ConfigRISCV(baseDir, target string) *compile.CompileConfig { "-I" + filepath.Join(libcDir, "posix"), "-I" + filepath.Join(libcDir, "stdlib"), }, - LDFlags: []string{"-nostdlib"}, - CCFlags: []string{ - "-Oz", + LDFlags: _libcLDFlags, + CCFlags: withDefaultCCFlags([]string{ "-fno-builtin", - "-ffreestanding", "-Wno-implicit-function-declaration", "-Wno-int-conversion", "-Wno-unused-command-line-argument", - }, + }), }, }, - ArchiveSrcDir: "newlib-0.2.0", } } func getNewlibESP32ConfigXtensa(baseDir, target string) *compile.CompileConfig { libcDir := filepath.Join(baseDir, "newlib", "libc") + libcIncludeDir := []string{ + "-I" + filepath.Join(libcDir, "include"), + "-I" + filepath.Join(baseDir, "newlib"), + "-I" + libcDir, + } + return &compile.CompileConfig{ - Url: "https://github.com/goplus/newlib/archive/refs/tags/v0.2.0.tar.gz", - Name: "newlib-esp32", + Url: _newlibUrl, + Name: "newlib-esp32", + ArchiveSrcDir: _archiveInternalSrcDir, + LibcCFlags: libcIncludeDir, Groups: []compile.CompileGroup{ { OutputFileName: fmt.Sprintf("libcrt0-%s.a", target), @@ -1095,10 +1126,8 @@ func getNewlibESP32ConfigXtensa(baseDir, target string) *compile.CompileConfig { "-I" + filepath.Join(baseDir, "libgloss", "xtensa", "include"), "-I" + filepath.Join(baseDir, "libgloss", "xtensa", "boards", "esp32", "include"), }, - LDFlags: []string{"-nostdlib"}, - CCFlags: []string{ - "-Oz", - }, + LDFlags: _libcLDFlags, + CCFlags: _libcCCFlags, }, { OutputFileName: fmt.Sprintf("libgloss-%s.a", target), @@ -1292,10 +1321,8 @@ func getNewlibESP32ConfigXtensa(baseDir, target string) *compile.CompileConfig { "-I" + filepath.Join(baseDir, "libgloss", "xtensa", "include"), "-I" + filepath.Join(baseDir, "libgloss", "xtensa", "boards", "esp32", "include"), }, - LDFlags: []string{"-nostdlib"}, - CCFlags: []string{ - "-Oz", - }, + LDFlags: _libcLDFlags, + CCFlags: _libcCCFlags, }, { OutputFileName: fmt.Sprintf("libc-%s.a", target), @@ -2035,18 +2062,15 @@ func getNewlibESP32ConfigXtensa(baseDir, target string) *compile.CompileConfig { "-I" + filepath.Join(libcDir, "posix"), "-I" + filepath.Join(libcDir, "stdlib"), }, - LDFlags: []string{"-nostdlib"}, - CCFlags: []string{ - "-Oz", + LDFlags: _libcLDFlags, + CCFlags: withDefaultCCFlags([]string{ "-fno-builtin", - "-ffreestanding", "-Wno-implicit-function-declaration", "-Wno-int-conversion", "-Wno-unused-command-line-argument", - }, + }), }, }, - ArchiveSrcDir: "newlib-0.1.0", } } diff --git a/internal/crosscompile/compile/libc/picolibc.go b/internal/crosscompile/compile/libc/picolibc.go index 0e35d9a4..7736ccf7 100644 --- a/internal/crosscompile/compile/libc/picolibc.go +++ b/internal/crosscompile/compile/libc/picolibc.go @@ -9,131 +9,132 @@ import ( // getPicolibcConfig returns configuration for picolibc 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") - return &compile.CompileConfig{ Url: "https://github.com/goplus/picolibc/archive/refs/heads/main.zip", Name: "picolibc", + LibcCFlags: []string{ + "-I" + baseDir, + "-isystem" + filepath.Join(baseDir, "newlib", "libc", "include"), + }, + Groups: []compile.CompileGroup{ { OutputFileName: fmt.Sprintf("libc-%s.a", target), Files: []string{ - filepath.Join(baseDir, "libc", "string", "bcmp.c"), - filepath.Join(baseDir, "libc", "string", "bcopy.c"), - filepath.Join(baseDir, "libc", "string", "bzero.c"), - filepath.Join(baseDir, "libc", "string", "explicit_bzero.c"), - filepath.Join(baseDir, "libc", "string", "ffsl.c"), - filepath.Join(baseDir, "libc", "string", "ffsll.c"), - filepath.Join(baseDir, "libc", "string", "fls.c"), - filepath.Join(baseDir, "libc", "string", "flsl.c"), - filepath.Join(baseDir, "libc", "string", "flsll.c"), - filepath.Join(baseDir, "libc", "string", "gnu_basename.c"), - filepath.Join(baseDir, "libc", "string", "index.c"), - filepath.Join(baseDir, "libc", "string", "memccpy.c"), - filepath.Join(baseDir, "libc", "string", "memchr.c"), - filepath.Join(baseDir, "libc", "string", "memcmp.c"), - filepath.Join(baseDir, "libc", "string", "memcpy.c"), - filepath.Join(baseDir, "libc", "string", "memmem.c"), - filepath.Join(baseDir, "libc", "string", "memmove.c"), - filepath.Join(baseDir, "libc", "string", "mempcpy.c"), - filepath.Join(baseDir, "libc", "string", "memrchr.c"), - filepath.Join(baseDir, "libc", "string", "memset.c"), - filepath.Join(baseDir, "libc", "string", "rawmemchr.c"), - filepath.Join(baseDir, "libc", "string", "rindex.c"), - filepath.Join(baseDir, "libc", "string", "stpcpy.c"), - filepath.Join(baseDir, "libc", "string", "stpncpy.c"), - filepath.Join(baseDir, "libc", "string", "strcasecmp.c"), - filepath.Join(baseDir, "libc", "string", "strcasecmp_l.c"), - filepath.Join(baseDir, "libc", "string", "strcasestr.c"), - filepath.Join(baseDir, "libc", "string", "strcat.c"), - filepath.Join(baseDir, "libc", "string", "strchr.c"), - filepath.Join(baseDir, "libc", "string", "strchrnul.c"), - filepath.Join(baseDir, "libc", "string", "strcmp.c"), - filepath.Join(baseDir, "libc", "string", "strcoll.c"), - filepath.Join(baseDir, "libc", "string", "strcoll_l.c"), - filepath.Join(baseDir, "libc", "string", "strcpy.c"), - filepath.Join(baseDir, "libc", "string", "strcspn.c"), - filepath.Join(baseDir, "libc", "string", "strerror_r.c"), - filepath.Join(baseDir, "libc", "string", "strlcat.c"), - filepath.Join(baseDir, "libc", "string", "strlcpy.c"), - filepath.Join(baseDir, "libc", "string", "strlen.c"), - filepath.Join(baseDir, "libc", "string", "strlwr.c"), - filepath.Join(baseDir, "libc", "string", "strncasecmp.c"), - filepath.Join(baseDir, "libc", "string", "strncasecmp_l.c"), - filepath.Join(baseDir, "libc", "string", "strncat.c"), - filepath.Join(baseDir, "libc", "string", "strncmp.c"), - filepath.Join(baseDir, "libc", "string", "strncpy.c"), - filepath.Join(baseDir, "libc", "string", "strndup.c"), - filepath.Join(baseDir, "libc", "string", "strnlen.c"), - filepath.Join(baseDir, "libc", "string", "strnstr.c"), - filepath.Join(baseDir, "libc", "string", "strpbrk.c"), - filepath.Join(baseDir, "libc", "string", "strrchr.c"), - filepath.Join(baseDir, "libc", "string", "strsep.c"), - filepath.Join(baseDir, "libc", "string", "strsignal.c"), - filepath.Join(baseDir, "libc", "string", "strspn.c"), - filepath.Join(baseDir, "libc", "string", "strstr.c"), - filepath.Join(baseDir, "libc", "string", "strtok.c"), - filepath.Join(baseDir, "libc", "string", "strtok_r.c"), - filepath.Join(baseDir, "libc", "string", "strupr.c"), - filepath.Join(baseDir, "libc", "string", "strverscmp.c"), - filepath.Join(baseDir, "libc", "string", "strxfrm.c"), - filepath.Join(baseDir, "libc", "string", "strxfrm_l.c"), - filepath.Join(baseDir, "libc", "string", "swab.c"), - filepath.Join(baseDir, "libc", "string", "timingsafe_bcmp.c"), - filepath.Join(baseDir, "libc", "string", "timingsafe_memcmp.c"), - filepath.Join(baseDir, "libc", "string", "strerror.c"), - filepath.Join(baseDir, "libc", "string", "wcpcpy.c"), - filepath.Join(baseDir, "libc", "string", "wcpncpy.c"), - filepath.Join(baseDir, "libc", "string", "wcscasecmp.c"), - filepath.Join(baseDir, "libc", "string", "wcscasecmp_l.c"), - filepath.Join(baseDir, "libc", "string", "wcscat.c"), - filepath.Join(baseDir, "libc", "string", "wcschr.c"), - filepath.Join(baseDir, "libc", "string", "wcscmp.c"), - filepath.Join(baseDir, "libc", "string", "wcscoll.c"), - filepath.Join(baseDir, "libc", "string", "wcscoll_l.c"), - filepath.Join(baseDir, "libc", "string", "wcscpy.c"), - filepath.Join(baseDir, "libc", "string", "wcscspn.c"), - filepath.Join(baseDir, "libc", "string", "wcsdup.c"), - filepath.Join(baseDir, "libc", "string", "wcslcat.c"), - filepath.Join(baseDir, "libc", "string", "wcslcpy.c"), - filepath.Join(baseDir, "libc", "string", "wcslen.c"), - filepath.Join(baseDir, "libc", "string", "wcsncasecmp.c"), - filepath.Join(baseDir, "libc", "string", "wcsncasecmp_l.c"), - filepath.Join(baseDir, "libc", "string", "wcsncat.c"), - filepath.Join(baseDir, "libc", "string", "wcsncmp.c"), - filepath.Join(baseDir, "libc", "string", "wcsncpy.c"), - filepath.Join(baseDir, "libc", "string", "wcsnlen.c"), - filepath.Join(baseDir, "libc", "string", "wcspbrk.c"), - filepath.Join(baseDir, "libc", "string", "wcsrchr.c"), - filepath.Join(baseDir, "libc", "string", "wcsspn.c"), - filepath.Join(baseDir, "libc", "string", "wcsstr.c"), - filepath.Join(baseDir, "libc", "string", "wcstok.c"), - filepath.Join(baseDir, "libc", "string", "wcswidth.c"), - filepath.Join(baseDir, "libc", "string", "wcsxfrm.c"), - filepath.Join(baseDir, "libc", "string", "wcsxfrm_l.c"), - filepath.Join(baseDir, "libc", "string", "wcwidth.c"), - filepath.Join(baseDir, "libc", "string", "wmemchr.c"), - filepath.Join(baseDir, "libc", "string", "wmemcmp.c"), - filepath.Join(baseDir, "libc", "string", "wmemcpy.c"), - filepath.Join(baseDir, "libc", "string", "wmemmove.c"), - filepath.Join(baseDir, "libc", "string", "wmempcpy.c"), - filepath.Join(baseDir, "libc", "string", "wmemset.c"), - filepath.Join(baseDir, "libc", "string", "xpg_strerror_r.c"), + filepath.Join(baseDir, "newlib", "libc", "string", "bcmp.c"), + filepath.Join(baseDir, "newlib", "libc", "string", "bcopy.c"), + filepath.Join(baseDir, "newlib", "libc", "string", "bzero.c"), + filepath.Join(baseDir, "newlib", "libc", "string", "explicit_bzero.c"), + filepath.Join(baseDir, "newlib", "libc", "string", "ffsl.c"), + filepath.Join(baseDir, "newlib", "libc", "string", "ffsll.c"), + filepath.Join(baseDir, "newlib", "libc", "string", "fls.c"), + filepath.Join(baseDir, "newlib", "libc", "string", "flsl.c"), + filepath.Join(baseDir, "newlib", "libc", "string", "flsll.c"), + filepath.Join(baseDir, "newlib", "libc", "string", "gnu_basename.c"), + filepath.Join(baseDir, "newlib", "libc", "string", "index.c"), + filepath.Join(baseDir, "newlib", "libc", "string", "memccpy.c"), + filepath.Join(baseDir, "newlib", "libc", "string", "memchr.c"), + filepath.Join(baseDir, "newlib", "libc", "string", "memcmp.c"), + filepath.Join(baseDir, "newlib", "libc", "string", "memcpy.c"), + filepath.Join(baseDir, "newlib", "libc", "string", "memmem.c"), + filepath.Join(baseDir, "newlib", "libc", "string", "memmove.c"), + filepath.Join(baseDir, "newlib", "libc", "string", "mempcpy.c"), + filepath.Join(baseDir, "newlib", "libc", "string", "memrchr.c"), + filepath.Join(baseDir, "newlib", "libc", "string", "memset.c"), + filepath.Join(baseDir, "newlib", "libc", "string", "rawmemchr.c"), + filepath.Join(baseDir, "newlib", "libc", "string", "rindex.c"), + filepath.Join(baseDir, "newlib", "libc", "string", "stpcpy.c"), + filepath.Join(baseDir, "newlib", "libc", "string", "stpncpy.c"), + filepath.Join(baseDir, "newlib", "libc", "string", "strcasecmp.c"), + filepath.Join(baseDir, "newlib", "libc", "string", "strcasecmp_l.c"), + filepath.Join(baseDir, "newlib", "libc", "string", "strcasestr.c"), + filepath.Join(baseDir, "newlib", "libc", "string", "strcat.c"), + filepath.Join(baseDir, "newlib", "libc", "string", "strchr.c"), + filepath.Join(baseDir, "newlib", "libc", "string", "strchrnul.c"), + filepath.Join(baseDir, "newlib", "libc", "string", "strcmp.c"), + filepath.Join(baseDir, "newlib", "libc", "string", "strcoll.c"), + filepath.Join(baseDir, "newlib", "libc", "string", "strcoll_l.c"), + filepath.Join(baseDir, "newlib", "libc", "string", "strcpy.c"), + filepath.Join(baseDir, "newlib", "libc", "string", "strcspn.c"), + filepath.Join(baseDir, "newlib", "libc", "string", "strerror_r.c"), + filepath.Join(baseDir, "newlib", "libc", "string", "strlcat.c"), + filepath.Join(baseDir, "newlib", "libc", "string", "strlcpy.c"), + filepath.Join(baseDir, "newlib", "libc", "string", "strlen.c"), + filepath.Join(baseDir, "newlib", "libc", "string", "strlwr.c"), + filepath.Join(baseDir, "newlib", "libc", "string", "strncasecmp.c"), + filepath.Join(baseDir, "newlib", "libc", "string", "strncasecmp_l.c"), + filepath.Join(baseDir, "newlib", "libc", "string", "strncat.c"), + filepath.Join(baseDir, "newlib", "libc", "string", "strncmp.c"), + filepath.Join(baseDir, "newlib", "libc", "string", "strncpy.c"), + filepath.Join(baseDir, "newlib", "libc", "string", "strndup.c"), + filepath.Join(baseDir, "newlib", "libc", "string", "strnlen.c"), + filepath.Join(baseDir, "newlib", "libc", "string", "strnstr.c"), + filepath.Join(baseDir, "newlib", "libc", "string", "strpbrk.c"), + filepath.Join(baseDir, "newlib", "libc", "string", "strrchr.c"), + filepath.Join(baseDir, "newlib", "libc", "string", "strsep.c"), + filepath.Join(baseDir, "newlib", "libc", "string", "strsignal.c"), + filepath.Join(baseDir, "newlib", "libc", "string", "strspn.c"), + filepath.Join(baseDir, "newlib", "libc", "string", "strstr.c"), + filepath.Join(baseDir, "newlib", "libc", "string", "strtok.c"), + filepath.Join(baseDir, "newlib", "libc", "string", "strtok_r.c"), + filepath.Join(baseDir, "newlib", "libc", "string", "strupr.c"), + filepath.Join(baseDir, "newlib", "libc", "string", "strverscmp.c"), + filepath.Join(baseDir, "newlib", "libc", "string", "strxfrm.c"), + filepath.Join(baseDir, "newlib", "libc", "string", "strxfrm_l.c"), + filepath.Join(baseDir, "newlib", "libc", "string", "swab.c"), + filepath.Join(baseDir, "newlib", "libc", "string", "timingsafe_bcmp.c"), + filepath.Join(baseDir, "newlib", "libc", "string", "timingsafe_memcmp.c"), + filepath.Join(baseDir, "newlib", "libc", "string", "strerror.c"), + filepath.Join(baseDir, "newlib", "libc", "string", "wcpcpy.c"), + filepath.Join(baseDir, "newlib", "libc", "string", "wcpncpy.c"), + filepath.Join(baseDir, "newlib", "libc", "string", "wcscasecmp.c"), + filepath.Join(baseDir, "newlib", "libc", "string", "wcscasecmp_l.c"), + filepath.Join(baseDir, "newlib", "libc", "string", "wcscat.c"), + filepath.Join(baseDir, "newlib", "libc", "string", "wcschr.c"), + filepath.Join(baseDir, "newlib", "libc", "string", "wcscmp.c"), + filepath.Join(baseDir, "newlib", "libc", "string", "wcscoll.c"), + filepath.Join(baseDir, "newlib", "libc", "string", "wcscoll_l.c"), + filepath.Join(baseDir, "newlib", "libc", "string", "wcscpy.c"), + filepath.Join(baseDir, "newlib", "libc", "string", "wcscspn.c"), + filepath.Join(baseDir, "newlib", "libc", "string", "wcsdup.c"), + filepath.Join(baseDir, "newlib", "libc", "string", "wcslcat.c"), + filepath.Join(baseDir, "newlib", "libc", "string", "wcslcpy.c"), + filepath.Join(baseDir, "newlib", "libc", "string", "wcslen.c"), + filepath.Join(baseDir, "newlib", "libc", "string", "wcsncasecmp.c"), + filepath.Join(baseDir, "newlib", "libc", "string", "wcsncasecmp_l.c"), + filepath.Join(baseDir, "newlib", "libc", "string", "wcsncat.c"), + filepath.Join(baseDir, "newlib", "libc", "string", "wcsncmp.c"), + filepath.Join(baseDir, "newlib", "libc", "string", "wcsncpy.c"), + filepath.Join(baseDir, "newlib", "libc", "string", "wcsnlen.c"), + filepath.Join(baseDir, "newlib", "libc", "string", "wcspbrk.c"), + filepath.Join(baseDir, "newlib", "libc", "string", "wcsrchr.c"), + filepath.Join(baseDir, "newlib", "libc", "string", "wcsspn.c"), + filepath.Join(baseDir, "newlib", "libc", "string", "wcsstr.c"), + filepath.Join(baseDir, "newlib", "libc", "string", "wcstok.c"), + filepath.Join(baseDir, "newlib", "libc", "string", "wcswidth.c"), + filepath.Join(baseDir, "newlib", "libc", "string", "wcsxfrm.c"), + filepath.Join(baseDir, "newlib", "libc", "string", "wcsxfrm_l.c"), + filepath.Join(baseDir, "newlib", "libc", "string", "wcwidth.c"), + filepath.Join(baseDir, "newlib", "libc", "string", "wmemchr.c"), + filepath.Join(baseDir, "newlib", "libc", "string", "wmemcmp.c"), + filepath.Join(baseDir, "newlib", "libc", "string", "wmemcpy.c"), + filepath.Join(baseDir, "newlib", "libc", "string", "wmemmove.c"), + filepath.Join(baseDir, "newlib", "libc", "string", "wmempcpy.c"), + filepath.Join(baseDir, "newlib", "libc", "string", "wmemset.c"), + filepath.Join(baseDir, "newlib", "libc", "string", "xpg_strerror_r.c"), - filepath.Join(baseDir, "libc", "stdlib", "nano-calloc.c"), - filepath.Join(baseDir, "libc", "stdlib", "nano-malloc.c"), - filepath.Join(baseDir, "libc", "stdlib", "nano-pvalloc.c"), - filepath.Join(baseDir, "libc", "stdlib", "nano-realloc.c"), - filepath.Join(baseDir, "libc", "stdlib", "nano-valloc.c"), - filepath.Join(baseDir, "libc", "stdlib", "rand.c"), - filepath.Join(baseDir, "libc", "stdlib", "srand.c"), - filepath.Join(baseDir, "libc", "stdlib", "nano-free.c"), + filepath.Join(baseDir, "newlib", "libc", "stdlib", "nano-calloc.c"), + filepath.Join(baseDir, "newlib", "libc", "stdlib", "nano-malloc.c"), + filepath.Join(baseDir, "newlib", "libc", "stdlib", "nano-pvalloc.c"), + filepath.Join(baseDir, "newlib", "libc", "stdlib", "nano-realloc.c"), + filepath.Join(baseDir, "newlib", "libc", "stdlib", "nano-valloc.c"), + filepath.Join(baseDir, "newlib", "libc", "stdlib", "rand.c"), + filepath.Join(baseDir, "newlib", "libc", "stdlib", "srand.c"), + filepath.Join(baseDir, "newlib", "libc", "stdlib", "nano-free.c"), - filepath.Join(baseDir, "libc", "tinystdio", "printf.c"), - filepath.Join(baseDir, "libc", "tinystdio", "putchar.c"), - filepath.Join(baseDir, "libc", "tinystdio", "puts.c"), + filepath.Join(baseDir, "newlib", "libc", "tinystdio", "printf.c"), + filepath.Join(baseDir, "newlib", "libc", "tinystdio", "putchar.c"), + filepath.Join(baseDir, "newlib", "libc", "tinystdio", "puts.c"), }, CFlags: []string{ "-D_COMPILING_NEWLIB", @@ -146,18 +147,17 @@ func GetPicolibcConfig(baseDir, target string) *compile.CompileConfig { "-D__OBSOLETE_MATH_DOUBLE=0", "-D_WANT_IO_C99_FORMATS", "-nostdlib", - "-isystem" + libcIncludeDir, - "-I" + libmIncludeDir, - "-I" + localeIncludeDir, "-I" + baseDir, - "-I" + filepath.Join(baseDir, "libc", "tinystdio"), - }, - LDFlags: []string{"-nostdlib"}, - CCFlags: []string{ - "-Oz", + "-isystem" + filepath.Join(baseDir, "newlib", "libc", "include"), + "-I" + filepath.Join(baseDir, "newlib", "libm", "common"), + "-I" + filepath.Join(baseDir, "newlib", "libc", "locale"), + + "-I" + filepath.Join(baseDir, "newlib", "libc", "tinystdio"), }, + LDFlags: _libcLDFlags, + CCFlags: _libcCCFlags, }, }, - ArchiveSrcDir: filepath.Join("picolibc-main", "newlib"), + ArchiveSrcDir: "picolibc-main", } } diff --git a/internal/crosscompile/compile/rtlib/compiler_rt.go b/internal/crosscompile/compile/rtlib/compiler_rt.go index 1a1e403a..d307132c 100644 --- a/internal/crosscompile/compile/rtlib/compiler_rt.go +++ b/internal/crosscompile/compile/rtlib/compiler_rt.go @@ -16,6 +16,7 @@ func platformSpecifiedFiles(builtinsDir, target string) []string { filepath.Join(builtinsDir, "riscv", "fp_mode.c"), filepath.Join(builtinsDir, "riscv", "save.S"), filepath.Join(builtinsDir, "riscv", "restore.S"), + filepath.Join(builtinsDir, "atomic.c"), } case strings.Contains(target, "riscv64"): return []string{ @@ -45,6 +46,7 @@ func platformSpecifiedFiles(builtinsDir, target string) []string { filepath.Join(builtinsDir, "trunctfdf2.c"), filepath.Join(builtinsDir, "trunctfhf2.c"), filepath.Join(builtinsDir, "trunctfsf2.c"), + filepath.Join(builtinsDir, "atomic.c"), } case strings.Contains(target, "arm"): return []string{ @@ -87,6 +89,7 @@ func platformSpecifiedFiles(builtinsDir, target string) []string { case target == "xtensa": return []string{ filepath.Join(builtinsDir, "xtensa", "ieee754_sqrtf.S"), + filepath.Join(builtinsDir, "atomic.c"), } } return nil @@ -252,7 +255,6 @@ func GetCompilerRTConfig(baseDir, target string) *compile.CompileConfig { filepath.Join(baseDir, "lib", "builtins", "trunctfdf2.c"), filepath.Join(baseDir, "lib", "builtins", "trunctfhf2.c"), filepath.Join(baseDir, "lib", "builtins", "trunctfsf2.c"), - filepath.Join(baseDir, "lib", "builtins", "atomic.c"), }), CFlags: []string{ "-DNDEBUG", diff --git a/internal/crosscompile/crosscompile.go b/internal/crosscompile/crosscompile.go index a53fff57..c128d8f1 100644 --- a/internal/crosscompile/crosscompile.go +++ b/internal/crosscompile/crosscompile.go @@ -223,8 +223,7 @@ func ldFlagsFromFileName(fileName string) string { func getOrCompileWithConfig( compileConfig *compile.CompileConfig, - outputDir, cc, linkerName string, - exportCCFlags, exportLDFlags []string, + outputDir string, options compile.CompileOptions, ) (ldflags []string, err error) { if err = checkDownloadAndExtractLib( compileConfig.Url, outputDir, @@ -235,7 +234,7 @@ func getOrCompileWithConfig( ldflags = append(ldflags, "-nostdlib", "-L"+outputDir) for _, group := range compileConfig.Groups { - err = group.Compile(outputDir, cc, linkerName, exportCCFlags, exportLDFlags) + err = group.Compile(outputDir, options) if err != nil { break } @@ -606,6 +605,8 @@ func useTarget(targetName string) (export Export, err error) { } ldflags = append(ldflags, "-L", env.LLGoROOT()) // search targets/*.ld + var libcIncludeDir []string + if config.Libc != "" { var libcLDFlags []string var compileConfig *compile.CompileConfig @@ -616,12 +617,19 @@ func useTarget(targetName string) (export Export, err error) { if err != nil { return } - libcLDFlags, err = getOrCompileWithConfig(compileConfig, outputDir, export.CC, export.Linker, ccflags, ldflags) + libcLDFlags, err = getOrCompileWithConfig(compileConfig, outputDir, compile.CompileOptions{ + CC: export.CC, + Linker: export.Linker, + CCFLAGS: ccflags, + LDFLAGS: ldflags, + }) if err != nil { return } + cflags = append(cflags, compileConfig.LibcCFlags...) ldflags = append(ldflags, libcLDFlags...) + libcIncludeDir = compileConfig.LibcCFlags export.Libc = config.Libc } @@ -635,7 +643,13 @@ func useTarget(targetName string) (export Export, err error) { if err != nil { return } - rtLibLDFlags, err = getOrCompileWithConfig(compileConfig, outputDir, export.CC, export.Linker, ccflags, ldflags) + rtLibLDFlags, err = getOrCompileWithConfig(compileConfig, outputDir, compile.CompileOptions{ + CC: export.CC, + Linker: export.Linker, + CCFLAGS: ccflags, + LDFLAGS: ldflags, + CFLAGS: libcIncludeDir, + }) if err != nil { return } @@ -654,7 +668,7 @@ func useTarget(targetName string) (export Export, err error) { // Use extends the original Use function to support target-based configuration // If targetName is provided, it takes precedence over goos/goarch func Use(goos, goarch string, wasiThreads bool, targetName string) (export Export, err error) { - if targetName != "" { + if targetName != "" && !strings.HasPrefix(targetName, "wasm") && !strings.HasPrefix(targetName, "wasi") { return useTarget(targetName) } return use(goos, goarch, wasiThreads) diff --git a/internal/crosscompile/fetch_test.go b/internal/crosscompile/fetch_test.go index 7f5d2af9..380cbaea 100644 --- a/internal/crosscompile/fetch_test.go +++ b/internal/crosscompile/fetch_test.go @@ -294,7 +294,7 @@ func TestDownloadAndExtractArchiveUnsupportedFormat(t *testing.T) { tempDir := t.TempDir() destDir := filepath.Join(tempDir, "extracted") - err := downloadAndExtractArchive(server.URL+"/test.zip", destDir, "Test Archive") + err := downloadAndExtractArchive(server.URL+"/test.7z", destDir, "Test Archive") if err == nil { t.Error("Expected error for unsupported format, got nil") }