Debian Codegraph @

internal/librariangen/protoc/protoc.go

raw
1 // Copyright 2025 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 package protoc
16
17 import (
18 "fmt"
19 "os"
20 "path/filepath"
21
22 "cloud.google.com/go/internal/postprocessor/librarian/librariangen/request"
23 )
24
25 // ConfigProvider is an interface that describes the configuration needed
26 // by the Build function. This allows the protoc package to be decoupled
27 // from the source of the configuration (e.g., Bazel files, JSON, etc.).
28 type ConfigProvider interface {
29 GAPICImportPath() string
30 ServiceYAML() string
31 GRPCServiceConfig() string
32 Transport() string
33 ReleaseLevel() string
34 HasMetadata() bool
35 HasDiregapic() bool
36 HasRESTNumericEnums() bool
37 HasGoGRPC() bool
38 HasGAPIC() bool
39 HasLegacyGRPC() bool
40 }
41
42 // Build constructs the full protoc command arguments for a given API.
43 func Build(lib *request.Library, api *request.API, apiServiceDir string, config ConfigProvider, sourceDir, outputDir string) ([]string, error) {
44 // Gather all .proto files in the API's source directory.
45 entries, err := os.ReadDir(apiServiceDir)
46 if err != nil {
47 return nil, fmt.Errorf("librariangen: failed to read API source directory %s: %w", apiServiceDir, err)
48 }
49
50 var protoFiles []string
51 for _, entry := range entries {
52 if !entry.IsDir() && filepath.Ext(entry.Name()) == ".proto" {
53 protoFiles = append(protoFiles, filepath.Join(apiServiceDir, entry.Name()))
54 }
55 }
56
57 if len(protoFiles) == 0 {
58 return nil, fmt.Errorf("librariangen: no .proto files found in %s", apiServiceDir)
59 }
60
61 // Construct the protoc command arguments.
62 var gapicOpts []string
63 if config.HasGAPIC() {
64 gapicOpts = append(gapicOpts, "go-gapic-package="+config.GAPICImportPath())
65 if config.ServiceYAML() != "" {
66 gapicOpts = append(gapicOpts, fmt.Sprintf("api-service-config=%s", filepath.Join(apiServiceDir, config.ServiceYAML())))
67 }
68 if config.GRPCServiceConfig() != "" {
69 gapicOpts = append(gapicOpts, fmt.Sprintf("grpc-service-config=%s", filepath.Join(apiServiceDir, config.GRPCServiceConfig())))
70 }
71 if config.Transport() != "" {
72 gapicOpts = append(gapicOpts, fmt.Sprintf("transport=%s", config.Transport()))
73 }
74 if config.ReleaseLevel() != "" {
75 gapicOpts = append(gapicOpts, fmt.Sprintf("release-level=%s", config.ReleaseLevel()))
76 }
77 if config.HasMetadata() {
78 gapicOpts = append(gapicOpts, "metadata")
79 }
80 if config.HasDiregapic() {
81 gapicOpts = append(gapicOpts, "diregapic")
82 }
83 if config.HasRESTNumericEnums() {
84 gapicOpts = append(gapicOpts, "rest-numeric-enums")
85 }
86 }
87
88 args := []string{
89 "protoc",
90 "--experimental_allow_proto3_optional",
91 }
92 // All generated files are written to the /output directory.
93 // Which plugin(s) we use depends on whether the Bazel rule was go_grpc_library
94 // or go_proto_library:
95 // - If we're using go_rpc, we use the newer go plugin and the go-grpc plugin
96 // - Otherwise, use the "old" plugin (built explicitly in the Dockerfile)
97 if config.HasGoGRPC() {
98 args = append(args, "--go_out="+outputDir, "--go-grpc_out="+outputDir, "--go-grpc_opt=require_unimplemented_servers=false")
99 } else {
100 args = append(args, "--go_v1_out="+outputDir)
101 if config.HasLegacyGRPC() {
102 args = append(args, "--go_v1_opt=plugins=grpc")
103 }
104 }
105 if config.HasGAPIC() {
106 args = append(args, "--go_gapic_out="+outputDir)
107
108 for _, opt := range gapicOpts {
109 args = append(args, "--go_gapic_opt="+opt)
110 }
111 }
112 args = append(args,
113 // The -I flag specifies the import path for protoc. All protos
114 // and their dependencies must be findable from this path.
115 // The /source mount contains the complete googleapis repository.
116 "-I="+sourceDir,
117 )
118
119 args = append(args, protoFiles...)
120
121 return args, nil
122 }