// Command genlicenses writes THIRD-PARTY-LICENSES.md, the licenses and // notices of the third-party software in the uberbringer binary, and the copy // of it that the binary embeds. That software is the Go standard library and // runtime, and every module ./cmd/uberbringer links on the platforms it is // released for. A module's texts are its license and notice files in the // directories of its linked packages and their parents, up to the module // root, so code that a module vendors under a license of its own is covered // too. Modules used only by tools and tests are not linked, so they are left // out. Nothing in the file is written by hand, and a test fails when a // committed copy differs from what this produces. // // go run ./tools/genlicenses (or: go generate ./...) package main import ( "bytes" "errors" "fmt" "os" "os/exec" "path/filepath" "strings" ) // The generated files, relative to the repository root. go:embed cannot // reach above the embedding package's directory, so the embedded copy sits // beside that package. const ( noticesPath = "THIRD-PARTY-LICENSES.md" embeddedPath = "internal/app/uberbringer/licenses_embed.md" ) func main() { if err := run(); err != nil { fmt.Fprintln(os.Stderr, "genlicenses:", err) os.Exit(1) } } func run() error { root, err := moduleRoot() if err != nil { return err } out, err := generate(root) if err != nil { return err } for _, rel := range []string{noticesPath, embeddedPath} { if err := writeIfChanged(root, rel, out); err != nil { return err } } return nil } // generate returns the notices for the binary built from the module at root. func generate(root string) ([]byte, error) { goroot, err := goOutput(root, nil, "env", "GOROOT") if err != nil { return nil, err } goroot = strings.TrimSpace(goroot) goFiles, err := licenseFiles(goroot) if err != nil { return nil, err } goComponent, err := readComponent("Go standard library and runtime", "", goroot, goFiles) if err != nil { return nil, err } components := []component{goComponent} modules, err := linkedModules(root) if err != nil { return nil, err } for _, m := range modules { c, err := readComponent(m.path, m.version, m.dir, m.files) if err != nil { return nil, err } components = append(components, c) } return render(components), nil } // moduleRoot returns the directory of the main module, which holds go.mod. func moduleRoot() (string, error) { out, err := goOutput("", nil, "list", "-m", "-f", "{{.Dir}}") if err != nil { return "", err } return strings.TrimSpace(out), nil } // goOutput runs the go command in dir, with env added to the environment, // and returns what it writes to standard output. func goOutput(dir string, env []string, args ...string) (string, error) { cmd := exec.Command("go", args...) cmd.Dir = dir cmd.Env = append(os.Environ(), env...) var stderr bytes.Buffer cmd.Stderr = &stderr out, err := cmd.Output() if err != nil { return "", fmt.Errorf("go %s: %w\n%s", strings.Join(args, " "), err, stderr.Bytes()) } return string(out), nil } // writeIfChanged writes out to the file rel under root unless it already // holds out, and says which it did. func writeIfChanged(root, rel string, out []byte) error { path := filepath.Join(root, filepath.FromSlash(rel)) current, err := os.ReadFile(path) if err != nil && !errors.Is(err, os.ErrNotExist) { return err } if bytes.Equal(current, out) { fmt.Printf("genlicenses: %s is up to date\n", rel) return nil } if err := os.WriteFile(path, out, 0o644); err != nil { return err } fmt.Printf("genlicenses: wrote %s\n", rel) return nil }