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 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500
|
/*
Copyright The containerd Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"bytes"
"errors"
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"os/exec"
"path"
"path/filepath"
"strconv"
"strings"
"syscall"
"github.com/golang/protobuf/protoc-gen-go/descriptor"
)
// defines several variables for parameterizing the protoc command. We can pull
// this out into a toml files in cases where we to vary this per package.
var (
configPath string
dryRun bool
quiet bool
)
func init() {
flag.StringVar(&configPath, "f", "Protobuild.toml", "override default config location")
flag.BoolVar(&dryRun, "dryrun", false, "prints commands without running")
flag.BoolVar(&quiet, "quiet", false, "suppress verbose output")
}
func parseVersion(s string) (int, error) {
if s == "unstable" {
return 0, nil
}
v, err := strconv.Atoi(s)
if err != nil {
return 0, fmt.Errorf("unknown file version %q: %w", s, err)
}
if v < 1 || v > 2 {
return 0, fmt.Errorf(`unknown file version %q; valid versions are "unstable", "1" and "2"`, s)
}
return v, nil
}
func importPath(base, target string) (string, error) {
rel, err := filepath.Rel(base, target)
if err != nil {
return "", err
}
return filepath.ToSlash(rel), nil
}
func main() {
flag.Parse()
c, err := readConfig(configPath)
if err != nil {
log.Fatalln(err)
}
version, err := parseVersion(c.Version)
if err != nil {
log.Fatalln(err)
}
pkgInfos, err := goPkgInfo(flag.Args()...)
if err != nil {
log.Fatalln(err)
}
gopath, err := gopathSrc()
if err != nil {
log.Fatalln(err)
}
gopathCurrent, err := gopathCurrent()
if err != nil {
log.Fatalln(err)
}
// For some reason, the golang protobuf generator makes the god awful
// decision to output the files relative to the gopath root. It doesn't do
// this only in the case where you give it ".".
outputDir := filepath.Join(gopathCurrent, "src")
// Index overrides by target import path
overrides := map[string]struct {
Prefixes []string
Generator string
Generators []string
Parameters map[string]map[string]string
Plugins *[]string
}{}
for _, override := range c.Overrides {
for _, prefix := range override.Prefixes {
overrides[prefix] = override
}
}
// Create include paths used to find the descriptor proto. As a special case
// we search the vendor directory relative to the current working directory.
// This handles the case where a user has vendored their descriptor proto.
var descriptorIncludes []string
descriptorIncludes = append(descriptorIncludes, c.Includes.Before...)
for _, vendorPath := range c.Includes.Vendored {
descriptorIncludes = append(descriptorIncludes,
filepath.Join("vendor", vendorPath))
}
descriptorIncludes = append(descriptorIncludes, c.Includes.Packages...)
descriptorIncludes = append(descriptorIncludes, c.Includes.After...)
descProto, includeDir, err := descriptorProto(descriptorIncludes)
if err != nil {
log.Fatalln(err)
}
// Aggregate descriptors for each descriptor prefix.
descriptorSets := map[string]*descriptorSet{}
for _, stable := range c.Descriptors {
descriptorSets[stable.Prefix] = newDescriptorSet(stable.IgnoreFiles, descProto, includeDir)
}
shouldGenerateDescriptors := func(p string) bool {
for prefix := range descriptorSets {
if strings.HasPrefix(p, prefix) {
return true
}
}
return false
}
var descriptors []*descriptor.FileDescriptorSet
for _, pkg := range pkgInfos {
var includes []string
includes = append(includes, c.Includes.Before...)
vendor, err := closestVendorDir(pkg.Dir)
if err != nil {
if err != errVendorNotFound {
log.Fatalln(err)
}
}
if vendor != "" {
// TODO(stevvooe): The use of the closest vendor directory is a
// little naive. We should probably resolve all possible vendor
// directories or at least match Go's behavior.
// we also special case the inclusion of gogoproto in the vendor dir.
// We could parameterize this better if we find it to be a common case.
var vendoredIncludesResolved []string
for _, vendoredInclude := range c.Includes.Vendored {
vendoredIncludesResolved = append(vendoredIncludesResolved,
filepath.Join(vendor, vendoredInclude))
}
// Also do this for pkg includes.
for _, pkgInclude := range c.Includes.Packages {
vendoredIncludesResolved = append(vendoredIncludesResolved,
filepath.Join(vendor, pkgInclude))
}
includes = append(includes, vendoredIncludesResolved...)
includes = append(includes, vendor)
} else if len(c.Includes.Vendored) > 0 {
log.Println("ignoring vendored includes: vendor directory not found")
}
// handle packages that we want to have as an include root from any of
// the gopaths.
for _, pkg := range c.Includes.Packages {
includes = append(includes, gopathJoin(gopath, pkg))
}
includes = append(includes, gopath)
includes = append(includes, c.Includes.After...)
protoc := protocCmd{
Generators: generators(c.Generators, outputDir),
Files: pkg.ProtoFiles,
OutputDir: outputDir,
Includes: includes,
Version: version,
Names: c.Generators,
Plugins: c.Plugins,
ImportPath: pkg.GoImportPath,
PackageMap: c.Packages,
}
importDirPath, err := importPath(outputDir, pkg.Dir)
if err != nil {
log.Fatalln(err)
}
parameters := map[string]map[string]string{}
for proto, pkg := range c.Packages {
parameters["go"] = mergeMap(parameters["go"], map[string]string{
fmt.Sprintf("M%s", proto): pkg,
})
}
for k, v := range c.Parameters {
parameters[k] = mergeMap(parameters[k], v)
}
if override, ok := overrides[importDirPath]; ok {
// selectively apply the overrides to the protoc structure.
if len(override.Generators) > 0 {
protoc.Names = override.Generators
protoc.Generators = generators(override.Generators, outputDir)
}
if override.Plugins != nil {
protoc.Plugins = *override.Plugins
}
for k, v := range override.Parameters {
parameters[k] = mergeMap(parameters[k], v)
}
}
// Set parameters per generator
for i := range protoc.Generators {
protoc.Generators[i].Parameters = parameters[protoc.Generators[i].Name]
}
var (
genDescriptors = shouldGenerateDescriptors(importDirPath)
dfp *os.File // tempfile for descriptors
)
if genDescriptors {
dfp, err = ioutil.TempFile("", "descriptors.pb-")
if err != nil {
log.Fatalln(err)
}
protoc.Descriptors = dfp.Name()
}
arg, err := protoc.mkcmd()
if err != nil {
log.Fatalln(err)
}
if !quiet {
fmt.Println(arg)
}
if dryRun {
continue
}
if err := protoc.run(); err != nil {
if quiet {
log.Println(arg)
}
if err, ok := err.(*exec.ExitError); ok {
if status, ok := err.Sys().(syscall.WaitStatus); ok {
os.Exit(status.ExitStatus()) // proxy protoc exit status
}
}
log.Fatalln(err)
}
if genDescriptors {
desc, err := readDesc(protoc.Descriptors)
if err != nil {
log.Fatalln(err)
}
for path, set := range descriptorSets {
if strings.HasPrefix(importDirPath, path) {
set.add(desc.File...)
}
}
descriptors = append(descriptors, desc)
// clean up descriptors file
if err := os.Remove(dfp.Name()); err != nil {
log.Fatalln(err)
}
if err := dfp.Close(); err != nil {
log.Fatalln(err)
}
}
}
for _, descriptorConfig := range c.Descriptors {
fp, err := os.OpenFile(descriptorConfig.Target, os.O_TRUNC|os.O_WRONLY|os.O_CREATE, 0777)
if err != nil {
log.Fatalln(err)
}
defer fp.Sync()
defer fp.Close()
set := descriptorSets[descriptorConfig.Prefix]
if len(set.merged.File) == 0 {
continue // just skip if there is nothing.
}
if err := set.marshalTo(fp); err != nil {
log.Fatalln(err)
}
}
}
func generators(names []string, outDir string) []generator {
g := make([]generator, len(names))
for i, name := range names {
g[i].Name = name
g[i].OutputDir = outDir
}
return g
}
type protoGoPkgInfo struct {
Dir string
GoImportPath string
ProtoFiles []string
}
// goPkgInfo hunts down packages with proto files.
func goPkgInfo(golistpath ...string) ([]protoGoPkgInfo, error) {
args := []string{
"list", "-e", "-f", "{{.ImportPath}} {{.Dir}}"}
args = append(args, golistpath...)
cmd := exec.Command("go", args...)
p, err := cmd.Output()
if err != nil {
return nil, err
}
var pkgInfos []protoGoPkgInfo
lines := bytes.Split(p, []byte("\n"))
for _, line := range lines {
if len(line) == 0 {
continue
}
parts := bytes.Fields(line)
if len(parts) != 2 {
return nil, fmt.Errorf("bad output from command: %s", p)
}
pkgInfo := protoGoPkgInfo{
Dir: string(parts[1]),
GoImportPath: string(parts[0]),
}
protoFiles, err := filepath.Glob(filepath.Join(pkgInfo.Dir, "*.proto"))
if err != nil {
return nil, err
}
if len(protoFiles) == 0 {
continue // not a proto directory, skip
}
pkgInfo.ProtoFiles = protoFiles
pkgInfos = append(pkgInfos, pkgInfo)
}
return pkgInfos, nil
}
func gopaths() ([]string, error) {
cmd := exec.Command("go", "env", "GOPATH")
out, err := cmd.Output()
if err != nil {
return nil, err
}
gp := strings.TrimSpace(string(out))
if gp == "" {
return nil, errors.New("go env GOPATH is empty")
}
return strings.Split(gp, string(filepath.ListSeparator)), nil
}
// gopathSrc modifies GOPATH elements from env to include the src directory.
func gopathSrc() (string, error) {
gps, err := gopaths()
if err != nil {
return "", err
}
if len(gps) == 0 {
return "", fmt.Errorf("must be run from a gopath")
}
var elements []string
for _, element := range gps {
elements = append(elements, filepath.Join(element, "src"))
}
return strings.Join(elements, string(filepath.ListSeparator)), nil
}
// gopathCurrent provides the top-level gopath for the current generation.
func gopathCurrent() (string, error) {
gps, err := gopaths()
if err != nil {
return "", err
}
if len(gps) == 0 {
return "", fmt.Errorf("must be run from a gopath")
}
return gps[0], nil
}
// gopathJoin combines the element with each path item and then recombines the set.
func gopathJoin(gopath, element string) string {
gps := strings.Split(gopath, string(filepath.ListSeparator))
var elements []string
for _, p := range gps {
elements = append(elements, filepath.Join(p, element))
}
return strings.Join(elements, string(filepath.ListSeparator))
}
// descriptorProto returns the full path to google/protobuf/descriptor.proto
// which might be different depending on whether it was installed. The argument
// is the list of paths to check.
func descriptorProto(paths []string) (string, string, error) {
const descProto = "google/protobuf/descriptor.proto"
for _, dir := range paths {
file := path.Join(dir, descProto)
if _, err := os.Stat(file); err == nil {
return file, dir, err
}
}
return "", "", fmt.Errorf("File %q not found (looked in: %v)", descProto, paths)
}
var errVendorNotFound = fmt.Errorf("no vendor dir found")
// closestVendorDir walks up from dir until it finds the vendor directory.
func closestVendorDir(dir string) (string, error) {
dir = filepath.Clean(dir)
for dir != filepath.Join(filepath.VolumeName(dir), string(filepath.Separator)) {
vendor := filepath.Join(dir, "vendor")
fi, err := os.Stat(vendor)
if err != nil {
if os.IsNotExist(err) {
// up we go!
dir = filepath.Dir(dir)
continue
}
return "", err
}
if !fi.IsDir() {
// up we go!
dir = filepath.Dir(dir)
continue
}
return vendor, nil
}
return "", errVendorNotFound
}
func mergeMap(m1, m2 map[string]string) map[string]string {
if m1 == nil {
m1 = map[string]string{}
}
for k, v := range m2 {
m1[k] = v
}
return m1
}
|