-
Notifications
You must be signed in to change notification settings - Fork 0
/
vendeps.go
216 lines (179 loc) · 5.43 KB
/
vendeps.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
// Copyright 2022 The Firefly Authors.
//
// Use of this source code is governed by a BSD 3-clause
// license that can be found in the LICENSE file.
// Package vendeps provides functionality for managing vendored external dependencies.
package vendeps
import (
"flag"
"fmt"
"log"
"os"
"path/filepath"
"firefly-os.dev/tools/simplehttp"
)
const (
buildBazel = "BUILD.bazel"
depsBzl = "deps.bzl"
manifestBzl = "manifest.bzl"
vendor = "vendor"
)
// Deps describes a set of software dependencies.
type Deps struct {
Rust []*RustCrate `bzl:"rust/crate"`
Go []*GoModule `bzl:"go/module"`
}
// RustCrate contains the dependency information for
// a Rust crate.
type RustCrate struct {
// Dependency details.
Name string `bzl:"name"`
Version string `bzl:"version"`
// Patches to be applied to the
// downloaded crate, before the
// BUILD file is copied/generated.
PatchArgs []string `bzl:"patch_args"`
Patches []string `bzl:"patches"`
// Manually-managed BUILD file.
BuildFile string `bzl:"build_file"`
// Build configuration
Edition string `bzl:"edition"`
Features []string `bzl:"features"`
Deps []string `bzl:"deps"`
ProcMacroDeps []string `bzl:"proc_macro_deps"`
RustcEnv map[string]string `bzl:"rustc_env"`
// Whether to create rustdocs.
NoDocs bool `bzl:"no_docs"`
// Whether the crate is a library or
// a procedural macro library.
ProcMacro bool `bzl:"proc_macro"`
// Build script configuration.
BuildScript string `bzl:"build_script"`
BuildScriptDeps []string `bzl:"build_script_deps"`
// Test configuration.
NoTests bool `bzl:"no_tests"`
TestData []string `bzl:"test_data"`
TestDataGlobs []string `bzl:"test_data_globs"`
TestDeps []string `bzl:"test_deps"`
TestEnv map[string]string `bzl:"test_env"`
// Generation details.
Digest string `bzl:"digest"`
PatchDigest string `bzl:"patch_digest"`
}
// GoModule contains the information necessary
// to vendor a Go module, specifying the set
// of packages within the module that are used.
type GoModule struct {
// Dependency details.
Name string `bzl:"name"`
Version string `bzl:"version"`
// Patches to be applied to the
// downloaded module, before the
// BUILD file is copied/generated.
PatchArgs []string `bzl:"patch_args"`
Patches []string `bzl:"patches"`
// Packages that should be used.
Packages []*GoPackage `bzl:"packages/package"`
// Generation details.
Digest string `bzl:"digest"`
PatchDigest string `bzl:"patch_digest"`
}
// GoPackage describes a package within
// a Go module.
type GoPackage struct {
// Dependency details.
Name string `bzl:"name"`
// Manually-managed BUILD file.
BuildFile string `bzl:"build_file"`
// Build configuration
Deps []string `bzl:"deps"`
Embed []string `bzl:"embed"`
EmbedGlobs []string `bzl:"embed_globs"`
// Test configuration.
NoTests bool `bzl:"no_tests"`
TestSize string `bzl:"test_size"`
TestData []string `bzl:"test_data"`
TestDataGlobs []string `bzl:"test_data_globs"`
TestDeps []string `bzl:"test_deps"`
}
// UpdateDeps includes a set of dependencies
// for the purposes of updating them.
type UpdateDeps struct {
Rust []*UpdateDep
Go []*UpdateDep
}
// UpdateDep describes the least information
// necessary to determine a third-party
// software library. This is used when
// determining whether updates are available.
type UpdateDep struct {
Name string
Version *string
}
func init() {
log.SetFlags(0)
log.SetOutput(os.Stderr)
log.SetPrefix("")
simplehttp.UserAgent = "Firefly-dependency-vendoring/1 (github.com/ProjectSerenity/firefly)"
}
func main() {
var help, noCache, dryRun, check, update bool
flag.BoolVar(&help, "h", false, "Show this message and exit.")
flag.BoolVar(&noCache, "no-cache", false, "Ignore any locally cached dependency data.")
flag.BoolVar(&dryRun, "dry-run", false, "Print the set of actions that would be performed, without performing them.")
flag.BoolVar(&check, "check", false, "Check the dependency set for unused dependencies.")
flag.BoolVar(&update, "update", false, "Check the dependency specification for dependency updates.")
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "Usage\n %s [OPTIONS]\n\n", filepath.Base(os.Args[0]))
fmt.Fprintf(os.Stderr, "Options:\n")
flag.PrintDefaults()
os.Exit(2)
}
flag.Parse()
// If we're being run with `bazel run`, we're in
// a semi-random build directory, and need to move
// to the workspace root directory.
//
workspace := os.Getenv("BUILD_WORKSPACE_DIRECTORY")
if workspace != "" {
err := os.Chdir(workspace)
if err != nil {
log.Printf("Failed to change directory to %q: %v", workspace, err)
}
}
if check {
fsys := os.DirFS(".")
err := CheckDependencies(fsys)
if err != nil {
log.Fatalf("Failed to check dependencies: %v", err)
}
return
}
if update {
err := UpdateDependencies(depsBzl)
if err != nil {
log.Fatalf("Failed to update dependencies: %v", err)
}
return
}
// Start by parsing the dependency manifest.
fsys := os.DirFS(".")
actions, err := Vendor(fsys)
if err != nil {
log.Fatalf("Failed to load dependency manifest: %v", err)
}
if !noCache {
actions = StripCachedActions(fsys, actions)
}
// Perform/print the actions.
for _, action := range actions {
if dryRun {
fmt.Println(action)
} else {
err = action.Do(fsys)
if err != nil {
log.Fatal(err)
}
}
}
}