blob: 0bb37c5bcf6e7cbf4b7510613e513133b2d0060c [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")
Dan Willemsena10504e2018-05-17 10:11:17 -070027 pctx.HostBinToolVariable("depFixCmd", "dep_fixer")
Colin Cross6af17aa2017-09-20 12:59:05 -070028}
29
30var (
31 proto = pctx.AndroidStaticRule("protoc",
32 blueprint.RuleParams{
Dan Willemsenab9f4262018-02-14 13:58:34 -080033 Command: `rm -rf $out.tmp && mkdir -p $out.tmp && ` +
Dan Willemsen43398532018-02-21 02:10:29 -080034 `$protocCmd $protoOut=$protoOutParams:$out.tmp --dependency_out=$out.d -I $protoBase $protoFlags $in && ` +
Dan Willemsena10504e2018-05-17 10:11:17 -070035 `$depFixCmd $out.d && ` +
Dan Willemsenab9f4262018-02-14 13:58:34 -080036 `${config.SoongZipCmd} -jar -o $out -C $out.tmp -D $out.tmp && rm -rf $out.tmp`,
Colin Cross59149b62017-10-16 18:07:29 -070037 CommandDeps: []string{
38 "$protocCmd",
Dan Willemsena10504e2018-05-17 10:11:17 -070039 "$depFixCmd",
Colin Cross59149b62017-10-16 18:07:29 -070040 "${config.SoongZipCmd}",
41 },
Dan Willemsen43398532018-02-21 02:10:29 -080042 Depfile: "${out}.d",
43 Deps: blueprint.DepsGCC,
Dan Willemsenab9f4262018-02-14 13:58:34 -080044 }, "protoBase", "protoFlags", "protoOut", "protoOutParams")
Colin Cross6af17aa2017-09-20 12:59:05 -070045)
46
Dan Willemsenab9f4262018-02-14 13:58:34 -080047func genProto(ctx android.ModuleContext, protoFile android.Path, flags javaBuilderFlags) android.Path {
48 srcJarFile := android.GenPathWithExt(ctx, "proto", protoFile, "srcjar")
49
50 var protoBase string
51 if flags.protoRoot {
52 protoBase = "."
53 } else {
54 protoBase = strings.TrimSuffix(protoFile.String(), protoFile.Rel())
55 }
Colin Cross6af17aa2017-09-20 12:59:05 -070056
Colin Crossae887032017-10-23 17:16:14 -070057 ctx.Build(pctx, android.BuildParams{
Colin Cross6af17aa2017-09-20 12:59:05 -070058 Rule: proto,
Dan Willemsenab9f4262018-02-14 13:58:34 -080059 Description: "protoc " + protoFile.Rel(),
60 Output: srcJarFile,
61 Input: protoFile,
Colin Cross5a5aca02018-10-26 13:24:01 -070062 Implicits: flags.protoDeps,
Colin Cross6af17aa2017-09-20 12:59:05 -070063 Args: map[string]string{
Dan Willemsenab9f4262018-02-14 13:58:34 -080064 "protoBase": protoBase,
65 "protoOut": flags.protoOutTypeFlag,
66 "protoOutParams": flags.protoOutParams,
67 "protoFlags": strings.Join(flags.protoFlags, " "),
Colin Cross6af17aa2017-09-20 12:59:05 -070068 },
69 })
Dan Willemsenab9f4262018-02-14 13:58:34 -080070
71 return srcJarFile
Colin Cross6af17aa2017-09-20 12:59:05 -070072}
73
74func protoDeps(ctx android.BottomUpMutatorContext, p *android.ProtoProperties) {
Colin Crossff3ae9d2018-04-10 16:15:18 -070075 switch String(p.Proto.Type) {
Colin Cross6af17aa2017-09-20 12:59:05 -070076 case "micro":
Colin Cross42d48b72018-08-29 14:10:52 -070077 ctx.AddVariationDependencies(nil, staticLibTag, "libprotobuf-java-micro")
Colin Cross6af17aa2017-09-20 12:59:05 -070078 case "nano":
Colin Cross42d48b72018-08-29 14:10:52 -070079 ctx.AddVariationDependencies(nil, staticLibTag, "libprotobuf-java-nano")
Colin Cross6af17aa2017-09-20 12:59:05 -070080 case "lite", "":
Colin Cross42d48b72018-08-29 14:10:52 -070081 ctx.AddVariationDependencies(nil, staticLibTag, "libprotobuf-java-lite")
Colin Cross6af17aa2017-09-20 12:59:05 -070082 case "full":
83 if ctx.Host() {
Colin Cross42d48b72018-08-29 14:10:52 -070084 ctx.AddVariationDependencies(nil, staticLibTag, "libprotobuf-java-full")
Colin Cross6af17aa2017-09-20 12:59:05 -070085 } else {
86 ctx.PropertyErrorf("proto.type", "full java protos only supported on the host")
87 }
88 default:
89 ctx.PropertyErrorf("proto.type", "unknown proto type %q",
Colin Crossff3ae9d2018-04-10 16:15:18 -070090 String(p.Proto.Type))
Colin Cross6af17aa2017-09-20 12:59:05 -070091 }
92}
93
Colin Cross0f2ee152017-12-14 15:22:43 -080094func protoFlags(ctx android.ModuleContext, j *CompilerProperties, p *android.ProtoProperties,
95 flags javaBuilderFlags) javaBuilderFlags {
96
Colin Cross5a5aca02018-10-26 13:24:01 -070097 var plugin string
98
Colin Crossff3ae9d2018-04-10 16:15:18 -070099 switch String(p.Proto.Type) {
Colin Cross6af17aa2017-09-20 12:59:05 -0700100 case "micro":
Joe Onorato09e94ab2017-11-18 18:23:14 -0800101 flags.protoOutTypeFlag = "--javamicro_out"
Colin Cross6af17aa2017-09-20 12:59:05 -0700102 case "nano":
Joe Onorato09e94ab2017-11-18 18:23:14 -0800103 flags.protoOutTypeFlag = "--javanano_out"
104 case "lite":
Colin Cross5a5aca02018-10-26 13:24:01 -0700105 plugin = "protoc-gen-javalite"
106 flags.protoOutTypeFlag = "--javalite_out"
Joe Onorato09e94ab2017-11-18 18:23:14 -0800107 case "full", "":
108 flags.protoOutTypeFlag = "--java_out"
Colin Cross6af17aa2017-09-20 12:59:05 -0700109 default:
110 ctx.PropertyErrorf("proto.type", "unknown proto type %q",
Colin Crossff3ae9d2018-04-10 16:15:18 -0700111 String(p.Proto.Type))
Colin Cross6af17aa2017-09-20 12:59:05 -0700112 }
Colin Crossd5dbfb72017-11-14 13:11:23 -0800113
Colin Cross5a5aca02018-10-26 13:24:01 -0700114 flags.protoOutParams = strings.Join(j.Proto.Output_params, ",")
Colin Crossd5dbfb72017-11-14 13:11:23 -0800115 flags.protoFlags = android.ProtoFlags(ctx, p)
Dan Willemsenab9f4262018-02-14 13:58:34 -0800116 flags.protoRoot = android.ProtoCanonicalPathFromRoot(ctx, p)
Colin Crossd5dbfb72017-11-14 13:11:23 -0800117
Colin Cross5a5aca02018-10-26 13:24:01 -0700118 if plugin != "" {
119 path := ctx.Config().HostToolPath(ctx, plugin)
120 flags.protoDeps = append(flags.protoDeps, path)
121 flags.protoFlags = append(flags.protoFlags, "--plugin="+path.String())
122 }
123
Colin Cross6af17aa2017-09-20 12:59:05 -0700124 return flags
125}