| 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 ( | 
|  | 18 | "github.com/google/blueprint" | 
|  | 19 | "github.com/google/blueprint/proptools" | 
|  | 20 |  | 
|  | 21 | "android/soong/android" | 
|  | 22 | ) | 
|  | 23 |  | 
|  | 24 | func init() { | 
|  | 25 | pctx.HostBinToolVariable("protocCmd", "aprotoc") | 
|  | 26 | } | 
|  | 27 |  | 
|  | 28 | var ( | 
|  | 29 | proto = pctx.AndroidStaticRule("protoc", | 
|  | 30 | blueprint.RuleParams{ | 
|  | 31 | Command: `rm -rf $outDir && mkdir -p $outDir && ` + | 
|  | 32 | `$protocCmd $protoOut=$protoOutFlags:$outDir $protoFlags $in && ` + | 
|  | 33 | `find $outDir -name "*.java" > $out`, | 
|  | 34 | CommandDeps: []string{"$protocCmd"}, | 
|  | 35 | }, "protoFlags", "protoOut", "protoOutFlags", "outDir") | 
|  | 36 | ) | 
|  | 37 |  | 
|  | 38 | func genProto(ctx android.ModuleContext, protoFiles android.Paths, | 
|  | 39 | protoFlags string, protoOut, protoOutFlags string) android.WritablePath { | 
|  | 40 |  | 
|  | 41 | protoFileList := android.PathForModuleGen(ctx, "proto.filelist") | 
|  | 42 |  | 
|  | 43 | ctx.ModuleBuild(pctx, android.ModuleBuildParams{ | 
|  | 44 | Rule:        proto, | 
|  | 45 | Description: "protoc " + protoFiles[0].Rel(), | 
|  | 46 | Output:      protoFileList, | 
|  | 47 | Inputs:      protoFiles, | 
|  | 48 | Args: map[string]string{ | 
|  | 49 | "outDir":        android.ProtoDir(ctx).String(), | 
|  | 50 | "protoOut":      protoOut, | 
|  | 51 | "protoOutFlags": protoOutFlags, | 
|  | 52 | "protoFlags":    protoFlags, | 
|  | 53 | }, | 
|  | 54 | }) | 
|  | 55 |  | 
|  | 56 | return protoFileList | 
|  | 57 | } | 
|  | 58 |  | 
|  | 59 | func protoDeps(ctx android.BottomUpMutatorContext, p *android.ProtoProperties) { | 
|  | 60 | switch proptools.String(p.Proto.Type) { | 
|  | 61 | case "micro": | 
|  | 62 | ctx.AddDependency(ctx.Module(), staticLibTag, "libprotobuf-java-micro") | 
|  | 63 | case "nano": | 
|  | 64 | ctx.AddDependency(ctx.Module(), staticLibTag, "libprotobuf-java-nano") | 
|  | 65 | case "stream": | 
|  | 66 | // TODO(ccross): add dependency on protoc-gen-java-stream binary | 
|  | 67 | ctx.PropertyErrorf("proto.type", `"stream" not supported yet`) | 
|  | 68 | // No library for stream protobufs | 
|  | 69 | case "lite", "": | 
|  | 70 | ctx.AddDependency(ctx.Module(), staticLibTag, "libprotobuf-java-lite") | 
|  | 71 | case "full": | 
|  | 72 | if ctx.Host() { | 
|  | 73 | ctx.AddDependency(ctx.Module(), staticLibTag, "libprotobuf-java-full") | 
|  | 74 | } else { | 
|  | 75 | ctx.PropertyErrorf("proto.type", "full java protos only supported on the host") | 
|  | 76 | } | 
|  | 77 | default: | 
|  | 78 | ctx.PropertyErrorf("proto.type", "unknown proto type %q", | 
|  | 79 | proptools.String(p.Proto.Type)) | 
|  | 80 | } | 
|  | 81 | } | 
|  | 82 |  | 
|  | 83 | func protoFlags(ctx android.ModuleContext, p *android.ProtoProperties, flags javaBuilderFlags) javaBuilderFlags { | 
|  | 84 | switch proptools.String(p.Proto.Type) { | 
|  | 85 | case "micro": | 
|  | 86 | flags.protoOutFlag = "--javamicro_out" | 
|  | 87 | case "nano": | 
|  | 88 | flags.protoOutFlag = "--javanano_out" | 
|  | 89 | case "stream": | 
|  | 90 | flags.protoOutFlag = "--javastream_out" | 
| Colin Cross | 647aa4f | 2017-10-05 13:41:32 -0700 | [diff] [blame] | 91 | case "lite", "full", "": | 
| Colin Cross | 6af17aa | 2017-09-20 12:59:05 -0700 | [diff] [blame] | 92 | flags.protoOutFlag = "--java_out" | 
|  | 93 | default: | 
|  | 94 | ctx.PropertyErrorf("proto.type", "unknown proto type %q", | 
|  | 95 | proptools.String(p.Proto.Type)) | 
|  | 96 | } | 
|  | 97 | return flags | 
|  | 98 | } |