blob: 8ad16a6a33b1dc273b73585f95c88934041bb09e [file] [log] [blame]
Colin Cross38f794e2017-09-07 10:53:07 -07001// Copyright 2017 Google Inc. All rights reserved.
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
15package android
16
Colin Cross19878da2019-03-28 14:45:07 -070017import (
Liz Kammer12615db2021-09-28 09:19:17 -040018 "android/soong/bazel"
Colin Cross19878da2019-03-28 14:45:07 -070019 "strings"
20
Colin Crossfe17f6f2019-03-28 19:30:56 -070021 "github.com/google/blueprint"
Colin Cross19878da2019-03-28 14:45:07 -070022 "github.com/google/blueprint/proptools"
23)
24
Liz Kammer12615db2021-09-28 09:19:17 -040025const (
26 canonicalPathFromRootDefault = true
27)
28
Colin Cross38f794e2017-09-07 10:53:07 -070029// TODO(ccross): protos are often used to communicate between multiple modules. If the only
30// way to convert a proto to source is to reference it as a source file, and external modules cannot
31// reference source files in other modules, then every module that owns a proto file will need to
32// export a library for every type of external user (lite vs. full, c vs. c++ vs. java). It would
33// be better to support a proto module type that exported a proto file along with some include dirs,
34// and then external modules could depend on the proto module but use their own settings to
35// generate the source.
36
Colin Cross19878da2019-03-28 14:45:07 -070037type ProtoFlags struct {
38 Flags []string
39 CanonicalPathFromRoot bool
40 Dir ModuleGenPath
41 SubDir ModuleGenPath
42 OutTypeFlag string
43 OutParams []string
Colin Crossfe17f6f2019-03-28 19:30:56 -070044 Deps Paths
45}
46
47type protoDependencyTag struct {
48 blueprint.BaseDependencyTag
49 name string
50}
51
52var ProtoPluginDepTag = protoDependencyTag{name: "plugin"}
53
54func ProtoDeps(ctx BottomUpMutatorContext, p *ProtoProperties) {
55 if String(p.Proto.Plugin) != "" && String(p.Proto.Type) != "" {
56 ctx.ModuleErrorf("only one of proto.type and proto.plugin can be specified.")
57 }
58
59 if plugin := String(p.Proto.Plugin); plugin != "" {
Colin Cross0f7d2ef2019-10-16 11:03:10 -070060 ctx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(),
61 ProtoPluginDepTag, "protoc-gen-"+plugin)
Colin Crossfe17f6f2019-03-28 19:30:56 -070062 }
Colin Cross19878da2019-03-28 14:45:07 -070063}
Colin Crossa3b25002017-12-15 13:41:30 -080064
Colin Cross19878da2019-03-28 14:45:07 -070065func GetProtoFlags(ctx ModuleContext, p *ProtoProperties) ProtoFlags {
Colin Crossfe17f6f2019-03-28 19:30:56 -070066 var flags []string
67 var deps Paths
68
Colin Cross38f794e2017-09-07 10:53:07 -070069 if len(p.Proto.Local_include_dirs) > 0 {
70 localProtoIncludeDirs := PathsForModuleSrc(ctx, p.Proto.Local_include_dirs)
Colin Crossfe17f6f2019-03-28 19:30:56 -070071 flags = append(flags, JoinWithPrefix(localProtoIncludeDirs.Strings(), "-I"))
Colin Cross38f794e2017-09-07 10:53:07 -070072 }
73 if len(p.Proto.Include_dirs) > 0 {
74 rootProtoIncludeDirs := PathsForSource(ctx, p.Proto.Include_dirs)
Colin Crossfe17f6f2019-03-28 19:30:56 -070075 flags = append(flags, JoinWithPrefix(rootProtoIncludeDirs.Strings(), "-I"))
76 }
77
78 ctx.VisitDirectDepsWithTag(ProtoPluginDepTag, func(dep Module) {
79 if hostTool, ok := dep.(HostToolProvider); !ok || !hostTool.HostToolPath().Valid() {
80 ctx.PropertyErrorf("proto.plugin", "module %q is not a host tool provider",
81 ctx.OtherModuleName(dep))
82 } else {
83 plugin := String(p.Proto.Plugin)
84 deps = append(deps, hostTool.HostToolPath().Path())
85 flags = append(flags, "--plugin=protoc-gen-"+plugin+"="+hostTool.HostToolPath().String())
86 }
87 })
88
89 var protoOutFlag string
90 if plugin := String(p.Proto.Plugin); plugin != "" {
91 protoOutFlag = "--" + plugin + "_out"
Colin Cross38f794e2017-09-07 10:53:07 -070092 }
93
Colin Cross19878da2019-03-28 14:45:07 -070094 return ProtoFlags{
Colin Crossfe17f6f2019-03-28 19:30:56 -070095 Flags: flags,
96 Deps: deps,
97 OutTypeFlag: protoOutFlag,
Liz Kammer12615db2021-09-28 09:19:17 -040098 CanonicalPathFromRoot: proptools.BoolDefault(p.Proto.Canonical_path_from_root, canonicalPathFromRootDefault),
Colin Cross19878da2019-03-28 14:45:07 -070099 Dir: PathForModuleGen(ctx, "proto"),
100 SubDir: PathForModuleGen(ctx, "proto", ctx.ModuleDir()),
Dan Willemsenab9f4262018-02-14 13:58:34 -0800101 }
Colin Cross38f794e2017-09-07 10:53:07 -0700102}
103
104type ProtoProperties struct {
105 Proto struct {
106 // Proto generator type. C++: full or lite. Java: micro, nano, stream, or lite.
107 Type *string `android:"arch_variant"`
108
Colin Crossfe17f6f2019-03-28 19:30:56 -0700109 // Proto plugin to use as the generator. Must be a cc_binary_host module.
110 Plugin *string `android:"arch_variant"`
111
Colin Cross38f794e2017-09-07 10:53:07 -0700112 // list of directories that will be added to the protoc include paths.
113 Include_dirs []string
114
115 // list of directories relative to the bp file that will
116 // be added to the protoc include paths.
117 Local_include_dirs []string
Dan Willemsenab9f4262018-02-14 13:58:34 -0800118
119 // whether to identify the proto files from the root of the
120 // source tree (the original method in Android, useful for
121 // android-specific protos), or relative from where they were
122 // specified (useful for external/third party protos).
123 //
124 // This defaults to true today, but is expected to default to
125 // false in the future.
126 Canonical_path_from_root *bool
Colin Cross38f794e2017-09-07 10:53:07 -0700127 } `android:"arch_variant"`
128}
Colin Cross19878da2019-03-28 14:45:07 -0700129
Colin Crossf1a035e2020-11-16 17:32:30 -0800130func ProtoRule(rule *RuleBuilder, protoFile Path, flags ProtoFlags, deps Paths,
Colin Cross19878da2019-03-28 14:45:07 -0700131 outDir WritablePath, depFile WritablePath, outputs WritablePaths) {
132
133 var protoBase string
134 if flags.CanonicalPathFromRoot {
135 protoBase = "."
136 } else {
137 rel := protoFile.Rel()
138 protoBase = strings.TrimSuffix(protoFile.String(), rel)
139 }
140
141 rule.Command().
Colin Crossf1a035e2020-11-16 17:32:30 -0800142 BuiltTool("aprotoc").
Colin Cross19878da2019-03-28 14:45:07 -0700143 FlagWithArg(flags.OutTypeFlag+"=", strings.Join(flags.OutParams, ",")+":"+outDir.String()).
144 FlagWithDepFile("--dependency_out=", depFile).
145 FlagWithArg("-I ", protoBase).
146 Flags(flags.Flags).
147 Input(protoFile).
148 Implicits(deps).
149 ImplicitOutputs(outputs)
150
151 rule.Command().
Colin Crossf1a035e2020-11-16 17:32:30 -0800152 BuiltTool("dep_fixer").Flag(depFile.String())
Colin Cross19878da2019-03-28 14:45:07 -0700153}
Liz Kammer12615db2021-09-28 09:19:17 -0400154
155// Bp2buildProtoInfo contains information necessary to pass on to language specific conversion.
156type Bp2buildProtoInfo struct {
157 Type *string
158 Name string
159}
160
161type protoAttrs struct {
162 Srcs bazel.LabelListAttribute
163 Strip_import_prefix *string
164}
165
166// Bp2buildProtoProperties converts proto properties, creating a proto_library and returning the
167// information necessary for language-specific handling.
Sam Delmericoc7681022022-02-04 21:01:20 +0000168func Bp2buildProtoProperties(ctx Bp2buildMutatorContext, m *ModuleBase, srcs bazel.LabelListAttribute) (Bp2buildProtoInfo, bool) {
Liz Kammer12615db2021-09-28 09:19:17 -0400169 var info Bp2buildProtoInfo
170 if srcs.IsEmpty() {
171 return info, false
172 }
Liz Kammer12615db2021-09-28 09:19:17 -0400173
174 info.Name = m.Name() + "_proto"
175 attrs := protoAttrs{
176 Srcs: srcs,
177 }
178
179 for axis, configToProps := range m.GetArchVariantProperties(ctx, &ProtoProperties{}) {
180 for _, rawProps := range configToProps {
181 var props *ProtoProperties
182 var ok bool
183 if props, ok = rawProps.(*ProtoProperties); !ok {
184 ctx.ModuleErrorf("Could not cast ProtoProperties to expected type")
185 }
186 if axis == bazel.NoConfigAxis {
187 info.Type = props.Proto.Type
188
Liz Kammer7756c8f2022-02-14 20:49:15 -0500189 if !proptools.BoolDefault(props.Proto.Canonical_path_from_root, canonicalPathFromRootDefault) {
Liz Kammer12615db2021-09-28 09:19:17 -0400190 // an empty string indicates to strips the package path
191 path := ""
192 attrs.Strip_import_prefix = &path
193 }
194 } else if props.Proto.Type != info.Type && props.Proto.Type != nil {
195 ctx.ModuleErrorf("Cannot handle arch-variant types for protos at this time.")
196 }
197 }
198 }
199
200 ctx.CreateBazelTargetModule(
201 bazel.BazelTargetModuleProperties{Rule_class: "proto_library"},
202 CommonAttributes{Name: info.Name},
203 &attrs)
204
205 return info, true
206}