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 | |
Colin Cross | 19878da | 2019-03-28 14:45:07 -0700 | [diff] [blame^] | 17 | import ( |
| 18 | "strings" |
| 19 | |
| 20 | "github.com/google/blueprint/proptools" |
| 21 | ) |
| 22 | |
Colin Cross | 38f794e | 2017-09-07 10:53:07 -0700 | [diff] [blame] | 23 | // TODO(ccross): protos are often used to communicate between multiple modules. If the only |
| 24 | // way to convert a proto to source is to reference it as a source file, and external modules cannot |
| 25 | // reference source files in other modules, then every module that owns a proto file will need to |
| 26 | // export a library for every type of external user (lite vs. full, c vs. c++ vs. java). It would |
| 27 | // be better to support a proto module type that exported a proto file along with some include dirs, |
| 28 | // and then external modules could depend on the proto module but use their own settings to |
| 29 | // generate the source. |
| 30 | |
Colin Cross | 19878da | 2019-03-28 14:45:07 -0700 | [diff] [blame^] | 31 | type ProtoFlags struct { |
| 32 | Flags []string |
| 33 | CanonicalPathFromRoot bool |
| 34 | Dir ModuleGenPath |
| 35 | SubDir ModuleGenPath |
| 36 | OutTypeFlag string |
| 37 | OutParams []string |
| 38 | } |
Colin Cross | a3b2500 | 2017-12-15 13:41:30 -0800 | [diff] [blame] | 39 | |
Colin Cross | 19878da | 2019-03-28 14:45:07 -0700 | [diff] [blame^] | 40 | func GetProtoFlags(ctx ModuleContext, p *ProtoProperties) ProtoFlags { |
| 41 | var protoFlags []string |
Colin Cross | 38f794e | 2017-09-07 10:53:07 -0700 | [diff] [blame] | 42 | if len(p.Proto.Local_include_dirs) > 0 { |
| 43 | localProtoIncludeDirs := PathsForModuleSrc(ctx, p.Proto.Local_include_dirs) |
| 44 | protoFlags = append(protoFlags, JoinWithPrefix(localProtoIncludeDirs.Strings(), "-I")) |
| 45 | } |
| 46 | if len(p.Proto.Include_dirs) > 0 { |
| 47 | rootProtoIncludeDirs := PathsForSource(ctx, p.Proto.Include_dirs) |
| 48 | protoFlags = append(protoFlags, JoinWithPrefix(rootProtoIncludeDirs.Strings(), "-I")) |
| 49 | } |
| 50 | |
Colin Cross | 19878da | 2019-03-28 14:45:07 -0700 | [diff] [blame^] | 51 | return ProtoFlags{ |
| 52 | Flags: protoFlags, |
| 53 | CanonicalPathFromRoot: proptools.BoolDefault(p.Proto.Canonical_path_from_root, true), |
| 54 | Dir: PathForModuleGen(ctx, "proto"), |
| 55 | SubDir: PathForModuleGen(ctx, "proto", ctx.ModuleDir()), |
Dan Willemsen | ab9f426 | 2018-02-14 13:58:34 -0800 | [diff] [blame] | 56 | } |
Colin Cross | 38f794e | 2017-09-07 10:53:07 -0700 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | type ProtoProperties struct { |
| 60 | Proto struct { |
| 61 | // Proto generator type. C++: full or lite. Java: micro, nano, stream, or lite. |
| 62 | Type *string `android:"arch_variant"` |
| 63 | |
| 64 | // list of directories that will be added to the protoc include paths. |
| 65 | Include_dirs []string |
| 66 | |
| 67 | // list of directories relative to the bp file that will |
| 68 | // be added to the protoc include paths. |
| 69 | Local_include_dirs []string |
Dan Willemsen | ab9f426 | 2018-02-14 13:58:34 -0800 | [diff] [blame] | 70 | |
| 71 | // whether to identify the proto files from the root of the |
| 72 | // source tree (the original method in Android, useful for |
| 73 | // android-specific protos), or relative from where they were |
| 74 | // specified (useful for external/third party protos). |
| 75 | // |
| 76 | // This defaults to true today, but is expected to default to |
| 77 | // false in the future. |
| 78 | Canonical_path_from_root *bool |
Colin Cross | 38f794e | 2017-09-07 10:53:07 -0700 | [diff] [blame] | 79 | } `android:"arch_variant"` |
| 80 | } |
Colin Cross | 19878da | 2019-03-28 14:45:07 -0700 | [diff] [blame^] | 81 | |
| 82 | func ProtoRule(ctx ModuleContext, rule *RuleBuilder, protoFile Path, flags ProtoFlags, deps Paths, |
| 83 | outDir WritablePath, depFile WritablePath, outputs WritablePaths) { |
| 84 | |
| 85 | var protoBase string |
| 86 | if flags.CanonicalPathFromRoot { |
| 87 | protoBase = "." |
| 88 | } else { |
| 89 | rel := protoFile.Rel() |
| 90 | protoBase = strings.TrimSuffix(protoFile.String(), rel) |
| 91 | } |
| 92 | |
| 93 | rule.Command(). |
| 94 | Tool(ctx.Config().HostToolPath(ctx, "aprotoc")). |
| 95 | FlagWithArg(flags.OutTypeFlag+"=", strings.Join(flags.OutParams, ",")+":"+outDir.String()). |
| 96 | FlagWithDepFile("--dependency_out=", depFile). |
| 97 | FlagWithArg("-I ", protoBase). |
| 98 | Flags(flags.Flags). |
| 99 | Input(protoFile). |
| 100 | Implicits(deps). |
| 101 | ImplicitOutputs(outputs) |
| 102 | |
| 103 | rule.Command(). |
| 104 | Tool(ctx.Config().HostToolPath(ctx, "dep_fixer")).Flag(depFile.String()) |
| 105 | } |