feat(crosscompile): add wasi-libc support with LLGoROOT priority

- Check LLGoROOT/crosscompile/wasi-libc first before downloading
- Fallback to cached wasi-sdk download if not found locally
- Update downloadAndExtract to return wasiSdkRoot path directly

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Li Jie
2025-08-02 20:18:37 +08:00
parent aa49fe665f
commit 3ac881b191
2 changed files with 24 additions and 17 deletions

View File

@@ -85,14 +85,21 @@ func Use(goos, goarch string, wasiThreads bool) (export Export, err error) {
// Configure based on GOOS // Configure based on GOOS
switch goos { switch goos {
case "wasip1": case "wasip1":
sdkDir := filepath.Join(cacheDir(), llvm.GetTargetTriple(goos, goarch)) // Set wasiSdkRoot path
if _, err = os.Stat(sdkDir); err != nil { llgoRoot := env.LLGoROOT()
if !errors.Is(err, fs.ErrNotExist) { wasiSdkRoot := filepath.Join(llgoRoot, "crosscompile", "wasi-libc")
return
}
if err = downloadAndExtract(wasiSdkUrl, sdkDir); err != nil { // If not exists in LLGoROOT, download and use cached wasiSdkRoot
return if _, err = os.Stat(wasiSdkRoot); err != nil {
sdkDir := filepath.Join(cacheDir(), llvm.GetTargetTriple(goos, goarch))
if _, err = os.Stat(sdkDir); err != nil {
if !errors.Is(err, fs.ErrNotExist) {
return
}
if wasiSdkRoot, err = downloadAndExtract(wasiSdkUrl, sdkDir); err != nil {
return
}
} }
} }
// WASI-SDK configuration // WASI-SDK configuration
@@ -101,8 +108,7 @@ func Use(goos, goarch string, wasiThreads bool) (export Export, err error) {
triple = "wasm32-wasip1-threads" triple = "wasm32-wasip1-threads"
} }
// Set up flags for the WASI-SDK // Set up flags for the WASI-SDK or wasi-libc
wasiSdkRoot := filepath.Join(sdkDir, "wasi-sdk-25.0-x86_64-macos")
sysrootDir := filepath.Join(wasiSdkRoot, "share", "wasi-sysroot") sysrootDir := filepath.Join(wasiSdkRoot, "share", "wasi-sysroot")
libclangDir := filepath.Join(wasiSdkRoot, "lib", "clang", "19") libclangDir := filepath.Join(wasiSdkRoot, "lib", "clang", "19")
includeDir := filepath.Join(sysrootDir, "include", triple) includeDir := filepath.Join(sysrootDir, "include", triple)

View File

@@ -11,36 +11,37 @@ import (
"strings" "strings"
) )
func downloadAndExtract(url, dir string) (err error) { func downloadAndExtract(url, dir string) (wasiSdkRoot string, err error) {
if _, err = os.Stat(dir); err == nil { if _, err = os.Stat(dir); err == nil {
os.RemoveAll(dir) os.RemoveAll(dir)
} }
tempDir := dir + ".temp" tempDir := dir + ".temp"
os.RemoveAll(tempDir) os.RemoveAll(tempDir)
if err = os.MkdirAll(tempDir, 0755); err != nil { if err := os.MkdirAll(tempDir, 0755); err != nil {
return fmt.Errorf("failed to create temporary directory: %w", err) return "", fmt.Errorf("failed to create temporary directory: %w", err)
} }
urlPath := strings.Split(url, "/") urlPath := strings.Split(url, "/")
filename := urlPath[len(urlPath)-1] filename := urlPath[len(urlPath)-1]
localFile := filepath.Join(tempDir, filename) localFile := filepath.Join(tempDir, filename)
if err = downloadFile(url, localFile); err != nil { if err = downloadFile(url, localFile); err != nil {
return fmt.Errorf("failed to download file: %w", err) return "", fmt.Errorf("failed to download file: %w", err)
} }
defer os.Remove(localFile) defer os.Remove(localFile)
if strings.HasSuffix(filename, ".tar.gz") || strings.HasSuffix(filename, ".tgz") { if strings.HasSuffix(filename, ".tar.gz") || strings.HasSuffix(filename, ".tgz") {
err = extractTarGz(localFile, tempDir) err = extractTarGz(localFile, tempDir)
} else { } else {
return fmt.Errorf("unsupported archive format: %s", filename) return "", fmt.Errorf("unsupported archive format: %s", filename)
} }
if err != nil { if err != nil {
return fmt.Errorf("failed to extract archive: %w", err) return "", fmt.Errorf("failed to extract archive: %w", err)
} }
if err = os.Rename(tempDir, dir); err != nil { if err = os.Rename(tempDir, dir); err != nil {
return fmt.Errorf("failed to rename directory: %w", err) return "", fmt.Errorf("failed to rename directory: %w", err)
} }
return nil wasiSdkRoot = filepath.Join(dir, "wasi-sdk-25.0-x86_64-macos")
return
} }
func downloadFile(url, filepath string) error { func downloadFile(url, filepath string) error {