2021-04-14 23:36:10 +02:00
|
|
|
// Copyright 2015 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
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
|
2021-09-20 21:14:39 +02:00
|
|
|
"android/soong/bazel"
|
|
|
|
|
2021-04-14 23:36:10 +02:00
|
|
|
"github.com/google/blueprint"
|
|
|
|
"github.com/google/blueprint/pathtools"
|
|
|
|
)
|
|
|
|
|
|
|
|
// bazel_paths contains methods to:
|
|
|
|
// * resolve Soong path and module references into bazel.LabelList
|
|
|
|
// * resolve Bazel path references into Soong-compatible paths
|
|
|
|
//
|
|
|
|
// There is often a similar method for Bazel as there is for Soong path handling and should be used
|
|
|
|
// in similar circumstances
|
|
|
|
//
|
2022-07-11 17:29:56 +02:00
|
|
|
// Bazel Soong
|
|
|
|
// ==============================================================
|
|
|
|
// BazelLabelForModuleSrc PathForModuleSrc
|
|
|
|
// BazelLabelForModuleSrcExcludes PathForModuleSrcExcludes
|
|
|
|
// BazelLabelForModuleDeps n/a
|
|
|
|
// tbd PathForSource
|
|
|
|
// tbd ExistentPathsForSources
|
|
|
|
// PathForBazelOut PathForModuleOut
|
2021-04-14 23:36:10 +02:00
|
|
|
//
|
|
|
|
// Use cases:
|
|
|
|
// * Module contains a property (often tagged `android:"path"`) that expects paths *relative to the
|
|
|
|
// module directory*:
|
|
|
|
// * BazelLabelForModuleSrcExcludes, if the module also contains an excludes_<propname> property
|
|
|
|
// * BazelLabelForModuleSrc, otherwise
|
|
|
|
// * Converting references to other modules to Bazel Labels:
|
|
|
|
// BazelLabelForModuleDeps
|
|
|
|
// * Converting a path obtained from bazel_handler cquery results:
|
|
|
|
// PathForBazelOut
|
|
|
|
//
|
|
|
|
// NOTE: all Soong globs are expanded within Soong rather than being converted to a Bazel glob
|
|
|
|
// syntax. This occurs because Soong does not have a concept of crossing package boundaries,
|
|
|
|
// so the glob as computed by Soong may contain paths that cross package-boundaries. These
|
|
|
|
// would be unknowingly omitted if the glob were handled by Bazel. By expanding globs within
|
|
|
|
// Soong, we support identification and detection (within Bazel) use of paths that cross
|
|
|
|
// package boundaries.
|
|
|
|
//
|
|
|
|
// Path resolution:
|
|
|
|
// * filepath/globs: resolves as itself or is converted to an absolute Bazel label (e.g.
|
|
|
|
// //path/to/dir:<filepath>) if path exists in a separate package or subpackage.
|
|
|
|
// * references to other modules (using the ":name{.tag}" syntax). These resolve as a Bazel label
|
|
|
|
// for a target. If the Bazel target is in the local module directory, it will be returned
|
|
|
|
// relative to the current package (e.g. ":<target>"). Otherwise, it will be returned as an
|
|
|
|
// absolute Bazel label (e.g. "//path/to/dir:<target>"). If the reference to another module
|
|
|
|
// cannot be resolved,the function will panic. This is often due to the dependency not being added
|
|
|
|
// via an AddDependency* method.
|
|
|
|
|
2022-07-11 17:29:56 +02:00
|
|
|
// BazelConversionContext is a minimal context interface to check if a module should be converted by bp2build,
|
2021-11-02 07:40:51 +01:00
|
|
|
// with functions containing information to match against allowlists and denylists.
|
|
|
|
// If a module is deemed to be convertible by bp2build, then it should rely on a
|
|
|
|
// BazelConversionPathContext for more functions for dep/path features.
|
|
|
|
type BazelConversionContext interface {
|
|
|
|
Config() Config
|
|
|
|
|
|
|
|
Module() Module
|
|
|
|
OtherModuleType(m blueprint.Module) string
|
|
|
|
OtherModuleName(m blueprint.Module) string
|
|
|
|
OtherModuleDir(m blueprint.Module) string
|
2022-03-28 21:53:03 +02:00
|
|
|
ModuleErrorf(format string, args ...interface{})
|
2021-11-02 07:40:51 +01:00
|
|
|
}
|
|
|
|
|
2021-04-14 23:36:10 +02:00
|
|
|
// A subset of the ModuleContext methods which are sufficient to resolve references to paths/deps in
|
|
|
|
// order to form a Bazel-compatible label for conversion.
|
|
|
|
type BazelConversionPathContext interface {
|
|
|
|
EarlyModulePathContext
|
2021-11-02 07:40:51 +01:00
|
|
|
BazelConversionContext
|
2021-04-14 23:36:10 +02:00
|
|
|
|
2023-08-11 22:15:12 +02:00
|
|
|
ModuleName() string
|
|
|
|
ModuleType() string
|
2021-11-02 07:40:51 +01:00
|
|
|
ModuleErrorf(fmt string, args ...interface{})
|
|
|
|
PropertyErrorf(property, fmt string, args ...interface{})
|
2021-04-14 23:36:10 +02:00
|
|
|
GetDirectDep(name string) (blueprint.Module, blueprint.DependencyTag)
|
2021-07-21 20:34:58 +02:00
|
|
|
ModuleFromName(name string) (blueprint.Module, bool)
|
2021-08-26 14:37:59 +02:00
|
|
|
AddUnconvertedBp2buildDep(string)
|
2021-12-15 21:35:38 +01:00
|
|
|
AddMissingBp2buildDep(dep string)
|
2021-04-14 23:36:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// BazelLabelForModuleDeps expects a list of reference to other modules, ("<module>"
|
|
|
|
// or ":<module>") and returns a Bazel-compatible label which corresponds to dependencies on the
|
|
|
|
// module within the given ctx.
|
2021-11-02 07:40:51 +01:00
|
|
|
func BazelLabelForModuleDeps(ctx BazelConversionPathContext, modules []string) bazel.LabelList {
|
2021-09-20 21:14:39 +02:00
|
|
|
return BazelLabelForModuleDepsWithFn(ctx, modules, BazelModuleLabel)
|
2021-06-11 00:20:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// BazelLabelForModuleWholeDepsExcludes expects two lists: modules (containing modules to include in
|
|
|
|
// the list), and excludes (modules to exclude from the list). Both of these should contain
|
|
|
|
// references to other modules, ("<module>" or ":<module>"). It returns a Bazel-compatible label
|
|
|
|
// list which corresponds to dependencies on the module within the given ctx, and the excluded
|
|
|
|
// dependencies. Prebuilt dependencies will be appended with _alwayslink so they can be handled as
|
|
|
|
// whole static libraries.
|
2021-11-02 07:40:51 +01:00
|
|
|
func BazelLabelForModuleDepsExcludes(ctx BazelConversionPathContext, modules, excludes []string) bazel.LabelList {
|
2021-09-20 21:14:39 +02:00
|
|
|
return BazelLabelForModuleDepsExcludesWithFn(ctx, modules, excludes, BazelModuleLabel)
|
2021-06-11 00:20:06 +02:00
|
|
|
}
|
|
|
|
|
2021-09-20 21:14:39 +02:00
|
|
|
// BazelLabelForModuleDepsWithFn expects a list of reference to other modules, ("<module>"
|
|
|
|
// or ":<module>") and applies moduleToLabelFn to determine and return a Bazel-compatible label
|
|
|
|
// which corresponds to dependencies on the module within the given ctx.
|
2021-11-02 07:40:51 +01:00
|
|
|
func BazelLabelForModuleDepsWithFn(ctx BazelConversionPathContext, modules []string,
|
|
|
|
moduleToLabelFn func(BazelConversionPathContext, blueprint.Module) string) bazel.LabelList {
|
2021-04-14 23:36:10 +02:00
|
|
|
var labels bazel.LabelList
|
bp2build: handle system_shared_libs
- If no system_shared_libs is specified, bp2build writes no attribute
value. In this case, the bazel library macros determine the correct
default behavior.
- If any system_shared_libs is specified for any variant, then bp2build
writes the value verbatim. This includes if an empty list is specified,
as this should override defaulting behavior.
Note this defaulting behavior is incomplete and will be incorrect in
corner cases. For example, if, in an Android.bp, system_shared_libs is
specified for os.linux_bionic but not for os.android, then the bazel
default for os.android will be incorrect. However, there are no current
modules in AOSP which fit this case.
As a related fix, supports static struct for cc_library_static.
Also, removes some elements from the bp2build denylist.
Test: mixed_droid CI
Change-Id: Iee5feeaaf05e8e7209c7a90c913173832ad7bf91
2021-08-04 03:01:05 +02:00
|
|
|
// In some cases, a nil string list is different than an explicitly empty list.
|
|
|
|
if len(modules) == 0 && modules != nil {
|
|
|
|
labels.Includes = []bazel.Label{}
|
|
|
|
return labels
|
|
|
|
}
|
2023-08-01 20:07:02 +02:00
|
|
|
modules = FirstUniqueStrings(modules)
|
2021-04-14 23:36:10 +02:00
|
|
|
for _, module := range modules {
|
|
|
|
bpText := module
|
|
|
|
if m := SrcIsModule(module); m == "" {
|
|
|
|
module = ":" + module
|
|
|
|
}
|
|
|
|
if m, t := SrcIsModuleWithTag(module); m != "" {
|
2021-09-20 21:14:39 +02:00
|
|
|
l := getOtherModuleLabel(ctx, m, t, moduleToLabelFn)
|
2021-12-15 21:35:38 +01:00
|
|
|
if l != nil {
|
|
|
|
l.OriginalModuleName = bpText
|
|
|
|
labels.Includes = append(labels.Includes, *l)
|
|
|
|
}
|
2021-04-14 23:36:10 +02:00
|
|
|
} else {
|
|
|
|
ctx.ModuleErrorf("%q, is not a module reference", module)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return labels
|
|
|
|
}
|
|
|
|
|
2021-09-20 21:14:39 +02:00
|
|
|
// BazelLabelForModuleDepsExcludesWithFn expects two lists: modules (containing modules to include in the
|
|
|
|
// list), and excludes (modules to exclude from the list). Both of these should contain references
|
|
|
|
// to other modules, ("<module>" or ":<module>"). It applies moduleToLabelFn to determine and return a
|
|
|
|
// Bazel-compatible label list which corresponds to dependencies on the module within the given ctx, and
|
|
|
|
// the excluded dependencies.
|
2021-11-02 07:40:51 +01:00
|
|
|
func BazelLabelForModuleDepsExcludesWithFn(ctx BazelConversionPathContext, modules, excludes []string,
|
|
|
|
moduleToLabelFn func(BazelConversionPathContext, blueprint.Module) string) bazel.LabelList {
|
2021-09-20 21:14:39 +02:00
|
|
|
moduleLabels := BazelLabelForModuleDepsWithFn(ctx, RemoveListFromList(modules, excludes), moduleToLabelFn)
|
2021-06-02 22:02:22 +02:00
|
|
|
if len(excludes) == 0 {
|
|
|
|
return moduleLabels
|
|
|
|
}
|
2021-09-20 21:14:39 +02:00
|
|
|
excludeLabels := BazelLabelForModuleDepsWithFn(ctx, excludes, moduleToLabelFn)
|
2021-06-02 22:02:22 +02:00
|
|
|
return bazel.LabelList{
|
|
|
|
Includes: moduleLabels.Includes,
|
|
|
|
Excludes: excludeLabels.Includes,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-02 07:40:51 +01:00
|
|
|
func BazelLabelForModuleSrcSingle(ctx BazelConversionPathContext, path string) bazel.Label {
|
2021-12-15 21:35:38 +01:00
|
|
|
if srcs := BazelLabelForModuleSrcExcludes(ctx, []string{path}, []string(nil)).Includes; len(srcs) > 0 {
|
|
|
|
return srcs[0]
|
|
|
|
}
|
|
|
|
return bazel.Label{}
|
2021-04-30 15:35:09 +02:00
|
|
|
}
|
|
|
|
|
2021-11-02 07:40:51 +01:00
|
|
|
func BazelLabelForModuleDepSingle(ctx BazelConversionPathContext, path string) bazel.Label {
|
2021-12-15 21:35:38 +01:00
|
|
|
if srcs := BazelLabelForModuleDepsExcludes(ctx, []string{path}, []string(nil)).Includes; len(srcs) > 0 {
|
|
|
|
return srcs[0]
|
|
|
|
}
|
|
|
|
return bazel.Label{}
|
2021-07-27 07:34:59 +02:00
|
|
|
}
|
|
|
|
|
2021-04-14 23:36:10 +02:00
|
|
|
// BazelLabelForModuleSrc expects a list of path (relative to local module directory) and module
|
|
|
|
// references (":<module>") and returns a bazel.LabelList{} containing the resolved references in
|
|
|
|
// paths, relative to the local module, or Bazel-labels (absolute if in a different package or
|
|
|
|
// relative if within the same package).
|
|
|
|
// Properties must have been annotated with struct tag `android:"path"` so that dependencies modules
|
2023-07-20 00:26:37 +02:00
|
|
|
// will have already been handled by the pathdeps mutator.
|
2021-11-02 07:40:51 +01:00
|
|
|
func BazelLabelForModuleSrc(ctx BazelConversionPathContext, paths []string) bazel.LabelList {
|
2021-04-14 23:36:10 +02:00
|
|
|
return BazelLabelForModuleSrcExcludes(ctx, paths, []string(nil))
|
|
|
|
}
|
|
|
|
|
|
|
|
// BazelLabelForModuleSrc expects lists of path and excludes (relative to local module directory)
|
|
|
|
// and module references (":<module>") and returns a bazel.LabelList{} containing the resolved
|
|
|
|
// references in paths, minus those in excludes, relative to the local module, or Bazel-labels
|
|
|
|
// (absolute if in a different package or relative if within the same package).
|
|
|
|
// Properties must have been annotated with struct tag `android:"path"` so that dependencies modules
|
2023-07-20 00:26:37 +02:00
|
|
|
// will have already been handled by the pathdeps mutator.
|
2021-11-02 07:40:51 +01:00
|
|
|
func BazelLabelForModuleSrcExcludes(ctx BazelConversionPathContext, paths, excludes []string) bazel.LabelList {
|
2021-04-14 23:36:10 +02:00
|
|
|
excludeLabels := expandSrcsForBazel(ctx, excludes, []string(nil))
|
|
|
|
excluded := make([]string, 0, len(excludeLabels.Includes))
|
|
|
|
for _, e := range excludeLabels.Includes {
|
|
|
|
excluded = append(excluded, e.Label)
|
|
|
|
}
|
|
|
|
labels := expandSrcsForBazel(ctx, paths, excluded)
|
|
|
|
labels.Excludes = excludeLabels.Includes
|
2023-06-21 03:50:33 +02:00
|
|
|
labels = TransformSubpackagePaths(ctx.Config(), ctx.ModuleDir(), labels)
|
2021-04-14 23:36:10 +02:00
|
|
|
return labels
|
|
|
|
}
|
|
|
|
|
2023-08-16 00:06:41 +02:00
|
|
|
func BazelLabelForSrcPatternExcludes(ctx BazelConversionPathContext, dir, pattern string, excludes []string) bazel.LabelList {
|
|
|
|
topRelPaths, err := ctx.GlobWithDeps(filepath.Join(dir, pattern), excludes)
|
|
|
|
if err != nil {
|
|
|
|
ctx.ModuleErrorf("Could not search dir: %s for pattern %s due to %v\n", dir, pattern, err)
|
|
|
|
}
|
|
|
|
// An intermediate list of labels relative to `dir` that assumes that there no subpacakges beneath `dir`
|
|
|
|
dirRelLabels := []bazel.Label{}
|
|
|
|
for _, topRelPath := range topRelPaths {
|
|
|
|
dirRelPath := Rel(ctx, dir, topRelPath)
|
|
|
|
dirRelLabels = append(dirRelLabels, bazel.Label{Label: "./" + dirRelPath})
|
|
|
|
}
|
|
|
|
// Return the package boudary resolved labels
|
|
|
|
return TransformSubpackagePaths(ctx.Config(), dir, bazel.MakeLabelList(dirRelLabels))
|
|
|
|
}
|
|
|
|
|
2022-09-21 12:27:42 +02:00
|
|
|
// Returns true if a prefix + components[:i] is a package boundary.
|
|
|
|
//
|
|
|
|
// A package boundary is determined by a BUILD file in the directory. This can happen in 2 cases:
|
|
|
|
//
|
2022-09-23 22:46:38 +02:00
|
|
|
// 1. An Android.bp exists, which bp2build will always convert to a sibling BUILD file.
|
|
|
|
// 2. An Android.bp doesn't exist, but a checked-in BUILD/BUILD.bazel file exists, and that file
|
|
|
|
// is allowlisted by the bp2build configuration to be merged into the symlink forest workspace.
|
2022-09-21 12:27:42 +02:00
|
|
|
func isPackageBoundary(config Config, prefix string, components []string, componentIndex int) bool {
|
|
|
|
prefix = filepath.Join(prefix, filepath.Join(components[:componentIndex+1]...))
|
|
|
|
if exists, _, _ := config.fs.Exists(filepath.Join(prefix, "Android.bp")); exists {
|
2021-04-14 23:36:10 +02:00
|
|
|
return true
|
2022-09-21 12:27:42 +02:00
|
|
|
} else if config.Bp2buildPackageConfig.ShouldKeepExistingBuildFileForDir(prefix) {
|
|
|
|
if exists, _, _ := config.fs.Exists(filepath.Join(prefix, "BUILD")); exists {
|
|
|
|
return true
|
|
|
|
} else if exists, _, _ := config.fs.Exists(filepath.Join(prefix, "BUILD.bazel")); exists {
|
|
|
|
return true
|
|
|
|
}
|
2021-04-14 23:36:10 +02:00
|
|
|
}
|
2022-09-21 12:27:42 +02:00
|
|
|
|
|
|
|
return false
|
2021-04-14 23:36:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Transform a path (if necessary) to acknowledge package boundaries
|
|
|
|
//
|
|
|
|
// e.g. something like
|
2022-08-16 19:27:33 +02:00
|
|
|
//
|
|
|
|
// async_safe/include/async_safe/CHECK.h
|
|
|
|
//
|
2021-04-14 23:36:10 +02:00
|
|
|
// might become
|
2022-08-16 19:27:33 +02:00
|
|
|
//
|
|
|
|
// //bionic/libc/async_safe:include/async_safe/CHECK.h
|
|
|
|
//
|
2021-04-14 23:36:10 +02:00
|
|
|
// if the "async_safe" directory is actually a package and not just a directory.
|
|
|
|
//
|
|
|
|
// In particular, paths that extend into packages are transformed into absolute labels beginning with //.
|
2023-06-21 03:50:33 +02:00
|
|
|
func transformSubpackagePath(cfg Config, dir string, path bazel.Label) bazel.Label {
|
2021-04-14 23:36:10 +02:00
|
|
|
var newPath bazel.Label
|
|
|
|
|
2021-04-19 07:00:15 +02:00
|
|
|
// Don't transform OriginalModuleName
|
|
|
|
newPath.OriginalModuleName = path.OriginalModuleName
|
2023-06-03 01:03:06 +02:00
|
|
|
// if it wasn't a module, store the original path. We may need the original path to replace
|
|
|
|
// references if it is actually in another package
|
|
|
|
if path.OriginalModuleName == "" {
|
|
|
|
newPath.OriginalModuleName = path.Label
|
|
|
|
}
|
2021-04-14 23:36:10 +02:00
|
|
|
|
|
|
|
if strings.HasPrefix(path.Label, "//") {
|
|
|
|
// Assume absolute labels are already correct (e.g. //path/to/some/package:foo.h)
|
|
|
|
newPath.Label = path.Label
|
|
|
|
return newPath
|
|
|
|
}
|
2022-09-23 22:46:38 +02:00
|
|
|
if strings.HasPrefix(path.Label, "./") {
|
|
|
|
// Drop "./" for consistent handling of paths.
|
|
|
|
// Specifically, to not let "." be considered a package boundary.
|
|
|
|
// Say `inputPath` is `x/Android.bp` and that file has some module
|
|
|
|
// with `srcs=["y/a.c", "z/b.c"]`.
|
|
|
|
// And say the directory tree is:
|
|
|
|
// x
|
|
|
|
// ├── Android.bp
|
|
|
|
// ├── y
|
|
|
|
// │ ├── a.c
|
|
|
|
// │ └── Android.bp
|
|
|
|
// └── z
|
|
|
|
// └── b.c
|
|
|
|
// Then bazel equivalent labels in srcs should be:
|
|
|
|
// //x/y:a.c, x/z/b.c
|
|
|
|
// The above should still be the case if `x/Android.bp` had
|
|
|
|
// srcs=["./y/a.c", "./z/b.c"]
|
|
|
|
// However, if we didn't strip "./", we'd get
|
|
|
|
// //x/./y:a.c, //x/.:z/b.c
|
|
|
|
path.Label = strings.TrimPrefix(path.Label, "./")
|
|
|
|
}
|
2021-04-14 23:36:10 +02:00
|
|
|
pathComponents := strings.Split(path.Label, "/")
|
2022-09-23 22:46:38 +02:00
|
|
|
newLabel := ""
|
2022-09-21 12:27:42 +02:00
|
|
|
foundPackageBoundary := false
|
2021-04-14 23:36:10 +02:00
|
|
|
// Check the deepest subdirectory first and work upwards
|
|
|
|
for i := len(pathComponents) - 1; i >= 0; i-- {
|
|
|
|
pathComponent := pathComponents[i]
|
|
|
|
var sep string
|
2023-06-21 03:50:33 +02:00
|
|
|
if !foundPackageBoundary && isPackageBoundary(cfg, dir, pathComponents, i) {
|
2021-04-14 23:36:10 +02:00
|
|
|
sep = ":"
|
2022-09-21 12:27:42 +02:00
|
|
|
foundPackageBoundary = true
|
2021-04-14 23:36:10 +02:00
|
|
|
} else {
|
|
|
|
sep = "/"
|
|
|
|
}
|
|
|
|
if newLabel == "" {
|
|
|
|
newLabel = pathComponent
|
|
|
|
} else {
|
|
|
|
newLabel = pathComponent + sep + newLabel
|
|
|
|
}
|
|
|
|
}
|
2022-09-21 12:27:42 +02:00
|
|
|
if foundPackageBoundary {
|
2021-04-14 23:36:10 +02:00
|
|
|
// Ensure paths end up looking like //bionic/... instead of //./bionic/...
|
2023-06-21 03:50:33 +02:00
|
|
|
moduleDir := dir
|
2021-04-14 23:36:10 +02:00
|
|
|
if strings.HasPrefix(moduleDir, ".") {
|
|
|
|
moduleDir = moduleDir[1:]
|
|
|
|
}
|
|
|
|
// Make the path into an absolute label (e.g. //bionic/libc/foo:bar.h instead of just foo:bar.h)
|
|
|
|
if moduleDir == "" {
|
|
|
|
newLabel = "//" + newLabel
|
|
|
|
} else {
|
|
|
|
newLabel = "//" + moduleDir + "/" + newLabel
|
|
|
|
}
|
|
|
|
}
|
|
|
|
newPath.Label = newLabel
|
|
|
|
|
|
|
|
return newPath
|
|
|
|
}
|
|
|
|
|
|
|
|
// Transform paths to acknowledge package boundaries
|
|
|
|
// See transformSubpackagePath() for more information
|
2023-06-21 03:50:33 +02:00
|
|
|
func TransformSubpackagePaths(cfg Config, dir string, paths bazel.LabelList) bazel.LabelList {
|
2021-04-14 23:36:10 +02:00
|
|
|
var newPaths bazel.LabelList
|
|
|
|
for _, include := range paths.Includes {
|
2023-06-21 03:50:33 +02:00
|
|
|
newPaths.Includes = append(newPaths.Includes, transformSubpackagePath(cfg, dir, include))
|
2021-04-14 23:36:10 +02:00
|
|
|
}
|
|
|
|
for _, exclude := range paths.Excludes {
|
2023-06-21 03:50:33 +02:00
|
|
|
newPaths.Excludes = append(newPaths.Excludes, transformSubpackagePath(cfg, dir, exclude))
|
2021-04-14 23:36:10 +02:00
|
|
|
}
|
|
|
|
return newPaths
|
|
|
|
}
|
|
|
|
|
Initial bp2build converter for android_app.
The only supported attributes at this point are:
- srcs
- manifest
- package_name
- resource_dirs
as they most easily map to bazel's android_binary's srcs, manifest, custom_package, and resource_files respectively.
Allow-listing all apps that use these fields, along with sdk_version and dex_preopt. The latter 2 are ignored by the converter,
- sdk_version because we're currently relying on a single pre-built SDK,
- dex_preopt because,
1. though it is not supported in Bazel builds yet, it doesn't prevent the apps from building, and
2. the apps being converted only use the dex_preopt attribute to disable dex_preopt, which is what is happening anyway.
Change-Id: I4a4f771eeb8f60a1cd4844b2ac1ce3df7c070e73
Test: ./build/bazel/scripts/run_presubmits.sh
Bug: 198224074
Bug: 203688791
2021-10-29 16:52:59 +02:00
|
|
|
// Converts root-relative Paths to a list of bazel.Label relative to the module in ctx.
|
|
|
|
func RootToModuleRelativePaths(ctx BazelConversionPathContext, paths Paths) []bazel.Label {
|
|
|
|
var newPaths []bazel.Label
|
|
|
|
for _, path := range PathsWithModuleSrcSubDir(ctx, paths, "") {
|
|
|
|
s := path.Rel()
|
|
|
|
newPaths = append(newPaths, bazel.Label{Label: s})
|
|
|
|
}
|
|
|
|
return newPaths
|
|
|
|
}
|
|
|
|
|
2021-04-14 23:36:10 +02:00
|
|
|
// expandSrcsForBazel returns bazel.LabelList with paths rooted from the module's local source
|
|
|
|
// directory and Bazel target labels, excluding those included in the excludes argument (which
|
|
|
|
// should already be expanded to resolve references to Soong-modules). Valid elements of paths
|
|
|
|
// include:
|
2022-08-16 19:27:33 +02:00
|
|
|
// - filepath, relative to local module directory, resolves as a filepath relative to the local
|
|
|
|
// source directory
|
|
|
|
// - glob, relative to the local module directory, resolves as filepath(s), relative to the local
|
|
|
|
// module directory. Because Soong does not have a concept of crossing package boundaries, the
|
|
|
|
// glob as computed by Soong may contain paths that cross package-boundaries that would be
|
|
|
|
// unknowingly omitted if the glob were handled by Bazel. To allow identification and detect
|
|
|
|
// (within Bazel) use of paths that cross package boundaries, we expand globs within Soong rather
|
|
|
|
// than converting Soong glob syntax to Bazel glob syntax. **Invalid for excludes.**
|
|
|
|
// - other modules using the ":name{.tag}" syntax. These modules must implement SourceFileProducer
|
|
|
|
// or OutputFileProducer. These resolve as a Bazel label for a target. If the Bazel target is in
|
|
|
|
// the local module directory, it will be returned relative to the current package (e.g.
|
|
|
|
// ":<target>"). Otherwise, it will be returned as an absolute Bazel label (e.g.
|
|
|
|
// "//path/to/dir:<target>"). If the reference to another module cannot be resolved,the function
|
|
|
|
// will panic.
|
|
|
|
//
|
2021-04-14 23:36:10 +02:00
|
|
|
// Properties passed as the paths or excludes argument must have been annotated with struct tag
|
|
|
|
// `android:"path"` so that dependencies on other modules will have already been handled by the
|
2023-07-20 00:26:37 +02:00
|
|
|
// pathdeps mutator.
|
2021-11-02 07:40:51 +01:00
|
|
|
func expandSrcsForBazel(ctx BazelConversionPathContext, paths, expandedExcludes []string) bazel.LabelList {
|
2021-04-14 23:36:10 +02:00
|
|
|
if paths == nil {
|
|
|
|
return bazel.LabelList{}
|
|
|
|
}
|
|
|
|
labels := bazel.LabelList{
|
|
|
|
Includes: []bazel.Label{},
|
|
|
|
}
|
2021-04-27 11:47:02 +02:00
|
|
|
|
|
|
|
// expandedExcludes contain module-dir relative paths, but root-relative paths
|
|
|
|
// are needed for GlobFiles later.
|
|
|
|
var rootRelativeExpandedExcludes []string
|
|
|
|
for _, e := range expandedExcludes {
|
|
|
|
rootRelativeExpandedExcludes = append(rootRelativeExpandedExcludes, filepath.Join(ctx.ModuleDir(), e))
|
|
|
|
}
|
|
|
|
|
2021-04-14 23:36:10 +02:00
|
|
|
for _, p := range paths {
|
|
|
|
if m, tag := SrcIsModuleWithTag(p); m != "" {
|
2021-09-20 21:14:39 +02:00
|
|
|
l := getOtherModuleLabel(ctx, m, tag, BazelModuleLabel)
|
2021-12-15 21:35:38 +01:00
|
|
|
if l != nil && !InList(l.Label, expandedExcludes) {
|
2023-08-18 00:35:04 +02:00
|
|
|
if strings.HasPrefix(m, "//") {
|
|
|
|
// this is a module in a soong namespace
|
|
|
|
// It appears as //<namespace>:<module_name> in srcs, and not ://<namespace>:<module_name>
|
|
|
|
l.OriginalModuleName = m
|
|
|
|
} else {
|
|
|
|
l.OriginalModuleName = fmt.Sprintf(":%s", m)
|
|
|
|
}
|
2021-12-15 21:35:38 +01:00
|
|
|
labels.Includes = append(labels.Includes, *l)
|
2021-04-14 23:36:10 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
var expandedPaths []bazel.Label
|
|
|
|
if pathtools.IsGlob(p) {
|
2021-04-27 11:47:02 +02:00
|
|
|
// e.g. turn "math/*.c" in
|
|
|
|
// external/arm-optimized-routines to external/arm-optimized-routines/math/*.c
|
|
|
|
rootRelativeGlobPath := pathForModuleSrc(ctx, p).String()
|
Initial bp2build converter for android_app.
The only supported attributes at this point are:
- srcs
- manifest
- package_name
- resource_dirs
as they most easily map to bazel's android_binary's srcs, manifest, custom_package, and resource_files respectively.
Allow-listing all apps that use these fields, along with sdk_version and dex_preopt. The latter 2 are ignored by the converter,
- sdk_version because we're currently relying on a single pre-built SDK,
- dex_preopt because,
1. though it is not supported in Bazel builds yet, it doesn't prevent the apps from building, and
2. the apps being converted only use the dex_preopt attribute to disable dex_preopt, which is what is happening anyway.
Change-Id: I4a4f771eeb8f60a1cd4844b2ac1ce3df7c070e73
Test: ./build/bazel/scripts/run_presubmits.sh
Bug: 198224074
Bug: 203688791
2021-10-29 16:52:59 +02:00
|
|
|
expandedPaths = RootToModuleRelativePaths(ctx, GlobFiles(ctx, rootRelativeGlobPath, rootRelativeExpandedExcludes))
|
2021-04-14 23:36:10 +02:00
|
|
|
} else {
|
|
|
|
if !InList(p, expandedExcludes) {
|
|
|
|
expandedPaths = append(expandedPaths, bazel.Label{Label: p})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
labels.Includes = append(labels.Includes, expandedPaths...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return labels
|
|
|
|
}
|
|
|
|
|
|
|
|
// getOtherModuleLabel returns a bazel.Label for the given dependency/tag combination for the
|
|
|
|
// module. The label will be relative to the current directory if appropriate. The dependency must
|
|
|
|
// already be resolved by either deps mutator or path deps mutator.
|
2021-11-02 07:40:51 +01:00
|
|
|
func getOtherModuleLabel(ctx BazelConversionPathContext, dep, tag string,
|
2021-12-15 21:35:38 +01:00
|
|
|
labelFromModule func(BazelConversionPathContext, blueprint.Module) string) *bazel.Label {
|
2021-07-21 20:34:58 +02:00
|
|
|
m, _ := ctx.ModuleFromName(dep)
|
2021-12-15 21:35:38 +01:00
|
|
|
// The module was not found in an Android.bp file, this is often due to:
|
|
|
|
// * a limited manifest
|
|
|
|
// * a required module not being converted from Android.mk
|
2021-04-14 23:36:10 +02:00
|
|
|
if m == nil {
|
2021-12-15 21:35:38 +01:00
|
|
|
ctx.AddMissingBp2buildDep(dep)
|
|
|
|
return &bazel.Label{
|
|
|
|
Label: ":" + dep + "__BP2BUILD__MISSING__DEP",
|
|
|
|
}
|
2021-04-14 23:36:10 +02:00
|
|
|
}
|
2021-08-26 14:37:59 +02:00
|
|
|
if !convertedToBazel(ctx, m) {
|
|
|
|
ctx.AddUnconvertedBp2buildDep(dep)
|
|
|
|
}
|
2021-09-20 21:14:39 +02:00
|
|
|
label := BazelModuleLabel(ctx, ctx.Module())
|
|
|
|
otherLabel := labelFromModule(ctx, m)
|
|
|
|
|
|
|
|
// TODO(b/165114590): Convert tag (":name{.tag}") to corresponding Bazel implicit output targets.
|
|
|
|
|
2021-04-14 23:36:10 +02:00
|
|
|
if samePackage(label, otherLabel) {
|
|
|
|
otherLabel = bazelShortLabel(otherLabel)
|
|
|
|
}
|
|
|
|
|
2021-12-15 21:35:38 +01:00
|
|
|
return &bazel.Label{
|
2021-04-14 23:36:10 +02:00
|
|
|
Label: otherLabel,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-02 07:40:51 +01:00
|
|
|
func BazelModuleLabel(ctx BazelConversionPathContext, module blueprint.Module) string {
|
2021-04-14 23:36:10 +02:00
|
|
|
// TODO(b/165114590): Convert tag (":name{.tag}") to corresponding Bazel implicit output targets.
|
2023-08-02 23:58:41 +02:00
|
|
|
if !convertedToBazel(ctx, module) || isGoModule(module) {
|
2021-04-14 23:36:10 +02:00
|
|
|
return bp2buildModuleLabel(ctx, module)
|
|
|
|
}
|
2021-08-26 14:37:59 +02:00
|
|
|
b, _ := module.(Bazelable)
|
2021-04-14 23:36:10 +02:00
|
|
|
return b.GetBazelLabel(ctx, module)
|
|
|
|
}
|
|
|
|
|
|
|
|
func bazelShortLabel(label string) string {
|
|
|
|
i := strings.Index(label, ":")
|
2021-11-02 07:23:07 +01:00
|
|
|
if i == -1 {
|
|
|
|
panic(fmt.Errorf("Could not find the ':' character in '%s', expected a fully qualified label.", label))
|
|
|
|
}
|
2021-04-14 23:36:10 +02:00
|
|
|
return label[i:]
|
|
|
|
}
|
|
|
|
|
|
|
|
func bazelPackage(label string) string {
|
|
|
|
i := strings.Index(label, ":")
|
2021-11-02 07:23:07 +01:00
|
|
|
if i == -1 {
|
|
|
|
panic(fmt.Errorf("Could not find the ':' character in '%s', expected a fully qualified label.", label))
|
|
|
|
}
|
2021-04-14 23:36:10 +02:00
|
|
|
return label[0:i]
|
|
|
|
}
|
|
|
|
|
|
|
|
func samePackage(label1, label2 string) bool {
|
|
|
|
return bazelPackage(label1) == bazelPackage(label2)
|
|
|
|
}
|
|
|
|
|
2021-11-02 07:40:51 +01:00
|
|
|
func bp2buildModuleLabel(ctx BazelConversionContext, module blueprint.Module) string {
|
2023-08-11 22:15:12 +02:00
|
|
|
moduleName := moduleNameWithPossibleOverride(ctx, module, ctx.OtherModuleName(module))
|
|
|
|
moduleDir := moduleDirWithPossibleOverride(ctx, module, ctx.OtherModuleDir(module))
|
2022-12-16 09:16:01 +01:00
|
|
|
if moduleDir == Bp2BuildTopLevel {
|
|
|
|
moduleDir = ""
|
|
|
|
}
|
2023-09-18 20:32:18 +02:00
|
|
|
if a, ok := module.(Module); ok && IsModulePrebuilt(a) {
|
|
|
|
moduleName = RemoveOptionalPrebuiltPrefix(moduleName)
|
|
|
|
}
|
2021-04-14 23:36:10 +02:00
|
|
|
return fmt.Sprintf("//%s:%s", moduleDir, moduleName)
|
|
|
|
}
|
|
|
|
|
|
|
|
// BazelOutPath is a Bazel output path compatible to be used for mixed builds within Soong/Ninja.
|
|
|
|
type BazelOutPath struct {
|
|
|
|
OutputPath
|
|
|
|
}
|
|
|
|
|
2021-09-28 19:48:21 +02:00
|
|
|
// ensure BazelOutPath implements Path
|
2021-04-14 23:36:10 +02:00
|
|
|
var _ Path = BazelOutPath{}
|
2021-09-28 19:48:21 +02:00
|
|
|
|
|
|
|
// ensure BazelOutPath implements genPathProvider
|
|
|
|
var _ genPathProvider = BazelOutPath{}
|
|
|
|
|
|
|
|
// ensure BazelOutPath implements objPathProvider
|
2021-04-14 23:36:10 +02:00
|
|
|
var _ objPathProvider = BazelOutPath{}
|
|
|
|
|
2021-09-28 19:48:21 +02:00
|
|
|
func (p BazelOutPath) genPathWithExt(ctx ModuleOutPathContext, subdir, ext string) ModuleGenPath {
|
|
|
|
return PathForModuleGen(ctx, subdir, pathtools.ReplaceExtension(p.path, ext))
|
|
|
|
}
|
|
|
|
|
2021-04-14 23:36:10 +02:00
|
|
|
func (p BazelOutPath) objPathWithExt(ctx ModuleOutPathContext, subdir, ext string) ModuleObjPath {
|
|
|
|
return PathForModuleObj(ctx, subdir, pathtools.ReplaceExtension(p.path, ext))
|
|
|
|
}
|
|
|
|
|
2022-06-04 01:00:11 +02:00
|
|
|
// PathForBazelOutRelative returns a BazelOutPath representing the path under an output directory dedicated to
|
|
|
|
// bazel-owned outputs. Calling .Rel() on the result will give the input path as relative to the given
|
|
|
|
// relativeRoot.
|
|
|
|
func PathForBazelOutRelative(ctx PathContext, relativeRoot string, path string) BazelOutPath {
|
|
|
|
validatedPath, err := validatePath(filepath.Join("execroot", "__main__", path))
|
2021-04-14 23:36:10 +02:00
|
|
|
if err != nil {
|
|
|
|
reportPathError(ctx, err)
|
|
|
|
}
|
2023-01-10 19:49:18 +01:00
|
|
|
var relativeRootPath string
|
|
|
|
if pathComponents := strings.SplitN(path, "/", 4); len(pathComponents) >= 3 &&
|
2022-06-04 01:00:11 +02:00
|
|
|
pathComponents[0] == "bazel-out" && pathComponents[2] == "bin" {
|
|
|
|
// If the path starts with something like: bazel-out/linux_x86_64-fastbuild-ST-b4ef1c4402f9/bin/
|
|
|
|
// make it relative to that folder. bazel-out/volatile-status.txt is an example
|
|
|
|
// of something that starts with bazel-out but is not relative to the bin folder
|
|
|
|
relativeRootPath = filepath.Join("execroot", "__main__", pathComponents[0], pathComponents[1], pathComponents[2], relativeRoot)
|
2023-01-10 19:49:18 +01:00
|
|
|
} else {
|
|
|
|
relativeRootPath = filepath.Join("execroot", "__main__", relativeRoot)
|
2022-06-04 01:00:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var relPath string
|
|
|
|
if relPath, err = filepath.Rel(relativeRootPath, validatedPath); err != nil || strings.HasPrefix(relPath, "../") {
|
|
|
|
// We failed to make this path relative to execroot/__main__, fall back to a non-relative path
|
|
|
|
// One case where this happens is when path is ../bazel_tools/something
|
|
|
|
relativeRootPath = ""
|
|
|
|
relPath = validatedPath
|
|
|
|
}
|
2021-04-14 23:36:10 +02:00
|
|
|
|
2022-06-04 01:00:11 +02:00
|
|
|
outputPath := OutputPath{
|
|
|
|
basePath{"", ""},
|
2021-08-26 15:07:24 +02:00
|
|
|
ctx.Config().soongOutDir,
|
2022-06-04 01:00:11 +02:00
|
|
|
ctx.Config().BazelContext.OutputBase(),
|
|
|
|
}
|
2021-04-14 23:36:10 +02:00
|
|
|
|
|
|
|
return BazelOutPath{
|
2022-06-04 01:00:11 +02:00
|
|
|
// .withRel() appends its argument onto the current path, and only the most
|
|
|
|
// recently appended part is returned by outputPath.rel().
|
|
|
|
// So outputPath.rel() will return relPath.
|
|
|
|
OutputPath: outputPath.withRel(relativeRootPath).withRel(relPath),
|
2021-04-14 23:36:10 +02:00
|
|
|
}
|
|
|
|
}
|
2021-04-12 21:42:51 +02:00
|
|
|
|
2022-06-04 01:00:11 +02:00
|
|
|
// PathForBazelOut returns a BazelOutPath representing the path under an output directory dedicated to
|
|
|
|
// bazel-owned outputs.
|
|
|
|
func PathForBazelOut(ctx PathContext, path string) BazelOutPath {
|
|
|
|
return PathForBazelOutRelative(ctx, "", path)
|
|
|
|
}
|
|
|
|
|
2021-04-12 21:42:51 +02:00
|
|
|
// PathsForBazelOut returns a list of paths representing the paths under an output directory
|
|
|
|
// dedicated to Bazel-owned outputs.
|
|
|
|
func PathsForBazelOut(ctx PathContext, paths []string) Paths {
|
|
|
|
outs := make(Paths, 0, len(paths))
|
|
|
|
for _, p := range paths {
|
|
|
|
outs = append(outs, PathForBazelOut(ctx, p))
|
|
|
|
}
|
|
|
|
return outs
|
|
|
|
}
|
2022-10-14 11:56:07 +02:00
|
|
|
|
|
|
|
// BazelStringOrLabelFromProp splits a Soong module property that can be
|
|
|
|
// either a string literal, path (with android:path tag) or a module reference
|
|
|
|
// into separate bazel string or label attributes. Bazel treats string and label
|
|
|
|
// attributes as distinct types, so this function categorizes a string property
|
|
|
|
// into either one of them.
|
|
|
|
//
|
|
|
|
// e.g. apex.private_key = "foo.pem" can either refer to:
|
|
|
|
//
|
|
|
|
// 1. "foo.pem" in the current directory -> file target
|
|
|
|
// 2. "foo.pem" module -> rule target
|
|
|
|
// 3. "foo.pem" file in a different directory, prefixed by a product variable handled
|
|
|
|
// in a bazel macro. -> string literal
|
|
|
|
//
|
|
|
|
// For the first two cases, they are defined using the label attribute. For the third case,
|
|
|
|
// it's defined with the string attribute.
|
|
|
|
func BazelStringOrLabelFromProp(
|
2023-09-19 22:09:00 +02:00
|
|
|
ctx Bp2buildMutatorContext,
|
2022-10-14 11:56:07 +02:00
|
|
|
propToDistinguish *string) (bazel.LabelAttribute, bazel.StringAttribute) {
|
|
|
|
|
|
|
|
var labelAttr bazel.LabelAttribute
|
|
|
|
var strAttr bazel.StringAttribute
|
|
|
|
|
|
|
|
if propToDistinguish == nil {
|
|
|
|
// nil pointer
|
|
|
|
return labelAttr, strAttr
|
|
|
|
}
|
|
|
|
|
|
|
|
prop := String(propToDistinguish)
|
|
|
|
if SrcIsModule(prop) != "" {
|
|
|
|
// If it's a module (SrcIsModule will return the module name), set the
|
|
|
|
// resolved label to the label attribute.
|
|
|
|
labelAttr.SetValue(BazelLabelForModuleDepSingle(ctx, prop))
|
|
|
|
} else {
|
|
|
|
// Not a module name. This could be a string literal or a file target in
|
|
|
|
// the current dir. Check if the path exists:
|
|
|
|
path := ExistentPathForSource(ctx, ctx.ModuleDir(), prop)
|
|
|
|
|
|
|
|
if path.Valid() && parentDir(path.String()) == ctx.ModuleDir() {
|
|
|
|
// If it exists and the path is relative to the current dir, resolve the bazel label
|
|
|
|
// for the _file target_ and set it to the label attribute.
|
|
|
|
//
|
|
|
|
// Resolution is necessary because this could be a file in a subpackage.
|
|
|
|
labelAttr.SetValue(BazelLabelForModuleSrcSingle(ctx, prop))
|
|
|
|
} else {
|
|
|
|
// Otherwise, treat it as a string literal and assign to the string attribute.
|
|
|
|
strAttr.Value = propToDistinguish
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return labelAttr, strAttr
|
|
|
|
}
|