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