2019-12-12 04:23:09 +01:00
|
|
|
|
// Copyright 2019 Google LLC.
|
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
|
|
|
|
|
// This tool analyses external (i.e. not built with `buildGo.nix`) Go
|
|
|
|
|
// packages to determine a build plan that Nix can import.
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"encoding/json"
|
|
|
|
|
"flag"
|
|
|
|
|
"fmt"
|
|
|
|
|
"go/build"
|
|
|
|
|
"log"
|
|
|
|
|
"os"
|
2019-12-12 04:46:28 +01:00
|
|
|
|
"path"
|
2019-12-12 04:23:09 +01:00
|
|
|
|
"path/filepath"
|
|
|
|
|
"strings"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// Path to a JSON file describing all standard library import paths.
|
|
|
|
|
// This file is generated and set here by Nix during the build
|
|
|
|
|
// process.
|
|
|
|
|
var stdlibList string
|
|
|
|
|
|
|
|
|
|
// pkg describes a single Go package within the specified source
|
|
|
|
|
// directory.
|
|
|
|
|
//
|
|
|
|
|
// Return information includes the local (relative from project root)
|
|
|
|
|
// and external (none-stdlib) dependencies of this package.
|
|
|
|
|
type pkg struct {
|
2021-04-05 11:41:37 +02:00
|
|
|
|
Name string `json:"name"`
|
|
|
|
|
Locator []string `json:"locator"`
|
|
|
|
|
Files []string `json:"files"`
|
|
|
|
|
SFiles []string `json:"sfiles"`
|
|
|
|
|
LocalDeps [][]string `json:"localDeps"`
|
|
|
|
|
ForeignDeps []foreignDep `json:"foreignDeps"`
|
|
|
|
|
IsCommand bool `json:"isCommand"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type foreignDep struct {
|
|
|
|
|
Path string `json:"path"`
|
|
|
|
|
// filename, column and line number of the import, if known
|
|
|
|
|
Position string `json:"position"`
|
2019-12-12 04:23:09 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// findGoDirs returns a filepath.WalkFunc that identifies all
|
|
|
|
|
// directories that contain Go source code in a certain tree.
|
|
|
|
|
func findGoDirs(at string) ([]string, error) {
|
2019-12-12 17:33:10 +01:00
|
|
|
|
dirSet := make(map[string]bool)
|
2019-12-12 04:23:09 +01:00
|
|
|
|
|
|
|
|
|
err := filepath.Walk(at, func(path string, info os.FileInfo, err error) error {
|
2020-05-26 00:24:57 +02:00
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-13 00:53:39 +01:00
|
|
|
|
name := info.Name()
|
2019-12-12 17:33:10 +01:00
|
|
|
|
// Skip folders that are guaranteed to not be relevant
|
2019-12-13 00:53:39 +01:00
|
|
|
|
if info.IsDir() && (name == "testdata" || name == ".git") {
|
2019-12-12 04:23:09 +01:00
|
|
|
|
return filepath.SkipDir
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// If the current file is a Go file, then the directory is popped
|
|
|
|
|
// (i.e. marked as a Go directory).
|
2019-12-13 00:53:39 +01:00
|
|
|
|
if !info.IsDir() && strings.HasSuffix(name, ".go") && !strings.HasSuffix(name, "_test.go") {
|
2019-12-12 17:33:10 +01:00
|
|
|
|
dirSet[filepath.Dir(path)] = true
|
2019-12-12 04:23:09 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-12 17:33:10 +01:00
|
|
|
|
goDirs := []string{}
|
2024-04-18 19:54:22 +02:00
|
|
|
|
for goDir := range dirSet {
|
|
|
|
|
goDirs = append(goDirs, goDir)
|
2019-12-12 17:33:10 +01:00
|
|
|
|
}
|
|
|
|
|
|
2019-12-12 04:23:09 +01:00
|
|
|
|
return goDirs, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// analysePackage loads and analyses the imports of a single Go
|
|
|
|
|
// package, returning the data that is required by the Nix code to
|
|
|
|
|
// generate a derivation for this package.
|
2019-12-12 04:46:28 +01:00
|
|
|
|
func analysePackage(root, source, importpath string, stdlib map[string]bool) (pkg, error) {
|
2019-12-12 04:23:09 +01:00
|
|
|
|
ctx := build.Default
|
2019-12-13 16:15:53 +01:00
|
|
|
|
ctx.CgoEnabled = false
|
2019-12-12 04:23:09 +01:00
|
|
|
|
|
|
|
|
|
p, err := ctx.ImportDir(source, build.IgnoreVendor)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return pkg{}, err
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-12 04:46:28 +01:00
|
|
|
|
local := [][]string{}
|
2021-04-05 11:41:37 +02:00
|
|
|
|
foreign := []foreignDep{}
|
2019-12-12 04:23:09 +01:00
|
|
|
|
|
|
|
|
|
for _, i := range p.Imports {
|
|
|
|
|
if stdlib[i] {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-13 13:10:32 +01:00
|
|
|
|
if i == importpath {
|
|
|
|
|
local = append(local, []string{})
|
2020-06-03 05:42:59 +02:00
|
|
|
|
} else if strings.HasPrefix(i, importpath+"/") {
|
2019-12-12 04:46:28 +01:00
|
|
|
|
local = append(local, strings.Split(strings.TrimPrefix(i, importpath+"/"), "/"))
|
2019-12-12 04:23:09 +01:00
|
|
|
|
} else {
|
2021-04-05 11:41:37 +02:00
|
|
|
|
// The import positions is a map keyed on the import name.
|
|
|
|
|
// The value is a list, presumably because an import can appear
|
|
|
|
|
// multiple times in a package. Let’s just take the first one,
|
|
|
|
|
// should be enough for a good error message.
|
|
|
|
|
firstPos := p.ImportPos[i][0].String()
|
|
|
|
|
foreign = append(foreign, foreignDep{Path: i, Position: firstPos})
|
2019-12-12 04:23:09 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-12 04:46:28 +01:00
|
|
|
|
prefix := strings.TrimPrefix(source, root+"/")
|
2019-12-12 18:04:40 +01:00
|
|
|
|
|
2019-12-12 23:58:51 +01:00
|
|
|
|
locator := []string{}
|
2019-12-12 18:04:40 +01:00
|
|
|
|
if len(prefix) != len(source) {
|
2019-12-12 23:58:51 +01:00
|
|
|
|
locator = strings.Split(prefix, "/")
|
2019-12-12 18:04:40 +01:00
|
|
|
|
} else {
|
2019-12-12 23:58:51 +01:00
|
|
|
|
// Otherwise, the locator is empty since its the root package and
|
|
|
|
|
// no prefix should be added to files.
|
2019-12-12 18:04:40 +01:00
|
|
|
|
prefix = ""
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-12 04:46:28 +01:00
|
|
|
|
files := []string{}
|
|
|
|
|
for _, f := range p.GoFiles {
|
|
|
|
|
files = append(files, path.Join(prefix, f))
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-13 16:15:53 +01:00
|
|
|
|
sfiles := []string{}
|
|
|
|
|
for _, f := range p.SFiles {
|
|
|
|
|
sfiles = append(sfiles, path.Join(prefix, f))
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-12 18:04:40 +01:00
|
|
|
|
return pkg{
|
2019-12-12 23:58:51 +01:00
|
|
|
|
Name: path.Join(importpath, prefix),
|
|
|
|
|
Locator: locator,
|
2019-12-12 04:46:28 +01:00
|
|
|
|
Files: files,
|
2019-12-13 16:15:53 +01:00
|
|
|
|
SFiles: sfiles,
|
2019-12-12 04:23:09 +01:00
|
|
|
|
LocalDeps: local,
|
|
|
|
|
ForeignDeps: foreign,
|
2019-12-13 00:26:08 +01:00
|
|
|
|
IsCommand: p.IsCommand(),
|
2019-12-12 18:04:40 +01:00
|
|
|
|
}, nil
|
2019-12-12 04:23:09 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func loadStdlibPkgs(from string) (pkgs map[string]bool, err error) {
|
2024-04-18 19:54:22 +02:00
|
|
|
|
f, err := os.ReadFile(from)
|
2019-12-12 04:23:09 +01:00
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err = json.Unmarshal(f, &pkgs)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func main() {
|
2019-12-12 04:46:28 +01:00
|
|
|
|
source := flag.String("source", "", "path to directory with sources to process")
|
|
|
|
|
path := flag.String("path", "", "import path for the package")
|
2019-12-12 04:23:09 +01:00
|
|
|
|
|
|
|
|
|
flag.Parse()
|
|
|
|
|
|
|
|
|
|
if *source == "" {
|
|
|
|
|
log.Fatalf("-source flag must be specified")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
stdlibPkgs, err := loadStdlibPkgs(stdlibList)
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Fatalf("failed to load standard library index from %q: %s\n", stdlibList, err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
goDirs, err := findGoDirs(*source)
|
|
|
|
|
if err != nil {
|
2020-04-13 16:47:07 +02:00
|
|
|
|
log.Fatalf("failed to walk source directory '%s': %s", *source, err)
|
2019-12-12 04:23:09 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
all := []pkg{}
|
|
|
|
|
for _, d := range goDirs {
|
|
|
|
|
analysed, err := analysePackage(*source, d, *path, stdlibPkgs)
|
2019-12-13 00:53:39 +01:00
|
|
|
|
|
|
|
|
|
// If the Go source analysis returned "no buildable Go files",
|
|
|
|
|
// that directory should be skipped.
|
|
|
|
|
//
|
|
|
|
|
// This might be due to `+build` flags on the platform and other
|
|
|
|
|
// reasons (such as test files).
|
|
|
|
|
if _, ok := err.(*build.NoGoError); ok {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-12 04:23:09 +01:00
|
|
|
|
if err != nil {
|
|
|
|
|
log.Fatalf("failed to analyse package at %q: %s", d, err)
|
|
|
|
|
}
|
|
|
|
|
all = append(all, analysed)
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-12 04:46:28 +01:00
|
|
|
|
j, _ := json.Marshal(all)
|
2019-12-12 04:23:09 +01:00
|
|
|
|
fmt.Println(string(j))
|
|
|
|
|
}
|