| Colin Cross | 0c461f1 | 2016-10-20 16:11:43 -0700 | [diff] [blame] | 1 | // Copyright 2016 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 cc | 
|  | 16 |  | 
|  | 17 | import ( | 
| Dan Willemsen | ab9f426 | 2018-02-14 13:58:34 -0800 | [diff] [blame] | 18 | "strings" | 
|  | 19 |  | 
| Colin Cross | 6af17aa | 2017-09-20 12:59:05 -0700 | [diff] [blame] | 20 | "github.com/google/blueprint" | 
| Dan Willemsen | ab9f426 | 2018-02-14 13:58:34 -0800 | [diff] [blame] | 21 | "github.com/google/blueprint/pathtools" | 
| Colin Cross | 0c461f1 | 2016-10-20 16:11:43 -0700 | [diff] [blame] | 22 |  | 
|  | 23 | "android/soong/android" | 
|  | 24 | ) | 
|  | 25 |  | 
| Colin Cross | 6af17aa | 2017-09-20 12:59:05 -0700 | [diff] [blame] | 26 | func init() { | 
|  | 27 | pctx.HostBinToolVariable("protocCmd", "aprotoc") | 
| Dan Willemsen | 4339853 | 2018-02-21 02:10:29 -0800 | [diff] [blame] | 28 | pctx.HostBinToolVariable("depFixCmd", "dep_fixer") | 
| Colin Cross | 6af17aa | 2017-09-20 12:59:05 -0700 | [diff] [blame] | 29 | } | 
|  | 30 |  | 
|  | 31 | var ( | 
|  | 32 | proto = pctx.AndroidStaticRule("protoc", | 
|  | 33 | blueprint.RuleParams{ | 
| Dan Willemsen | 60e62f0 | 2018-11-16 21:05:32 -0800 | [diff] [blame] | 34 | Command: "$protocCmd $protoOut=$protoOutParams:$outDir --dependency_out=$out.d -I $protoBase $protoFlags $in && " + | 
| Dan Willemsen | 4339853 | 2018-02-21 02:10:29 -0800 | [diff] [blame] | 35 | `$depFixCmd $out.d`, | 
|  | 36 | CommandDeps: []string{"$protocCmd", "$depFixCmd"}, | 
|  | 37 | Depfile:     "${out}.d", | 
|  | 38 | Deps:        blueprint.DepsGCC, | 
| Dan Willemsen | 60e62f0 | 2018-11-16 21:05:32 -0800 | [diff] [blame] | 39 | }, "protoFlags", "protoOut", "protoOutParams", "protoBase", "outDir") | 
| Colin Cross | 6af17aa | 2017-09-20 12:59:05 -0700 | [diff] [blame] | 40 | ) | 
|  | 41 |  | 
|  | 42 | // genProto creates a rule to convert a .proto file to generated .pb.cc and .pb.h files and returns | 
|  | 43 | // the paths to the generated files. | 
| Dan Willemsen | 60e62f0 | 2018-11-16 21:05:32 -0800 | [diff] [blame] | 44 | func genProto(ctx android.ModuleContext, protoFile android.Path, flags builderFlags) (ccFile, headerFile android.WritablePath) { | 
|  | 45 |  | 
|  | 46 | srcSuffix := ".cc" | 
|  | 47 | if flags.protoC { | 
|  | 48 | srcSuffix = ".c" | 
|  | 49 | } | 
| Colin Cross | 6af17aa | 2017-09-20 12:59:05 -0700 | [diff] [blame] | 50 |  | 
| Dan Willemsen | ab9f426 | 2018-02-14 13:58:34 -0800 | [diff] [blame] | 51 | var protoBase string | 
| Dan Willemsen | 60e62f0 | 2018-11-16 21:05:32 -0800 | [diff] [blame] | 52 | if flags.protoRoot { | 
| Dan Willemsen | ab9f426 | 2018-02-14 13:58:34 -0800 | [diff] [blame] | 53 | protoBase = "." | 
| Dan Willemsen | 60e62f0 | 2018-11-16 21:05:32 -0800 | [diff] [blame] | 54 | ccFile = android.GenPathWithExt(ctx, "proto", protoFile, "pb"+srcSuffix) | 
| Dan Willemsen | ab9f426 | 2018-02-14 13:58:34 -0800 | [diff] [blame] | 55 | headerFile = android.GenPathWithExt(ctx, "proto", protoFile, "pb.h") | 
|  | 56 | } else { | 
|  | 57 | rel := protoFile.Rel() | 
|  | 58 | protoBase = strings.TrimSuffix(protoFile.String(), rel) | 
| Dan Willemsen | 60e62f0 | 2018-11-16 21:05:32 -0800 | [diff] [blame] | 59 | ccFile = android.PathForModuleGen(ctx, "proto", pathtools.ReplaceExtension(rel, "pb"+srcSuffix)) | 
| Dan Willemsen | ab9f426 | 2018-02-14 13:58:34 -0800 | [diff] [blame] | 60 | headerFile = android.PathForModuleGen(ctx, "proto", pathtools.ReplaceExtension(rel, "pb.h")) | 
|  | 61 | } | 
| Colin Cross | 6af17aa | 2017-09-20 12:59:05 -0700 | [diff] [blame] | 62 |  | 
| Dan Willemsen | 60e62f0 | 2018-11-16 21:05:32 -0800 | [diff] [blame] | 63 | protoDeps := flags.protoDeps | 
|  | 64 | if flags.protoOptionsFile { | 
|  | 65 | optionsFile := pathtools.ReplaceExtension(protoFile.String(), "options") | 
|  | 66 | optionsPath := android.ExistentPathForSource(ctx, optionsFile) | 
|  | 67 | if optionsPath.Valid() { | 
|  | 68 | protoDeps = append(android.Paths{optionsPath.Path()}, protoDeps...) | 
|  | 69 | } | 
|  | 70 | } | 
|  | 71 |  | 
| Colin Cross | ae88703 | 2017-10-23 17:16:14 -0700 | [diff] [blame] | 72 | ctx.Build(pctx, android.BuildParams{ | 
| Dan Willemsen | 4339853 | 2018-02-21 02:10:29 -0800 | [diff] [blame] | 73 | Rule:           proto, | 
|  | 74 | Description:    "protoc " + protoFile.Rel(), | 
|  | 75 | Output:         ccFile, | 
|  | 76 | ImplicitOutput: headerFile, | 
|  | 77 | Input:          protoFile, | 
| Dan Willemsen | 60e62f0 | 2018-11-16 21:05:32 -0800 | [diff] [blame] | 78 | Implicits:      protoDeps, | 
| Colin Cross | 6af17aa | 2017-09-20 12:59:05 -0700 | [diff] [blame] | 79 | Args: map[string]string{ | 
| Joe Onorato | 09e94ab | 2017-11-18 18:23:14 -0800 | [diff] [blame] | 80 | "outDir":         android.ProtoDir(ctx).String(), | 
| Dan Willemsen | 60e62f0 | 2018-11-16 21:05:32 -0800 | [diff] [blame] | 81 | "protoFlags":     flags.protoFlags, | 
|  | 82 | "protoOut":       flags.protoOutTypeFlag, | 
|  | 83 | "protoOutParams": flags.protoOutParams, | 
| Dan Willemsen | ab9f426 | 2018-02-14 13:58:34 -0800 | [diff] [blame] | 84 | "protoBase":      protoBase, | 
| Colin Cross | 6af17aa | 2017-09-20 12:59:05 -0700 | [diff] [blame] | 85 | }, | 
|  | 86 | }) | 
|  | 87 |  | 
|  | 88 | return ccFile, headerFile | 
|  | 89 | } | 
|  | 90 |  | 
| Colin Cross | 38f794e | 2017-09-07 10:53:07 -0700 | [diff] [blame] | 91 | func protoDeps(ctx BaseModuleContext, deps Deps, p *android.ProtoProperties, static bool) Deps { | 
| Colin Cross | 0c461f1 | 2016-10-20 16:11:43 -0700 | [diff] [blame] | 92 | var lib string | 
| Colin Cross | 0c461f1 | 2016-10-20 16:11:43 -0700 | [diff] [blame] | 93 |  | 
| Nan Zhang | 0007d81 | 2017-11-07 10:57:05 -0800 | [diff] [blame] | 94 | switch String(p.Proto.Type) { | 
| Colin Cross | 0c461f1 | 2016-10-20 16:11:43 -0700 | [diff] [blame] | 95 | case "full": | 
| Jeff Gaston | af3cc2d | 2017-09-27 17:01:44 -0700 | [diff] [blame] | 96 | if ctx.useSdk() { | 
| Colin Cross | 0c461f1 | 2016-10-20 16:11:43 -0700 | [diff] [blame] | 97 | lib = "libprotobuf-cpp-full-ndk" | 
|  | 98 | static = true | 
|  | 99 | } else { | 
|  | 100 | lib = "libprotobuf-cpp-full" | 
|  | 101 | } | 
|  | 102 | case "lite", "": | 
| Jeff Gaston | af3cc2d | 2017-09-27 17:01:44 -0700 | [diff] [blame] | 103 | if ctx.useSdk() { | 
| Colin Cross | 0c461f1 | 2016-10-20 16:11:43 -0700 | [diff] [blame] | 104 | lib = "libprotobuf-cpp-lite-ndk" | 
|  | 105 | static = true | 
|  | 106 | } else { | 
|  | 107 | lib = "libprotobuf-cpp-lite" | 
| Colin Cross | 0c461f1 | 2016-10-20 16:11:43 -0700 | [diff] [blame] | 108 | } | 
| Dan Willemsen | 60e62f0 | 2018-11-16 21:05:32 -0800 | [diff] [blame] | 109 | case "nanopb-c": | 
|  | 110 | lib = "libprotobuf-c-nano" | 
|  | 111 | static = true | 
|  | 112 | case "nanopb-c-enable_malloc": | 
|  | 113 | lib = "libprotobuf-c-nano-enable_malloc" | 
|  | 114 | static = true | 
| Yu Shan | 76dd005 | 2019-01-25 17:08:21 -0800 | [diff] [blame] | 115 | case "nanopb-c-16bit": | 
|  | 116 | lib = "libprotobuf-c-nano-16bit" | 
|  | 117 | static = true | 
|  | 118 | case "nanopb-c-enable_malloc-16bit": | 
|  | 119 | lib = "libprotobuf-c-nano-enable_malloc-16bit" | 
|  | 120 | static = true | 
|  | 121 | case "nanopb-c-32bit": | 
|  | 122 | lib = "libprotobuf-c-nano-32bit" | 
|  | 123 | static = true | 
|  | 124 | case "nanopb-c-enable_malloc-32bit": | 
|  | 125 | lib = "libprotobuf-c-nano-enable_malloc-32bit" | 
|  | 126 | static = true | 
| Colin Cross | 0c461f1 | 2016-10-20 16:11:43 -0700 | [diff] [blame] | 127 | default: | 
| Colin Cross | 5ff51b5 | 2017-05-02 13:34:32 -0700 | [diff] [blame] | 128 | ctx.PropertyErrorf("proto.type", "unknown proto type %q", | 
| Nan Zhang | 0007d81 | 2017-11-07 10:57:05 -0800 | [diff] [blame] | 129 | String(p.Proto.Type)) | 
| Colin Cross | 0c461f1 | 2016-10-20 16:11:43 -0700 | [diff] [blame] | 130 | } | 
|  | 131 |  | 
|  | 132 | if static { | 
|  | 133 | deps.StaticLibs = append(deps.StaticLibs, lib) | 
|  | 134 | deps.ReexportStaticLibHeaders = append(deps.ReexportStaticLibHeaders, lib) | 
|  | 135 | } else { | 
|  | 136 | deps.SharedLibs = append(deps.SharedLibs, lib) | 
|  | 137 | deps.ReexportSharedLibHeaders = append(deps.ReexportSharedLibHeaders, lib) | 
|  | 138 | } | 
|  | 139 |  | 
|  | 140 | return deps | 
|  | 141 | } | 
|  | 142 |  | 
| Colin Cross | 38f794e | 2017-09-07 10:53:07 -0700 | [diff] [blame] | 143 | func protoFlags(ctx ModuleContext, flags Flags, p *android.ProtoProperties) Flags { | 
| Colin Cross | 0c461f1 | 2016-10-20 16:11:43 -0700 | [diff] [blame] | 144 | flags.CFlags = append(flags.CFlags, "-DGOOGLE_PROTOBUF_NO_RTTI") | 
| Dan Willemsen | ab9f426 | 2018-02-14 13:58:34 -0800 | [diff] [blame] | 145 |  | 
|  | 146 | flags.ProtoRoot = android.ProtoCanonicalPathFromRoot(ctx, p) | 
|  | 147 | if flags.ProtoRoot { | 
|  | 148 | flags.GlobalFlags = append(flags.GlobalFlags, "-I"+android.ProtoSubDir(ctx).String()) | 
|  | 149 | } | 
|  | 150 | flags.GlobalFlags = append(flags.GlobalFlags, "-I"+android.ProtoDir(ctx).String()) | 
| Colin Cross | 0c461f1 | 2016-10-20 16:11:43 -0700 | [diff] [blame] | 151 |  | 
| Colin Cross | 38f794e | 2017-09-07 10:53:07 -0700 | [diff] [blame] | 152 | flags.protoFlags = android.ProtoFlags(ctx, p) | 
| Colin Cross | 5ff51b5 | 2017-05-02 13:34:32 -0700 | [diff] [blame] | 153 |  | 
| Dan Willemsen | 60e62f0 | 2018-11-16 21:05:32 -0800 | [diff] [blame] | 154 | var plugin string | 
|  | 155 |  | 
|  | 156 | switch String(p.Proto.Type) { | 
| Yu Shan | 76dd005 | 2019-01-25 17:08:21 -0800 | [diff] [blame] | 157 | case "nanopb-c", "nanopb-c-enable_malloc", "nanopb-c-16bit", "nanopb-c-enable_malloc-16bit", "nanopb-c-32bit", "nanopb-c-enable_malloc-32bit": | 
| Dan Willemsen | 60e62f0 | 2018-11-16 21:05:32 -0800 | [diff] [blame] | 158 | flags.protoC = true | 
|  | 159 | flags.protoOptionsFile = true | 
|  | 160 | flags.protoOutTypeFlag = "--nanopb_out" | 
|  | 161 | plugin = "protoc-gen-nanopb" | 
|  | 162 | case "full": | 
|  | 163 | flags.protoOutTypeFlag = "--cpp_out" | 
|  | 164 | case "lite": | 
|  | 165 | flags.protoOutTypeFlag = "--cpp_out" | 
| Kweku Adams | fb5b31c | 2018-04-05 17:48:32 -0700 | [diff] [blame] | 166 | flags.protoOutParams = append(flags.protoOutParams, "lite") | 
| Dan Willemsen | 60e62f0 | 2018-11-16 21:05:32 -0800 | [diff] [blame] | 167 | case "": | 
|  | 168 | // TODO(b/119714316): this should be equivalent to "lite" in | 
|  | 169 | // order to match protoDeps, but some modules are depending on | 
|  | 170 | // this behavior | 
|  | 171 | flags.protoOutTypeFlag = "--cpp_out" | 
|  | 172 | default: | 
|  | 173 | ctx.PropertyErrorf("proto.type", "unknown proto type %q", | 
|  | 174 | String(p.Proto.Type)) | 
|  | 175 | } | 
|  | 176 |  | 
|  | 177 | if plugin != "" { | 
|  | 178 | path := ctx.Config().HostToolPath(ctx, plugin) | 
|  | 179 | flags.protoDeps = append(flags.protoDeps, path) | 
|  | 180 | flags.protoFlags = append(flags.protoFlags, "--plugin="+path.String()) | 
| Joe Onorato | 09e94ab | 2017-11-18 18:23:14 -0800 | [diff] [blame] | 181 | } | 
|  | 182 |  | 
| Colin Cross | 0c461f1 | 2016-10-20 16:11:43 -0700 | [diff] [blame] | 183 | return flags | 
|  | 184 | } |