blob: 8de9e26da408ae0af2f00f12000aa3c4c1a0eaaa [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 Cross6af17aa2017-09-20 12:59:05 -070018 "android/soong/android"
19)
20
Dan Willemsenab9f4262018-02-14 13:58:34 -080021func genProto(ctx android.ModuleContext, protoFile android.Path, flags javaBuilderFlags) android.Path {
22 srcJarFile := android.GenPathWithExt(ctx, "proto", protoFile, "srcjar")
23
Colin Cross19878da2019-03-28 14:45:07 -070024 outDir := srcJarFile.ReplaceExtension(ctx, "tmp")
25 depFile := srcJarFile.ReplaceExtension(ctx, "srcjar.d")
Colin Cross6af17aa2017-09-20 12:59:05 -070026
Colin Cross19878da2019-03-28 14:45:07 -070027 rule := android.NewRuleBuilder()
28
29 rule.Command().Text("rm -rf").Flag(outDir.String())
30 rule.Command().Text("mkdir -p").Flag(outDir.String())
31
32 android.ProtoRule(ctx, rule, protoFile, flags.proto, nil, outDir, depFile, nil)
33
34 // Proto generated java files have an unknown package name in the path, so package the entire output directory
35 // into a srcjar.
36 rule.Command().
37 Tool(ctx.Config().HostToolPath(ctx, "soong_zip")).
38 Flag("-jar").
39 FlagWithOutput("-o ", srcJarFile).
40 FlagWithArg("-C ", outDir.String()).
41 FlagWithArg("-D ", outDir.String())
42
43 rule.Command().Text("rm -rf").Flag(outDir.String())
44
45 rule.Build(pctx, ctx, "protoc_"+protoFile.Rel(), "protoc "+protoFile.Rel())
Dan Willemsenab9f4262018-02-14 13:58:34 -080046
47 return srcJarFile
Colin Cross6af17aa2017-09-20 12:59:05 -070048}
49
50func protoDeps(ctx android.BottomUpMutatorContext, p *android.ProtoProperties) {
Colin Crossff3ae9d2018-04-10 16:15:18 -070051 switch String(p.Proto.Type) {
Colin Cross6af17aa2017-09-20 12:59:05 -070052 case "micro":
Colin Cross42d48b72018-08-29 14:10:52 -070053 ctx.AddVariationDependencies(nil, staticLibTag, "libprotobuf-java-micro")
Colin Cross6af17aa2017-09-20 12:59:05 -070054 case "nano":
Colin Cross42d48b72018-08-29 14:10:52 -070055 ctx.AddVariationDependencies(nil, staticLibTag, "libprotobuf-java-nano")
Colin Cross6af17aa2017-09-20 12:59:05 -070056 case "lite", "":
Colin Cross42d48b72018-08-29 14:10:52 -070057 ctx.AddVariationDependencies(nil, staticLibTag, "libprotobuf-java-lite")
Colin Cross6af17aa2017-09-20 12:59:05 -070058 case "full":
59 if ctx.Host() {
Colin Cross42d48b72018-08-29 14:10:52 -070060 ctx.AddVariationDependencies(nil, staticLibTag, "libprotobuf-java-full")
Colin Cross6af17aa2017-09-20 12:59:05 -070061 } else {
62 ctx.PropertyErrorf("proto.type", "full java protos only supported on the host")
63 }
64 default:
65 ctx.PropertyErrorf("proto.type", "unknown proto type %q",
Colin Crossff3ae9d2018-04-10 16:15:18 -070066 String(p.Proto.Type))
Colin Cross6af17aa2017-09-20 12:59:05 -070067 }
68}
69
Colin Cross0f2ee152017-12-14 15:22:43 -080070func protoFlags(ctx android.ModuleContext, j *CompilerProperties, p *android.ProtoProperties,
71 flags javaBuilderFlags) javaBuilderFlags {
72
Colin Cross19878da2019-03-28 14:45:07 -070073 flags.proto = android.GetProtoFlags(ctx, p)
74
Colin Crossff3ae9d2018-04-10 16:15:18 -070075 switch String(p.Proto.Type) {
Colin Cross6af17aa2017-09-20 12:59:05 -070076 case "micro":
Colin Cross19878da2019-03-28 14:45:07 -070077 flags.proto.OutTypeFlag = "--javamicro_out"
Colin Cross6af17aa2017-09-20 12:59:05 -070078 case "nano":
Colin Cross19878da2019-03-28 14:45:07 -070079 flags.proto.OutTypeFlag = "--javanano_out"
Joe Onorato09e94ab2017-11-18 18:23:14 -080080 case "lite":
Colin Cross19878da2019-03-28 14:45:07 -070081 flags.proto.OutTypeFlag = "--java_out"
82 flags.proto.OutParams = append(flags.proto.OutParams, "lite")
Joe Onorato09e94ab2017-11-18 18:23:14 -080083 case "full", "":
Colin Cross19878da2019-03-28 14:45:07 -070084 flags.proto.OutTypeFlag = "--java_out"
Colin Cross6af17aa2017-09-20 12:59:05 -070085 default:
86 ctx.PropertyErrorf("proto.type", "unknown proto type %q",
Colin Crossff3ae9d2018-04-10 16:15:18 -070087 String(p.Proto.Type))
Colin Cross6af17aa2017-09-20 12:59:05 -070088 }
Colin Crossd5dbfb72017-11-14 13:11:23 -080089
Colin Cross19878da2019-03-28 14:45:07 -070090 flags.proto.OutParams = append(flags.proto.OutParams, j.Proto.Output_params...)
Colin Crossd5dbfb72017-11-14 13:11:23 -080091
Colin Cross6af17aa2017-09-20 12:59:05 -070092 return flags
93}