blob: 83dc32a994d642cc45764c243c1a67f406ec66e7 [file] [log] [blame]
Colin Cross38f794e2017-09-07 10:53:07 -07001// 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
15package android
16
Colin Cross19878da2019-03-28 14:45:07 -070017import (
18 "strings"
19
20 "github.com/google/blueprint/proptools"
21)
22
Colin Cross38f794e2017-09-07 10:53:07 -070023// 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 Cross19878da2019-03-28 14:45:07 -070031type ProtoFlags struct {
32 Flags []string
33 CanonicalPathFromRoot bool
34 Dir ModuleGenPath
35 SubDir ModuleGenPath
36 OutTypeFlag string
37 OutParams []string
38}
Colin Crossa3b25002017-12-15 13:41:30 -080039
Colin Cross19878da2019-03-28 14:45:07 -070040func GetProtoFlags(ctx ModuleContext, p *ProtoProperties) ProtoFlags {
41 var protoFlags []string
Colin Cross38f794e2017-09-07 10:53:07 -070042 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 Cross19878da2019-03-28 14:45:07 -070051 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 Willemsenab9f4262018-02-14 13:58:34 -080056 }
Colin Cross38f794e2017-09-07 10:53:07 -070057}
58
59type 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 Willemsenab9f4262018-02-14 13:58:34 -080070
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 Cross38f794e2017-09-07 10:53:07 -070079 } `android:"arch_variant"`
80}
Colin Cross19878da2019-03-28 14:45:07 -070081
82func 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}