blob: cfd733ab754e1e3bd7ed0fefc8a5d708f7a67992 [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"
Colin Cross6af17aa2017-09-20 12:59:05 -070021
22 "android/soong/android"
23)
24
25func init() {
26 pctx.HostBinToolVariable("protocCmd", "aprotoc")
27}
28
29var (
30 proto = pctx.AndroidStaticRule("protoc",
31 blueprint.RuleParams{
Dan Willemsenab9f4262018-02-14 13:58:34 -080032 Command: `rm -rf $out.tmp && mkdir -p $out.tmp && ` +
33 `$protocCmd $protoOut=$protoOutParams:$out.tmp -I $protoBase $protoFlags $in && ` +
34 `${config.SoongZipCmd} -jar -o $out -C $out.tmp -D $out.tmp && rm -rf $out.tmp`,
Colin Cross59149b62017-10-16 18:07:29 -070035 CommandDeps: []string{
36 "$protocCmd",
37 "${config.SoongZipCmd}",
38 },
Dan Willemsenab9f4262018-02-14 13:58:34 -080039 }, "protoBase", "protoFlags", "protoOut", "protoOutParams")
Colin Cross6af17aa2017-09-20 12:59:05 -070040)
41
Dan Willemsenab9f4262018-02-14 13:58:34 -080042func genProto(ctx android.ModuleContext, protoFile android.Path, flags javaBuilderFlags) android.Path {
43 srcJarFile := android.GenPathWithExt(ctx, "proto", protoFile, "srcjar")
44
45 var protoBase string
46 if flags.protoRoot {
47 protoBase = "."
48 } else {
49 protoBase = strings.TrimSuffix(protoFile.String(), protoFile.Rel())
50 }
Colin Cross6af17aa2017-09-20 12:59:05 -070051
Colin Crossae887032017-10-23 17:16:14 -070052 ctx.Build(pctx, android.BuildParams{
Colin Cross6af17aa2017-09-20 12:59:05 -070053 Rule: proto,
Dan Willemsenab9f4262018-02-14 13:58:34 -080054 Description: "protoc " + protoFile.Rel(),
55 Output: srcJarFile,
56 Input: protoFile,
Colin Cross6af17aa2017-09-20 12:59:05 -070057 Args: map[string]string{
Dan Willemsenab9f4262018-02-14 13:58:34 -080058 "protoBase": protoBase,
59 "protoOut": flags.protoOutTypeFlag,
60 "protoOutParams": flags.protoOutParams,
61 "protoFlags": strings.Join(flags.protoFlags, " "),
Colin Cross6af17aa2017-09-20 12:59:05 -070062 },
63 })
Dan Willemsenab9f4262018-02-14 13:58:34 -080064
65 return srcJarFile
Colin Cross6af17aa2017-09-20 12:59:05 -070066}
67
68func protoDeps(ctx android.BottomUpMutatorContext, p *android.ProtoProperties) {
Colin Crossff3ae9d2018-04-10 16:15:18 -070069 switch String(p.Proto.Type) {
Colin Cross6af17aa2017-09-20 12:59:05 -070070 case "micro":
71 ctx.AddDependency(ctx.Module(), staticLibTag, "libprotobuf-java-micro")
72 case "nano":
73 ctx.AddDependency(ctx.Module(), staticLibTag, "libprotobuf-java-nano")
Colin Cross6af17aa2017-09-20 12:59:05 -070074 case "lite", "":
75 ctx.AddDependency(ctx.Module(), staticLibTag, "libprotobuf-java-lite")
76 case "full":
77 if ctx.Host() {
78 ctx.AddDependency(ctx.Module(), staticLibTag, "libprotobuf-java-full")
79 } else {
80 ctx.PropertyErrorf("proto.type", "full java protos only supported on the host")
81 }
82 default:
83 ctx.PropertyErrorf("proto.type", "unknown proto type %q",
Colin Crossff3ae9d2018-04-10 16:15:18 -070084 String(p.Proto.Type))
Colin Cross6af17aa2017-09-20 12:59:05 -070085 }
86}
87
Colin Cross0f2ee152017-12-14 15:22:43 -080088func protoFlags(ctx android.ModuleContext, j *CompilerProperties, p *android.ProtoProperties,
89 flags javaBuilderFlags) javaBuilderFlags {
90
Colin Crossff3ae9d2018-04-10 16:15:18 -070091 switch String(p.Proto.Type) {
Colin Cross6af17aa2017-09-20 12:59:05 -070092 case "micro":
Joe Onorato09e94ab2017-11-18 18:23:14 -080093 flags.protoOutTypeFlag = "--javamicro_out"
Colin Cross6af17aa2017-09-20 12:59:05 -070094 case "nano":
Joe Onorato09e94ab2017-11-18 18:23:14 -080095 flags.protoOutTypeFlag = "--javanano_out"
96 case "lite":
97 flags.protoOutTypeFlag = "--java_out"
98 flags.protoOutParams = "lite"
99 case "full", "":
100 flags.protoOutTypeFlag = "--java_out"
Colin Cross6af17aa2017-09-20 12:59:05 -0700101 default:
102 ctx.PropertyErrorf("proto.type", "unknown proto type %q",
Colin Crossff3ae9d2018-04-10 16:15:18 -0700103 String(p.Proto.Type))
Colin Cross6af17aa2017-09-20 12:59:05 -0700104 }
Colin Crossd5dbfb72017-11-14 13:11:23 -0800105
Colin Cross0f2ee152017-12-14 15:22:43 -0800106 if len(j.Proto.Output_params) > 0 {
107 if flags.protoOutParams != "" {
108 flags.protoOutParams += ","
109 }
110 flags.protoOutParams += strings.Join(j.Proto.Output_params, ",")
111 }
112
Colin Crossd5dbfb72017-11-14 13:11:23 -0800113 flags.protoFlags = android.ProtoFlags(ctx, p)
Dan Willemsenab9f4262018-02-14 13:58:34 -0800114 flags.protoRoot = android.ProtoCanonicalPathFromRoot(ctx, p)
Colin Crossd5dbfb72017-11-14 13:11:23 -0800115
Colin Cross6af17aa2017-09-20 12:59:05 -0700116 return flags
117}