blob: eeb5124d9430bea73acd080b1eb1260908e39e7c [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 && ` +
34 `$protocCmd $protoOut=$protoOutFlags:$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 },
Colin Cross6af17aa2017-09-20 12:59:05 -070040 }, "protoFlags", "protoOut", "protoOutFlags", "outDir")
41)
42
Colin Cross59149b62017-10-16 18:07:29 -070043func genProto(ctx android.ModuleContext, outputSrcJar android.WritablePath,
Colin Crossd5dbfb72017-11-14 13:11:23 -080044 protoFiles android.Paths, protoFlags []string, protoOut, protoOutFlags 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{
52 "outDir": android.ProtoDir(ctx).String(),
53 "protoOut": protoOut,
54 "protoOutFlags": protoOutFlags,
Colin Crossd5dbfb72017-11-14 13:11:23 -080055 "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")
66 case "stream":
67 // TODO(ccross): add dependency on protoc-gen-java-stream binary
68 ctx.PropertyErrorf("proto.type", `"stream" not supported yet`)
69 // No library for stream protobufs
70 case "lite", "":
71 ctx.AddDependency(ctx.Module(), staticLibTag, "libprotobuf-java-lite")
72 case "full":
73 if ctx.Host() {
74 ctx.AddDependency(ctx.Module(), staticLibTag, "libprotobuf-java-full")
75 } else {
76 ctx.PropertyErrorf("proto.type", "full java protos only supported on the host")
77 }
78 default:
79 ctx.PropertyErrorf("proto.type", "unknown proto type %q",
80 proptools.String(p.Proto.Type))
81 }
82}
83
84func protoFlags(ctx android.ModuleContext, p *android.ProtoProperties, flags javaBuilderFlags) javaBuilderFlags {
85 switch proptools.String(p.Proto.Type) {
86 case "micro":
87 flags.protoOutFlag = "--javamicro_out"
88 case "nano":
89 flags.protoOutFlag = "--javanano_out"
90 case "stream":
91 flags.protoOutFlag = "--javastream_out"
Colin Cross647aa4f2017-10-05 13:41:32 -070092 case "lite", "full", "":
Colin Cross6af17aa2017-09-20 12:59:05 -070093 flags.protoOutFlag = "--java_out"
94 default:
95 ctx.PropertyErrorf("proto.type", "unknown proto type %q",
96 proptools.String(p.Proto.Type))
97 }
Colin Crossd5dbfb72017-11-14 13:11:23 -080098
99 flags.protoFlags = android.ProtoFlags(ctx, p)
100
Colin Cross6af17aa2017-09-20 12:59:05 -0700101 return flags
102}