blob: c6100e44ee48bbcdd70c063cf69484be1c5eab28 [file] [log] [blame]
Serge Bazanskif369cfa2020-05-22 18:36:42 +02001// Copyright 2020 The Monogon Project Authors.
2//
3// SPDX-License-Identifier: Apache-2.0
4//
5// Licensed under the Apache License, Version 2.0 (the "License");
6// you may not use this file except in compliance with the License.
7// You may obtain a copy of the License at
8//
9// http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing, software
12// distributed under the License is distributed on an "AS IS" BASIS,
13// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14// See the License for the specific language governing permissions and
15// limitations under the License.
16
17package main
18
19import (
20 "encoding/json"
21 "fmt"
22 "log"
23 "os"
24 "os/exec"
25 "path/filepath"
26
27 "github.com/bazelbuild/bazel-gazelle/label"
28)
29
30// dependency is an external Go package/module, requested by the user of Fietsje directly or indirectly.
31type dependency struct {
32 // importpath is the Go import path that was used to import this dependency.
33 importpath string
34 // version at which this dependency has been requested. This can be in any form that `go get` or the go module
35 // system understands.
36 version string
37
38 // locked is the 'resolved' version of a dependency, containing information about the dependency's hash, etc.
39 locked *locked
40
41 // parent is the dependency that pulled in this one, or nil if pulled in by the user.
42 parent *dependency
43
44 shelf *shelf
45
46 // Build specific settings passed to gazelle.
47 disableProtoBuild bool
48 buildTags []string
49 patches []string
50}
51
52// locked is information about a dependency resolved from the go module system. It is expensive to get, and as such
53// it is cached both in memory (as .locked in a dependency) and in the shelf.
54type locked struct {
55 // bazelName is the external workspace name that Bazel should use for this dependency, eg. com_github_google_glog.
56 bazelName string
57 // sum is the gomod compatible checksum of the depdendency, egh1:4A07+ZFc2wgJwo8YNlQpr1rVlgUDlxXHhPJciaPY5gs=.
58 sum string
59 // semver is the gomod-compatible version of this dependency. If the dependency was requested by git hash that does
60 // not resolve to a particular release, this will be in the form of v0.0.0-20200520133742-deadbeefcafe.
61 semver string
62}
63
64// child creates a new child dependence for this dependency, ie. one where the 'parent' pointer points to the dependency
65// on which this method is called.
66func (d *dependency) child(importpath, version string) *dependency {
67 return &dependency{
68 importpath: importpath,
69 version: version,
70 shelf: d.shelf,
71 parent: d,
72 }
73}
74
75func (d *dependency) String() string {
76 return fmt.Sprintf("%s@%s", d.importpath, d.version)
77}
78
79// lock ensures that this dependency is locked, which means that it has been resolved to a particular, stable version
80// and VCS details. We lock a dependency by either asking the go module subsystem (via a go module proxy or a download),
81// or by consulting the shelf as a cache.
82func (d *dependency) lock() error {
83 // If already locked in-memory, use that.
84 if d.locked != nil {
85 return nil
86 }
87
88 // If already locked in the shelf, use that.
89 if shelved := d.shelf.get(d.importpath, d.version); shelved != nil {
90 d.locked = shelved
91 return nil
92 }
93
94 // Otherwise, download module.
95 semver, _, sum, err := d.download()
96 if err != nil {
97 return fmt.Errorf("could not download: %v", err)
98 }
99
100 // And resolve its bazelName.
101 name := label.ImportPathToBazelRepoName(d.importpath)
102
103 // Hack for github.com/google/gvisor: it requests @com_github_opencontainers_runtime-spec.
104 // We fix the generated name for this repo so it conforms to what gvisor expects.
105 // TODO(q3k): instead of this, patch gvisor?
106 if name == "com_github_opencontainers_runtime_spec" {
107 name = "com_github_opencontainers_runtime-spec"
108 }
109
110 d.locked = &locked{
111 bazelName: name,
112 sum: sum,
113 semver: semver,
114 }
115 log.Printf("%s: locked to %s", d, d.locked)
116
117 // Save locked version to shelf.
118 d.shelf.put(d.importpath, d.version, d.locked)
119 return d.shelf.save()
120}
121
122func (l *locked) String() string {
123 return fmt.Sprintf("%s@%s", l.bazelName, l.sum)
124}
125
126// download ensures that this dependency is download locally, and returns the download location and the dependency's
127// gomod-compatible sum.
128func (d *dependency) download() (version, dir, sum string, err error) {
129 goroot := os.Getenv("GOROOT")
130 if goroot == "" {
131 err = fmt.Errorf("GOROOT must be set")
132 return
133 }
134 goTool := filepath.Join(goroot, "bin", "go")
135
136 query := fmt.Sprintf("%s@%s", d.importpath, d.version)
137 cmd := exec.Command(goTool, "mod", "download", "-json", "--", query)
138 out, err := cmd.Output()
139 if err != nil {
140 log.Printf("go mod returned: %q", out)
141 err = fmt.Errorf("go mod failed: %v", err)
142 return
143 }
144
145 var res struct{ Version, Sum, Dir string }
146 err = json.Unmarshal(out, &res)
147 if err != nil {
148 return
149 }
150
151 version = res.Version
152 dir = res.Dir
153 sum = res.Sum
154 return
155}