| 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" | 
| Colin Cross | 6af17aa | 2017-09-20 12:59:05 -0700 | [diff] [blame] | 21 |  | 
|  | 22 | "android/soong/android" | 
|  | 23 | ) | 
|  | 24 |  | 
|  | 25 | func init() { | 
|  | 26 | pctx.HostBinToolVariable("protocCmd", "aprotoc") | 
| Dan Willemsen | a10504e | 2018-05-17 10:11:17 -0700 | [diff] [blame] | 27 | pctx.HostBinToolVariable("depFixCmd", "dep_fixer") | 
| Colin Cross | 6af17aa | 2017-09-20 12:59:05 -0700 | [diff] [blame] | 28 | } | 
|  | 29 |  | 
|  | 30 | var ( | 
|  | 31 | proto = pctx.AndroidStaticRule("protoc", | 
|  | 32 | blueprint.RuleParams{ | 
| Dan Willemsen | ab9f426 | 2018-02-14 13:58:34 -0800 | [diff] [blame] | 33 | Command: `rm -rf $out.tmp && mkdir -p $out.tmp && ` + | 
| Dan Willemsen | 4339853 | 2018-02-21 02:10:29 -0800 | [diff] [blame] | 34 | `$protocCmd $protoOut=$protoOutParams:$out.tmp --dependency_out=$out.d -I $protoBase $protoFlags $in && ` + | 
| Dan Willemsen | a10504e | 2018-05-17 10:11:17 -0700 | [diff] [blame] | 35 | `$depFixCmd $out.d && ` + | 
| Dan Willemsen | ab9f426 | 2018-02-14 13:58:34 -0800 | [diff] [blame] | 36 | `${config.SoongZipCmd} -jar -o $out -C $out.tmp -D $out.tmp && rm -rf $out.tmp`, | 
| Colin Cross | 59149b6 | 2017-10-16 18:07:29 -0700 | [diff] [blame] | 37 | CommandDeps: []string{ | 
|  | 38 | "$protocCmd", | 
| Dan Willemsen | a10504e | 2018-05-17 10:11:17 -0700 | [diff] [blame] | 39 | "$depFixCmd", | 
| Colin Cross | 59149b6 | 2017-10-16 18:07:29 -0700 | [diff] [blame] | 40 | "${config.SoongZipCmd}", | 
|  | 41 | }, | 
| Dan Willemsen | 4339853 | 2018-02-21 02:10:29 -0800 | [diff] [blame] | 42 | Depfile: "${out}.d", | 
|  | 43 | Deps:    blueprint.DepsGCC, | 
| Dan Willemsen | ab9f426 | 2018-02-14 13:58:34 -0800 | [diff] [blame] | 44 | }, "protoBase", "protoFlags", "protoOut", "protoOutParams") | 
| Colin Cross | 6af17aa | 2017-09-20 12:59:05 -0700 | [diff] [blame] | 45 | ) | 
|  | 46 |  | 
| Dan Willemsen | ab9f426 | 2018-02-14 13:58:34 -0800 | [diff] [blame] | 47 | func genProto(ctx android.ModuleContext, protoFile android.Path, flags javaBuilderFlags) android.Path { | 
|  | 48 | srcJarFile := android.GenPathWithExt(ctx, "proto", protoFile, "srcjar") | 
|  | 49 |  | 
|  | 50 | var protoBase string | 
|  | 51 | if flags.protoRoot { | 
|  | 52 | protoBase = "." | 
|  | 53 | } else { | 
|  | 54 | protoBase = strings.TrimSuffix(protoFile.String(), protoFile.Rel()) | 
|  | 55 | } | 
| Colin Cross | 6af17aa | 2017-09-20 12:59:05 -0700 | [diff] [blame] | 56 |  | 
| Colin Cross | ae88703 | 2017-10-23 17:16:14 -0700 | [diff] [blame] | 57 | ctx.Build(pctx, android.BuildParams{ | 
| Colin Cross | 6af17aa | 2017-09-20 12:59:05 -0700 | [diff] [blame] | 58 | Rule:        proto, | 
| Dan Willemsen | ab9f426 | 2018-02-14 13:58:34 -0800 | [diff] [blame] | 59 | Description: "protoc " + protoFile.Rel(), | 
|  | 60 | Output:      srcJarFile, | 
|  | 61 | Input:       protoFile, | 
| Colin Cross | 6af17aa | 2017-09-20 12:59:05 -0700 | [diff] [blame] | 62 | Args: map[string]string{ | 
| Dan Willemsen | ab9f426 | 2018-02-14 13:58:34 -0800 | [diff] [blame] | 63 | "protoBase":      protoBase, | 
|  | 64 | "protoOut":       flags.protoOutTypeFlag, | 
|  | 65 | "protoOutParams": flags.protoOutParams, | 
|  | 66 | "protoFlags":     strings.Join(flags.protoFlags, " "), | 
| Colin Cross | 6af17aa | 2017-09-20 12:59:05 -0700 | [diff] [blame] | 67 | }, | 
|  | 68 | }) | 
| Dan Willemsen | ab9f426 | 2018-02-14 13:58:34 -0800 | [diff] [blame] | 69 |  | 
|  | 70 | return srcJarFile | 
| Colin Cross | 6af17aa | 2017-09-20 12:59:05 -0700 | [diff] [blame] | 71 | } | 
|  | 72 |  | 
|  | 73 | func protoDeps(ctx android.BottomUpMutatorContext, p *android.ProtoProperties) { | 
| Colin Cross | ff3ae9d | 2018-04-10 16:15:18 -0700 | [diff] [blame] | 74 | switch String(p.Proto.Type) { | 
| Colin Cross | 6af17aa | 2017-09-20 12:59:05 -0700 | [diff] [blame] | 75 | case "micro": | 
|  | 76 | ctx.AddDependency(ctx.Module(), staticLibTag, "libprotobuf-java-micro") | 
|  | 77 | case "nano": | 
|  | 78 | ctx.AddDependency(ctx.Module(), staticLibTag, "libprotobuf-java-nano") | 
| Colin Cross | 6af17aa | 2017-09-20 12:59:05 -0700 | [diff] [blame] | 79 | case "lite", "": | 
|  | 80 | ctx.AddDependency(ctx.Module(), staticLibTag, "libprotobuf-java-lite") | 
|  | 81 | case "full": | 
|  | 82 | if ctx.Host() { | 
|  | 83 | ctx.AddDependency(ctx.Module(), staticLibTag, "libprotobuf-java-full") | 
|  | 84 | } else { | 
|  | 85 | ctx.PropertyErrorf("proto.type", "full java protos only supported on the host") | 
|  | 86 | } | 
|  | 87 | default: | 
|  | 88 | ctx.PropertyErrorf("proto.type", "unknown proto type %q", | 
| Colin Cross | ff3ae9d | 2018-04-10 16:15:18 -0700 | [diff] [blame] | 89 | String(p.Proto.Type)) | 
| Colin Cross | 6af17aa | 2017-09-20 12:59:05 -0700 | [diff] [blame] | 90 | } | 
|  | 91 | } | 
|  | 92 |  | 
| Colin Cross | 0f2ee15 | 2017-12-14 15:22:43 -0800 | [diff] [blame] | 93 | func protoFlags(ctx android.ModuleContext, j *CompilerProperties, p *android.ProtoProperties, | 
|  | 94 | flags javaBuilderFlags) javaBuilderFlags { | 
|  | 95 |  | 
| Colin Cross | ff3ae9d | 2018-04-10 16:15:18 -0700 | [diff] [blame] | 96 | switch String(p.Proto.Type) { | 
| Colin Cross | 6af17aa | 2017-09-20 12:59:05 -0700 | [diff] [blame] | 97 | case "micro": | 
| Joe Onorato | 09e94ab | 2017-11-18 18:23:14 -0800 | [diff] [blame] | 98 | flags.protoOutTypeFlag = "--javamicro_out" | 
| Colin Cross | 6af17aa | 2017-09-20 12:59:05 -0700 | [diff] [blame] | 99 | case "nano": | 
| Joe Onorato | 09e94ab | 2017-11-18 18:23:14 -0800 | [diff] [blame] | 100 | flags.protoOutTypeFlag = "--javanano_out" | 
|  | 101 | case "lite": | 
|  | 102 | flags.protoOutTypeFlag = "--java_out" | 
|  | 103 | flags.protoOutParams = "lite" | 
|  | 104 | case "full", "": | 
|  | 105 | flags.protoOutTypeFlag = "--java_out" | 
| Colin Cross | 6af17aa | 2017-09-20 12:59:05 -0700 | [diff] [blame] | 106 | default: | 
|  | 107 | ctx.PropertyErrorf("proto.type", "unknown proto type %q", | 
| Colin Cross | ff3ae9d | 2018-04-10 16:15:18 -0700 | [diff] [blame] | 108 | String(p.Proto.Type)) | 
| Colin Cross | 6af17aa | 2017-09-20 12:59:05 -0700 | [diff] [blame] | 109 | } | 
| Colin Cross | d5dbfb7 | 2017-11-14 13:11:23 -0800 | [diff] [blame] | 110 |  | 
| Colin Cross | 0f2ee15 | 2017-12-14 15:22:43 -0800 | [diff] [blame] | 111 | if len(j.Proto.Output_params) > 0 { | 
|  | 112 | if flags.protoOutParams != "" { | 
|  | 113 | flags.protoOutParams += "," | 
|  | 114 | } | 
|  | 115 | flags.protoOutParams += strings.Join(j.Proto.Output_params, ",") | 
|  | 116 | } | 
|  | 117 |  | 
| Colin Cross | d5dbfb7 | 2017-11-14 13:11:23 -0800 | [diff] [blame] | 118 | flags.protoFlags = android.ProtoFlags(ctx, p) | 
| Dan Willemsen | ab9f426 | 2018-02-14 13:58:34 -0800 | [diff] [blame] | 119 | flags.protoRoot = android.ProtoCanonicalPathFromRoot(ctx, p) | 
| Colin Cross | d5dbfb7 | 2017-11-14 13:11:23 -0800 | [diff] [blame] | 120 |  | 
| Colin Cross | 6af17aa | 2017-09-20 12:59:05 -0700 | [diff] [blame] | 121 | return flags | 
|  | 122 | } |