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 | 5ff51b5 | 2017-05-02 13:34:32 -0700 | [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{ |
| 31 | Command: "$protocCmd --cpp_out=$outDir $protoFlags $in", |
| 32 | CommandDeps: []string{"$protocCmd"}, |
| 33 | }, "protoFlags", "outDir") |
| 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, |
| 39 | protoFlags string) (ccFile, headerFile android.WritablePath) { |
| 40 | |
| 41 | ccFile = android.GenPathWithExt(ctx, "proto", protoFile, "pb.cc") |
| 42 | headerFile = android.GenPathWithExt(ctx, "proto", protoFile, "pb.h") |
| 43 | |
| 44 | ctx.ModuleBuild(pctx, android.ModuleBuildParams{ |
| 45 | Rule: proto, |
| 46 | Description: "protoc " + protoFile.Rel(), |
| 47 | Outputs: android.WritablePaths{ccFile, headerFile}, |
| 48 | Input: protoFile, |
| 49 | Args: map[string]string{ |
| 50 | "outDir": android.ProtoDir(ctx).String(), |
| 51 | "protoFlags": protoFlags, |
| 52 | }, |
| 53 | }) |
| 54 | |
| 55 | return ccFile, headerFile |
| 56 | } |
| 57 | |
Colin Cross | 38f794e | 2017-09-07 10:53:07 -0700 | [diff] [blame] | 58 | func protoDeps(ctx BaseModuleContext, deps Deps, p *android.ProtoProperties, static bool) Deps { |
Colin Cross | 0c461f1 | 2016-10-20 16:11:43 -0700 | [diff] [blame] | 59 | var lib string |
Colin Cross | 0c461f1 | 2016-10-20 16:11:43 -0700 | [diff] [blame] | 60 | |
Colin Cross | 5ff51b5 | 2017-05-02 13:34:32 -0700 | [diff] [blame] | 61 | switch proptools.String(p.Proto.Type) { |
Colin Cross | 0c461f1 | 2016-10-20 16:11:43 -0700 | [diff] [blame] | 62 | case "full": |
| 63 | if ctx.sdk() { |
| 64 | lib = "libprotobuf-cpp-full-ndk" |
| 65 | static = true |
| 66 | } else { |
| 67 | lib = "libprotobuf-cpp-full" |
| 68 | } |
| 69 | case "lite", "": |
| 70 | if ctx.sdk() { |
| 71 | lib = "libprotobuf-cpp-lite-ndk" |
| 72 | static = true |
| 73 | } else { |
| 74 | lib = "libprotobuf-cpp-lite" |
Colin Cross | 0c461f1 | 2016-10-20 16:11:43 -0700 | [diff] [blame] | 75 | } |
| 76 | default: |
Colin Cross | 5ff51b5 | 2017-05-02 13:34:32 -0700 | [diff] [blame] | 77 | ctx.PropertyErrorf("proto.type", "unknown proto type %q", |
| 78 | proptools.String(p.Proto.Type)) |
Colin Cross | 0c461f1 | 2016-10-20 16:11:43 -0700 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | if static { |
| 82 | deps.StaticLibs = append(deps.StaticLibs, lib) |
| 83 | deps.ReexportStaticLibHeaders = append(deps.ReexportStaticLibHeaders, lib) |
| 84 | } else { |
| 85 | deps.SharedLibs = append(deps.SharedLibs, lib) |
| 86 | deps.ReexportSharedLibHeaders = append(deps.ReexportSharedLibHeaders, lib) |
| 87 | } |
| 88 | |
| 89 | return deps |
| 90 | } |
| 91 | |
Colin Cross | 38f794e | 2017-09-07 10:53:07 -0700 | [diff] [blame] | 92 | func protoFlags(ctx ModuleContext, flags Flags, p *android.ProtoProperties) Flags { |
Colin Cross | 0c461f1 | 2016-10-20 16:11:43 -0700 | [diff] [blame] | 93 | flags.CFlags = append(flags.CFlags, "-DGOOGLE_PROTOBUF_NO_RTTI") |
| 94 | flags.GlobalFlags = append(flags.GlobalFlags, |
Colin Cross | 38f794e | 2017-09-07 10:53:07 -0700 | [diff] [blame] | 95 | "-I"+android.ProtoSubDir(ctx).String(), |
| 96 | "-I"+android.ProtoDir(ctx).String(), |
Colin Cross | 0c461f1 | 2016-10-20 16:11:43 -0700 | [diff] [blame] | 97 | ) |
| 98 | |
Colin Cross | 38f794e | 2017-09-07 10:53:07 -0700 | [diff] [blame] | 99 | flags.protoFlags = android.ProtoFlags(ctx, p) |
Colin Cross | 5ff51b5 | 2017-05-02 13:34:32 -0700 | [diff] [blame] | 100 | |
Colin Cross | 0c461f1 | 2016-10-20 16:11:43 -0700 | [diff] [blame] | 101 | return flags |
| 102 | } |