Colin Cross | 38f794e | 2017-09-07 10:53:07 -0700 | [diff] [blame^] | 1 | // Copyright 2017 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 android |
| 16 | |
| 17 | import ( |
| 18 | "github.com/google/blueprint" |
| 19 | ) |
| 20 | |
| 21 | func init() { |
| 22 | pctx.HostBinToolVariable("protocCmd", "aprotoc") |
| 23 | } |
| 24 | |
| 25 | var ( |
| 26 | proto = pctx.AndroidStaticRule("protoc", |
| 27 | blueprint.RuleParams{ |
| 28 | Command: "$protocCmd $protoOut=$protoOutFlags:$outDir $protoFlags $in", |
| 29 | CommandDeps: []string{"$protocCmd"}, |
| 30 | }, "protoFlags", "protoOut", "protoOutFlags", "outDir") |
| 31 | ) |
| 32 | |
| 33 | // TODO(ccross): protos are often used to communicate between multiple modules. If the only |
| 34 | // way to convert a proto to source is to reference it as a source file, and external modules cannot |
| 35 | // reference source files in other modules, then every module that owns a proto file will need to |
| 36 | // export a library for every type of external user (lite vs. full, c vs. c++ vs. java). It would |
| 37 | // be better to support a proto module type that exported a proto file along with some include dirs, |
| 38 | // and then external modules could depend on the proto module but use their own settings to |
| 39 | // generate the source. |
| 40 | |
| 41 | func GenProto(ctx ModuleContext, protoFile Path, |
| 42 | protoFlags string, protoOut, protoOutFlags string, extensions []string) WritablePaths { |
| 43 | |
| 44 | var outFiles WritablePaths |
| 45 | for _, ext := range extensions { |
| 46 | outFiles = append(outFiles, GenPathWithExt(ctx, "proto", protoFile, ext)) |
| 47 | } |
| 48 | |
| 49 | ctx.ModuleBuild(pctx, ModuleBuildParams{ |
| 50 | Rule: proto, |
| 51 | Description: "protoc " + protoFile.Rel(), |
| 52 | Outputs: outFiles, |
| 53 | Input: protoFile, |
| 54 | Args: map[string]string{ |
| 55 | "outDir": ProtoDir(ctx).String(), |
| 56 | "protoOut": protoOut, |
| 57 | "protoOutFlags": protoOutFlags, |
| 58 | "protoFlags": protoFlags, |
| 59 | }, |
| 60 | }) |
| 61 | |
| 62 | return outFiles |
| 63 | } |
| 64 | |
| 65 | func ProtoFlags(ctx ModuleContext, p *ProtoProperties) []string { |
| 66 | var protoFlags []string |
| 67 | if len(p.Proto.Local_include_dirs) > 0 { |
| 68 | localProtoIncludeDirs := PathsForModuleSrc(ctx, p.Proto.Local_include_dirs) |
| 69 | protoFlags = append(protoFlags, JoinWithPrefix(localProtoIncludeDirs.Strings(), "-I")) |
| 70 | } |
| 71 | if len(p.Proto.Include_dirs) > 0 { |
| 72 | rootProtoIncludeDirs := PathsForSource(ctx, p.Proto.Include_dirs) |
| 73 | protoFlags = append(protoFlags, JoinWithPrefix(rootProtoIncludeDirs.Strings(), "-I")) |
| 74 | } |
| 75 | |
| 76 | protoFlags = append(protoFlags, "-I .") |
| 77 | |
| 78 | return protoFlags |
| 79 | } |
| 80 | |
| 81 | // ProtoDir returns the module's "gen/proto" directory |
| 82 | func ProtoDir(ctx ModuleContext) ModuleGenPath { |
| 83 | return PathForModuleGen(ctx, "proto") |
| 84 | } |
| 85 | |
| 86 | // ProtoSubDir returns the module's "gen/proto/path/to/module" directory |
| 87 | func ProtoSubDir(ctx ModuleContext) ModuleGenPath { |
| 88 | return PathForModuleGen(ctx, "proto", ctx.ModuleDir()) |
| 89 | } |
| 90 | |
| 91 | type ProtoProperties struct { |
| 92 | Proto struct { |
| 93 | // Proto generator type. C++: full or lite. Java: micro, nano, stream, or lite. |
| 94 | Type *string `android:"arch_variant"` |
| 95 | |
| 96 | // list of directories that will be added to the protoc include paths. |
| 97 | Include_dirs []string |
| 98 | |
| 99 | // list of directories relative to the bp file that will |
| 100 | // be added to the protoc include paths. |
| 101 | Local_include_dirs []string |
| 102 | } `android:"arch_variant"` |
| 103 | } |