generate.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. // Copyright (C) 2020 The Syncthing Authors.
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this file,
  5. // You can obtain one at https://mozilla.org/MPL/2.0/.
  6. //+build ignore
  7. package main
  8. import (
  9. "log"
  10. "os"
  11. "os/exec"
  12. "path/filepath"
  13. )
  14. //go:generate go run scripts/protofmt.go .
  15. // First generate extensions using standard proto compiler.
  16. //go:generate protoc -I ../ -I . --gogofast_out=Mgoogle/protobuf/descriptor.proto=github.com/gogo/protobuf/protoc-gen-gogo/descriptor,paths=source_relative:ext ext.proto
  17. // Then build our vanity compiler that uses the new extensions
  18. //go:generate go build -o scripts/protoc-gen-gosyncthing scripts/protoc_plugin.go
  19. // Inception, go generate calls the script itself that then deals with generation.
  20. // This is only done because go:generate does not support wildcards in paths.
  21. //go:generate go run generate.go lib/config lib/fs
  22. // Use the standard compiler here. We can revisit this later, but we don't plan on exposing this via any APIs.
  23. //go:generate protoc -I ../ -I . --gogofast_out=paths=source_relative:.. lib/protocol/bep.proto
  24. func main() {
  25. for _, path := range os.Args[1:] {
  26. matches, err := filepath.Glob(filepath.Join(path, "*proto"))
  27. if err != nil {
  28. log.Fatal(err)
  29. }
  30. log.Println(path, "returned:", matches)
  31. args := []string{
  32. "-I", "..",
  33. "-I", ".",
  34. "--plugin=protoc-gen-gosyncthing=scripts/protoc-gen-gosyncthing",
  35. "--gosyncthing_out=paths=source_relative:..",
  36. }
  37. args = append(args, matches...)
  38. cmd := exec.Command("protoc", args...)
  39. cmd.Stdout = os.Stdout
  40. cmd.Stderr = os.Stderr
  41. if err := cmd.Run(); err != nil {
  42. log.Fatal("Failed generating", path)
  43. }
  44. }
  45. }