fix sysroot on macos with esp clang

This commit is contained in:
Li Jie
2025-08-16 10:46:44 +08:00
parent 87f4f618aa
commit e580ecc55c

View File

@@ -5,6 +5,7 @@ import (
"fmt" "fmt"
"io/fs" "io/fs"
"os" "os"
"os/exec"
"path/filepath" "path/filepath"
"runtime" "runtime"
"strings" "strings"
@@ -44,6 +45,16 @@ func cacheDir() string {
return filepath.Join(env.LLGoCacheDir(), "crosscompile") 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, // getESPClangRoot returns the ESP Clang root directory, checking LLGoROOT first,
// then downloading if needed and platform is supported // then downloading if needed and platform is supported
func getESPClangRoot() (clangRoot string, err error) { func getESPClangRoot() (clangRoot string, err error) {
@@ -118,6 +129,16 @@ func use(goos, goarch string, wasiThreads bool) (export Export, err error) {
"-fuse-ld=lld", "-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 // Add OS-specific flags
switch goos { switch goos {
case "darwin": // ld64.lld (macOS) case "darwin": // ld64.lld (macOS)