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" |
Joe Onorato | 09e94ab | 2017-11-18 18:23:14 -0800 | [diff] [blame] | 22 | "github.com/google/blueprint/proptools" |
Colin Cross | 0c461f1 | 2016-10-20 16:11:43 -0700 | [diff] [blame] | 23 | |
| 24 | "android/soong/android" |
| 25 | ) |
| 26 | |
Colin Cross | 6af17aa | 2017-09-20 12:59:05 -0700 | [diff] [blame] | 27 | func init() { |
| 28 | pctx.HostBinToolVariable("protocCmd", "aprotoc") |
| 29 | } |
| 30 | |
| 31 | var ( |
| 32 | proto = pctx.AndroidStaticRule("protoc", |
| 33 | blueprint.RuleParams{ |
Dan Willemsen | ab9f426 | 2018-02-14 13:58:34 -0800 | [diff] [blame^] | 34 | Command: "$protocCmd --cpp_out=$protoOutParams:$outDir -I $protoBase $protoFlags $in", |
Colin Cross | 6af17aa | 2017-09-20 12:59:05 -0700 | [diff] [blame] | 35 | CommandDeps: []string{"$protocCmd"}, |
Dan Willemsen | ab9f426 | 2018-02-14 13:58:34 -0800 | [diff] [blame^] | 36 | }, "protoFlags", "protoOutParams", "protoBase", "outDir") |
Colin Cross | 6af17aa | 2017-09-20 12:59:05 -0700 | [diff] [blame] | 37 | ) |
| 38 | |
| 39 | // genProto creates a rule to convert a .proto file to generated .pb.cc and .pb.h files and returns |
| 40 | // the paths to the generated files. |
| 41 | func genProto(ctx android.ModuleContext, protoFile android.Path, |
Dan Willemsen | ab9f426 | 2018-02-14 13:58:34 -0800 | [diff] [blame^] | 42 | protoFlags, protoOutParams string, root bool) (ccFile, headerFile android.WritablePath) { |
Colin Cross | 6af17aa | 2017-09-20 12:59:05 -0700 | [diff] [blame] | 43 | |
Dan Willemsen | ab9f426 | 2018-02-14 13:58:34 -0800 | [diff] [blame^] | 44 | var protoBase string |
| 45 | if root { |
| 46 | protoBase = "." |
| 47 | ccFile = android.GenPathWithExt(ctx, "proto", protoFile, "pb.cc") |
| 48 | headerFile = android.GenPathWithExt(ctx, "proto", protoFile, "pb.h") |
| 49 | } else { |
| 50 | rel := protoFile.Rel() |
| 51 | protoBase = strings.TrimSuffix(protoFile.String(), rel) |
| 52 | ccFile = android.PathForModuleGen(ctx, "proto", pathtools.ReplaceExtension(rel, "pb.cc")) |
| 53 | headerFile = android.PathForModuleGen(ctx, "proto", pathtools.ReplaceExtension(rel, "pb.h")) |
| 54 | } |
Colin Cross | 6af17aa | 2017-09-20 12:59:05 -0700 | [diff] [blame] | 55 | |
Colin Cross | ae88703 | 2017-10-23 17:16:14 -0700 | [diff] [blame] | 56 | ctx.Build(pctx, android.BuildParams{ |
Colin Cross | 6af17aa | 2017-09-20 12:59:05 -0700 | [diff] [blame] | 57 | Rule: proto, |
| 58 | Description: "protoc " + protoFile.Rel(), |
| 59 | Outputs: android.WritablePaths{ccFile, headerFile}, |
| 60 | Input: protoFile, |
| 61 | Args: map[string]string{ |
Joe Onorato | 09e94ab | 2017-11-18 18:23:14 -0800 | [diff] [blame] | 62 | "outDir": android.ProtoDir(ctx).String(), |
| 63 | "protoFlags": protoFlags, |
| 64 | "protoOutParams": protoOutParams, |
Dan Willemsen | ab9f426 | 2018-02-14 13:58:34 -0800 | [diff] [blame^] | 65 | "protoBase": protoBase, |
Colin Cross | 6af17aa | 2017-09-20 12:59:05 -0700 | [diff] [blame] | 66 | }, |
| 67 | }) |
| 68 | |
| 69 | return ccFile, headerFile |
| 70 | } |
| 71 | |
Colin Cross | 38f794e | 2017-09-07 10:53:07 -0700 | [diff] [blame] | 72 | func protoDeps(ctx BaseModuleContext, deps Deps, p *android.ProtoProperties, static bool) Deps { |
Colin Cross | 0c461f1 | 2016-10-20 16:11:43 -0700 | [diff] [blame] | 73 | var lib string |
Colin Cross | 0c461f1 | 2016-10-20 16:11:43 -0700 | [diff] [blame] | 74 | |
Nan Zhang | 0007d81 | 2017-11-07 10:57:05 -0800 | [diff] [blame] | 75 | switch String(p.Proto.Type) { |
Colin Cross | 0c461f1 | 2016-10-20 16:11:43 -0700 | [diff] [blame] | 76 | case "full": |
Jeff Gaston | af3cc2d | 2017-09-27 17:01:44 -0700 | [diff] [blame] | 77 | if ctx.useSdk() { |
Colin Cross | 0c461f1 | 2016-10-20 16:11:43 -0700 | [diff] [blame] | 78 | lib = "libprotobuf-cpp-full-ndk" |
| 79 | static = true |
| 80 | } else { |
| 81 | lib = "libprotobuf-cpp-full" |
| 82 | } |
| 83 | case "lite", "": |
Jeff Gaston | af3cc2d | 2017-09-27 17:01:44 -0700 | [diff] [blame] | 84 | if ctx.useSdk() { |
Colin Cross | 0c461f1 | 2016-10-20 16:11:43 -0700 | [diff] [blame] | 85 | lib = "libprotobuf-cpp-lite-ndk" |
| 86 | static = true |
| 87 | } else { |
| 88 | lib = "libprotobuf-cpp-lite" |
Colin Cross | 0c461f1 | 2016-10-20 16:11:43 -0700 | [diff] [blame] | 89 | } |
| 90 | default: |
Colin Cross | 5ff51b5 | 2017-05-02 13:34:32 -0700 | [diff] [blame] | 91 | ctx.PropertyErrorf("proto.type", "unknown proto type %q", |
Nan Zhang | 0007d81 | 2017-11-07 10:57:05 -0800 | [diff] [blame] | 92 | String(p.Proto.Type)) |
Colin Cross | 0c461f1 | 2016-10-20 16:11:43 -0700 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | if static { |
| 96 | deps.StaticLibs = append(deps.StaticLibs, lib) |
| 97 | deps.ReexportStaticLibHeaders = append(deps.ReexportStaticLibHeaders, lib) |
| 98 | } else { |
| 99 | deps.SharedLibs = append(deps.SharedLibs, lib) |
| 100 | deps.ReexportSharedLibHeaders = append(deps.ReexportSharedLibHeaders, lib) |
| 101 | } |
| 102 | |
| 103 | return deps |
| 104 | } |
| 105 | |
Colin Cross | 38f794e | 2017-09-07 10:53:07 -0700 | [diff] [blame] | 106 | func protoFlags(ctx ModuleContext, flags Flags, p *android.ProtoProperties) Flags { |
Colin Cross | 0c461f1 | 2016-10-20 16:11:43 -0700 | [diff] [blame] | 107 | flags.CFlags = append(flags.CFlags, "-DGOOGLE_PROTOBUF_NO_RTTI") |
Dan Willemsen | ab9f426 | 2018-02-14 13:58:34 -0800 | [diff] [blame^] | 108 | |
| 109 | flags.ProtoRoot = android.ProtoCanonicalPathFromRoot(ctx, p) |
| 110 | if flags.ProtoRoot { |
| 111 | flags.GlobalFlags = append(flags.GlobalFlags, "-I"+android.ProtoSubDir(ctx).String()) |
| 112 | } |
| 113 | flags.GlobalFlags = append(flags.GlobalFlags, "-I"+android.ProtoDir(ctx).String()) |
Colin Cross | 0c461f1 | 2016-10-20 16:11:43 -0700 | [diff] [blame] | 114 | |
Colin Cross | 38f794e | 2017-09-07 10:53:07 -0700 | [diff] [blame] | 115 | flags.protoFlags = android.ProtoFlags(ctx, p) |
Colin Cross | 5ff51b5 | 2017-05-02 13:34:32 -0700 | [diff] [blame] | 116 | |
Joe Onorato | 09e94ab | 2017-11-18 18:23:14 -0800 | [diff] [blame] | 117 | if proptools.String(p.Proto.Type) == "lite" { |
| 118 | flags.protoOutParams = []string{"lite"} |
| 119 | } |
| 120 | |
Colin Cross | 0c461f1 | 2016-10-20 16:11:43 -0700 | [diff] [blame] | 121 | return flags |
| 122 | } |