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 | |
Dan Willemsen | 60e62f0 | 2018-11-16 21:05:32 -0800 | [diff] [blame] | 42 | protoDeps := flags.protoDeps |
| 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 | 38f794e | 2017-09-07 10:53:07 -0700 | [diff] [blame] | 62 | func protoDeps(ctx BaseModuleContext, 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 | |
Nan Zhang | 0007d81 | 2017-11-07 10:57:05 -0800 | [diff] [blame] | 65 | switch String(p.Proto.Type) { |
Colin Cross | 0c461f1 | 2016-10-20 16:11:43 -0700 | [diff] [blame] | 66 | case "full": |
Jeff Gaston | af3cc2d | 2017-09-27 17:01:44 -0700 | [diff] [blame] | 67 | if ctx.useSdk() { |
Colin Cross | 0c461f1 | 2016-10-20 16:11:43 -0700 | [diff] [blame] | 68 | lib = "libprotobuf-cpp-full-ndk" |
| 69 | static = true |
| 70 | } else { |
| 71 | lib = "libprotobuf-cpp-full" |
| 72 | } |
| 73 | case "lite", "": |
Jeff Gaston | af3cc2d | 2017-09-27 17:01:44 -0700 | [diff] [blame] | 74 | if ctx.useSdk() { |
Colin Cross | 0c461f1 | 2016-10-20 16:11:43 -0700 | [diff] [blame] | 75 | lib = "libprotobuf-cpp-lite-ndk" |
| 76 | static = true |
| 77 | } else { |
| 78 | lib = "libprotobuf-cpp-lite" |
Colin Cross | 0c461f1 | 2016-10-20 16:11:43 -0700 | [diff] [blame] | 79 | } |
Dan Willemsen | 60e62f0 | 2018-11-16 21:05:32 -0800 | [diff] [blame] | 80 | case "nanopb-c": |
| 81 | lib = "libprotobuf-c-nano" |
| 82 | static = true |
| 83 | case "nanopb-c-enable_malloc": |
| 84 | lib = "libprotobuf-c-nano-enable_malloc" |
| 85 | static = true |
Yu Shan | 76dd005 | 2019-01-25 17:08:21 -0800 | [diff] [blame] | 86 | case "nanopb-c-16bit": |
| 87 | lib = "libprotobuf-c-nano-16bit" |
| 88 | static = true |
| 89 | case "nanopb-c-enable_malloc-16bit": |
| 90 | lib = "libprotobuf-c-nano-enable_malloc-16bit" |
| 91 | static = true |
| 92 | case "nanopb-c-32bit": |
| 93 | lib = "libprotobuf-c-nano-32bit" |
| 94 | static = true |
| 95 | case "nanopb-c-enable_malloc-32bit": |
| 96 | lib = "libprotobuf-c-nano-enable_malloc-32bit" |
| 97 | static = true |
Colin Cross | 0c461f1 | 2016-10-20 16:11:43 -0700 | [diff] [blame] | 98 | default: |
Colin Cross | 5ff51b5 | 2017-05-02 13:34:32 -0700 | [diff] [blame] | 99 | ctx.PropertyErrorf("proto.type", "unknown proto type %q", |
Nan Zhang | 0007d81 | 2017-11-07 10:57:05 -0800 | [diff] [blame] | 100 | String(p.Proto.Type)) |
Colin Cross | 0c461f1 | 2016-10-20 16:11:43 -0700 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | if static { |
| 104 | deps.StaticLibs = append(deps.StaticLibs, lib) |
| 105 | deps.ReexportStaticLibHeaders = append(deps.ReexportStaticLibHeaders, lib) |
| 106 | } else { |
| 107 | deps.SharedLibs = append(deps.SharedLibs, lib) |
| 108 | deps.ReexportSharedLibHeaders = append(deps.ReexportSharedLibHeaders, lib) |
| 109 | } |
| 110 | |
| 111 | return deps |
| 112 | } |
| 113 | |
Colin Cross | 38f794e | 2017-09-07 10:53:07 -0700 | [diff] [blame] | 114 | func protoFlags(ctx ModuleContext, flags Flags, p *android.ProtoProperties) Flags { |
Colin Cross | 0c461f1 | 2016-10-20 16:11:43 -0700 | [diff] [blame] | 115 | flags.CFlags = append(flags.CFlags, "-DGOOGLE_PROTOBUF_NO_RTTI") |
Dan Willemsen | ab9f426 | 2018-02-14 13:58:34 -0800 | [diff] [blame] | 116 | |
Colin Cross | 19878da | 2019-03-28 14:45:07 -0700 | [diff] [blame^] | 117 | flags.proto = android.GetProtoFlags(ctx, p) |
| 118 | if flags.proto.CanonicalPathFromRoot { |
| 119 | flags.GlobalFlags = append(flags.GlobalFlags, "-I"+flags.proto.SubDir.String()) |
Dan Willemsen | ab9f426 | 2018-02-14 13:58:34 -0800 | [diff] [blame] | 120 | } |
Colin Cross | 19878da | 2019-03-28 14:45:07 -0700 | [diff] [blame^] | 121 | flags.GlobalFlags = append(flags.GlobalFlags, "-I"+flags.proto.Dir.String()) |
Colin Cross | 5ff51b5 | 2017-05-02 13:34:32 -0700 | [diff] [blame] | 122 | |
Dan Willemsen | 60e62f0 | 2018-11-16 21:05:32 -0800 | [diff] [blame] | 123 | var plugin string |
| 124 | |
| 125 | switch String(p.Proto.Type) { |
Yu Shan | 76dd005 | 2019-01-25 17:08:21 -0800 | [diff] [blame] | 126 | 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] | 127 | flags.protoC = true |
| 128 | flags.protoOptionsFile = true |
Colin Cross | 19878da | 2019-03-28 14:45:07 -0700 | [diff] [blame^] | 129 | flags.proto.OutTypeFlag = "--nanopb_out" |
Dan Willemsen | 60e62f0 | 2018-11-16 21:05:32 -0800 | [diff] [blame] | 130 | plugin = "protoc-gen-nanopb" |
| 131 | case "full": |
Colin Cross | 19878da | 2019-03-28 14:45:07 -0700 | [diff] [blame^] | 132 | flags.proto.OutTypeFlag = "--cpp_out" |
Dan Willemsen | 60e62f0 | 2018-11-16 21:05:32 -0800 | [diff] [blame] | 133 | case "lite": |
Colin Cross | 19878da | 2019-03-28 14:45:07 -0700 | [diff] [blame^] | 134 | flags.proto.OutTypeFlag = "--cpp_out" |
| 135 | flags.proto.OutParams = append(flags.proto.OutParams, "lite") |
Dan Willemsen | 60e62f0 | 2018-11-16 21:05:32 -0800 | [diff] [blame] | 136 | case "": |
| 137 | // TODO(b/119714316): this should be equivalent to "lite" in |
| 138 | // order to match protoDeps, but some modules are depending on |
| 139 | // this behavior |
Colin Cross | 19878da | 2019-03-28 14:45:07 -0700 | [diff] [blame^] | 140 | flags.proto.OutTypeFlag = "--cpp_out" |
Dan Willemsen | 60e62f0 | 2018-11-16 21:05:32 -0800 | [diff] [blame] | 141 | default: |
| 142 | ctx.PropertyErrorf("proto.type", "unknown proto type %q", |
| 143 | String(p.Proto.Type)) |
| 144 | } |
| 145 | |
| 146 | if plugin != "" { |
| 147 | path := ctx.Config().HostToolPath(ctx, plugin) |
| 148 | flags.protoDeps = append(flags.protoDeps, path) |
Colin Cross | 19878da | 2019-03-28 14:45:07 -0700 | [diff] [blame^] | 149 | flags.proto.Flags = append(flags.proto.Flags, "--plugin="+path.String()) |
Joe Onorato | 09e94ab | 2017-11-18 18:23:14 -0800 | [diff] [blame] | 150 | } |
| 151 | |
Colin Cross | 0c461f1 | 2016-10-20 16:11:43 -0700 | [diff] [blame] | 152 | return flags |
| 153 | } |