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