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 && ` + |
| 34 | `$protocCmd $protoOut=$protoOutFlags:$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 | }, |
Colin Cross | 6af17aa | 2017-09-20 12:59:05 -0700 | [diff] [blame] | 40 | }, "protoFlags", "protoOut", "protoOutFlags", "outDir") |
| 41 | ) |
| 42 | |
Colin Cross | 59149b6 | 2017-10-16 18:07:29 -0700 | [diff] [blame] | 43 | func genProto(ctx android.ModuleContext, outputSrcJar android.WritablePath, |
Colin Cross | d5dbfb7 | 2017-11-14 13:11:23 -0800 | [diff] [blame^] | 44 | protoFiles android.Paths, protoFlags []string, protoOut, protoOutFlags 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{ |
| 52 | "outDir": android.ProtoDir(ctx).String(), |
| 53 | "protoOut": protoOut, |
| 54 | "protoOutFlags": protoOutFlags, |
Colin Cross | d5dbfb7 | 2017-11-14 13:11:23 -0800 | [diff] [blame^] | 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") |
| 66 | case "stream": |
| 67 | // TODO(ccross): add dependency on protoc-gen-java-stream binary |
| 68 | ctx.PropertyErrorf("proto.type", `"stream" not supported yet`) |
| 69 | // No library for stream protobufs |
| 70 | case "lite", "": |
| 71 | ctx.AddDependency(ctx.Module(), staticLibTag, "libprotobuf-java-lite") |
| 72 | case "full": |
| 73 | if ctx.Host() { |
| 74 | ctx.AddDependency(ctx.Module(), staticLibTag, "libprotobuf-java-full") |
| 75 | } else { |
| 76 | ctx.PropertyErrorf("proto.type", "full java protos only supported on the host") |
| 77 | } |
| 78 | default: |
| 79 | ctx.PropertyErrorf("proto.type", "unknown proto type %q", |
| 80 | proptools.String(p.Proto.Type)) |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | func protoFlags(ctx android.ModuleContext, p *android.ProtoProperties, flags javaBuilderFlags) javaBuilderFlags { |
| 85 | switch proptools.String(p.Proto.Type) { |
| 86 | case "micro": |
| 87 | flags.protoOutFlag = "--javamicro_out" |
| 88 | case "nano": |
| 89 | flags.protoOutFlag = "--javanano_out" |
| 90 | case "stream": |
| 91 | flags.protoOutFlag = "--javastream_out" |
Colin Cross | 647aa4f | 2017-10-05 13:41:32 -0700 | [diff] [blame] | 92 | case "lite", "full", "": |
Colin Cross | 6af17aa | 2017-09-20 12:59:05 -0700 | [diff] [blame] | 93 | flags.protoOutFlag = "--java_out" |
| 94 | default: |
| 95 | ctx.PropertyErrorf("proto.type", "unknown proto type %q", |
| 96 | proptools.String(p.Proto.Type)) |
| 97 | } |
Colin Cross | d5dbfb7 | 2017-11-14 13:11:23 -0800 | [diff] [blame^] | 98 | |
| 99 | flags.protoFlags = android.ProtoFlags(ctx, p) |
| 100 | |
Colin Cross | 6af17aa | 2017-09-20 12:59:05 -0700 | [diff] [blame] | 101 | return flags |
| 102 | } |