Colin Cross | 38f794e | 2017-09-07 10:53:07 -0700 | [diff] [blame] | 1 | // 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 | |
| 15 | package android |
| 16 | |
Colin Cross | 19878da | 2019-03-28 14:45:07 -0700 | [diff] [blame] | 17 | import ( |
Liz Kammer | 12615db | 2021-09-28 09:19:17 -0400 | [diff] [blame^] | 18 | "android/soong/bazel" |
Colin Cross | 19878da | 2019-03-28 14:45:07 -0700 | [diff] [blame] | 19 | "strings" |
| 20 | |
Colin Cross | fe17f6f | 2019-03-28 19:30:56 -0700 | [diff] [blame] | 21 | "github.com/google/blueprint" |
Colin Cross | 19878da | 2019-03-28 14:45:07 -0700 | [diff] [blame] | 22 | "github.com/google/blueprint/proptools" |
| 23 | ) |
| 24 | |
Liz Kammer | 12615db | 2021-09-28 09:19:17 -0400 | [diff] [blame^] | 25 | const ( |
| 26 | canonicalPathFromRootDefault = true |
| 27 | ) |
| 28 | |
Colin Cross | 38f794e | 2017-09-07 10:53:07 -0700 | [diff] [blame] | 29 | // 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 Cross | 19878da | 2019-03-28 14:45:07 -0700 | [diff] [blame] | 37 | type ProtoFlags struct { |
| 38 | Flags []string |
| 39 | CanonicalPathFromRoot bool |
| 40 | Dir ModuleGenPath |
| 41 | SubDir ModuleGenPath |
| 42 | OutTypeFlag string |
| 43 | OutParams []string |
Colin Cross | fe17f6f | 2019-03-28 19:30:56 -0700 | [diff] [blame] | 44 | Deps Paths |
| 45 | } |
| 46 | |
| 47 | type protoDependencyTag struct { |
| 48 | blueprint.BaseDependencyTag |
| 49 | name string |
| 50 | } |
| 51 | |
| 52 | var ProtoPluginDepTag = protoDependencyTag{name: "plugin"} |
| 53 | |
| 54 | func 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 Cross | 0f7d2ef | 2019-10-16 11:03:10 -0700 | [diff] [blame] | 60 | ctx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), |
| 61 | ProtoPluginDepTag, "protoc-gen-"+plugin) |
Colin Cross | fe17f6f | 2019-03-28 19:30:56 -0700 | [diff] [blame] | 62 | } |
Colin Cross | 19878da | 2019-03-28 14:45:07 -0700 | [diff] [blame] | 63 | } |
Colin Cross | a3b2500 | 2017-12-15 13:41:30 -0800 | [diff] [blame] | 64 | |
Colin Cross | 19878da | 2019-03-28 14:45:07 -0700 | [diff] [blame] | 65 | func GetProtoFlags(ctx ModuleContext, p *ProtoProperties) ProtoFlags { |
Colin Cross | fe17f6f | 2019-03-28 19:30:56 -0700 | [diff] [blame] | 66 | var flags []string |
| 67 | var deps Paths |
| 68 | |
Colin Cross | 38f794e | 2017-09-07 10:53:07 -0700 | [diff] [blame] | 69 | if len(p.Proto.Local_include_dirs) > 0 { |
| 70 | localProtoIncludeDirs := PathsForModuleSrc(ctx, p.Proto.Local_include_dirs) |
Colin Cross | fe17f6f | 2019-03-28 19:30:56 -0700 | [diff] [blame] | 71 | flags = append(flags, JoinWithPrefix(localProtoIncludeDirs.Strings(), "-I")) |
Colin Cross | 38f794e | 2017-09-07 10:53:07 -0700 | [diff] [blame] | 72 | } |
| 73 | if len(p.Proto.Include_dirs) > 0 { |
| 74 | rootProtoIncludeDirs := PathsForSource(ctx, p.Proto.Include_dirs) |
Colin Cross | fe17f6f | 2019-03-28 19:30:56 -0700 | [diff] [blame] | 75 | 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 Cross | 38f794e | 2017-09-07 10:53:07 -0700 | [diff] [blame] | 92 | } |
| 93 | |
Colin Cross | 19878da | 2019-03-28 14:45:07 -0700 | [diff] [blame] | 94 | return ProtoFlags{ |
Colin Cross | fe17f6f | 2019-03-28 19:30:56 -0700 | [diff] [blame] | 95 | Flags: flags, |
| 96 | Deps: deps, |
| 97 | OutTypeFlag: protoOutFlag, |
Liz Kammer | 12615db | 2021-09-28 09:19:17 -0400 | [diff] [blame^] | 98 | CanonicalPathFromRoot: proptools.BoolDefault(p.Proto.Canonical_path_from_root, canonicalPathFromRootDefault), |
Colin Cross | 19878da | 2019-03-28 14:45:07 -0700 | [diff] [blame] | 99 | Dir: PathForModuleGen(ctx, "proto"), |
| 100 | SubDir: PathForModuleGen(ctx, "proto", ctx.ModuleDir()), |
Dan Willemsen | ab9f426 | 2018-02-14 13:58:34 -0800 | [diff] [blame] | 101 | } |
Colin Cross | 38f794e | 2017-09-07 10:53:07 -0700 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | type 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 Cross | fe17f6f | 2019-03-28 19:30:56 -0700 | [diff] [blame] | 109 | // Proto plugin to use as the generator. Must be a cc_binary_host module. |
| 110 | Plugin *string `android:"arch_variant"` |
| 111 | |
Colin Cross | 38f794e | 2017-09-07 10:53:07 -0700 | [diff] [blame] | 112 | // 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 Willemsen | ab9f426 | 2018-02-14 13:58:34 -0800 | [diff] [blame] | 118 | |
| 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 Cross | 38f794e | 2017-09-07 10:53:07 -0700 | [diff] [blame] | 127 | } `android:"arch_variant"` |
| 128 | } |
Colin Cross | 19878da | 2019-03-28 14:45:07 -0700 | [diff] [blame] | 129 | |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 130 | func ProtoRule(rule *RuleBuilder, protoFile Path, flags ProtoFlags, deps Paths, |
Colin Cross | 19878da | 2019-03-28 14:45:07 -0700 | [diff] [blame] | 131 | 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 Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 142 | BuiltTool("aprotoc"). |
Colin Cross | 19878da | 2019-03-28 14:45:07 -0700 | [diff] [blame] | 143 | 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 Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 152 | BuiltTool("dep_fixer").Flag(depFile.String()) |
Colin Cross | 19878da | 2019-03-28 14:45:07 -0700 | [diff] [blame] | 153 | } |
Liz Kammer | 12615db | 2021-09-28 09:19:17 -0400 | [diff] [blame^] | 154 | |
| 155 | // Bp2buildProtoInfo contains information necessary to pass on to language specific conversion. |
| 156 | type Bp2buildProtoInfo struct { |
| 157 | Type *string |
| 158 | Name string |
| 159 | } |
| 160 | |
| 161 | type 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. |
| 168 | func Bp2buildProtoProperties(ctx Bp2buildMutatorContext, module Module, srcs bazel.LabelListAttribute) (Bp2buildProtoInfo, bool) { |
| 169 | var info Bp2buildProtoInfo |
| 170 | if srcs.IsEmpty() { |
| 171 | return info, false |
| 172 | } |
| 173 | m := module.base() |
| 174 | |
| 175 | info.Name = m.Name() + "_proto" |
| 176 | attrs := protoAttrs{ |
| 177 | Srcs: srcs, |
| 178 | } |
| 179 | |
| 180 | for axis, configToProps := range m.GetArchVariantProperties(ctx, &ProtoProperties{}) { |
| 181 | for _, rawProps := range configToProps { |
| 182 | var props *ProtoProperties |
| 183 | var ok bool |
| 184 | if props, ok = rawProps.(*ProtoProperties); !ok { |
| 185 | ctx.ModuleErrorf("Could not cast ProtoProperties to expected type") |
| 186 | } |
| 187 | if axis == bazel.NoConfigAxis { |
| 188 | info.Type = props.Proto.Type |
| 189 | |
| 190 | if proptools.BoolDefault(props.Proto.Canonical_path_from_root, canonicalPathFromRootDefault) { |
| 191 | // an empty string indicates to strips the package path |
| 192 | path := "" |
| 193 | attrs.Strip_import_prefix = &path |
| 194 | } |
| 195 | } else if props.Proto.Type != info.Type && props.Proto.Type != nil { |
| 196 | ctx.ModuleErrorf("Cannot handle arch-variant types for protos at this time.") |
| 197 | } |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | ctx.CreateBazelTargetModule( |
| 202 | bazel.BazelTargetModuleProperties{Rule_class: "proto_library"}, |
| 203 | CommonAttributes{Name: info.Name}, |
| 204 | &attrs) |
| 205 | |
| 206 | return info, true |
| 207 | } |