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