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