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