| 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 | 9516a6c | 2019-06-14 18:51:12 -0700 | [diff] [blame] | 18 | "path/filepath" | 
|  | 19 | "strconv" | 
|  | 20 |  | 
| Colin Cross | 6af17aa | 2017-09-20 12:59:05 -0700 | [diff] [blame] | 21 | "android/soong/android" | 
|  | 22 | ) | 
|  | 23 |  | 
| Colin Cross | 9516a6c | 2019-06-14 18:51:12 -0700 | [diff] [blame] | 24 | func genProto(ctx android.ModuleContext, protoFiles android.Paths, flags android.ProtoFlags) android.Paths { | 
|  | 25 | // Shard proto files into groups of 100 to avoid having to recompile all of them if one changes and to avoid | 
|  | 26 | // hitting command line length limits. | 
|  | 27 | shards := android.ShardPaths(protoFiles, 100) | 
| Dan Willemsen | ab9f426 | 2018-02-14 13:58:34 -0800 | [diff] [blame] | 28 |  | 
| Colin Cross | 9516a6c | 2019-06-14 18:51:12 -0700 | [diff] [blame] | 29 | srcJarFiles := make(android.Paths, 0, len(shards)) | 
| Colin Cross | 6af17aa | 2017-09-20 12:59:05 -0700 | [diff] [blame] | 30 |  | 
| Colin Cross | 9516a6c | 2019-06-14 18:51:12 -0700 | [diff] [blame] | 31 | for i, shard := range shards { | 
|  | 32 | srcJarFile := android.PathForModuleGen(ctx, "proto", "proto"+strconv.Itoa(i)+".srcjar") | 
|  | 33 | srcJarFiles = append(srcJarFiles, srcJarFile) | 
| Colin Cross | 19878da | 2019-03-28 14:45:07 -0700 | [diff] [blame] | 34 |  | 
| Colin Cross | 9516a6c | 2019-06-14 18:51:12 -0700 | [diff] [blame] | 35 | outDir := srcJarFile.ReplaceExtension(ctx, "tmp") | 
| Colin Cross | 19878da | 2019-03-28 14:45:07 -0700 | [diff] [blame] | 36 |  | 
| Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 37 | rule := android.NewRuleBuilder(pctx, ctx) | 
| Colin Cross | 19878da | 2019-03-28 14:45:07 -0700 | [diff] [blame] | 38 |  | 
| Colin Cross | 9516a6c | 2019-06-14 18:51:12 -0700 | [diff] [blame] | 39 | rule.Command().Text("rm -rf").Flag(outDir.String()) | 
|  | 40 | rule.Command().Text("mkdir -p").Flag(outDir.String()) | 
| Colin Cross | 19878da | 2019-03-28 14:45:07 -0700 | [diff] [blame] | 41 |  | 
| Colin Cross | 9516a6c | 2019-06-14 18:51:12 -0700 | [diff] [blame] | 42 | for _, protoFile := range shard { | 
|  | 43 | depFile := srcJarFile.InSameDir(ctx, protoFile.String()+".d") | 
|  | 44 | rule.Command().Text("mkdir -p").Flag(filepath.Dir(depFile.String())) | 
| Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 45 | android.ProtoRule(rule, protoFile, flags, flags.Deps, outDir, depFile, nil) | 
| Colin Cross | 9516a6c | 2019-06-14 18:51:12 -0700 | [diff] [blame] | 46 | } | 
| Colin Cross | 19878da | 2019-03-28 14:45:07 -0700 | [diff] [blame] | 47 |  | 
| Colin Cross | 9516a6c | 2019-06-14 18:51:12 -0700 | [diff] [blame] | 48 | // Proto generated java files have an unknown package name in the path, so package the entire output directory | 
|  | 49 | // into a srcjar. | 
|  | 50 | rule.Command(). | 
| Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 51 | BuiltTool("soong_zip"). | 
| Colin Cross | cf02ec8 | 2020-12-23 17:13:16 -0800 | [diff] [blame] | 52 | Flag("-srcjar"). | 
| Colin Cross | 9516a6c | 2019-06-14 18:51:12 -0700 | [diff] [blame] | 53 | Flag("-write_if_changed"). | 
|  | 54 | FlagWithOutput("-o ", srcJarFile). | 
|  | 55 | FlagWithArg("-C ", outDir.String()). | 
|  | 56 | FlagWithArg("-D ", outDir.String()) | 
| Dan Willemsen | ab9f426 | 2018-02-14 13:58:34 -0800 | [diff] [blame] | 57 |  | 
| Colin Cross | 9516a6c | 2019-06-14 18:51:12 -0700 | [diff] [blame] | 58 | rule.Command().Text("rm -rf").Flag(outDir.String()) | 
|  | 59 |  | 
|  | 60 | rule.Restat() | 
|  | 61 |  | 
|  | 62 | ruleName := "protoc" | 
|  | 63 | ruleDesc := "protoc" | 
|  | 64 | if len(shards) > 1 { | 
|  | 65 | ruleName += "_" + strconv.Itoa(i) | 
|  | 66 | ruleDesc += " " + strconv.Itoa(i) | 
|  | 67 | } | 
|  | 68 |  | 
| Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 69 | rule.Build(ruleName, ruleDesc) | 
| Colin Cross | 9516a6c | 2019-06-14 18:51:12 -0700 | [diff] [blame] | 70 | } | 
|  | 71 |  | 
|  | 72 | return srcJarFiles | 
| Colin Cross | 6af17aa | 2017-09-20 12:59:05 -0700 | [diff] [blame] | 73 | } | 
|  | 74 |  | 
|  | 75 | func protoDeps(ctx android.BottomUpMutatorContext, p *android.ProtoProperties) { | 
| Colin Cross | fe17f6f | 2019-03-28 19:30:56 -0700 | [diff] [blame] | 76 | if String(p.Proto.Plugin) == "" { | 
|  | 77 | switch String(p.Proto.Type) { | 
|  | 78 | case "micro": | 
|  | 79 | ctx.AddVariationDependencies(nil, staticLibTag, "libprotobuf-java-micro") | 
|  | 80 | case "nano": | 
|  | 81 | ctx.AddVariationDependencies(nil, staticLibTag, "libprotobuf-java-nano") | 
|  | 82 | case "lite", "": | 
|  | 83 | ctx.AddVariationDependencies(nil, staticLibTag, "libprotobuf-java-lite") | 
|  | 84 | case "full": | 
| Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 85 | if ctx.Host() || ctx.BazelConversionMode() { | 
| Colin Cross | fe17f6f | 2019-03-28 19:30:56 -0700 | [diff] [blame] | 86 | ctx.AddVariationDependencies(nil, staticLibTag, "libprotobuf-java-full") | 
|  | 87 | } else { | 
|  | 88 | ctx.PropertyErrorf("proto.type", "full java protos only supported on the host") | 
|  | 89 | } | 
|  | 90 | default: | 
|  | 91 | ctx.PropertyErrorf("proto.type", "unknown proto type %q", | 
|  | 92 | String(p.Proto.Type)) | 
| Colin Cross | 6af17aa | 2017-09-20 12:59:05 -0700 | [diff] [blame] | 93 | } | 
| Colin Cross | 6af17aa | 2017-09-20 12:59:05 -0700 | [diff] [blame] | 94 | } | 
|  | 95 | } | 
|  | 96 |  | 
| Jaewoong Jung | bc15e3a | 2021-03-10 17:02:43 -0800 | [diff] [blame] | 97 | func protoFlags(ctx android.ModuleContext, j *CommonProperties, p *android.ProtoProperties, | 
| Colin Cross | 0f2ee15 | 2017-12-14 15:22:43 -0800 | [diff] [blame] | 98 | flags javaBuilderFlags) javaBuilderFlags { | 
|  | 99 |  | 
| Colin Cross | 19878da | 2019-03-28 14:45:07 -0700 | [diff] [blame] | 100 | flags.proto = android.GetProtoFlags(ctx, p) | 
|  | 101 |  | 
| Colin Cross | fe17f6f | 2019-03-28 19:30:56 -0700 | [diff] [blame] | 102 | if String(p.Proto.Plugin) == "" { | 
| Colin Cross | 6f20509 | 2019-08-13 16:53:19 -0700 | [diff] [blame] | 103 | var typeToPlugin string | 
| Colin Cross | fe17f6f | 2019-03-28 19:30:56 -0700 | [diff] [blame] | 104 | switch String(p.Proto.Type) { | 
|  | 105 | case "micro": | 
|  | 106 | flags.proto.OutTypeFlag = "--javamicro_out" | 
| Colin Cross | 6f20509 | 2019-08-13 16:53:19 -0700 | [diff] [blame] | 107 | typeToPlugin = "javamicro" | 
| Colin Cross | fe17f6f | 2019-03-28 19:30:56 -0700 | [diff] [blame] | 108 | case "nano": | 
|  | 109 | flags.proto.OutTypeFlag = "--javanano_out" | 
| Colin Cross | 6ac0460 | 2019-08-13 16:54:20 -0700 | [diff] [blame] | 110 | typeToPlugin = "javanano" | 
| Colin Cross | 0f97ada | 2019-10-25 18:24:13 -0700 | [diff] [blame] | 111 | case "lite", "": | 
| Colin Cross | fe17f6f | 2019-03-28 19:30:56 -0700 | [diff] [blame] | 112 | flags.proto.OutTypeFlag = "--java_out" | 
|  | 113 | flags.proto.OutParams = append(flags.proto.OutParams, "lite") | 
| Colin Cross | 0f97ada | 2019-10-25 18:24:13 -0700 | [diff] [blame] | 114 | case "full": | 
| Colin Cross | fe17f6f | 2019-03-28 19:30:56 -0700 | [diff] [blame] | 115 | flags.proto.OutTypeFlag = "--java_out" | 
|  | 116 | default: | 
|  | 117 | ctx.PropertyErrorf("proto.type", "unknown proto type %q", | 
|  | 118 | String(p.Proto.Type)) | 
|  | 119 | } | 
| Colin Cross | 6f20509 | 2019-08-13 16:53:19 -0700 | [diff] [blame] | 120 |  | 
|  | 121 | if typeToPlugin != "" { | 
|  | 122 | hostTool := ctx.Config().HostToolPath(ctx, "protoc-gen-"+typeToPlugin) | 
|  | 123 | flags.proto.Deps = append(flags.proto.Deps, hostTool) | 
|  | 124 | flags.proto.Flags = append(flags.proto.Flags, "--plugin=protoc-gen-"+typeToPlugin+"="+hostTool.String()) | 
|  | 125 | } | 
| Colin Cross | 6af17aa | 2017-09-20 12:59:05 -0700 | [diff] [blame] | 126 | } | 
| Colin Cross | d5dbfb7 | 2017-11-14 13:11:23 -0800 | [diff] [blame] | 127 |  | 
| Colin Cross | 19878da | 2019-03-28 14:45:07 -0700 | [diff] [blame] | 128 | flags.proto.OutParams = append(flags.proto.OutParams, j.Proto.Output_params...) | 
| Colin Cross | d5dbfb7 | 2017-11-14 13:11:23 -0800 | [diff] [blame] | 129 |  | 
| Colin Cross | 6af17aa | 2017-09-20 12:59:05 -0700 | [diff] [blame] | 130 | return flags | 
|  | 131 | } |