| 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 | 6af17aa | 2017-09-20 12:59:05 -0700 | [diff] [blame] | 18 | "android/soong/android" | 
|  | 19 | ) | 
|  | 20 |  | 
| Colin Cross | fe17f6f | 2019-03-28 19:30:56 -0700 | [diff] [blame] | 21 | func genProto(ctx android.ModuleContext, protoFile android.Path, flags android.ProtoFlags) android.Path { | 
| Dan Willemsen | ab9f426 | 2018-02-14 13:58:34 -0800 | [diff] [blame] | 22 | srcJarFile := android.GenPathWithExt(ctx, "proto", protoFile, "srcjar") | 
|  | 23 |  | 
| Colin Cross | 19878da | 2019-03-28 14:45:07 -0700 | [diff] [blame] | 24 | outDir := srcJarFile.ReplaceExtension(ctx, "tmp") | 
|  | 25 | depFile := srcJarFile.ReplaceExtension(ctx, "srcjar.d") | 
| Colin Cross | 6af17aa | 2017-09-20 12:59:05 -0700 | [diff] [blame] | 26 |  | 
| Colin Cross | 19878da | 2019-03-28 14:45:07 -0700 | [diff] [blame] | 27 | rule := android.NewRuleBuilder() | 
|  | 28 |  | 
|  | 29 | rule.Command().Text("rm -rf").Flag(outDir.String()) | 
|  | 30 | rule.Command().Text("mkdir -p").Flag(outDir.String()) | 
|  | 31 |  | 
| Colin Cross | fe17f6f | 2019-03-28 19:30:56 -0700 | [diff] [blame] | 32 | android.ProtoRule(ctx, rule, protoFile, flags, flags.Deps, outDir, depFile, nil) | 
| Colin Cross | 19878da | 2019-03-28 14:45:07 -0700 | [diff] [blame] | 33 |  | 
|  | 34 | // Proto generated java files have an unknown package name in the path, so package the entire output directory | 
|  | 35 | // into a srcjar. | 
|  | 36 | rule.Command(). | 
| Colin Cross | ee94d6a | 2019-07-08 17:08:34 -0700 | [diff] [blame] | 37 | BuiltTool(ctx, "soong_zip"). | 
| Colin Cross | 19878da | 2019-03-28 14:45:07 -0700 | [diff] [blame] | 38 | Flag("-jar"). | 
|  | 39 | FlagWithOutput("-o ", srcJarFile). | 
|  | 40 | FlagWithArg("-C ", outDir.String()). | 
|  | 41 | FlagWithArg("-D ", outDir.String()) | 
|  | 42 |  | 
|  | 43 | rule.Command().Text("rm -rf").Flag(outDir.String()) | 
|  | 44 |  | 
|  | 45 | rule.Build(pctx, ctx, "protoc_"+protoFile.Rel(), "protoc "+protoFile.Rel()) | 
| Dan Willemsen | ab9f426 | 2018-02-14 13:58:34 -0800 | [diff] [blame] | 46 |  | 
|  | 47 | return srcJarFile | 
| Colin Cross | 6af17aa | 2017-09-20 12:59:05 -0700 | [diff] [blame] | 48 | } | 
|  | 49 |  | 
|  | 50 | func protoDeps(ctx android.BottomUpMutatorContext, p *android.ProtoProperties) { | 
| Colin Cross | fe17f6f | 2019-03-28 19:30:56 -0700 | [diff] [blame] | 51 | if String(p.Proto.Plugin) == "" { | 
|  | 52 | switch String(p.Proto.Type) { | 
|  | 53 | case "micro": | 
|  | 54 | ctx.AddVariationDependencies(nil, staticLibTag, "libprotobuf-java-micro") | 
|  | 55 | case "nano": | 
|  | 56 | ctx.AddVariationDependencies(nil, staticLibTag, "libprotobuf-java-nano") | 
|  | 57 | case "lite", "": | 
|  | 58 | ctx.AddVariationDependencies(nil, staticLibTag, "libprotobuf-java-lite") | 
|  | 59 | case "full": | 
|  | 60 | if ctx.Host() { | 
|  | 61 | ctx.AddVariationDependencies(nil, staticLibTag, "libprotobuf-java-full") | 
|  | 62 | } else { | 
|  | 63 | ctx.PropertyErrorf("proto.type", "full java protos only supported on the host") | 
|  | 64 | } | 
|  | 65 | default: | 
|  | 66 | ctx.PropertyErrorf("proto.type", "unknown proto type %q", | 
|  | 67 | String(p.Proto.Type)) | 
| Colin Cross | 6af17aa | 2017-09-20 12:59:05 -0700 | [diff] [blame] | 68 | } | 
| Colin Cross | 6af17aa | 2017-09-20 12:59:05 -0700 | [diff] [blame] | 69 | } | 
|  | 70 | } | 
|  | 71 |  | 
| Colin Cross | 0f2ee15 | 2017-12-14 15:22:43 -0800 | [diff] [blame] | 72 | func protoFlags(ctx android.ModuleContext, j *CompilerProperties, p *android.ProtoProperties, | 
|  | 73 | flags javaBuilderFlags) javaBuilderFlags { | 
|  | 74 |  | 
| Colin Cross | 19878da | 2019-03-28 14:45:07 -0700 | [diff] [blame] | 75 | flags.proto = android.GetProtoFlags(ctx, p) | 
|  | 76 |  | 
| Colin Cross | fe17f6f | 2019-03-28 19:30:56 -0700 | [diff] [blame] | 77 | if String(p.Proto.Plugin) == "" { | 
| Colin Cross | 6f20509 | 2019-08-13 16:53:19 -0700 | [diff] [blame] | 78 | var typeToPlugin string | 
| Colin Cross | fe17f6f | 2019-03-28 19:30:56 -0700 | [diff] [blame] | 79 | switch String(p.Proto.Type) { | 
|  | 80 | case "micro": | 
|  | 81 | flags.proto.OutTypeFlag = "--javamicro_out" | 
| Colin Cross | 6f20509 | 2019-08-13 16:53:19 -0700 | [diff] [blame] | 82 | typeToPlugin = "javamicro" | 
| Colin Cross | fe17f6f | 2019-03-28 19:30:56 -0700 | [diff] [blame] | 83 | case "nano": | 
|  | 84 | flags.proto.OutTypeFlag = "--javanano_out" | 
| Colin Cross | 6ac0460 | 2019-08-13 16:54:20 -0700 | [diff] [blame] | 85 | typeToPlugin = "javanano" | 
| Colin Cross | fe17f6f | 2019-03-28 19:30:56 -0700 | [diff] [blame] | 86 | case "lite": | 
|  | 87 | flags.proto.OutTypeFlag = "--java_out" | 
|  | 88 | flags.proto.OutParams = append(flags.proto.OutParams, "lite") | 
|  | 89 | case "full", "": | 
|  | 90 | flags.proto.OutTypeFlag = "--java_out" | 
|  | 91 | default: | 
|  | 92 | ctx.PropertyErrorf("proto.type", "unknown proto type %q", | 
|  | 93 | String(p.Proto.Type)) | 
|  | 94 | } | 
| Colin Cross | 6f20509 | 2019-08-13 16:53:19 -0700 | [diff] [blame] | 95 |  | 
|  | 96 | if typeToPlugin != "" { | 
|  | 97 | hostTool := ctx.Config().HostToolPath(ctx, "protoc-gen-"+typeToPlugin) | 
|  | 98 | flags.proto.Deps = append(flags.proto.Deps, hostTool) | 
|  | 99 | flags.proto.Flags = append(flags.proto.Flags, "--plugin=protoc-gen-"+typeToPlugin+"="+hostTool.String()) | 
|  | 100 | } | 
| Colin Cross | 6af17aa | 2017-09-20 12:59:05 -0700 | [diff] [blame] | 101 | } | 
| Colin Cross | d5dbfb7 | 2017-11-14 13:11:23 -0800 | [diff] [blame] | 102 |  | 
| Colin Cross | 19878da | 2019-03-28 14:45:07 -0700 | [diff] [blame] | 103 | flags.proto.OutParams = append(flags.proto.OutParams, j.Proto.Output_params...) | 
| Colin Cross | d5dbfb7 | 2017-11-14 13:11:23 -0800 | [diff] [blame] | 104 |  | 
| Colin Cross | 6af17aa | 2017-09-20 12:59:05 -0700 | [diff] [blame] | 105 | return flags | 
|  | 106 | } |