blob: 0ec64997e2ee2b610421ffca299c5e0f68012c8e [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
Colin Crossfe17f6f2019-03-28 19:30:56 -070021func genProto(ctx android.ModuleContext, protoFile android.Path, flags android.ProtoFlags) android.Path {
Dan Willemsenab9f4262018-02-14 13:58:34 -080022 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
Colin Crossfe17f6f2019-03-28 19:30:56 -070032 android.ProtoRule(ctx, rule, protoFile, flags, flags.Deps, outDir, depFile, nil)
Colin Cross19878da2019-03-28 14:45:07 -070033
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().
Colin Crossee94d6a2019-07-08 17:08:34 -070037 BuiltTool(ctx, "soong_zip").
Colin Cross19878da2019-03-28 14:45:07 -070038 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 Crossfe17f6f2019-03-28 19:30:56 -070051 if String(p.Proto.Plugin) == "" {
52 switch String(p.Proto.Type) {
53 case "micro":
54 ctx.AddVariationDependencies(nil, staticLibTag, "libprotobuf-java-micro")
55 case "nano":
56 ctx.AddVariationDependencies(nil, staticLibTag, "libprotobuf-java-nano")
57 case "lite", "":
58 ctx.AddVariationDependencies(nil, staticLibTag, "libprotobuf-java-lite")
59 case "full":
60 if ctx.Host() {
61 ctx.AddVariationDependencies(nil, staticLibTag, "libprotobuf-java-full")
62 } else {
63 ctx.PropertyErrorf("proto.type", "full java protos only supported on the host")
64 }
65 default:
66 ctx.PropertyErrorf("proto.type", "unknown proto type %q",
67 String(p.Proto.Type))
Colin Cross6af17aa2017-09-20 12:59:05 -070068 }
Colin Cross6af17aa2017-09-20 12:59:05 -070069 }
70}
71
Colin Cross0f2ee152017-12-14 15:22:43 -080072func protoFlags(ctx android.ModuleContext, j *CompilerProperties, p *android.ProtoProperties,
73 flags javaBuilderFlags) javaBuilderFlags {
74
Colin Cross19878da2019-03-28 14:45:07 -070075 flags.proto = android.GetProtoFlags(ctx, p)
76
Colin Crossfe17f6f2019-03-28 19:30:56 -070077 if String(p.Proto.Plugin) == "" {
78 switch String(p.Proto.Type) {
79 case "micro":
80 flags.proto.OutTypeFlag = "--javamicro_out"
81 case "nano":
82 flags.proto.OutTypeFlag = "--javanano_out"
83 case "lite":
84 flags.proto.OutTypeFlag = "--java_out"
85 flags.proto.OutParams = append(flags.proto.OutParams, "lite")
86 case "full", "":
87 flags.proto.OutTypeFlag = "--java_out"
88 default:
89 ctx.PropertyErrorf("proto.type", "unknown proto type %q",
90 String(p.Proto.Type))
91 }
Colin Cross6af17aa2017-09-20 12:59:05 -070092 }
Colin Crossd5dbfb72017-11-14 13:11:23 -080093
Colin Cross19878da2019-03-28 14:45:07 -070094 flags.proto.OutParams = append(flags.proto.OutParams, j.Proto.Output_params...)
Colin Crossd5dbfb72017-11-14 13:11:23 -080095
Colin Cross6af17aa2017-09-20 12:59:05 -070096 return flags
97}