blob: 428413f4c646ba6e8603332a3f38065faab53f74 [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 Crossd5dbfb72017-11-14 13:11:23 -080018 "strings"
19
Colin Cross6af17aa2017-09-20 12:59:05 -070020 "github.com/google/blueprint"
21 "github.com/google/blueprint/proptools"
22
23 "android/soong/android"
24)
25
26func init() {
27 pctx.HostBinToolVariable("protocCmd", "aprotoc")
28}
29
30var (
31 proto = pctx.AndroidStaticRule("protoc",
32 blueprint.RuleParams{
33 Command: `rm -rf $outDir && mkdir -p $outDir && ` +
Joe Onorato09e94ab2017-11-18 18:23:14 -080034 `$protocCmd $protoOut=$protoOutParams:$outDir $protoFlags $in && ` +
Colin Cross59149b62017-10-16 18:07:29 -070035 `${config.SoongZipCmd} -jar -o $out -C $outDir -D $outDir`,
36 CommandDeps: []string{
37 "$protocCmd",
38 "${config.SoongZipCmd}",
39 },
Joe Onorato09e94ab2017-11-18 18:23:14 -080040 }, "protoFlags", "protoOut", "protoOutParams", "outDir")
Colin Cross6af17aa2017-09-20 12:59:05 -070041)
42
Colin Cross59149b62017-10-16 18:07:29 -070043func genProto(ctx android.ModuleContext, outputSrcJar android.WritablePath,
Joe Onorato09e94ab2017-11-18 18:23:14 -080044 protoFiles android.Paths, protoFlags []string, protoOut, protoOutParams string) {
Colin Cross6af17aa2017-09-20 12:59:05 -070045
Colin Crossae887032017-10-23 17:16:14 -070046 ctx.Build(pctx, android.BuildParams{
Colin Cross6af17aa2017-09-20 12:59:05 -070047 Rule: proto,
48 Description: "protoc " + protoFiles[0].Rel(),
Colin Cross59149b62017-10-16 18:07:29 -070049 Output: outputSrcJar,
Colin Cross6af17aa2017-09-20 12:59:05 -070050 Inputs: protoFiles,
51 Args: map[string]string{
Joe Onorato09e94ab2017-11-18 18:23:14 -080052 "outDir": android.ProtoDir(ctx).String(),
53 "protoOut": protoOut,
54 "protoOutParams": protoOutParams,
55 "protoFlags": strings.Join(protoFlags, " "),
Colin Cross6af17aa2017-09-20 12:59:05 -070056 },
57 })
Colin Cross6af17aa2017-09-20 12:59:05 -070058}
59
60func protoDeps(ctx android.BottomUpMutatorContext, p *android.ProtoProperties) {
61 switch proptools.String(p.Proto.Type) {
62 case "micro":
63 ctx.AddDependency(ctx.Module(), staticLibTag, "libprotobuf-java-micro")
64 case "nano":
65 ctx.AddDependency(ctx.Module(), staticLibTag, "libprotobuf-java-nano")
Colin Cross6af17aa2017-09-20 12:59:05 -070066 case "lite", "":
67 ctx.AddDependency(ctx.Module(), staticLibTag, "libprotobuf-java-lite")
68 case "full":
69 if ctx.Host() {
70 ctx.AddDependency(ctx.Module(), staticLibTag, "libprotobuf-java-full")
71 } else {
72 ctx.PropertyErrorf("proto.type", "full java protos only supported on the host")
73 }
74 default:
75 ctx.PropertyErrorf("proto.type", "unknown proto type %q",
76 proptools.String(p.Proto.Type))
77 }
78}
79
80func protoFlags(ctx android.ModuleContext, p *android.ProtoProperties, flags javaBuilderFlags) javaBuilderFlags {
81 switch proptools.String(p.Proto.Type) {
82 case "micro":
Joe Onorato09e94ab2017-11-18 18:23:14 -080083 flags.protoOutTypeFlag = "--javamicro_out"
Colin Cross6af17aa2017-09-20 12:59:05 -070084 case "nano":
Joe Onorato09e94ab2017-11-18 18:23:14 -080085 flags.protoOutTypeFlag = "--javanano_out"
86 case "lite":
87 flags.protoOutTypeFlag = "--java_out"
88 flags.protoOutParams = "lite"
89 case "full", "":
90 flags.protoOutTypeFlag = "--java_out"
Colin Cross6af17aa2017-09-20 12:59:05 -070091 default:
92 ctx.PropertyErrorf("proto.type", "unknown proto type %q",
93 proptools.String(p.Proto.Type))
94 }
Colin Crossd5dbfb72017-11-14 13:11:23 -080095
96 flags.protoFlags = android.ProtoFlags(ctx, p)
97
Colin Cross6af17aa2017-09-20 12:59:05 -070098 return flags
99}