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{ |
| 33 | Command: `rm -rf $outDir && mkdir -p $outDir && ` + |
Joe Onorato | 09e94ab | 2017-11-18 18:23:14 -0800 | [diff] [blame] | 34 | `$protocCmd $protoOut=$protoOutParams:$outDir $protoFlags $in && ` + |
Colin Cross | 59149b6 | 2017-10-16 18:07:29 -0700 | [diff] [blame] | 35 | `${config.SoongZipCmd} -jar -o $out -C $outDir -D $outDir`, |
| 36 | CommandDeps: []string{ |
| 37 | "$protocCmd", |
| 38 | "${config.SoongZipCmd}", |
| 39 | }, |
Joe Onorato | 09e94ab | 2017-11-18 18:23:14 -0800 | [diff] [blame] | 40 | }, "protoFlags", "protoOut", "protoOutParams", "outDir") |
Colin Cross | 6af17aa | 2017-09-20 12:59:05 -0700 | [diff] [blame] | 41 | ) |
| 42 | |
Colin Cross | 59149b6 | 2017-10-16 18:07:29 -0700 | [diff] [blame] | 43 | func genProto(ctx android.ModuleContext, outputSrcJar android.WritablePath, |
Joe Onorato | 09e94ab | 2017-11-18 18:23:14 -0800 | [diff] [blame] | 44 | protoFiles android.Paths, protoFlags []string, protoOut, protoOutParams string) { |
Colin Cross | 6af17aa | 2017-09-20 12:59:05 -0700 | [diff] [blame] | 45 | |
Colin Cross | ae88703 | 2017-10-23 17:16:14 -0700 | [diff] [blame] | 46 | ctx.Build(pctx, android.BuildParams{ |
Colin Cross | 6af17aa | 2017-09-20 12:59:05 -0700 | [diff] [blame] | 47 | Rule: proto, |
| 48 | Description: "protoc " + protoFiles[0].Rel(), |
Colin Cross | 59149b6 | 2017-10-16 18:07:29 -0700 | [diff] [blame] | 49 | Output: outputSrcJar, |
Colin Cross | 6af17aa | 2017-09-20 12:59:05 -0700 | [diff] [blame] | 50 | Inputs: protoFiles, |
| 51 | Args: map[string]string{ |
Joe Onorato | 09e94ab | 2017-11-18 18:23:14 -0800 | [diff] [blame] | 52 | "outDir": android.ProtoDir(ctx).String(), |
| 53 | "protoOut": protoOut, |
| 54 | "protoOutParams": protoOutParams, |
| 55 | "protoFlags": strings.Join(protoFlags, " "), |
Colin Cross | 6af17aa | 2017-09-20 12:59:05 -0700 | [diff] [blame] | 56 | }, |
| 57 | }) |
Colin Cross | 6af17aa | 2017-09-20 12:59:05 -0700 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | func 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 Cross | 6af17aa | 2017-09-20 12:59:05 -0700 | [diff] [blame] | 66 | 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 | |
| 80 | func protoFlags(ctx android.ModuleContext, p *android.ProtoProperties, flags javaBuilderFlags) javaBuilderFlags { |
| 81 | switch proptools.String(p.Proto.Type) { |
| 82 | case "micro": |
Joe Onorato | 09e94ab | 2017-11-18 18:23:14 -0800 | [diff] [blame] | 83 | flags.protoOutTypeFlag = "--javamicro_out" |
Colin Cross | 6af17aa | 2017-09-20 12:59:05 -0700 | [diff] [blame] | 84 | case "nano": |
Joe Onorato | 09e94ab | 2017-11-18 18:23:14 -0800 | [diff] [blame] | 85 | flags.protoOutTypeFlag = "--javanano_out" |
| 86 | case "lite": |
| 87 | flags.protoOutTypeFlag = "--java_out" |
| 88 | flags.protoOutParams = "lite" |
| 89 | case "full", "": |
| 90 | flags.protoOutTypeFlag = "--java_out" |
Colin Cross | 6af17aa | 2017-09-20 12:59:05 -0700 | [diff] [blame] | 91 | default: |
| 92 | ctx.PropertyErrorf("proto.type", "unknown proto type %q", |
| 93 | proptools.String(p.Proto.Type)) |
| 94 | } |
Colin Cross | d5dbfb7 | 2017-11-14 13:11:23 -0800 | [diff] [blame] | 95 | |
| 96 | flags.protoFlags = android.ProtoFlags(ctx, p) |
| 97 | |
Colin Cross | 6af17aa | 2017-09-20 12:59:05 -0700 | [diff] [blame] | 98 | return flags |
| 99 | } |