| 1 | /* |
| 2 | Copyright The containerd Authors. |
| 3 | |
| 4 | Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | you may not use this file except in compliance with the License. |
| 6 | You may obtain a copy of the License at |
| 7 | |
| 8 | http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | |
| 10 | Unless required by applicable law or agreed to in writing, software |
| 11 | distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | See the License for the specific language governing permissions and |
| 14 | limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package main |
| 18 | |
| 19 | import ( |
| 20 | "bytes" |
| 21 | "fmt" |
| 22 | "os" |
| 23 | "os/exec" |
| 24 | "path/filepath" |
| 25 | "strings" |
| 26 | "text/template" |
| 27 | ) |
| 28 | |
| 29 | var ( |
| 30 | tmpl = template.Must(template.New("protoc").Parse(`protoc -I |
| 31 | {{- range $index, $include := .Includes -}} |
| 32 | {{if $index}}` + string(filepath.ListSeparator) + `{{end -}} |
| 33 | {{.}} |
| 34 | {{- end -}} |
| 35 | {{- if .Descriptors}} --include_imports --descriptor_set_out={{.Descriptors}}{{- end -}} |
| 36 | |
| 37 | {{ if lt .Version 2 }} |
| 38 | {{- range $index, $name := .Names }} --{{- $name -}}_out={{- $.GoOutV1 }}{{- end -}} |
| 39 | {{- else -}} |
| 40 | {{- range $gen := .Generators }} --{{- $gen.Name -}}_out={{- $gen.OutputDir }} |
| 41 | {{- range $k, $v := $gen.Parameters }} --{{- $gen.Name -}}_opt={{- $k -}}={{- $v -}}{{- end -}} |
| 42 | {{- end -}} |
| 43 | {{- end -}} |
| 44 | |
| 45 | {{- range .Files}} {{.}}{{end -}} |
| 46 | `)) |
| 47 | ) |
| 48 | |
| 49 | type generator struct { |
| 50 | Name string |
| 51 | OutputDir string |
| 52 | Parameters map[string]string |
| 53 | } |
| 54 | |
| 55 | // protocParams defines inputs to a protoc command string. |
| 56 | type protocCmd struct { |
| 57 | Generators []generator |
| 58 | Includes []string |
| 59 | Descriptors string |
| 60 | Files []string |
| 61 | // Version is Protobuild's version. |
| 62 | Version int |
| 63 | |
| 64 | // V1 fields |
| 65 | Names []string |
| 66 | OutputDir string |
| 67 | PackageMap map[string]string |
| 68 | Plugins []string |
| 69 | ImportPath string |
| 70 | } |
| 71 | |
| 72 | func (p *protocCmd) mkcmd() (string, error) { |
| 73 | var buf bytes.Buffer |
| 74 | if err := tmpl.Execute(&buf, p); err != nil { |
| 75 | return "", err |
| 76 | } |
| 77 | |
| 78 | return buf.String(), nil |
| 79 | } |
| 80 | |
| 81 | // GoOutV1 returns the parameter for --go_out= for protoc-gen-go < 1.4.0. |
| 82 | // Note that plugins and import_path are no longer supported by |
| 83 | // newer protoc-gen-go versions. |
| 84 | func (p *protocCmd) GoOutV1() string { |
| 85 | var result string |
| 86 | if len(p.Plugins) > 0 { |
| 87 | result += "plugins=" + strings.Join(p.Plugins, "+") + "," |
| 88 | } |
| 89 | result += "import_path=" + p.ImportPath |
| 90 | |
| 91 | for proto, pkg := range p.PackageMap { |
| 92 | result += fmt.Sprintf(",M%s=%s", proto, pkg) |
| 93 | } |
| 94 | result += ":" + p.OutputDir |
| 95 | |
| 96 | return result |
| 97 | } |
| 98 | |
| 99 | func (p *protocCmd) run() error { |
| 100 | arg, err := p.mkcmd() |
| 101 | if err != nil { |
| 102 | return err |
| 103 | } |
| 104 | |
| 105 | // pass to sh -c so we don't need to re-split here. |
| 106 | args := []string{shArg, arg} |
| 107 | cmd := exec.Command(shCmd, args...) |
| 108 | cmd.Stderr = os.Stderr |
| 109 | cmd.Stdout = os.Stdout |
| 110 | return cmd.Run() |
| 111 | } |