|
| 1 | +// Program precompile updates pre-built standard library packages for the |
| 2 | +// playground. |
| 3 | +// |
| 4 | +// This script performs the following sequence of steps: |
| 5 | +// |
| 6 | +// - Enumerate all standard packages that should be available in the playground. |
| 7 | +// - Precompile them, including transitive dependencies. |
| 8 | +// - Delete all old precompiled archive. |
| 9 | +// - Write all new precompiled archive in their place. |
| 10 | +// |
| 11 | +// This will use the same GopherJS version as specified in the playground gm.mod |
| 12 | +// to ensure consistency. The script uses GopherJS compiler API directly, so |
| 13 | +// it doesn't require the GopherJS tool to be installed. |
| 14 | +package main |
| 15 | + |
| 16 | +import ( |
| 17 | + "flag" |
| 18 | + "fmt" |
| 19 | + gobuild "go/build" |
| 20 | + "os" |
| 21 | + "path/filepath" |
| 22 | + "strings" |
| 23 | + |
| 24 | + "github.com/gopherjs/gopherjs/build" |
| 25 | + "github.com/gopherjs/gopherjs/compiler" |
| 26 | + log "github.com/sirupsen/logrus" |
| 27 | +) |
| 28 | + |
| 29 | +type logLevelFlag struct{ log.Level } |
| 30 | + |
| 31 | +func (l *logLevelFlag) Set(raw string) error { return l.UnmarshalText([]byte(raw)) } |
| 32 | + |
| 33 | +var ( |
| 34 | + logLevel logLevelFlag = logLevelFlag{Level: log.ErrorLevel} |
| 35 | +) |
| 36 | + |
| 37 | +func init() { |
| 38 | + flag.Var(&logLevel, "log_level", "Default logging level.") |
| 39 | +} |
| 40 | + |
| 41 | +func run() error { |
| 42 | + s, err := build.NewSession(&build.Options{ |
| 43 | + Verbose: true, |
| 44 | + Minify: true, |
| 45 | + NoCache: true, |
| 46 | + }) |
| 47 | + if err != nil { |
| 48 | + return fmt.Errorf("failed to create a build session: %w", err) |
| 49 | + } |
| 50 | + |
| 51 | + packages, err := s.XContext().Match([]string{"std"}) |
| 52 | + if err != nil { |
| 53 | + return fmt.Errorf("failed to enumerate standard library packages") |
| 54 | + } |
| 55 | + packages = importable(packages) |
| 56 | + packages = append(packages, "github.com/gopherjs/gopherjs/js", "github.com/gopherjs/gopherjs/nosync") |
| 57 | + |
| 58 | + for _, pkg := range packages { |
| 59 | + _, err := s.BuildImportPath(pkg) |
| 60 | + if err != nil { |
| 61 | + return fmt.Errorf("failed to precompile package %q: %w", pkg, err) |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + target, err := targetDir(s) |
| 66 | + if err := os.RemoveAll(target); err != nil { |
| 67 | + return fmt.Errorf("failed to clean out old precompiled archives: %w", err) |
| 68 | + } |
| 69 | + |
| 70 | + for _, archive := range s.UpToDateArchives { |
| 71 | + if err := writeArchive(target, archive); err != nil { |
| 72 | + return fmt.Errorf("failed to write package %q archive: %w", archive.ImportPath, err) |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + return nil |
| 77 | +} |
| 78 | + |
| 79 | +func writeArchive(target string, archive *compiler.Archive) error { |
| 80 | + path := filepath.Join(target, filepath.FromSlash(archive.ImportPath)+".a.js") |
| 81 | + if err := os.MkdirAll(filepath.Dir(path), 0755); err != nil { |
| 82 | + return fmt.Errorf("failed to create precompiled package directory %q: %w", filepath.Dir(path), err) |
| 83 | + } |
| 84 | + f, err := os.Create(path) |
| 85 | + if err != nil { |
| 86 | + return fmt.Errorf("failed to create precompiled archive %q: %w", path, err) |
| 87 | + } |
| 88 | + defer f.Close() |
| 89 | + |
| 90 | + return compiler.WriteArchive(archive, f) |
| 91 | +} |
| 92 | + |
| 93 | +// targetDir returns path to the directory where precompiled packages must be |
| 94 | +// stored. |
| 95 | +func targetDir(s *build.Session) (string, error) { |
| 96 | + pkg, err := s.XContext().Import("github.com/gopherjs/gopherjs.github.io/playground", "", gobuild.FindOnly) |
| 97 | + if err != nil { |
| 98 | + return "", fmt.Errorf("failed to find playground package directory: %w", err) |
| 99 | + } |
| 100 | + target := filepath.Join(pkg.Dir, "pkg") |
| 101 | + if _, err := os.Stat(target); os.IsNotExist(err) { |
| 102 | + return "", fmt.Errorf("target directory %q not found", target) |
| 103 | + } |
| 104 | + return target, nil |
| 105 | +} |
| 106 | + |
| 107 | +// importable excludes packages that are incompatible with GopherJS or can't be |
| 108 | +// directly imported by the user code. The remaining packages will be used as a |
| 109 | +// starting points for precompilation. |
| 110 | +func importable(all []string) []string { |
| 111 | + result := []string{} |
| 112 | + for _, pkg := range all { |
| 113 | + switch { |
| 114 | + case strings.HasPrefix(pkg, "vendor/"), |
| 115 | + strings.Contains(pkg, "internal"), |
| 116 | + strings.Contains(pkg, "pprof"), |
| 117 | + strings.Contains(pkg, "plugin"): |
| 118 | + continue |
| 119 | + default: |
| 120 | + result = append(result, pkg) |
| 121 | + } |
| 122 | + } |
| 123 | + return result |
| 124 | +} |
| 125 | + |
| 126 | +func main() { |
| 127 | + flag.Parse() |
| 128 | + log.SetLevel(logLevel.Level) |
| 129 | + if err := run(); err != nil { |
| 130 | + log.Fatalf("Precompilation failed: %v", err) |
| 131 | + } |
| 132 | +} |
0 commit comments