blob: 5280077f12c4e3c6d5807f60162efd5d3013de56 [file] [log] [blame]
Colin Cross6af17aa2017-09-20 12:59:05 -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 java
16
17import (
Colin Cross9516a6c2019-06-14 18:51:12 -070018 "path/filepath"
19 "strconv"
20
Colin Cross6af17aa2017-09-20 12:59:05 -070021 "android/soong/android"
Sam Delmericoc7681022022-02-04 21:01:20 +000022 "android/soong/bazel"
23
24 "github.com/google/blueprint/proptools"
25)
26
27const (
28 protoTypeDefault = "lite"
Colin Cross6af17aa2017-09-20 12:59:05 -070029)
30
Colin Cross9516a6c2019-06-14 18:51:12 -070031func 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 Cross93fa5ef2021-10-18 10:19:03 -070034 shards := android.ShardPaths(protoFiles, 50)
Dan Willemsenab9f4262018-02-14 13:58:34 -080035
Colin Cross9516a6c2019-06-14 18:51:12 -070036 srcJarFiles := make(android.Paths, 0, len(shards))
Colin Cross6af17aa2017-09-20 12:59:05 -070037
Colin Cross9516a6c2019-06-14 18:51:12 -070038 for i, shard := range shards {
39 srcJarFile := android.PathForModuleGen(ctx, "proto", "proto"+strconv.Itoa(i)+".srcjar")
40 srcJarFiles = append(srcJarFiles, srcJarFile)
Colin Cross19878da2019-03-28 14:45:07 -070041
Colin Cross9516a6c2019-06-14 18:51:12 -070042 outDir := srcJarFile.ReplaceExtension(ctx, "tmp")
Colin Cross19878da2019-03-28 14:45:07 -070043
Colin Crossf1a035e2020-11-16 17:32:30 -080044 rule := android.NewRuleBuilder(pctx, ctx)
Colin Cross19878da2019-03-28 14:45:07 -070045
Colin Cross9516a6c2019-06-14 18:51:12 -070046 rule.Command().Text("rm -rf").Flag(outDir.String())
47 rule.Command().Text("mkdir -p").Flag(outDir.String())
Colin Cross19878da2019-03-28 14:45:07 -070048
Colin Cross9516a6c2019-06-14 18:51:12 -070049 for _, protoFile := range shard {
50 depFile := srcJarFile.InSameDir(ctx, protoFile.String()+".d")
51 rule.Command().Text("mkdir -p").Flag(filepath.Dir(depFile.String()))
Colin Crossf1a035e2020-11-16 17:32:30 -080052 android.ProtoRule(rule, protoFile, flags, flags.Deps, outDir, depFile, nil)
Colin Cross9516a6c2019-06-14 18:51:12 -070053 }
Colin Cross19878da2019-03-28 14:45:07 -070054
Colin Cross9516a6c2019-06-14 18:51:12 -070055 // 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 Crossf1a035e2020-11-16 17:32:30 -080058 BuiltTool("soong_zip").
Colin Crosscf02ec82020-12-23 17:13:16 -080059 Flag("-srcjar").
Colin Cross9516a6c2019-06-14 18:51:12 -070060 Flag("-write_if_changed").
61 FlagWithOutput("-o ", srcJarFile).
62 FlagWithArg("-C ", outDir.String()).
63 FlagWithArg("-D ", outDir.String())
Dan Willemsenab9f4262018-02-14 13:58:34 -080064
Colin Cross9516a6c2019-06-14 18:51:12 -070065 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 Crossf1a035e2020-11-16 17:32:30 -080076 rule.Build(ruleName, ruleDesc)
Colin Cross9516a6c2019-06-14 18:51:12 -070077 }
78
79 return srcJarFiles
Colin Cross6af17aa2017-09-20 12:59:05 -070080}
81
82func protoDeps(ctx android.BottomUpMutatorContext, p *android.ProtoProperties) {
Sam Delmerico9f047d92022-02-03 20:27:31 +000083 const unspecifiedProtobufPluginType = ""
Colin Crossfe17f6f2019-03-28 19:30:56 -070084 if String(p.Proto.Plugin) == "" {
85 switch String(p.Proto.Type) {
Sam Delmerico9f047d92022-02-03 20:27:31 +000086 case "stream": // does not require additional dependencies
Colin Crossfe17f6f2019-03-28 19:30:56 -070087 case "micro":
88 ctx.AddVariationDependencies(nil, staticLibTag, "libprotobuf-java-micro")
89 case "nano":
90 ctx.AddVariationDependencies(nil, staticLibTag, "libprotobuf-java-nano")
Sam Delmerico9f047d92022-02-03 20:27:31 +000091 case "lite", unspecifiedProtobufPluginType:
Colin Crossfe17f6f2019-03-28 19:30:56 -070092 ctx.AddVariationDependencies(nil, staticLibTag, "libprotobuf-java-lite")
93 case "full":
Liz Kammer3bf97bd2022-04-26 09:38:20 -040094 if ctx.Host() {
Colin Crossfe17f6f2019-03-28 19:30:56 -070095 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 Cross6af17aa2017-09-20 12:59:05 -0700102 }
Colin Cross6af17aa2017-09-20 12:59:05 -0700103 }
104}
105
Jaewoong Jungbc15e3a2021-03-10 17:02:43 -0800106func protoFlags(ctx android.ModuleContext, j *CommonProperties, p *android.ProtoProperties,
Colin Cross0f2ee152017-12-14 15:22:43 -0800107 flags javaBuilderFlags) javaBuilderFlags {
108
Colin Cross19878da2019-03-28 14:45:07 -0700109 flags.proto = android.GetProtoFlags(ctx, p)
110
Colin Crossfe17f6f2019-03-28 19:30:56 -0700111 if String(p.Proto.Plugin) == "" {
Colin Cross6f205092019-08-13 16:53:19 -0700112 var typeToPlugin string
Colin Crossfe17f6f2019-03-28 19:30:56 -0700113 switch String(p.Proto.Type) {
Joe Onorato83fdc942021-10-28 15:05:59 -0700114 case "stream":
115 flags.proto.OutTypeFlag = "--javastream_out"
116 typeToPlugin = "javastream"
Colin Crossfe17f6f2019-03-28 19:30:56 -0700117 case "micro":
118 flags.proto.OutTypeFlag = "--javamicro_out"
Colin Cross6f205092019-08-13 16:53:19 -0700119 typeToPlugin = "javamicro"
Colin Crossfe17f6f2019-03-28 19:30:56 -0700120 case "nano":
121 flags.proto.OutTypeFlag = "--javanano_out"
Colin Cross6ac04602019-08-13 16:54:20 -0700122 typeToPlugin = "javanano"
Colin Cross0f97ada2019-10-25 18:24:13 -0700123 case "lite", "":
Colin Crossfe17f6f2019-03-28 19:30:56 -0700124 flags.proto.OutTypeFlag = "--java_out"
125 flags.proto.OutParams = append(flags.proto.OutParams, "lite")
Colin Cross0f97ada2019-10-25 18:24:13 -0700126 case "full":
Colin Crossfe17f6f2019-03-28 19:30:56 -0700127 flags.proto.OutTypeFlag = "--java_out"
128 default:
129 ctx.PropertyErrorf("proto.type", "unknown proto type %q",
130 String(p.Proto.Type))
131 }
Colin Cross6f205092019-08-13 16:53:19 -0700132
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 Cross6af17aa2017-09-20 12:59:05 -0700138 }
Colin Crossd5dbfb72017-11-14 13:11:23 -0800139
Colin Cross19878da2019-03-28 14:45:07 -0700140 flags.proto.OutParams = append(flags.proto.OutParams, j.Proto.Output_params...)
Colin Crossd5dbfb72017-11-14 13:11:23 -0800141
Colin Cross6af17aa2017-09-20 12:59:05 -0700142 return flags
143}
Sam Delmericoc7681022022-02-04 21:01:20 +0000144
145type protoAttributes struct {
146 Deps bazel.LabelListAttribute
147}
148
149func bp2buildProto(ctx android.Bp2buildMutatorContext, m *Module, protoSrcs bazel.LabelListAttribute) *bazel.Label {
150 protoInfo, ok := android.Bp2buildProtoProperties(ctx, &m.ModuleBase, protoSrcs)
151 if !ok {
152 return nil
153 }
154
155 typ := proptools.StringDefault(protoInfo.Type, protoTypeDefault)
156 var rule_class string
157 suffix := "_java_proto"
158 switch typ {
159 case "nano":
160 suffix += "_nano"
161 rule_class = "java_nano_proto_library"
162 case "micro":
163 suffix += "_micro"
164 rule_class = "java_micro_proto_library"
165 case "lite":
166 suffix += "_lite"
167 rule_class = "java_lite_proto_library"
168 case "stream":
169 suffix += "_stream"
170 rule_class = "java_stream_proto_library"
171 case "full":
172 rule_class = "java_proto_library"
173 default:
174 ctx.PropertyErrorf("proto.type", "cannot handle conversion at this time: %q", typ)
175 }
176
177 protoLabel := bazel.Label{Label: ":" + m.Name() + "_proto"}
178 var protoAttrs protoAttributes
179 protoAttrs.Deps.SetValue(bazel.LabelList{Includes: []bazel.Label{protoLabel}})
180
181 name := m.Name() + suffix
182
183 ctx.CreateBazelTargetModule(
184 bazel.BazelTargetModuleProperties{
185 Rule_class: rule_class,
186 Bzl_load_location: "//build/bazel/rules/java:proto.bzl",
187 },
188 android.CommonAttributes{Name: name},
189 &protoAttrs)
190
191 return &bazel.Label{Label: ":" + name}
192}