Colin Cross | 6af17aa | 2017-09-20 12:59:05 -0700 | [diff] [blame] | 1 | // 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 | |
| 15 | package java |
| 16 | |
| 17 | import ( |
Colin Cross | d5dbfb7 | 2017-11-14 13:11:23 -0800 | [diff] [blame] | 18 | "strings" |
| 19 | |
Colin Cross | 6af17aa | 2017-09-20 12:59:05 -0700 | [diff] [blame] | 20 | "github.com/google/blueprint" |
| 21 | "github.com/google/blueprint/proptools" |
| 22 | |
| 23 | "android/soong/android" |
| 24 | ) |
| 25 | |
| 26 | func init() { |
| 27 | pctx.HostBinToolVariable("protocCmd", "aprotoc") |
| 28 | } |
| 29 | |
| 30 | var ( |
| 31 | proto = pctx.AndroidStaticRule("protoc", |
| 32 | blueprint.RuleParams{ |
Dan Willemsen | ab9f426 | 2018-02-14 13:58:34 -0800 | [diff] [blame^] | 33 | Command: `rm -rf $out.tmp && mkdir -p $out.tmp && ` + |
| 34 | `$protocCmd $protoOut=$protoOutParams:$out.tmp -I $protoBase $protoFlags $in && ` + |
| 35 | `${config.SoongZipCmd} -jar -o $out -C $out.tmp -D $out.tmp && rm -rf $out.tmp`, |
Colin Cross | 59149b6 | 2017-10-16 18:07:29 -0700 | [diff] [blame] | 36 | CommandDeps: []string{ |
| 37 | "$protocCmd", |
| 38 | "${config.SoongZipCmd}", |
| 39 | }, |
Dan Willemsen | ab9f426 | 2018-02-14 13:58:34 -0800 | [diff] [blame^] | 40 | }, "protoBase", "protoFlags", "protoOut", "protoOutParams") |
Colin Cross | 6af17aa | 2017-09-20 12:59:05 -0700 | [diff] [blame] | 41 | ) |
| 42 | |
Dan Willemsen | ab9f426 | 2018-02-14 13:58:34 -0800 | [diff] [blame^] | 43 | func genProto(ctx android.ModuleContext, protoFile android.Path, flags javaBuilderFlags) android.Path { |
| 44 | srcJarFile := android.GenPathWithExt(ctx, "proto", protoFile, "srcjar") |
| 45 | |
| 46 | var protoBase string |
| 47 | if flags.protoRoot { |
| 48 | protoBase = "." |
| 49 | } else { |
| 50 | protoBase = strings.TrimSuffix(protoFile.String(), protoFile.Rel()) |
| 51 | } |
Colin Cross | 6af17aa | 2017-09-20 12:59:05 -0700 | [diff] [blame] | 52 | |
Colin Cross | ae88703 | 2017-10-23 17:16:14 -0700 | [diff] [blame] | 53 | ctx.Build(pctx, android.BuildParams{ |
Colin Cross | 6af17aa | 2017-09-20 12:59:05 -0700 | [diff] [blame] | 54 | Rule: proto, |
Dan Willemsen | ab9f426 | 2018-02-14 13:58:34 -0800 | [diff] [blame^] | 55 | Description: "protoc " + protoFile.Rel(), |
| 56 | Output: srcJarFile, |
| 57 | Input: protoFile, |
Colin Cross | 6af17aa | 2017-09-20 12:59:05 -0700 | [diff] [blame] | 58 | Args: map[string]string{ |
Dan Willemsen | ab9f426 | 2018-02-14 13:58:34 -0800 | [diff] [blame^] | 59 | "protoBase": protoBase, |
| 60 | "protoOut": flags.protoOutTypeFlag, |
| 61 | "protoOutParams": flags.protoOutParams, |
| 62 | "protoFlags": strings.Join(flags.protoFlags, " "), |
Colin Cross | 6af17aa | 2017-09-20 12:59:05 -0700 | [diff] [blame] | 63 | }, |
| 64 | }) |
Dan Willemsen | ab9f426 | 2018-02-14 13:58:34 -0800 | [diff] [blame^] | 65 | |
| 66 | return srcJarFile |
Colin Cross | 6af17aa | 2017-09-20 12:59:05 -0700 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | func protoDeps(ctx android.BottomUpMutatorContext, p *android.ProtoProperties) { |
| 70 | switch proptools.String(p.Proto.Type) { |
| 71 | case "micro": |
| 72 | ctx.AddDependency(ctx.Module(), staticLibTag, "libprotobuf-java-micro") |
| 73 | case "nano": |
| 74 | ctx.AddDependency(ctx.Module(), staticLibTag, "libprotobuf-java-nano") |
Colin Cross | 6af17aa | 2017-09-20 12:59:05 -0700 | [diff] [blame] | 75 | case "lite", "": |
| 76 | ctx.AddDependency(ctx.Module(), staticLibTag, "libprotobuf-java-lite") |
| 77 | case "full": |
| 78 | if ctx.Host() { |
| 79 | ctx.AddDependency(ctx.Module(), staticLibTag, "libprotobuf-java-full") |
| 80 | } else { |
| 81 | ctx.PropertyErrorf("proto.type", "full java protos only supported on the host") |
| 82 | } |
| 83 | default: |
| 84 | ctx.PropertyErrorf("proto.type", "unknown proto type %q", |
| 85 | proptools.String(p.Proto.Type)) |
| 86 | } |
| 87 | } |
| 88 | |
Colin Cross | 0f2ee15 | 2017-12-14 15:22:43 -0800 | [diff] [blame] | 89 | func protoFlags(ctx android.ModuleContext, j *CompilerProperties, p *android.ProtoProperties, |
| 90 | flags javaBuilderFlags) javaBuilderFlags { |
| 91 | |
Colin Cross | 6af17aa | 2017-09-20 12:59:05 -0700 | [diff] [blame] | 92 | switch proptools.String(p.Proto.Type) { |
| 93 | case "micro": |
Joe Onorato | 09e94ab | 2017-11-18 18:23:14 -0800 | [diff] [blame] | 94 | flags.protoOutTypeFlag = "--javamicro_out" |
Colin Cross | 6af17aa | 2017-09-20 12:59:05 -0700 | [diff] [blame] | 95 | case "nano": |
Joe Onorato | 09e94ab | 2017-11-18 18:23:14 -0800 | [diff] [blame] | 96 | flags.protoOutTypeFlag = "--javanano_out" |
| 97 | case "lite": |
| 98 | flags.protoOutTypeFlag = "--java_out" |
| 99 | flags.protoOutParams = "lite" |
| 100 | case "full", "": |
| 101 | flags.protoOutTypeFlag = "--java_out" |
Colin Cross | 6af17aa | 2017-09-20 12:59:05 -0700 | [diff] [blame] | 102 | default: |
| 103 | ctx.PropertyErrorf("proto.type", "unknown proto type %q", |
| 104 | proptools.String(p.Proto.Type)) |
| 105 | } |
Colin Cross | d5dbfb7 | 2017-11-14 13:11:23 -0800 | [diff] [blame] | 106 | |
Colin Cross | 0f2ee15 | 2017-12-14 15:22:43 -0800 | [diff] [blame] | 107 | if len(j.Proto.Output_params) > 0 { |
| 108 | if flags.protoOutParams != "" { |
| 109 | flags.protoOutParams += "," |
| 110 | } |
| 111 | flags.protoOutParams += strings.Join(j.Proto.Output_params, ",") |
| 112 | } |
| 113 | |
Colin Cross | d5dbfb7 | 2017-11-14 13:11:23 -0800 | [diff] [blame] | 114 | flags.protoFlags = android.ProtoFlags(ctx, p) |
Dan Willemsen | ab9f426 | 2018-02-14 13:58:34 -0800 | [diff] [blame^] | 115 | flags.protoRoot = android.ProtoCanonicalPathFromRoot(ctx, p) |
Colin Cross | d5dbfb7 | 2017-11-14 13:11:23 -0800 | [diff] [blame] | 116 | |
Colin Cross | 6af17aa | 2017-09-20 12:59:05 -0700 | [diff] [blame] | 117 | return flags |
| 118 | } |