blob: 226fac0868383e91efc2633138e2622c32cdfc4d [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
Colin Cross0f2ee152017-12-14 15:22:43 -080080func protoFlags(ctx android.ModuleContext, j *CompilerProperties, p *android.ProtoProperties,
81 flags javaBuilderFlags) javaBuilderFlags {
82
Colin Cross6af17aa2017-09-20 12:59:05 -070083 switch proptools.String(p.Proto.Type) {
84 case "micro":
Joe Onorato09e94ab2017-11-18 18:23:14 -080085 flags.protoOutTypeFlag = "--javamicro_out"
Colin Cross6af17aa2017-09-20 12:59:05 -070086 case "nano":
Joe Onorato09e94ab2017-11-18 18:23:14 -080087 flags.protoOutTypeFlag = "--javanano_out"
88 case "lite":
89 flags.protoOutTypeFlag = "--java_out"
90 flags.protoOutParams = "lite"
91 case "full", "":
92 flags.protoOutTypeFlag = "--java_out"
Colin Cross6af17aa2017-09-20 12:59:05 -070093 default:
94 ctx.PropertyErrorf("proto.type", "unknown proto type %q",
95 proptools.String(p.Proto.Type))
96 }
Colin Crossd5dbfb72017-11-14 13:11:23 -080097
Colin Cross0f2ee152017-12-14 15:22:43 -080098 if len(j.Proto.Output_params) > 0 {
99 if flags.protoOutParams != "" {
100 flags.protoOutParams += ","
101 }
102 flags.protoOutParams += strings.Join(j.Proto.Output_params, ",")
103 }
104
Colin Crossd5dbfb72017-11-14 13:11:23 -0800105 flags.protoFlags = android.ProtoFlags(ctx, p)
106
Colin Cross6af17aa2017-09-20 12:59:05 -0700107 return flags
108}