55 lines
1.9 KiB
Go
55 lines
1.9 KiB
Go
// 测试 pack 的固定目标源码选择器;产品只运行签名 pack 提供的对应工具。
|
|
package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"go/build"
|
|
"io/fs"
|
|
"os"
|
|
"path/filepath"
|
|
"sort"
|
|
"strings"
|
|
)
|
|
|
|
func main() {
|
|
if err := selectPackage(); err != nil { fmt.Fprintln(os.Stderr, err); os.Exit(1) }
|
|
}
|
|
|
|
func selectPackage() error {
|
|
if len(os.Args) != 4 { return fmt.Errorf("expected OS architecture directory") }
|
|
context := build.Default
|
|
context.GOOS, context.GOARCH = os.Args[1], os.Args[2]
|
|
context.GOROOT, context.GOPATH = "", ""
|
|
context.CgoEnabled = true
|
|
directory := os.Args[3]
|
|
selected, err := context.ImportDir(directory, 0)
|
|
if err != nil { return err }
|
|
if len(selected.CgoFiles)+len(selected.CFiles)+len(selected.CXXFiles)+len(selected.SFiles)+len(selected.SysoFiles) != 0 {
|
|
return fmt.Errorf("cgo/assembly/native objects are unsupported")
|
|
}
|
|
embed := map[string][]string{}
|
|
for _, pattern := range selected.EmbedPatterns {
|
|
glob := strings.TrimPrefix(pattern, "all:")
|
|
if filepath.IsAbs(glob) || strings.Contains(glob, "..") { return fmt.Errorf("invalid embed pattern") }
|
|
matches, err := filepath.Glob(filepath.Join(directory, glob))
|
|
if err != nil || len(matches) == 0 { return fmt.Errorf("unmatched embed pattern") }
|
|
for _, match := range matches {
|
|
err = filepath.WalkDir(match, func(path string, entry fs.DirEntry, failure error) error {
|
|
if failure != nil { return failure }
|
|
if entry.Type()&os.ModeSymlink != 0 { return fmt.Errorf("embed link") }
|
|
if entry.IsDir() { return nil }
|
|
relative, err := filepath.Rel(directory, path)
|
|
if err != nil { return err }
|
|
embed[pattern] = append(embed[pattern], filepath.ToSlash(relative))
|
|
return nil
|
|
})
|
|
if err != nil { return err }
|
|
}
|
|
sort.Strings(embed[pattern])
|
|
}
|
|
imports := selected.Imports
|
|
if imports == nil { imports = []string{} }
|
|
return json.NewEncoder(os.Stdout).Encode(map[string]any{"name":selected.Name,"files":selected.GoFiles,"imports":imports,"embed":embed})
|
|
}
|