Colin Cross | 6af17aa | 2017-09-20 12:59:05 -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 java |
| 16 | |
| 17 | import ( |
Colin Cross | 9516a6c | 2019-06-14 18:51:12 -0700 | [diff] [blame] | 18 | "path/filepath" |
| 19 | "strconv" |
| 20 | |
Colin Cross | 6af17aa | 2017-09-20 12:59:05 -0700 | [diff] [blame] | 21 | "android/soong/android" |
Sam Delmerico | c768102 | 2022-02-04 21:01:20 +0000 | [diff] [blame] | 22 | "android/soong/bazel" |
| 23 | |
| 24 | "github.com/google/blueprint/proptools" |
| 25 | ) |
| 26 | |
| 27 | const ( |
| 28 | protoTypeDefault = "lite" |
Colin Cross | 6af17aa | 2017-09-20 12:59:05 -0700 | [diff] [blame] | 29 | ) |
| 30 | |
Colin Cross | 9516a6c | 2019-06-14 18:51:12 -0700 | [diff] [blame] | 31 | func genProto(ctx android.ModuleContext, protoFiles android.Paths, flags android.ProtoFlags) android.Paths { |
| 32 | // Shard proto files into groups of 100 to avoid having to recompile all of them if one changes and to avoid |
| 33 | // hitting command line length limits. |
Colin Cross | 93fa5ef | 2021-10-18 10:19:03 -0700 | [diff] [blame] | 34 | shards := android.ShardPaths(protoFiles, 50) |
Dan Willemsen | ab9f426 | 2018-02-14 13:58:34 -0800 | [diff] [blame] | 35 | |
Colin Cross | 9516a6c | 2019-06-14 18:51:12 -0700 | [diff] [blame] | 36 | srcJarFiles := make(android.Paths, 0, len(shards)) |
Colin Cross | 6af17aa | 2017-09-20 12:59:05 -0700 | [diff] [blame] | 37 | |
Colin Cross | 9516a6c | 2019-06-14 18:51:12 -0700 | [diff] [blame] | 38 | for i, shard := range shards { |
| 39 | srcJarFile := android.PathForModuleGen(ctx, "proto", "proto"+strconv.Itoa(i)+".srcjar") |
| 40 | srcJarFiles = append(srcJarFiles, srcJarFile) |
Colin Cross | 19878da | 2019-03-28 14:45:07 -0700 | [diff] [blame] | 41 | |
Colin Cross | 9516a6c | 2019-06-14 18:51:12 -0700 | [diff] [blame] | 42 | outDir := srcJarFile.ReplaceExtension(ctx, "tmp") |
Colin Cross | 19878da | 2019-03-28 14:45:07 -0700 | [diff] [blame] | 43 | |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 44 | rule := android.NewRuleBuilder(pctx, ctx) |
Colin Cross | 19878da | 2019-03-28 14:45:07 -0700 | [diff] [blame] | 45 | |
Colin Cross | 9516a6c | 2019-06-14 18:51:12 -0700 | [diff] [blame] | 46 | rule.Command().Text("rm -rf").Flag(outDir.String()) |
| 47 | rule.Command().Text("mkdir -p").Flag(outDir.String()) |
Colin Cross | 19878da | 2019-03-28 14:45:07 -0700 | [diff] [blame] | 48 | |
Colin Cross | 9516a6c | 2019-06-14 18:51:12 -0700 | [diff] [blame] | 49 | for _, protoFile := range shard { |
| 50 | depFile := srcJarFile.InSameDir(ctx, protoFile.String()+".d") |
| 51 | rule.Command().Text("mkdir -p").Flag(filepath.Dir(depFile.String())) |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 52 | android.ProtoRule(rule, protoFile, flags, flags.Deps, outDir, depFile, nil) |
Colin Cross | 9516a6c | 2019-06-14 18:51:12 -0700 | [diff] [blame] | 53 | } |
Colin Cross | 19878da | 2019-03-28 14:45:07 -0700 | [diff] [blame] | 54 | |
Colin Cross | 9516a6c | 2019-06-14 18:51:12 -0700 | [diff] [blame] | 55 | // Proto generated java files have an unknown package name in the path, so package the entire output directory |
| 56 | // into a srcjar. |
| 57 | rule.Command(). |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 58 | BuiltTool("soong_zip"). |
Colin Cross | cf02ec8 | 2020-12-23 17:13:16 -0800 | [diff] [blame] | 59 | Flag("-srcjar"). |
Colin Cross | 9516a6c | 2019-06-14 18:51:12 -0700 | [diff] [blame] | 60 | Flag("-write_if_changed"). |
| 61 | FlagWithOutput("-o ", srcJarFile). |
| 62 | FlagWithArg("-C ", outDir.String()). |
| 63 | FlagWithArg("-D ", outDir.String()) |
Dan Willemsen | ab9f426 | 2018-02-14 13:58:34 -0800 | [diff] [blame] | 64 | |
Colin Cross | 9516a6c | 2019-06-14 18:51:12 -0700 | [diff] [blame] | 65 | rule.Command().Text("rm -rf").Flag(outDir.String()) |
| 66 | |
| 67 | rule.Restat() |
| 68 | |
| 69 | ruleName := "protoc" |
| 70 | ruleDesc := "protoc" |
| 71 | if len(shards) > 1 { |
| 72 | ruleName += "_" + strconv.Itoa(i) |
| 73 | ruleDesc += " " + strconv.Itoa(i) |
| 74 | } |
| 75 | |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 76 | rule.Build(ruleName, ruleDesc) |
Colin Cross | 9516a6c | 2019-06-14 18:51:12 -0700 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | return srcJarFiles |
Colin Cross | 6af17aa | 2017-09-20 12:59:05 -0700 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | func protoDeps(ctx android.BottomUpMutatorContext, p *android.ProtoProperties) { |
Sam Delmerico | 9f047d9 | 2022-02-03 20:27:31 +0000 | [diff] [blame] | 83 | const unspecifiedProtobufPluginType = "" |
Colin Cross | fe17f6f | 2019-03-28 19:30:56 -0700 | [diff] [blame] | 84 | if String(p.Proto.Plugin) == "" { |
| 85 | switch String(p.Proto.Type) { |
Sam Delmerico | 9f047d9 | 2022-02-03 20:27:31 +0000 | [diff] [blame] | 86 | case "stream": // does not require additional dependencies |
Colin Cross | fe17f6f | 2019-03-28 19:30:56 -0700 | [diff] [blame] | 87 | case "micro": |
| 88 | ctx.AddVariationDependencies(nil, staticLibTag, "libprotobuf-java-micro") |
| 89 | case "nano": |
| 90 | ctx.AddVariationDependencies(nil, staticLibTag, "libprotobuf-java-nano") |
Sam Delmerico | 9f047d9 | 2022-02-03 20:27:31 +0000 | [diff] [blame] | 91 | case "lite", unspecifiedProtobufPluginType: |
Colin Cross | fe17f6f | 2019-03-28 19:30:56 -0700 | [diff] [blame] | 92 | ctx.AddVariationDependencies(nil, staticLibTag, "libprotobuf-java-lite") |
| 93 | case "full": |
Liz Kammer | 3bf97bd | 2022-04-26 09:38:20 -0400 | [diff] [blame] | 94 | if ctx.Host() { |
Colin Cross | fe17f6f | 2019-03-28 19:30:56 -0700 | [diff] [blame] | 95 | ctx.AddVariationDependencies(nil, staticLibTag, "libprotobuf-java-full") |
| 96 | } else { |
| 97 | ctx.PropertyErrorf("proto.type", "full java protos only supported on the host") |
| 98 | } |
| 99 | default: |
| 100 | ctx.PropertyErrorf("proto.type", "unknown proto type %q", |
| 101 | String(p.Proto.Type)) |
Colin Cross | 6af17aa | 2017-09-20 12:59:05 -0700 | [diff] [blame] | 102 | } |
Colin Cross | 6af17aa | 2017-09-20 12:59:05 -0700 | [diff] [blame] | 103 | } |
| 104 | } |
| 105 | |
Jaewoong Jung | bc15e3a | 2021-03-10 17:02:43 -0800 | [diff] [blame] | 106 | func protoFlags(ctx android.ModuleContext, j *CommonProperties, p *android.ProtoProperties, |
Colin Cross | 0f2ee15 | 2017-12-14 15:22:43 -0800 | [diff] [blame] | 107 | flags javaBuilderFlags) javaBuilderFlags { |
| 108 | |
Colin Cross | 19878da | 2019-03-28 14:45:07 -0700 | [diff] [blame] | 109 | flags.proto = android.GetProtoFlags(ctx, p) |
| 110 | |
Colin Cross | fe17f6f | 2019-03-28 19:30:56 -0700 | [diff] [blame] | 111 | if String(p.Proto.Plugin) == "" { |
Colin Cross | 6f20509 | 2019-08-13 16:53:19 -0700 | [diff] [blame] | 112 | var typeToPlugin string |
Colin Cross | fe17f6f | 2019-03-28 19:30:56 -0700 | [diff] [blame] | 113 | switch String(p.Proto.Type) { |
Joe Onorato | 83fdc94 | 2021-10-28 15:05:59 -0700 | [diff] [blame] | 114 | case "stream": |
| 115 | flags.proto.OutTypeFlag = "--javastream_out" |
| 116 | typeToPlugin = "javastream" |
Colin Cross | fe17f6f | 2019-03-28 19:30:56 -0700 | [diff] [blame] | 117 | case "micro": |
| 118 | flags.proto.OutTypeFlag = "--javamicro_out" |
Colin Cross | 6f20509 | 2019-08-13 16:53:19 -0700 | [diff] [blame] | 119 | typeToPlugin = "javamicro" |
Colin Cross | fe17f6f | 2019-03-28 19:30:56 -0700 | [diff] [blame] | 120 | case "nano": |
| 121 | flags.proto.OutTypeFlag = "--javanano_out" |
Colin Cross | 6ac0460 | 2019-08-13 16:54:20 -0700 | [diff] [blame] | 122 | typeToPlugin = "javanano" |
Colin Cross | 0f97ada | 2019-10-25 18:24:13 -0700 | [diff] [blame] | 123 | case "lite", "": |
Colin Cross | fe17f6f | 2019-03-28 19:30:56 -0700 | [diff] [blame] | 124 | flags.proto.OutTypeFlag = "--java_out" |
| 125 | flags.proto.OutParams = append(flags.proto.OutParams, "lite") |
Colin Cross | 0f97ada | 2019-10-25 18:24:13 -0700 | [diff] [blame] | 126 | case "full": |
Colin Cross | fe17f6f | 2019-03-28 19:30:56 -0700 | [diff] [blame] | 127 | flags.proto.OutTypeFlag = "--java_out" |
| 128 | default: |
| 129 | ctx.PropertyErrorf("proto.type", "unknown proto type %q", |
| 130 | String(p.Proto.Type)) |
| 131 | } |
Colin Cross | 6f20509 | 2019-08-13 16:53:19 -0700 | [diff] [blame] | 132 | |
| 133 | if typeToPlugin != "" { |
| 134 | hostTool := ctx.Config().HostToolPath(ctx, "protoc-gen-"+typeToPlugin) |
| 135 | flags.proto.Deps = append(flags.proto.Deps, hostTool) |
| 136 | flags.proto.Flags = append(flags.proto.Flags, "--plugin=protoc-gen-"+typeToPlugin+"="+hostTool.String()) |
| 137 | } |
Colin Cross | 6af17aa | 2017-09-20 12:59:05 -0700 | [diff] [blame] | 138 | } |
Colin Cross | d5dbfb7 | 2017-11-14 13:11:23 -0800 | [diff] [blame] | 139 | |
Colin Cross | 19878da | 2019-03-28 14:45:07 -0700 | [diff] [blame] | 140 | flags.proto.OutParams = append(flags.proto.OutParams, j.Proto.Output_params...) |
Colin Cross | d5dbfb7 | 2017-11-14 13:11:23 -0800 | [diff] [blame] | 141 | |
Colin Cross | 6af17aa | 2017-09-20 12:59:05 -0700 | [diff] [blame] | 142 | return flags |
| 143 | } |
Sam Delmerico | c768102 | 2022-02-04 21:01:20 +0000 | [diff] [blame] | 144 | |
| 145 | type protoAttributes struct { |
Spandan Das | e8a90c5 | 2023-08-24 00:30:13 +0000 | [diff] [blame] | 146 | Deps bazel.LabelListAttribute |
| 147 | |
| 148 | // A list of proto_library targets that the proto_library in `deps` depends on |
| 149 | // This list is overestimation. |
| 150 | // Overestimation is necessary since Soong includes other protos via proto.include_dirs and not |
| 151 | // a specific .proto file module explicitly. |
| 152 | Transitive_deps bazel.LabelListAttribute |
| 153 | |
Zi Wang | 4dbd0e8 | 2023-09-21 16:00:45 -0700 | [diff] [blame] | 154 | // This is the libs and the static_libs of the original java_library module. |
| 155 | // On the bazel side, after proto sources are generated in java_*_proto_library, a java_library |
| 156 | // will compile them. The libs and static_libs from the original java_library module need |
| 157 | // to be linked because they are necessary in compile-time classpath. |
| 158 | Additional_proto_deps bazel.LabelListAttribute |
| 159 | |
Romain Jobredeaux | 2eef2e1 | 2023-02-24 12:07:08 -0500 | [diff] [blame] | 160 | Sdk_version bazel.StringAttribute |
| 161 | Java_version bazel.StringAttribute |
Zi Wang | dbaf6a9 | 2023-10-03 09:58:34 -0700 | [diff] [blame] | 162 | |
| 163 | Plugin bazel.LabelAttribute |
Sam Delmerico | c768102 | 2022-02-04 21:01:20 +0000 | [diff] [blame] | 164 | } |
| 165 | |
Zi Wang | 4dbd0e8 | 2023-09-21 16:00:45 -0700 | [diff] [blame] | 166 | func bp2buildProto(ctx android.Bp2buildMutatorContext, m *Module, protoSrcs bazel.LabelListAttribute, AdditionalProtoDeps bazel.LabelListAttribute) *bazel.Label { |
Sam Delmerico | c768102 | 2022-02-04 21:01:20 +0000 | [diff] [blame] | 167 | protoInfo, ok := android.Bp2buildProtoProperties(ctx, &m.ModuleBase, protoSrcs) |
| 168 | if !ok { |
| 169 | return nil |
| 170 | } |
| 171 | |
| 172 | typ := proptools.StringDefault(protoInfo.Type, protoTypeDefault) |
| 173 | var rule_class string |
| 174 | suffix := "_java_proto" |
| 175 | switch typ { |
| 176 | case "nano": |
| 177 | suffix += "_nano" |
| 178 | rule_class = "java_nano_proto_library" |
| 179 | case "micro": |
| 180 | suffix += "_micro" |
| 181 | rule_class = "java_micro_proto_library" |
| 182 | case "lite": |
| 183 | suffix += "_lite" |
| 184 | rule_class = "java_lite_proto_library" |
| 185 | case "stream": |
| 186 | suffix += "_stream" |
| 187 | rule_class = "java_stream_proto_library" |
| 188 | case "full": |
| 189 | rule_class = "java_proto_library" |
| 190 | default: |
| 191 | ctx.PropertyErrorf("proto.type", "cannot handle conversion at this time: %q", typ) |
| 192 | } |
| 193 | |
Zi Wang | dbaf6a9 | 2023-10-03 09:58:34 -0700 | [diff] [blame] | 194 | plugin := bazel.LabelAttribute{} |
| 195 | if m.protoProperties.Proto.Plugin != nil { |
| 196 | plugin.SetValue(android.BazelLabelForModuleDepSingle(ctx, "protoc-gen-"+*m.protoProperties.Proto.Plugin)) |
| 197 | } |
| 198 | |
Romain Jobredeaux | 2eef2e1 | 2023-02-24 12:07:08 -0500 | [diff] [blame] | 199 | protoAttrs := &protoAttributes{ |
Zi Wang | 4dbd0e8 | 2023-09-21 16:00:45 -0700 | [diff] [blame] | 200 | Deps: bazel.MakeLabelListAttribute(protoInfo.Proto_libs), |
| 201 | Transitive_deps: bazel.MakeLabelListAttribute(protoInfo.Transitive_proto_libs), |
| 202 | Additional_proto_deps: AdditionalProtoDeps, |
| 203 | Java_version: bazel.StringAttribute{Value: m.properties.Java_version}, |
| 204 | Sdk_version: bazel.StringAttribute{Value: m.deviceProperties.Sdk_version}, |
Zi Wang | dbaf6a9 | 2023-10-03 09:58:34 -0700 | [diff] [blame] | 205 | Plugin: plugin, |
Romain Jobredeaux | 2eef2e1 | 2023-02-24 12:07:08 -0500 | [diff] [blame] | 206 | } |
Sam Delmerico | c768102 | 2022-02-04 21:01:20 +0000 | [diff] [blame] | 207 | |
| 208 | name := m.Name() + suffix |
| 209 | |
| 210 | ctx.CreateBazelTargetModule( |
| 211 | bazel.BazelTargetModuleProperties{ |
| 212 | Rule_class: rule_class, |
| 213 | Bzl_load_location: "//build/bazel/rules/java:proto.bzl", |
| 214 | }, |
| 215 | android.CommonAttributes{Name: name}, |
Romain Jobredeaux | 2eef2e1 | 2023-02-24 12:07:08 -0500 | [diff] [blame] | 216 | protoAttrs) |
Sam Delmerico | c768102 | 2022-02-04 21:01:20 +0000 | [diff] [blame] | 217 | |
| 218 | return &bazel.Label{Label: ":" + name} |
| 219 | } |