blob: 1077fd69be3af3491006663184d74d51cef9388e [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 (
18 "github.com/google/blueprint"
19 "github.com/google/blueprint/proptools"
20
21 "android/soong/android"
22)
23
24func init() {
25 pctx.HostBinToolVariable("protocCmd", "aprotoc")
26}
27
28var (
29 proto = pctx.AndroidStaticRule("protoc",
30 blueprint.RuleParams{
31 Command: `rm -rf $outDir && mkdir -p $outDir && ` +
32 `$protocCmd $protoOut=$protoOutFlags:$outDir $protoFlags $in && ` +
Colin Cross59149b62017-10-16 18:07:29 -070033 `${config.SoongZipCmd} -jar -o $out -C $outDir -D $outDir`,
34 CommandDeps: []string{
35 "$protocCmd",
36 "${config.SoongZipCmd}",
37 },
Colin Cross6af17aa2017-09-20 12:59:05 -070038 }, "protoFlags", "protoOut", "protoOutFlags", "outDir")
39)
40
Colin Cross59149b62017-10-16 18:07:29 -070041func genProto(ctx android.ModuleContext, outputSrcJar android.WritablePath,
42 protoFiles android.Paths, protoFlags string, protoOut, protoOutFlags string) {
Colin Cross6af17aa2017-09-20 12:59:05 -070043
Colin Crossae887032017-10-23 17:16:14 -070044 ctx.Build(pctx, android.BuildParams{
Colin Cross6af17aa2017-09-20 12:59:05 -070045 Rule: proto,
46 Description: "protoc " + protoFiles[0].Rel(),
Colin Cross59149b62017-10-16 18:07:29 -070047 Output: outputSrcJar,
Colin Cross6af17aa2017-09-20 12:59:05 -070048 Inputs: protoFiles,
49 Args: map[string]string{
50 "outDir": android.ProtoDir(ctx).String(),
51 "protoOut": protoOut,
52 "protoOutFlags": protoOutFlags,
53 "protoFlags": protoFlags,
54 },
55 })
Colin Cross6af17aa2017-09-20 12:59:05 -070056}
57
58func protoDeps(ctx android.BottomUpMutatorContext, p *android.ProtoProperties) {
59 switch proptools.String(p.Proto.Type) {
60 case "micro":
61 ctx.AddDependency(ctx.Module(), staticLibTag, "libprotobuf-java-micro")
62 case "nano":
63 ctx.AddDependency(ctx.Module(), staticLibTag, "libprotobuf-java-nano")
64 case "stream":
65 // TODO(ccross): add dependency on protoc-gen-java-stream binary
66 ctx.PropertyErrorf("proto.type", `"stream" not supported yet`)
67 // No library for stream protobufs
68 case "lite", "":
69 ctx.AddDependency(ctx.Module(), staticLibTag, "libprotobuf-java-lite")
70 case "full":
71 if ctx.Host() {
72 ctx.AddDependency(ctx.Module(), staticLibTag, "libprotobuf-java-full")
73 } else {
74 ctx.PropertyErrorf("proto.type", "full java protos only supported on the host")
75 }
76 default:
77 ctx.PropertyErrorf("proto.type", "unknown proto type %q",
78 proptools.String(p.Proto.Type))
79 }
80}
81
82func protoFlags(ctx android.ModuleContext, p *android.ProtoProperties, flags javaBuilderFlags) javaBuilderFlags {
83 switch proptools.String(p.Proto.Type) {
84 case "micro":
85 flags.protoOutFlag = "--javamicro_out"
86 case "nano":
87 flags.protoOutFlag = "--javanano_out"
88 case "stream":
89 flags.protoOutFlag = "--javastream_out"
Colin Cross647aa4f2017-10-05 13:41:32 -070090 case "lite", "full", "":
Colin Cross6af17aa2017-09-20 12:59:05 -070091 flags.protoOutFlag = "--java_out"
92 default:
93 ctx.PropertyErrorf("proto.type", "unknown proto type %q",
94 proptools.String(p.Proto.Type))
95 }
96 return flags
97}