2017-09-07 19:53:07 +02:00
|
|
|
// Copyright 2017 Google Inc. All rights reserved.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package android
|
|
|
|
|
2019-03-28 22:45:07 +01:00
|
|
|
import (
|
2021-09-28 15:19:17 +02:00
|
|
|
"android/soong/bazel"
|
2022-02-04 22:01:20 +01:00
|
|
|
"regexp"
|
2019-03-28 22:45:07 +01:00
|
|
|
"strings"
|
|
|
|
|
2019-03-29 03:30:56 +01:00
|
|
|
"github.com/google/blueprint"
|
2019-03-28 22:45:07 +01:00
|
|
|
"github.com/google/blueprint/proptools"
|
|
|
|
)
|
|
|
|
|
2021-09-28 15:19:17 +02:00
|
|
|
const (
|
|
|
|
canonicalPathFromRootDefault = true
|
|
|
|
)
|
|
|
|
|
2022-02-04 22:01:20 +01:00
|
|
|
var (
|
|
|
|
// ignoring case, checks for proto or protos as an independent word in the name, whether at the
|
|
|
|
// beginning, end, or middle. e.g. "proto.foo", "bar-protos", "baz_proto_srcs" would all match
|
|
|
|
filegroupLikelyProtoPattern = regexp.MustCompile("(?i)(^|[^a-z])proto(s)?([^a-z]|$)")
|
|
|
|
|
|
|
|
ProtoSrcLabelPartition = bazel.LabelPartition{Extensions: []string{".proto"}, LabelMapper: isProtoFilegroup}
|
|
|
|
)
|
|
|
|
|
2017-09-07 19:53:07 +02:00
|
|
|
// TODO(ccross): protos are often used to communicate between multiple modules. If the only
|
|
|
|
// way to convert a proto to source is to reference it as a source file, and external modules cannot
|
|
|
|
// reference source files in other modules, then every module that owns a proto file will need to
|
|
|
|
// export a library for every type of external user (lite vs. full, c vs. c++ vs. java). It would
|
|
|
|
// be better to support a proto module type that exported a proto file along with some include dirs,
|
|
|
|
// and then external modules could depend on the proto module but use their own settings to
|
|
|
|
// generate the source.
|
|
|
|
|
2019-03-28 22:45:07 +01:00
|
|
|
type ProtoFlags struct {
|
|
|
|
Flags []string
|
|
|
|
CanonicalPathFromRoot bool
|
|
|
|
Dir ModuleGenPath
|
|
|
|
SubDir ModuleGenPath
|
|
|
|
OutTypeFlag string
|
|
|
|
OutParams []string
|
2019-03-29 03:30:56 +01:00
|
|
|
Deps Paths
|
|
|
|
}
|
|
|
|
|
|
|
|
type protoDependencyTag struct {
|
|
|
|
blueprint.BaseDependencyTag
|
|
|
|
name string
|
|
|
|
}
|
|
|
|
|
|
|
|
var ProtoPluginDepTag = protoDependencyTag{name: "plugin"}
|
|
|
|
|
|
|
|
func ProtoDeps(ctx BottomUpMutatorContext, p *ProtoProperties) {
|
|
|
|
if String(p.Proto.Plugin) != "" && String(p.Proto.Type) != "" {
|
|
|
|
ctx.ModuleErrorf("only one of proto.type and proto.plugin can be specified.")
|
|
|
|
}
|
|
|
|
|
|
|
|
if plugin := String(p.Proto.Plugin); plugin != "" {
|
2019-10-16 20:03:10 +02:00
|
|
|
ctx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(),
|
|
|
|
ProtoPluginDepTag, "protoc-gen-"+plugin)
|
2019-03-29 03:30:56 +01:00
|
|
|
}
|
2019-03-28 22:45:07 +01:00
|
|
|
}
|
2017-12-15 22:41:30 +01:00
|
|
|
|
2019-03-28 22:45:07 +01:00
|
|
|
func GetProtoFlags(ctx ModuleContext, p *ProtoProperties) ProtoFlags {
|
2019-03-29 03:30:56 +01:00
|
|
|
var flags []string
|
|
|
|
var deps Paths
|
|
|
|
|
2017-09-07 19:53:07 +02:00
|
|
|
if len(p.Proto.Local_include_dirs) > 0 {
|
|
|
|
localProtoIncludeDirs := PathsForModuleSrc(ctx, p.Proto.Local_include_dirs)
|
2019-03-29 03:30:56 +01:00
|
|
|
flags = append(flags, JoinWithPrefix(localProtoIncludeDirs.Strings(), "-I"))
|
2017-09-07 19:53:07 +02:00
|
|
|
}
|
|
|
|
if len(p.Proto.Include_dirs) > 0 {
|
|
|
|
rootProtoIncludeDirs := PathsForSource(ctx, p.Proto.Include_dirs)
|
2019-03-29 03:30:56 +01:00
|
|
|
flags = append(flags, JoinWithPrefix(rootProtoIncludeDirs.Strings(), "-I"))
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.VisitDirectDepsWithTag(ProtoPluginDepTag, func(dep Module) {
|
|
|
|
if hostTool, ok := dep.(HostToolProvider); !ok || !hostTool.HostToolPath().Valid() {
|
|
|
|
ctx.PropertyErrorf("proto.plugin", "module %q is not a host tool provider",
|
|
|
|
ctx.OtherModuleName(dep))
|
|
|
|
} else {
|
|
|
|
plugin := String(p.Proto.Plugin)
|
|
|
|
deps = append(deps, hostTool.HostToolPath().Path())
|
|
|
|
flags = append(flags, "--plugin=protoc-gen-"+plugin+"="+hostTool.HostToolPath().String())
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
var protoOutFlag string
|
|
|
|
if plugin := String(p.Proto.Plugin); plugin != "" {
|
|
|
|
protoOutFlag = "--" + plugin + "_out"
|
2017-09-07 19:53:07 +02:00
|
|
|
}
|
|
|
|
|
2019-03-28 22:45:07 +01:00
|
|
|
return ProtoFlags{
|
2019-03-29 03:30:56 +01:00
|
|
|
Flags: flags,
|
|
|
|
Deps: deps,
|
|
|
|
OutTypeFlag: protoOutFlag,
|
2021-09-28 15:19:17 +02:00
|
|
|
CanonicalPathFromRoot: proptools.BoolDefault(p.Proto.Canonical_path_from_root, canonicalPathFromRootDefault),
|
2019-03-28 22:45:07 +01:00
|
|
|
Dir: PathForModuleGen(ctx, "proto"),
|
|
|
|
SubDir: PathForModuleGen(ctx, "proto", ctx.ModuleDir()),
|
2018-02-14 22:58:34 +01:00
|
|
|
}
|
2017-09-07 19:53:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type ProtoProperties struct {
|
|
|
|
Proto struct {
|
|
|
|
// Proto generator type. C++: full or lite. Java: micro, nano, stream, or lite.
|
|
|
|
Type *string `android:"arch_variant"`
|
|
|
|
|
2019-03-29 03:30:56 +01:00
|
|
|
// Proto plugin to use as the generator. Must be a cc_binary_host module.
|
|
|
|
Plugin *string `android:"arch_variant"`
|
|
|
|
|
2017-09-07 19:53:07 +02:00
|
|
|
// list of directories that will be added to the protoc include paths.
|
|
|
|
Include_dirs []string
|
|
|
|
|
|
|
|
// list of directories relative to the bp file that will
|
|
|
|
// be added to the protoc include paths.
|
|
|
|
Local_include_dirs []string
|
2018-02-14 22:58:34 +01:00
|
|
|
|
|
|
|
// whether to identify the proto files from the root of the
|
|
|
|
// source tree (the original method in Android, useful for
|
|
|
|
// android-specific protos), or relative from where they were
|
|
|
|
// specified (useful for external/third party protos).
|
|
|
|
//
|
|
|
|
// This defaults to true today, but is expected to default to
|
|
|
|
// false in the future.
|
|
|
|
Canonical_path_from_root *bool
|
2017-09-07 19:53:07 +02:00
|
|
|
} `android:"arch_variant"`
|
|
|
|
}
|
2019-03-28 22:45:07 +01:00
|
|
|
|
2020-11-17 02:32:30 +01:00
|
|
|
func ProtoRule(rule *RuleBuilder, protoFile Path, flags ProtoFlags, deps Paths,
|
2019-03-28 22:45:07 +01:00
|
|
|
outDir WritablePath, depFile WritablePath, outputs WritablePaths) {
|
|
|
|
|
|
|
|
var protoBase string
|
|
|
|
if flags.CanonicalPathFromRoot {
|
|
|
|
protoBase = "."
|
|
|
|
} else {
|
|
|
|
rel := protoFile.Rel()
|
|
|
|
protoBase = strings.TrimSuffix(protoFile.String(), rel)
|
|
|
|
}
|
|
|
|
|
|
|
|
rule.Command().
|
2020-11-17 02:32:30 +01:00
|
|
|
BuiltTool("aprotoc").
|
2019-03-28 22:45:07 +01:00
|
|
|
FlagWithArg(flags.OutTypeFlag+"=", strings.Join(flags.OutParams, ",")+":"+outDir.String()).
|
|
|
|
FlagWithDepFile("--dependency_out=", depFile).
|
|
|
|
FlagWithArg("-I ", protoBase).
|
|
|
|
Flags(flags.Flags).
|
|
|
|
Input(protoFile).
|
|
|
|
Implicits(deps).
|
|
|
|
ImplicitOutputs(outputs)
|
|
|
|
|
|
|
|
rule.Command().
|
2020-11-17 02:32:30 +01:00
|
|
|
BuiltTool("dep_fixer").Flag(depFile.String())
|
2019-03-28 22:45:07 +01:00
|
|
|
}
|
2021-09-28 15:19:17 +02:00
|
|
|
|
|
|
|
// Bp2buildProtoInfo contains information necessary to pass on to language specific conversion.
|
|
|
|
type Bp2buildProtoInfo struct {
|
|
|
|
Type *string
|
|
|
|
Name string
|
|
|
|
}
|
|
|
|
|
|
|
|
type protoAttrs struct {
|
|
|
|
Srcs bazel.LabelListAttribute
|
|
|
|
Strip_import_prefix *string
|
|
|
|
}
|
|
|
|
|
|
|
|
// Bp2buildProtoProperties converts proto properties, creating a proto_library and returning the
|
|
|
|
// information necessary for language-specific handling.
|
2022-02-04 22:01:20 +01:00
|
|
|
func Bp2buildProtoProperties(ctx Bp2buildMutatorContext, m *ModuleBase, srcs bazel.LabelListAttribute) (Bp2buildProtoInfo, bool) {
|
2021-09-28 15:19:17 +02:00
|
|
|
var info Bp2buildProtoInfo
|
|
|
|
if srcs.IsEmpty() {
|
|
|
|
return info, false
|
|
|
|
}
|
|
|
|
|
|
|
|
info.Name = m.Name() + "_proto"
|
|
|
|
attrs := protoAttrs{
|
|
|
|
Srcs: srcs,
|
|
|
|
}
|
|
|
|
|
|
|
|
for axis, configToProps := range m.GetArchVariantProperties(ctx, &ProtoProperties{}) {
|
|
|
|
for _, rawProps := range configToProps {
|
|
|
|
var props *ProtoProperties
|
|
|
|
var ok bool
|
|
|
|
if props, ok = rawProps.(*ProtoProperties); !ok {
|
|
|
|
ctx.ModuleErrorf("Could not cast ProtoProperties to expected type")
|
|
|
|
}
|
|
|
|
if axis == bazel.NoConfigAxis {
|
|
|
|
info.Type = props.Proto.Type
|
|
|
|
|
2022-02-15 02:49:15 +01:00
|
|
|
if !proptools.BoolDefault(props.Proto.Canonical_path_from_root, canonicalPathFromRootDefault) {
|
2021-09-28 15:19:17 +02:00
|
|
|
// an empty string indicates to strips the package path
|
|
|
|
path := ""
|
|
|
|
attrs.Strip_import_prefix = &path
|
|
|
|
}
|
|
|
|
} else if props.Proto.Type != info.Type && props.Proto.Type != nil {
|
|
|
|
ctx.ModuleErrorf("Cannot handle arch-variant types for protos at this time.")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.CreateBazelTargetModule(
|
|
|
|
bazel.BazelTargetModuleProperties{Rule_class: "proto_library"},
|
|
|
|
CommonAttributes{Name: info.Name},
|
|
|
|
&attrs)
|
|
|
|
|
|
|
|
return info, true
|
|
|
|
}
|
2022-02-04 22:01:20 +01:00
|
|
|
|
|
|
|
func isProtoFilegroup(ctx bazel.OtherModuleContext, label bazel.Label) (string, bool) {
|
|
|
|
m, exists := ctx.ModuleFromName(label.OriginalModuleName)
|
|
|
|
labelStr := label.Label
|
|
|
|
if !exists || !IsFilegroup(ctx, m) {
|
|
|
|
return labelStr, false
|
|
|
|
}
|
|
|
|
likelyProtos := filegroupLikelyProtoPattern.MatchString(label.OriginalModuleName)
|
|
|
|
return labelStr, likelyProtos
|
|
|
|
}
|