| 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 | "github.com/google/blueprint/pathtools" | 
| Liz Kammer | 12615db | 2021-09-28 09:19:17 -0400 | [diff] [blame] | 19 | "github.com/google/blueprint/proptools" | 
| Colin Cross | 0c461f1 | 2016-10-20 16:11:43 -0700 | [diff] [blame] | 20 |  | 
|  | 21 | "android/soong/android" | 
| Liz Kammer | 12615db | 2021-09-28 09:19:17 -0400 | [diff] [blame] | 22 | ) | 
|  | 23 |  | 
|  | 24 | const ( | 
|  | 25 | protoTypeDefault = "lite" | 
| Colin Cross | 0c461f1 | 2016-10-20 16:11:43 -0700 | [diff] [blame] | 26 | ) | 
|  | 27 |  | 
| Colin Cross | 6af17aa | 2017-09-20 12:59:05 -0700 | [diff] [blame] | 28 | // genProto creates a rule to convert a .proto file to generated .pb.cc and .pb.h files and returns | 
|  | 29 | // the paths to the generated files. | 
| Colin Cross | 19878da | 2019-03-28 14:45:07 -0700 | [diff] [blame] | 30 | func genProto(ctx android.ModuleContext, protoFile android.Path, flags builderFlags) (cc, header android.WritablePath) { | 
|  | 31 | var ccFile, headerFile android.ModuleGenPath | 
| Dan Willemsen | 60e62f0 | 2018-11-16 21:05:32 -0800 | [diff] [blame] | 32 |  | 
|  | 33 | srcSuffix := ".cc" | 
|  | 34 | if flags.protoC { | 
|  | 35 | srcSuffix = ".c" | 
|  | 36 | } | 
| Colin Cross | 6af17aa | 2017-09-20 12:59:05 -0700 | [diff] [blame] | 37 |  | 
| Colin Cross | 19878da | 2019-03-28 14:45:07 -0700 | [diff] [blame] | 38 | if flags.proto.CanonicalPathFromRoot { | 
| Dan Willemsen | 60e62f0 | 2018-11-16 21:05:32 -0800 | [diff] [blame] | 39 | ccFile = android.GenPathWithExt(ctx, "proto", protoFile, "pb"+srcSuffix) | 
| Dan Willemsen | ab9f426 | 2018-02-14 13:58:34 -0800 | [diff] [blame] | 40 | headerFile = android.GenPathWithExt(ctx, "proto", protoFile, "pb.h") | 
|  | 41 | } else { | 
|  | 42 | rel := protoFile.Rel() | 
| Dan Willemsen | 60e62f0 | 2018-11-16 21:05:32 -0800 | [diff] [blame] | 43 | ccFile = android.PathForModuleGen(ctx, "proto", pathtools.ReplaceExtension(rel, "pb"+srcSuffix)) | 
| Dan Willemsen | ab9f426 | 2018-02-14 13:58:34 -0800 | [diff] [blame] | 44 | headerFile = android.PathForModuleGen(ctx, "proto", pathtools.ReplaceExtension(rel, "pb.h")) | 
|  | 45 | } | 
| Colin Cross | 6af17aa | 2017-09-20 12:59:05 -0700 | [diff] [blame] | 46 |  | 
| Colin Cross | fe17f6f | 2019-03-28 19:30:56 -0700 | [diff] [blame] | 47 | protoDeps := flags.proto.Deps | 
| Dan Willemsen | 60e62f0 | 2018-11-16 21:05:32 -0800 | [diff] [blame] | 48 | if flags.protoOptionsFile { | 
|  | 49 | optionsFile := pathtools.ReplaceExtension(protoFile.String(), "options") | 
| Colin Cross | 19878da | 2019-03-28 14:45:07 -0700 | [diff] [blame] | 50 | optionsPath := android.PathForSource(ctx, optionsFile) | 
|  | 51 | protoDeps = append(android.Paths{optionsPath}, protoDeps...) | 
| Dan Willemsen | 60e62f0 | 2018-11-16 21:05:32 -0800 | [diff] [blame] | 52 | } | 
|  | 53 |  | 
| Colin Cross | 19878da | 2019-03-28 14:45:07 -0700 | [diff] [blame] | 54 | outDir := flags.proto.Dir | 
|  | 55 | depFile := ccFile.ReplaceExtension(ctx, "d") | 
|  | 56 | outputs := android.WritablePaths{ccFile, headerFile} | 
|  | 57 |  | 
| Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 58 | rule := android.NewRuleBuilder(pctx, ctx) | 
| Colin Cross | 19878da | 2019-03-28 14:45:07 -0700 | [diff] [blame] | 59 |  | 
| Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 60 | android.ProtoRule(rule, protoFile, flags.proto, protoDeps, outDir, depFile, outputs) | 
| Colin Cross | 19878da | 2019-03-28 14:45:07 -0700 | [diff] [blame] | 61 |  | 
| Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 62 | rule.Build("protoc_"+protoFile.Rel(), "protoc "+protoFile.Rel()) | 
| Colin Cross | 6af17aa | 2017-09-20 12:59:05 -0700 | [diff] [blame] | 63 |  | 
|  | 64 | return ccFile, headerFile | 
|  | 65 | } | 
|  | 66 |  | 
| Colin Cross | fe17f6f | 2019-03-28 19:30:56 -0700 | [diff] [blame] | 67 | func protoDeps(ctx DepsContext, deps Deps, p *android.ProtoProperties, static bool) Deps { | 
| Colin Cross | 0c461f1 | 2016-10-20 16:11:43 -0700 | [diff] [blame] | 68 | var lib string | 
| Colin Cross | 0c461f1 | 2016-10-20 16:11:43 -0700 | [diff] [blame] | 69 |  | 
| Colin Cross | fe17f6f | 2019-03-28 19:30:56 -0700 | [diff] [blame] | 70 | if String(p.Proto.Plugin) == "" { | 
| Liz Kammer | 12615db | 2021-09-28 09:19:17 -0400 | [diff] [blame] | 71 | switch proptools.StringDefault(p.Proto.Type, protoTypeDefault) { | 
| Colin Cross | fe17f6f | 2019-03-28 19:30:56 -0700 | [diff] [blame] | 72 | case "full": | 
|  | 73 | if ctx.useSdk() { | 
|  | 74 | lib = "libprotobuf-cpp-full-ndk" | 
|  | 75 | static = true | 
|  | 76 | } else { | 
|  | 77 | lib = "libprotobuf-cpp-full" | 
|  | 78 | } | 
| Liz Kammer | 12615db | 2021-09-28 09:19:17 -0400 | [diff] [blame] | 79 | case "lite": | 
| Colin Cross | fe17f6f | 2019-03-28 19:30:56 -0700 | [diff] [blame] | 80 | if ctx.useSdk() { | 
|  | 81 | lib = "libprotobuf-cpp-lite-ndk" | 
|  | 82 | static = true | 
|  | 83 | } else { | 
|  | 84 | lib = "libprotobuf-cpp-lite" | 
|  | 85 | } | 
|  | 86 | case "nanopb-c": | 
|  | 87 | lib = "libprotobuf-c-nano" | 
| Colin Cross | 0c461f1 | 2016-10-20 16:11:43 -0700 | [diff] [blame] | 88 | static = true | 
| Colin Cross | fe17f6f | 2019-03-28 19:30:56 -0700 | [diff] [blame] | 89 | case "nanopb-c-enable_malloc": | 
|  | 90 | lib = "libprotobuf-c-nano-enable_malloc" | 
| Colin Cross | 0c461f1 | 2016-10-20 16:11:43 -0700 | [diff] [blame] | 91 | static = true | 
| Colin Cross | fe17f6f | 2019-03-28 19:30:56 -0700 | [diff] [blame] | 92 | case "nanopb-c-16bit": | 
|  | 93 | lib = "libprotobuf-c-nano-16bit" | 
|  | 94 | static = true | 
|  | 95 | case "nanopb-c-enable_malloc-16bit": | 
|  | 96 | lib = "libprotobuf-c-nano-enable_malloc-16bit" | 
|  | 97 | static = true | 
|  | 98 | case "nanopb-c-32bit": | 
|  | 99 | lib = "libprotobuf-c-nano-32bit" | 
|  | 100 | static = true | 
|  | 101 | case "nanopb-c-enable_malloc-32bit": | 
|  | 102 | lib = "libprotobuf-c-nano-enable_malloc-32bit" | 
|  | 103 | static = true | 
|  | 104 | default: | 
|  | 105 | ctx.PropertyErrorf("proto.type", "unknown proto type %q", | 
|  | 106 | String(p.Proto.Type)) | 
| Colin Cross | 0c461f1 | 2016-10-20 16:11:43 -0700 | [diff] [blame] | 107 | } | 
| Colin Cross | 0c461f1 | 2016-10-20 16:11:43 -0700 | [diff] [blame] | 108 |  | 
| Colin Cross | fe17f6f | 2019-03-28 19:30:56 -0700 | [diff] [blame] | 109 | if static { | 
|  | 110 | deps.StaticLibs = append(deps.StaticLibs, lib) | 
|  | 111 | deps.ReexportStaticLibHeaders = append(deps.ReexportStaticLibHeaders, lib) | 
|  | 112 | } else { | 
|  | 113 | deps.SharedLibs = append(deps.SharedLibs, lib) | 
|  | 114 | deps.ReexportSharedLibHeaders = append(deps.ReexportSharedLibHeaders, lib) | 
|  | 115 | } | 
| Colin Cross | 0c461f1 | 2016-10-20 16:11:43 -0700 | [diff] [blame] | 116 | } | 
|  | 117 |  | 
|  | 118 | return deps | 
|  | 119 | } | 
|  | 120 |  | 
| Colin Cross | 38f794e | 2017-09-07 10:53:07 -0700 | [diff] [blame] | 121 | func protoFlags(ctx ModuleContext, flags Flags, p *android.ProtoProperties) Flags { | 
| Colin Cross | 4af21ed | 2019-11-04 09:37:55 -0800 | [diff] [blame] | 122 | flags.Local.CFlags = append(flags.Local.CFlags, "-DGOOGLE_PROTOBUF_NO_RTTI") | 
| Dan Willemsen | ab9f426 | 2018-02-14 13:58:34 -0800 | [diff] [blame] | 123 |  | 
| Colin Cross | 19878da | 2019-03-28 14:45:07 -0700 | [diff] [blame] | 124 | flags.proto = android.GetProtoFlags(ctx, p) | 
|  | 125 | if flags.proto.CanonicalPathFromRoot { | 
| Colin Cross | 4af21ed | 2019-11-04 09:37:55 -0800 | [diff] [blame] | 126 | flags.Local.CommonFlags = append(flags.Local.CommonFlags, "-I"+flags.proto.SubDir.String()) | 
| Dan Willemsen | ab9f426 | 2018-02-14 13:58:34 -0800 | [diff] [blame] | 127 | } | 
| Colin Cross | 4af21ed | 2019-11-04 09:37:55 -0800 | [diff] [blame] | 128 | flags.Local.CommonFlags = append(flags.Local.CommonFlags, "-I"+flags.proto.Dir.String()) | 
| Colin Cross | 5ff51b5 | 2017-05-02 13:34:32 -0700 | [diff] [blame] | 129 |  | 
| Colin Cross | fe17f6f | 2019-03-28 19:30:56 -0700 | [diff] [blame] | 130 | if String(p.Proto.Plugin) == "" { | 
|  | 131 | var plugin string | 
| Dan Willemsen | 60e62f0 | 2018-11-16 21:05:32 -0800 | [diff] [blame] | 132 |  | 
| Colin Cross | fe17f6f | 2019-03-28 19:30:56 -0700 | [diff] [blame] | 133 | switch String(p.Proto.Type) { | 
|  | 134 | case "nanopb-c", "nanopb-c-enable_malloc", "nanopb-c-16bit", "nanopb-c-enable_malloc-16bit", "nanopb-c-32bit", "nanopb-c-enable_malloc-32bit": | 
|  | 135 | flags.protoC = true | 
|  | 136 | flags.protoOptionsFile = true | 
|  | 137 | flags.proto.OutTypeFlag = "--nanopb_out" | 
| Ulf Adams | 82fd89b | 2020-11-25 22:37:22 +0100 | [diff] [blame] | 138 | // Disable nanopb timestamps to support remote caching. | 
|  | 139 | flags.proto.OutParams = append(flags.proto.OutParams, "-T") | 
| Colin Cross | fe17f6f | 2019-03-28 19:30:56 -0700 | [diff] [blame] | 140 | plugin = "protoc-gen-nanopb" | 
|  | 141 | case "full": | 
|  | 142 | flags.proto.OutTypeFlag = "--cpp_out" | 
|  | 143 | case "lite": | 
|  | 144 | flags.proto.OutTypeFlag = "--cpp_out" | 
|  | 145 | flags.proto.OutParams = append(flags.proto.OutParams, "lite") | 
|  | 146 | case "": | 
|  | 147 | // TODO(b/119714316): this should be equivalent to "lite" in | 
|  | 148 | // order to match protoDeps, but some modules are depending on | 
|  | 149 | // this behavior | 
|  | 150 | flags.proto.OutTypeFlag = "--cpp_out" | 
|  | 151 | default: | 
|  | 152 | ctx.PropertyErrorf("proto.type", "unknown proto type %q", | 
|  | 153 | String(p.Proto.Type)) | 
|  | 154 | } | 
| Dan Willemsen | 60e62f0 | 2018-11-16 21:05:32 -0800 | [diff] [blame] | 155 |  | 
| Colin Cross | fe17f6f | 2019-03-28 19:30:56 -0700 | [diff] [blame] | 156 | if plugin != "" { | 
|  | 157 | path := ctx.Config().HostToolPath(ctx, plugin) | 
|  | 158 | flags.proto.Deps = append(flags.proto.Deps, path) | 
|  | 159 | flags.proto.Flags = append(flags.proto.Flags, "--plugin="+path.String()) | 
|  | 160 | } | 
| Joe Onorato | 09e94ab | 2017-11-18 18:23:14 -0800 | [diff] [blame] | 161 | } | 
|  | 162 |  | 
| Colin Cross | 0c461f1 | 2016-10-20 16:11:43 -0700 | [diff] [blame] | 163 | return flags | 
|  | 164 | } |