diff --git a/internal/crosscompile/crosscompile.go b/internal/crosscompile/crosscompile.go index d1befdb5..a1b4da87 100644 --- a/internal/crosscompile/crosscompile.go +++ b/internal/crosscompile/crosscompile.go @@ -5,6 +5,7 @@ import ( "fmt" "io/fs" "os" + "os/exec" "path/filepath" "runtime" "strings" @@ -44,6 +45,16 @@ func cacheDir() string { return filepath.Join(env.LLGoCacheDir(), "crosscompile") } +// getMacOSSysroot returns the macOS SDK path using xcrun +func getMacOSSysroot() (string, error) { + cmd := exec.Command("xcrun", "--sdk", "macosx", "--show-sdk-path") + output, err := cmd.Output() + if err != nil { + return "", err + } + return strings.TrimSpace(string(output)), nil +} + // getESPClangRoot returns the ESP Clang root directory, checking LLGoROOT first, // then downloading if needed and platform is supported func getESPClangRoot() (clangRoot string, err error) { @@ -118,6 +129,16 @@ func use(goos, goarch string, wasiThreads bool) (export Export, err error) { "-fuse-ld=lld", } + // Add sysroot for macOS only + if goos == "darwin" { + sysrootPath, sysrootErr := getMacOSSysroot() + if sysrootErr != nil { + err = fmt.Errorf("failed to get macOS SDK path: %w", sysrootErr) + return + } + export.LDFLAGS = append([]string{"--sysroot=" + sysrootPath}, export.LDFLAGS...) + } + // Add OS-specific flags switch goos { case "darwin": // ld64.lld (macOS)