Serge Bazanski | f369cfa | 2020-05-22 18:36:42 +0200 | [diff] [blame] | 1 | // 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 | |
| 17 | package main |
| 18 | |
| 19 | import ( |
| 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. |
| 31 | type 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. |
Serge Bazanski | 14cf750 | 2020-05-28 14:29:56 +0200 | [diff] [blame] | 47 | disableProtoBuild bool |
| 48 | forceBazelGeneration bool |
| 49 | buildTags []string |
| 50 | patches []string |
Lorenz Brun | efb028f | 2020-07-28 17:04:49 +0200 | [diff] [blame^] | 51 | prePatches []string |
Serge Bazanski | 14cf750 | 2020-05-28 14:29:56 +0200 | [diff] [blame] | 52 | buildExtraArgs []string |
| 53 | // replace is an importpath that this dependency will replace. If this is set, this dependency will be visible |
| 54 | // in the build as 'importpath', but downloaded at 'replace'/'version'. This might be slighly confusing, but |
| 55 | // follows the semantics of what Gazelle exposes via 'replace' in 'go_repository'. |
| 56 | replace string |
| 57 | } |
| 58 | |
| 59 | func (d *dependency) remoteImportpath() string { |
| 60 | if d.replace != "" { |
| 61 | return d.replace |
| 62 | } |
| 63 | return d.importpath |
Serge Bazanski | f369cfa | 2020-05-22 18:36:42 +0200 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | // locked is information about a dependency resolved from the go module system. It is expensive to get, and as such |
| 67 | // it is cached both in memory (as .locked in a dependency) and in the shelf. |
| 68 | type locked struct { |
| 69 | // bazelName is the external workspace name that Bazel should use for this dependency, eg. com_github_google_glog. |
| 70 | bazelName string |
| 71 | // sum is the gomod compatible checksum of the depdendency, egh1:4A07+ZFc2wgJwo8YNlQpr1rVlgUDlxXHhPJciaPY5gs=. |
| 72 | sum string |
| 73 | // semver is the gomod-compatible version of this dependency. If the dependency was requested by git hash that does |
| 74 | // not resolve to a particular release, this will be in the form of v0.0.0-20200520133742-deadbeefcafe. |
| 75 | semver string |
| 76 | } |
| 77 | |
| 78 | // child creates a new child dependence for this dependency, ie. one where the 'parent' pointer points to the dependency |
| 79 | // on which this method is called. |
| 80 | func (d *dependency) child(importpath, version string) *dependency { |
| 81 | return &dependency{ |
| 82 | importpath: importpath, |
| 83 | version: version, |
| 84 | shelf: d.shelf, |
| 85 | parent: d, |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | func (d *dependency) String() string { |
Serge Bazanski | 14cf750 | 2020-05-28 14:29:56 +0200 | [diff] [blame] | 90 | if d.replace != "" { |
| 91 | return fmt.Sprintf("%s@%s (replacing %s)", d.replace, d.version, d.importpath) |
| 92 | } |
Serge Bazanski | f369cfa | 2020-05-22 18:36:42 +0200 | [diff] [blame] | 93 | return fmt.Sprintf("%s@%s", d.importpath, d.version) |
| 94 | } |
| 95 | |
| 96 | // lock ensures that this dependency is locked, which means that it has been resolved to a particular, stable version |
| 97 | // and VCS details. We lock a dependency by either asking the go module subsystem (via a go module proxy or a download), |
| 98 | // or by consulting the shelf as a cache. |
| 99 | func (d *dependency) lock() error { |
| 100 | // If already locked in-memory, use that. |
| 101 | if d.locked != nil { |
| 102 | return nil |
| 103 | } |
| 104 | |
| 105 | // If already locked in the shelf, use that. |
Serge Bazanski | 14cf750 | 2020-05-28 14:29:56 +0200 | [diff] [blame] | 106 | if shelved := d.shelf.get(d.remoteImportpath(), d.version); shelved != nil { |
Serge Bazanski | f369cfa | 2020-05-22 18:36:42 +0200 | [diff] [blame] | 107 | d.locked = shelved |
| 108 | return nil |
| 109 | } |
| 110 | |
| 111 | // Otherwise, download module. |
| 112 | semver, _, sum, err := d.download() |
| 113 | if err != nil { |
| 114 | return fmt.Errorf("could not download: %v", err) |
| 115 | } |
| 116 | |
| 117 | // And resolve its bazelName. |
| 118 | name := label.ImportPathToBazelRepoName(d.importpath) |
| 119 | |
| 120 | // Hack for github.com/google/gvisor: it requests @com_github_opencontainers_runtime-spec. |
| 121 | // We fix the generated name for this repo so it conforms to what gvisor expects. |
| 122 | // TODO(q3k): instead of this, patch gvisor? |
| 123 | if name == "com_github_opencontainers_runtime_spec" { |
| 124 | name = "com_github_opencontainers_runtime-spec" |
| 125 | } |
| 126 | |
| 127 | d.locked = &locked{ |
| 128 | bazelName: name, |
| 129 | sum: sum, |
| 130 | semver: semver, |
| 131 | } |
| 132 | log.Printf("%s: locked to %s", d, d.locked) |
| 133 | |
| 134 | // Save locked version to shelf. |
Serge Bazanski | 14cf750 | 2020-05-28 14:29:56 +0200 | [diff] [blame] | 135 | d.shelf.put(d.remoteImportpath(), d.version, d.locked) |
Serge Bazanski | f369cfa | 2020-05-22 18:36:42 +0200 | [diff] [blame] | 136 | return d.shelf.save() |
| 137 | } |
| 138 | |
| 139 | func (l *locked) String() string { |
| 140 | return fmt.Sprintf("%s@%s", l.bazelName, l.sum) |
| 141 | } |
| 142 | |
| 143 | // download ensures that this dependency is download locally, and returns the download location and the dependency's |
| 144 | // gomod-compatible sum. |
| 145 | func (d *dependency) download() (version, dir, sum string, err error) { |
| 146 | goroot := os.Getenv("GOROOT") |
| 147 | if goroot == "" { |
| 148 | err = fmt.Errorf("GOROOT must be set") |
| 149 | return |
| 150 | } |
| 151 | goTool := filepath.Join(goroot, "bin", "go") |
| 152 | |
Serge Bazanski | 14cf750 | 2020-05-28 14:29:56 +0200 | [diff] [blame] | 153 | query := fmt.Sprintf("%s@%s", d.remoteImportpath(), d.version) |
Serge Bazanski | f369cfa | 2020-05-22 18:36:42 +0200 | [diff] [blame] | 154 | cmd := exec.Command(goTool, "mod", "download", "-json", "--", query) |
| 155 | out, err := cmd.Output() |
| 156 | if err != nil { |
| 157 | log.Printf("go mod returned: %q", out) |
| 158 | err = fmt.Errorf("go mod failed: %v", err) |
| 159 | return |
| 160 | } |
| 161 | |
| 162 | var res struct{ Version, Sum, Dir string } |
| 163 | err = json.Unmarshal(out, &res) |
| 164 | if err != nil { |
| 165 | return |
| 166 | } |
| 167 | |
| 168 | version = res.Version |
| 169 | dir = res.Dir |
| 170 | sum = res.Sum |
| 171 | return |
| 172 | } |