feat: support libc for small devices
This commit is contained in:
@@ -2,6 +2,7 @@ package crosscompile
|
||||
|
||||
import (
|
||||
"archive/tar"
|
||||
"archive/zip"
|
||||
"compress/gzip"
|
||||
"fmt"
|
||||
"io"
|
||||
@@ -79,9 +80,9 @@ func checkDownloadAndExtractESPClang(platformSuffix, dir string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func checkDownloadAndExtractLibc(url, dstDir, internalArchiveSrcDir string) error {
|
||||
func checkDownloadAndExtractLibc(cfg *compileLibcConfig, url, dstDir, internalArchiveSrcDir string) error {
|
||||
// Check if already exists
|
||||
if _, err := os.Stat(dstDir); err == nil {
|
||||
if cfg.IsCompiled(dstDir) {
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -94,9 +95,10 @@ func checkDownloadAndExtractLibc(url, dstDir, internalArchiveSrcDir string) erro
|
||||
defer releaseLock(lockFile)
|
||||
|
||||
// Double-check after acquiring lock
|
||||
if _, err := os.Stat(dstDir); err == nil {
|
||||
if cfg.IsCompiled(dstDir) {
|
||||
return nil
|
||||
}
|
||||
fmt.Fprintf(os.Stderr, "%s not found in LLGO_ROOT or cache, will download and compile.\n", dstDir)
|
||||
|
||||
description := fmt.Sprintf("Libc %s", path.Base(url))
|
||||
|
||||
@@ -105,7 +107,7 @@ func checkDownloadAndExtractLibc(url, dstDir, internalArchiveSrcDir string) erro
|
||||
if err := downloadAndExtractArchive(url, tempExtractDir, description); err != nil {
|
||||
return err
|
||||
}
|
||||
defer os.RemoveAll(tempExtractDir)
|
||||
// defer os.RemoveAll(tempExtractDir)
|
||||
|
||||
srcDir := tempExtractDir
|
||||
|
||||
@@ -182,10 +184,14 @@ func downloadAndExtractArchive(url, destDir, description string) error {
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to extract %s archive: %w", description, err)
|
||||
}
|
||||
} else if strings.HasSuffix(filename, ".zip") {
|
||||
err := extractZip(localFile, tempDir)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to extract %s archive: %w", description, err)
|
||||
}
|
||||
} else {
|
||||
return fmt.Errorf("unsupported archive format: %s", filename)
|
||||
}
|
||||
|
||||
// Rename temp directory to target directory
|
||||
if err := os.Rename(tempDir, destDir); err != nil {
|
||||
return fmt.Errorf("failed to rename directory: %w", err)
|
||||
@@ -265,3 +271,44 @@ func extractTarXz(tarXzFile, dest string) error {
|
||||
cmd := exec.Command("tar", "-xf", tarXzFile, "-C", dest)
|
||||
return cmd.Run()
|
||||
}
|
||||
|
||||
func extractZip(zipFile, dest string) error {
|
||||
r, err := zip.OpenReader(zipFile)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer r.Close()
|
||||
decompress := func(file *zip.File) error {
|
||||
path := filepath.Join(dest, file.Name)
|
||||
|
||||
if file.FileInfo().IsDir() {
|
||||
return os.MkdirAll(path, 0700)
|
||||
}
|
||||
|
||||
fs, err := file.Open()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer fs.Close()
|
||||
|
||||
w, err := os.Create(path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err := io.Copy(w, fs); err != nil {
|
||||
w.Close()
|
||||
return err
|
||||
}
|
||||
if err := w.Close(); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
for _, file := range r.File {
|
||||
if err = decompress(file); err != nil {
|
||||
break
|
||||
}
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user