blob: 6b75000a0c8a99dfbfa73d16a2ce56caca78702a [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.
Serge Bazanski14cf7502020-05-28 14:29:56 +020047 disableProtoBuild bool
48 forceBazelGeneration bool
49 buildTags []string
50 patches []string
Lorenz Brunefb028f2020-07-28 17:04:49 +020051 prePatches []string
Serge Bazanski14cf7502020-05-28 14:29:56 +020052 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
59func (d *dependency) remoteImportpath() string {
60 if d.replace != "" {
61 return d.replace
62 }
63 return d.importpath
Serge Bazanskif369cfa2020-05-22 18:36:42 +020064}
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.
68type 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.
80func (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
89func (d *dependency) String() string {
Serge Bazanski14cf7502020-05-28 14:29:56 +020090 if d.replace != "" {
91 return fmt.Sprintf("%s@%s (replacing %s)", d.replace, d.version, d.importpath)
92 }
Serge Bazanskif369cfa2020-05-22 18:36:42 +020093 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.
99func (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 Bazanski14cf7502020-05-28 14:29:56 +0200106 if shelved := d.shelf.get(d.remoteImportpath(), d.version); shelved != nil {
Serge Bazanskif369cfa2020-05-22 18:36:42 +0200107 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
Serge Bazanskif369cfa2020-05-22 18:36:42 +0200120 d.locked = &locked{
121 bazelName: name,
122 sum: sum,
123 semver: semver,
124 }
125 log.Printf("%s: locked to %s", d, d.locked)
126
127 // Save locked version to shelf.
Serge Bazanski14cf7502020-05-28 14:29:56 +0200128 d.shelf.put(d.remoteImportpath(), d.version, d.locked)
Serge Bazanskif369cfa2020-05-22 18:36:42 +0200129 return d.shelf.save()
130}
131
132func (l *locked) String() string {
133 return fmt.Sprintf("%s@%s", l.bazelName, l.sum)
134}
135
136// download ensures that this dependency is download locally, and returns the download location and the dependency's
137// gomod-compatible sum.
138func (d *dependency) download() (version, dir, sum string, err error) {
139 goroot := os.Getenv("GOROOT")
140 if goroot == "" {
141 err = fmt.Errorf("GOROOT must be set")
142 return
143 }
144 goTool := filepath.Join(goroot, "bin", "go")
145
Serge Bazanski14cf7502020-05-28 14:29:56 +0200146 query := fmt.Sprintf("%s@%s", d.remoteImportpath(), d.version)
Serge Bazanskif369cfa2020-05-22 18:36:42 +0200147 cmd := exec.Command(goTool, "mod", "download", "-json", "--", query)
148 out, err := cmd.Output()
149 if err != nil {
150 log.Printf("go mod returned: %q", out)
151 err = fmt.Errorf("go mod failed: %v", err)
152 return
153 }
154
155 var res struct{ Version, Sum, Dir string }
156 err = json.Unmarshal(out, &res)
157 if err != nil {
158 return
159 }
160
161 version = res.Version
162 dir = res.Dir
163 sum = res.Sum
164 return
165}