Merge "Show less ambiguous paths in the error when it fails to match the given output file."
diff --git a/android/mutator.go b/android/mutator.go
index c6b6676..b361c51 100644
--- a/android/mutator.go
+++ b/android/mutator.go
@@ -16,9 +16,7 @@
import (
"android/soong/bazel"
- "fmt"
"reflect"
- "strings"
"sync"
"github.com/google/blueprint"
@@ -519,12 +517,6 @@
name string,
bazelProps bazel.BazelTargetModuleProperties,
attrs interface{}) {
- if strings.HasPrefix(name, bazel.BazelTargetModuleNamePrefix) {
- panic(fmt.Errorf(
- "The %s name prefix is added automatically, do not set it manually: %s",
- bazel.BazelTargetModuleNamePrefix,
- name))
- }
info := bp2buildInfo{
Name: name,
diff --git a/android/paths.go b/android/paths.go
index 763cd7c..2e378ba 100644
--- a/android/paths.go
+++ b/android/paths.go
@@ -263,38 +263,56 @@
// OptionalPath is a container that may or may not contain a valid Path.
type OptionalPath struct {
- valid bool
- path Path
+ path Path // nil if invalid.
+ invalidReason string // Not applicable if path != nil. "" if the reason is unknown.
}
// OptionalPathForPath returns an OptionalPath containing the path.
func OptionalPathForPath(path Path) OptionalPath {
- if path == nil {
- return OptionalPath{}
- }
- return OptionalPath{valid: true, path: path}
+ return OptionalPath{path: path}
+}
+
+// InvalidOptionalPath returns an OptionalPath that is invalid with the given reason.
+func InvalidOptionalPath(reason string) OptionalPath {
+
+ return OptionalPath{invalidReason: reason}
}
// Valid returns whether there is a valid path
func (p OptionalPath) Valid() bool {
- return p.valid
+ return p.path != nil
}
// Path returns the Path embedded in this OptionalPath. You must be sure that
// there is a valid path, since this method will panic if there is not.
func (p OptionalPath) Path() Path {
- if !p.valid {
- panic("Requesting an invalid path")
+ if p.path == nil {
+ msg := "Requesting an invalid path"
+ if p.invalidReason != "" {
+ msg += ": " + p.invalidReason
+ }
+ panic(msg)
}
return p.path
}
+// InvalidReason returns the reason that the optional path is invalid, or "" if it is valid.
+func (p OptionalPath) InvalidReason() string {
+ if p.path != nil {
+ return ""
+ }
+ if p.invalidReason == "" {
+ return "unknown"
+ }
+ return p.invalidReason
+}
+
// AsPaths converts the OptionalPath into Paths.
//
// It returns nil if this is not valid, or a single length slice containing the Path embedded in
// this OptionalPath.
func (p OptionalPath) AsPaths() Paths {
- if !p.valid {
+ if p.path == nil {
return nil
}
return Paths{p.path}
@@ -303,7 +321,7 @@
// RelativeToTop returns an OptionalPath with the path that was embedded having been replaced by the
// result of calling Path.RelativeToTop on it.
func (p OptionalPath) RelativeToTop() OptionalPath {
- if !p.valid {
+ if p.path == nil {
return p
}
p.path = p.path.RelativeToTop()
@@ -312,7 +330,7 @@
// String returns the string version of the Path, or "" if it isn't valid.
func (p OptionalPath) String() string {
- if p.valid {
+ if p.path != nil {
return p.path.String()
} else {
return ""
@@ -1077,6 +1095,7 @@
path, err := pathForSource(ctx, pathComponents...)
if err != nil {
reportPathError(ctx, err)
+ // No need to put the error message into the returned path since it has been reported already.
return OptionalPath{}
}
@@ -1091,7 +1110,7 @@
return OptionalPath{}
}
if !exists {
- return OptionalPath{}
+ return InvalidOptionalPath(path.String() + " does not exist")
}
return OptionalPathForPath(path)
}
@@ -1127,6 +1146,7 @@
relDir = srcPath.path
} else {
ReportPathErrorf(ctx, "Cannot find relative path for %s(%s)", reflect.TypeOf(path).Name(), path)
+ // No need to put the error message into the returned path since it has been reported already.
return OptionalPath{}
}
dir := filepath.Join(p.srcDir, p.path, relDir)
@@ -1140,7 +1160,7 @@
return OptionalPath{}
}
if len(paths) == 0 {
- return OptionalPath{}
+ return InvalidOptionalPath(dir + " does not exist")
}
relPath := Rel(ctx, p.srcDir, paths[0])
return OptionalPathForPath(PathForSource(ctx, relPath))
diff --git a/android/paths_test.go b/android/paths_test.go
index f4e4ce1..3f4625d 100644
--- a/android/paths_test.go
+++ b/android/paths_test.go
@@ -137,26 +137,35 @@
func TestOptionalPath(t *testing.T) {
var path OptionalPath
- checkInvalidOptionalPath(t, path)
+ checkInvalidOptionalPath(t, path, "unknown")
path = OptionalPathForPath(nil)
- checkInvalidOptionalPath(t, path)
+ checkInvalidOptionalPath(t, path, "unknown")
+
+ path = InvalidOptionalPath("foo")
+ checkInvalidOptionalPath(t, path, "foo")
+
+ path = InvalidOptionalPath("")
+ checkInvalidOptionalPath(t, path, "unknown")
path = OptionalPathForPath(PathForTesting("path"))
checkValidOptionalPath(t, path, "path")
}
-func checkInvalidOptionalPath(t *testing.T, path OptionalPath) {
+func checkInvalidOptionalPath(t *testing.T, path OptionalPath, expectedInvalidReason string) {
t.Helper()
if path.Valid() {
- t.Errorf("Uninitialized OptionalPath should not be valid")
+ t.Errorf("Invalid OptionalPath should not be valid")
+ }
+ if path.InvalidReason() != expectedInvalidReason {
+ t.Errorf("Wrong invalid reason: expected %q, got %q", expectedInvalidReason, path.InvalidReason())
}
if path.String() != "" {
- t.Errorf("Uninitialized OptionalPath String() should return \"\", not %q", path.String())
+ t.Errorf("Invalid OptionalPath String() should return \"\", not %q", path.String())
}
paths := path.AsPaths()
if len(paths) != 0 {
- t.Errorf("Uninitialized OptionalPath AsPaths() should return empty Paths, not %q", paths)
+ t.Errorf("Invalid OptionalPath AsPaths() should return empty Paths, not %q", paths)
}
defer func() {
if r := recover(); r == nil {
@@ -171,6 +180,9 @@
if !path.Valid() {
t.Errorf("Initialized OptionalPath should not be invalid")
}
+ if path.InvalidReason() != "" {
+ t.Errorf("Initialized OptionalPath should not have an invalid reason, got: %q", path.InvalidReason())
+ }
if path.String() != expectedString {
t.Errorf("Initialized OptionalPath String() should return %q, not %q", expectedString, path.String())
}
diff --git a/android/testing.go b/android/testing.go
index 78b3299..b9d8fa8 100644
--- a/android/testing.go
+++ b/android/testing.go
@@ -491,6 +491,66 @@
ctx.preSingletons = append(ctx.preSingletons, newPreSingleton(name, factory))
}
+// ModuleVariantForTests selects a specific variant of the module with the given
+// name by matching the variations map against the variations of each module
+// variant. A module variant matches the map if every variation that exists in
+// both have the same value. Both the module and the map are allowed to have
+// extra variations that the other doesn't have. Panics if not exactly one
+// module variant matches.
+func (ctx *TestContext) ModuleVariantForTests(name string, matchVariations map[string]string) TestingModule {
+ modules := []Module{}
+ ctx.VisitAllModules(func(m blueprint.Module) {
+ if ctx.ModuleName(m) == name {
+ am := m.(Module)
+ amMut := am.base().commonProperties.DebugMutators
+ amVar := am.base().commonProperties.DebugVariations
+ matched := true
+ for i, mut := range amMut {
+ if wantedVar, found := matchVariations[mut]; found && amVar[i] != wantedVar {
+ matched = false
+ break
+ }
+ }
+ if matched {
+ modules = append(modules, am)
+ }
+ }
+ })
+
+ if len(modules) == 0 {
+ // Show all the modules or module variants that do exist.
+ var allModuleNames []string
+ var allVariants []string
+ ctx.VisitAllModules(func(m blueprint.Module) {
+ allModuleNames = append(allModuleNames, ctx.ModuleName(m))
+ if ctx.ModuleName(m) == name {
+ allVariants = append(allVariants, m.(Module).String())
+ }
+ })
+
+ if len(allVariants) == 0 {
+ panic(fmt.Errorf("failed to find module %q. All modules:\n %s",
+ name, strings.Join(SortedUniqueStrings(allModuleNames), "\n ")))
+ } else {
+ sort.Strings(allVariants)
+ panic(fmt.Errorf("failed to find module %q matching %v. All variants:\n %s",
+ name, matchVariations, strings.Join(allVariants, "\n ")))
+ }
+ }
+
+ if len(modules) > 1 {
+ moduleStrings := []string{}
+ for _, m := range modules {
+ moduleStrings = append(moduleStrings, m.String())
+ }
+ sort.Strings(moduleStrings)
+ panic(fmt.Errorf("module %q has more than one variant that match %v:\n %s",
+ name, matchVariations, strings.Join(moduleStrings, "\n ")))
+ }
+
+ return newTestingModule(ctx.config, modules[0])
+}
+
func (ctx *TestContext) ModuleForTests(name, variant string) TestingModule {
var module Module
ctx.VisitAllModules(func(m blueprint.Module) {
diff --git a/apex/apex_test.go b/apex/apex_test.go
index bcbf1ad..2a2a1f4 100644
--- a/apex/apex_test.go
+++ b/apex/apex_test.go
@@ -2187,6 +2187,7 @@
name string
expectedError string
bp string
+ preparer android.FixturePreparer
}{
{
name: "Non-updatable apex with non-stable dep",
@@ -2258,6 +2259,30 @@
`,
},
{
+ name: "Updatable apex with non-stable legacy core platform dep",
+ expectedError: `\Qcannot depend on "myjar-uses-legacy": non stable SDK core_platform_current - uses legacy core platform\E`,
+ bp: `
+ apex {
+ name: "myapex",
+ java_libs: ["myjar-uses-legacy"],
+ key: "myapex.key",
+ updatable: true,
+ }
+ apex_key {
+ name: "myapex.key",
+ public_key: "testkey.avbpubkey",
+ private_key: "testkey.pem",
+ }
+ java_library {
+ name: "myjar-uses-legacy",
+ srcs: ["foo/bar/MyClass.java"],
+ sdk_version: "core_platform",
+ apex_available: ["myapex"],
+ }
+ `,
+ preparer: java.FixtureUseLegacyCorePlatformApi("myjar-uses-legacy"),
+ },
+ {
name: "Updatable apex with non-stable transitive dep",
// This is not actually detecting that the transitive dependency is unstable, rather it is
// detecting that the transitive dependency is building against a wider API surface than the
@@ -2293,12 +2318,22 @@
}
for _, test := range testCases {
+ if test.name != "Updatable apex with non-stable legacy core platform dep" {
+ continue
+ }
t.Run(test.name, func(t *testing.T) {
- if test.expectedError == "" {
- testApex(t, test.bp)
- } else {
- testApexError(t, test.expectedError, test.bp)
+ errorHandler := android.FixtureExpectsNoErrors
+ if test.expectedError != "" {
+ errorHandler = android.FixtureExpectsAtLeastOneErrorMatchingPattern(test.expectedError)
}
+ android.GroupFixturePreparers(
+ java.PrepareForTestWithJavaDefaultModules,
+ PrepareForTestWithApexBuildComponents,
+ prepareForTestWithMyapex,
+ android.OptionalFixturePreparer(test.preparer),
+ ).
+ ExtendWithErrorHandler(errorHandler).
+ RunTestWithBp(t, test.bp)
})
}
}
diff --git a/bazel/cquery/request_type.go b/bazel/cquery/request_type.go
index 131f0ec..d731f3e 100644
--- a/bazel/cquery/request_type.go
+++ b/bazel/cquery/request_type.go
@@ -25,6 +25,7 @@
// be a subset of OutputFiles. (or shared libraries, this will be equal to OutputFiles,
// but general cc_library will also have dynamic libraries in output files).
RootDynamicLibraries []string
+ TocFile string
}
type getOutputFilesRequestType struct{}
@@ -100,14 +101,15 @@
func (g getCcInfoType) StarlarkFunctionBody() string {
return `
outputFiles = [f.path for f in target.files.to_list()]
+cc_info = providers(target)["CcInfo"]
-includes = providers(target)["CcInfo"].compilation_context.includes.to_list()
-system_includes = providers(target)["CcInfo"].compilation_context.system_includes.to_list()
+includes = cc_info.compilation_context.includes.to_list()
+system_includes = cc_info.compilation_context.system_includes.to_list()
ccObjectFiles = []
staticLibraries = []
rootStaticArchives = []
-linker_inputs = providers(target)["CcInfo"].linking_context.linker_inputs.to_list()
+linker_inputs = cc_info.linking_context.linker_inputs.to_list()
for linker_input in linker_inputs:
for library in linker_input.libraries:
@@ -120,11 +122,17 @@
rootDynamicLibraries = []
-if "@rules_cc//examples:experimental_cc_shared_library.bzl%CcSharedLibraryInfo" in providers(target):
- shared_info = providers(target)["@rules_cc//examples:experimental_cc_shared_library.bzl%CcSharedLibraryInfo"]
+shared_info_tag = "@rules_cc//examples:experimental_cc_shared_library.bzl%CcSharedLibraryInfo"
+if shared_info_tag in providers(target):
+ shared_info = providers(target)[shared_info_tag]
for lib in shared_info.linker_input.libraries:
rootDynamicLibraries += [lib.dynamic_library.path]
+toc_file = ""
+toc_file_tag = "//build/bazel/rules:generate_toc.bzl%CcTocInfo"
+if toc_file_tag in providers(target):
+ toc_file = providers(target)[toc_file_tag].toc.path
+
returns = [
outputFiles,
staticLibraries,
@@ -132,10 +140,10 @@
includes,
system_includes,
rootStaticArchives,
- rootDynamicLibraries
+ rootDynamicLibraries,
]
-return "|".join([", ".join(r) for r in returns])`
+return "|".join([", ".join(r) for r in returns] + [toc_file])`
}
// ParseResult returns a value obtained by parsing the result of the request's Starlark function.
@@ -146,7 +154,7 @@
var ccObjects []string
splitString := strings.Split(rawString, "|")
- if expectedLen := 7; len(splitString) != expectedLen {
+ if expectedLen := 8; len(splitString) != expectedLen {
return CcInfo{}, fmt.Errorf("Expected %d items, got %q", expectedLen, splitString)
}
outputFilesString := splitString[0]
@@ -159,6 +167,7 @@
systemIncludes := splitOrEmpty(splitString[4], ", ")
rootStaticArchives := splitOrEmpty(splitString[5], ", ")
rootDynamicLibraries := splitOrEmpty(splitString[6], ", ")
+ tocFile := splitString[7] // NOTE: Will be the empty string if there wasn't
return CcInfo{
OutputFiles: outputFiles,
CcObjectFiles: ccObjects,
@@ -167,6 +176,7 @@
SystemIncludes: systemIncludes,
RootStaticArchives: rootStaticArchives,
RootDynamicLibraries: rootDynamicLibraries,
+ TocFile: tocFile,
}, nil
}
diff --git a/bazel/cquery/request_type_test.go b/bazel/cquery/request_type_test.go
index 49019ab..34d0832 100644
--- a/bazel/cquery/request_type_test.go
+++ b/bazel/cquery/request_type_test.go
@@ -71,7 +71,7 @@
}{
{
description: "no result",
- input: "||||||",
+ input: "|||||||",
expectedOutput: CcInfo{
OutputFiles: []string{},
CcObjectFiles: []string{},
@@ -80,11 +80,12 @@
SystemIncludes: []string{},
RootStaticArchives: []string{},
RootDynamicLibraries: []string{},
+ TocFile: "",
},
},
{
description: "only output",
- input: "test||||||",
+ input: "test|||||||",
expectedOutput: CcInfo{
OutputFiles: []string{"test"},
CcObjectFiles: []string{},
@@ -93,11 +94,12 @@
SystemIncludes: []string{},
RootStaticArchives: []string{},
RootDynamicLibraries: []string{},
+ TocFile: "",
},
},
{
description: "all items set",
- input: "out1, out2|static_lib1, static_lib2|object1, object2|., dir/subdir|system/dir, system/other/dir|rootstaticarchive1|rootdynamiclibrary1",
+ input: "out1, out2|static_lib1, static_lib2|object1, object2|., dir/subdir|system/dir, system/other/dir|rootstaticarchive1|rootdynamiclibrary1|lib.so.toc",
expectedOutput: CcInfo{
OutputFiles: []string{"out1", "out2"},
CcObjectFiles: []string{"object1", "object2"},
@@ -106,19 +108,20 @@
SystemIncludes: []string{"system/dir", "system/other/dir"},
RootStaticArchives: []string{"rootstaticarchive1"},
RootDynamicLibraries: []string{"rootdynamiclibrary1"},
+ TocFile: "lib.so.toc",
},
},
{
description: "too few result splits",
input: "|",
expectedOutput: CcInfo{},
- expectedErrorMessage: fmt.Sprintf("Expected %d items, got %q", 7, []string{"", ""}),
+ expectedErrorMessage: fmt.Sprintf("Expected %d items, got %q", 8, []string{"", ""}),
},
{
description: "too many result splits",
input: strings.Repeat("|", 8),
expectedOutput: CcInfo{},
- expectedErrorMessage: fmt.Sprintf("Expected %d items, got %q", 7, make([]string, 9)),
+ expectedErrorMessage: fmt.Sprintf("Expected %d items, got %q", 8, make([]string, 9)),
},
}
for _, tc := range testCases {
diff --git a/bazel/properties.go b/bazel/properties.go
index ed4e9fc..d3b40a2 100644
--- a/bazel/properties.go
+++ b/bazel/properties.go
@@ -19,7 +19,6 @@
"path/filepath"
"regexp"
"sort"
- "strings"
)
// BazelTargetModuleProperties contain properties and metadata used for
@@ -32,12 +31,6 @@
Bzl_load_location string `blueprint:"mutated"`
}
-const BazelTargetModuleNamePrefix = "__bp2build__"
-
-func StripNamePrefix(moduleName string) string {
- return strings.TrimPrefix(moduleName, BazelTargetModuleNamePrefix)
-}
-
var productVariableSubstitutionPattern = regexp.MustCompile("%(d|s)")
// Label is used to represent a Bazel compatible Label. Also stores the original
diff --git a/bp2build/Android.bp b/bp2build/Android.bp
index 1d52a70..40526a6 100644
--- a/bp2build/Android.bp
+++ b/bp2build/Android.bp
@@ -11,7 +11,6 @@
"build_conversion.go",
"bzl_conversion.go",
"configurability.go",
- "compatibility.go",
"constants.go",
"conversion.go",
"metrics.go",
diff --git a/bp2build/bp2build.go b/bp2build/bp2build.go
index 48b2945..45a3cb6 100644
--- a/bp2build/bp2build.go
+++ b/bp2build/bp2build.go
@@ -43,7 +43,7 @@
writeFiles(ctx, bp2buildDir, bp2buildFiles)
soongInjectionDir := android.PathForOutput(ctx, bazel.SoongInjectionDirName)
- writeFiles(ctx, soongInjectionDir, CreateSoongInjectionFiles(res.compatLayer))
+ writeFiles(ctx, soongInjectionDir, CreateSoongInjectionFiles(res.metrics))
return res.metrics
}
diff --git a/bp2build/build_conversion.go b/bp2build/build_conversion.go
index 10e2329..f7b392b 100644
--- a/bp2build/build_conversion.go
+++ b/bp2build/build_conversion.go
@@ -153,10 +153,10 @@
}
type CodegenContext struct {
- config android.Config
- context android.Context
- mode CodegenMode
- additionalDeps []string
+ config android.Config
+ context android.Context
+ mode CodegenMode
+ additionalDeps []string
unconvertedDepMode unconvertedDepsMode
}
@@ -249,7 +249,6 @@
type conversionResults struct {
buildFileToTargets map[string]BazelTargets
metrics CodegenMetrics
- compatLayer CodegenCompatLayer
}
func (r conversionResults) BuildDirToTargets() map[string]BazelTargets {
@@ -265,10 +264,6 @@
RuleClassCount: make(map[string]int),
}
- compatLayer := CodegenCompatLayer{
- NameToLabelMap: make(map[string]string),
- }
-
dirs := make(map[string]bool)
var errs []error
@@ -285,7 +280,7 @@
if b, ok := m.(android.Bazelable); ok && b.HasHandcraftedLabel() {
metrics.handCraftedTargetCount += 1
metrics.TotalModuleCount += 1
- compatLayer.AddNameToLabelEntry(m.Name(), b.HandcraftedLabel())
+ metrics.AddConvertedModule(m.Name())
pathToBuildFile := getBazelPackagePath(b)
// We are using the entire contents of handcrafted build file, so if multiple targets within
// a package have handcrafted targets, we only want to include the contents one time.
@@ -314,10 +309,8 @@
}
targets = generateBazelTargets(bpCtx, aModule)
for _, t := range targets {
- if t.name == m.Name() {
- // only add targets that exist in Soong to compatibility layer
- compatLayer.AddNameToLabelEntry(m.Name(), t.Label())
- }
+ // only add targets that exist in Soong to compatibility layer
+ metrics.AddConvertedModule(m.Name())
metrics.RuleClassCount[t.ruleClass] += 1
}
} else {
@@ -360,7 +353,6 @@
return conversionResults{
buildFileToTargets: buildFileToTargets,
metrics: metrics,
- compatLayer: compatLayer,
}, errs
}
@@ -710,10 +702,6 @@
return strings.Repeat(" ", indent)
}
-func targetNameForBp2Build(c bpToBuildContext, logicModule blueprint.Module) string {
- return strings.Replace(c.ModuleName(logicModule), bazel.BazelTargetModuleNamePrefix, "", 1)
-}
-
func targetNameWithVariant(c bpToBuildContext, logicModule blueprint.Module) string {
name := ""
if c.ModuleSubDir(logicModule) != "" {
diff --git a/bp2build/compatibility.go b/bp2build/compatibility.go
deleted file mode 100644
index 5baa524..0000000
--- a/bp2build/compatibility.go
+++ /dev/null
@@ -1,31 +0,0 @@
-package bp2build
-
-import (
- "android/soong/bazel"
- "fmt"
-)
-
-// Data from the code generation process that is used to improve compatibility
-// between build systems.
-type CodegenCompatLayer struct {
- // A map from the original module name to the generated/handcrafted Bazel
- // label for legacy build systems to be able to build a fully-qualified
- // Bazel target from an unique module name.
- NameToLabelMap map[string]string
-}
-
-// Log an entry of module name -> Bazel target label.
-func (compatLayer CodegenCompatLayer) AddNameToLabelEntry(name, label string) {
- // The module name may be prefixed with bazel.BazelTargetModuleNamePrefix if
- // generated from bp2build.
- name = bazel.StripNamePrefix(name)
- if existingLabel, ok := compatLayer.NameToLabelMap[name]; ok {
- panic(fmt.Errorf(
- "Module '%s' maps to more than one Bazel target label: %s, %s. "+
- "This shouldn't happen. It probably indicates a bug with the bp2build internals.",
- name,
- existingLabel,
- label))
- }
- compatLayer.NameToLabelMap[name] = label
-}
diff --git a/bp2build/conversion.go b/bp2build/conversion.go
index 75bc2b4..354abf6 100644
--- a/bp2build/conversion.go
+++ b/bp2build/conversion.go
@@ -16,29 +16,19 @@
Contents string
}
-func CreateSoongInjectionFiles(compatLayer CodegenCompatLayer) []BazelFile {
+func CreateSoongInjectionFiles(metrics CodegenMetrics) []BazelFile {
var files []BazelFile
files = append(files, newFile("cc_toolchain", GeneratedBuildFileName, "")) // Creates a //cc_toolchain package.
files = append(files, newFile("cc_toolchain", "constants.bzl", config.BazelCcToolchainVars()))
- files = append(files, newFile("module_name_to_label", GeneratedBuildFileName, nameToLabelAliases(compatLayer.NameToLabelMap)))
+ files = append(files, newFile("metrics", "converted_modules.txt", strings.Join(metrics.convertedModules, "\n")))
return files
}
-func nameToLabelAliases(nameToLabelMap map[string]string) string {
- ret := make([]string, len(nameToLabelMap))
-
- for k, v := range nameToLabelMap {
- // v is the fully qualified label rooted at '//'
- ret = append(ret, fmt.Sprintf(
- `alias(
- name = "%s",
- actual = "@%s",
-)`, k, v))
- }
- return strings.Join(ret, "\n\n")
+func convertedModules(convertedModules []string) string {
+ return strings.Join(convertedModules, "\n")
}
func CreateBazelFiles(
diff --git a/bp2build/conversion_test.go b/bp2build/conversion_test.go
index 56ea589..dfa1a9e 100644
--- a/bp2build/conversion_test.go
+++ b/bp2build/conversion_test.go
@@ -80,7 +80,7 @@
}
func TestCreateBazelFiles_Bp2Build_CreatesDefaultFiles(t *testing.T) {
- files := CreateSoongInjectionFiles(CodegenCompatLayer{})
+ files := CreateSoongInjectionFiles(CodegenMetrics{})
expectedFilePaths := []bazelFilepath{
{
@@ -92,8 +92,8 @@
basename: "constants.bzl",
},
{
- dir: "module_name_to_label",
- basename: GeneratedBuildFileName,
+ dir: "metrics",
+ basename: "converted_modules.txt",
},
}
@@ -107,9 +107,5 @@
if actualFile.Dir != expectedFile.dir || actualFile.Basename != expectedFile.basename {
t.Errorf("Did not find expected file %s/%s", actualFile.Dir, actualFile.Basename)
}
-
- if expectedFile.basename != GeneratedBuildFileName && actualFile.Contents == "" {
- t.Errorf("Contents of %s unexpected empty.", actualFile)
- }
}
}
diff --git a/bp2build/metrics.go b/bp2build/metrics.go
index 645ef2d..55b928b 100644
--- a/bp2build/metrics.go
+++ b/bp2build/metrics.go
@@ -19,6 +19,8 @@
handCraftedTargetCount int
moduleWithUnconvertedDepsMsgs []string
+
+ convertedModules []string
}
// Print the codegen metrics to stdout.
@@ -37,3 +39,9 @@
len(metrics.moduleWithUnconvertedDepsMsgs),
strings.Join(metrics.moduleWithUnconvertedDepsMsgs, "\n\t"))
}
+
+func (metrics CodegenMetrics) AddConvertedModule(moduleName string) {
+ // Undo prebuilt_ module name prefix modifications
+ moduleName = android.RemoveOptionalPrebuiltPrefix(moduleName)
+ metrics.convertedModules = append(metrics.convertedModules, moduleName)
+}
diff --git a/cc/library.go b/cc/library.go
index 8a663ff..703d57f 100644
--- a/cc/library.go
+++ b/cc/library.go
@@ -370,6 +370,7 @@
module, library := NewLibrary(android.HostAndDeviceSupported)
library.BuildOnlyShared()
module.sdkMemberTypes = []android.SdkMemberType{sharedLibrarySdkMemberType}
+ module.bazelHandler = &ccLibraryBazelHandler{module: module}
return module.Init()
}
@@ -603,7 +604,10 @@
handler.module.linker.(*libraryDecorator).unstrippedOutputFile = outputFilePath
- tocFile := getTocFile(ctx, label, ccInfo.OutputFiles)
+ var tocFile android.OptionalPath
+ if len(ccInfo.TocFile) > 0 {
+ tocFile = android.OptionalPathForPath(android.PathForBazelOut(ctx, ccInfo.TocFile))
+ }
handler.module.linker.(*libraryDecorator).tocFile = tocFile
ctx.SetProvider(SharedLibraryInfoProvider, SharedLibraryInfo{
@@ -616,25 +620,6 @@
return true
}
-// getTocFile looks for the .so.toc file in the target's output files, if any. The .so.toc file
-// contains the table of contents of all symbols of a shared object.
-func getTocFile(ctx android.ModuleContext, label string, outputFiles []string) android.OptionalPath {
- var tocFile string
- for _, file := range outputFiles {
- if strings.HasSuffix(file, ".so.toc") {
- if tocFile != "" {
- ctx.ModuleErrorf("The %s target cannot produce more than 1 .toc file.", label)
- }
- tocFile = file
- // Don't break to validate that there are no multiple .toc files per .so.
- }
- }
- if tocFile == "" {
- return android.OptionalPath{}
- }
- return android.OptionalPathForPath(android.PathForBazelOut(ctx, tocFile))
-}
-
func (handler *ccLibraryBazelHandler) GenerateBazelBuildActions(ctx android.ModuleContext, label string) bool {
bazelCtx := ctx.Config().BazelContext
ccInfo, ok, err := bazelCtx.GetCcInfo(label, ctx.Arch().ArchType)
diff --git a/cc/library_test.go b/cc/library_test.go
index 6b349b6..7ddfaa7 100644
--- a/cc/library_test.go
+++ b/cc/library_test.go
@@ -320,3 +320,48 @@
libfoo.Args["ldFlags"], "-Wl,--dynamic-list,foo.dynamic.txt")
}
+
+func TestCcLibrarySharedWithBazel(t *testing.T) {
+ bp := `
+cc_library_shared {
+ name: "foo",
+ srcs: ["foo.cc"],
+ bazel_module: { label: "//foo/bar:bar" },
+}`
+ config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
+ config.BazelContext = android.MockBazelContext{
+ OutputBaseDir: "outputbase",
+ LabelToCcInfo: map[string]cquery.CcInfo{
+ "//foo/bar:bar": cquery.CcInfo{
+ CcObjectFiles: []string{"foo.o"},
+ Includes: []string{"include"},
+ SystemIncludes: []string{"system_include"},
+ RootDynamicLibraries: []string{"foo.so"},
+ TocFile: "foo.so.toc",
+ },
+ },
+ }
+ ctx := testCcWithConfig(t, config)
+
+ sharedFoo := ctx.ModuleForTests("foo", "android_arm_armv7-a-neon_shared").Module()
+ producer := sharedFoo.(android.OutputFileProducer)
+ outputFiles, err := producer.OutputFiles("")
+ if err != nil {
+ t.Errorf("Unexpected error getting cc_object outputfiles %s", err)
+ }
+ expectedOutputFiles := []string{"outputbase/execroot/__main__/foo.so"}
+ android.AssertDeepEquals(t, "output files", expectedOutputFiles, outputFiles.Strings())
+
+ tocFilePath := sharedFoo.(*Module).Toc()
+ if !tocFilePath.Valid() {
+ t.Errorf("Invalid tocFilePath: %s", tocFilePath)
+ }
+ tocFile := tocFilePath.Path()
+ expectedToc := "outputbase/execroot/__main__/foo.so.toc"
+ android.AssertStringEquals(t, "toc file", expectedToc, tocFile.String())
+
+ entries := android.AndroidMkEntriesForTest(t, ctx, sharedFoo)[0]
+ expectedFlags := []string{"-Ioutputbase/execroot/__main__/include", "-isystem outputbase/execroot/__main__/system_include"}
+ gotFlags := entries.EntryMap["LOCAL_EXPORT_CFLAGS"]
+ android.AssertDeepEquals(t, "androidmk exported cflags", expectedFlags, gotFlags)
+}
diff --git a/java/base.go b/java/base.go
index 86022c3..da9293c 100644
--- a/java/base.go
+++ b/java/base.go
@@ -382,7 +382,7 @@
return nil
}
if sdkVersion.Kind == android.SdkCorePlatform {
- if useLegacyCorePlatformApiByName(j.BaseModuleName()) {
+ if useLegacyCorePlatformApi(ctx, j.BaseModuleName()) {
return fmt.Errorf("non stable SDK %v - uses legacy core platform", sdkVersion)
} else {
// Treat stable core platform as stable.
diff --git a/java/legacy_core_platform_api_usage.go b/java/legacy_core_platform_api_usage.go
index 8c401a7..7749310 100644
--- a/java/legacy_core_platform_api_usage.go
+++ b/java/legacy_core_platform_api_usage.go
@@ -163,17 +163,27 @@
}
}
-func useLegacyCorePlatformApi(ctx android.EarlyModuleContext) bool {
- return useLegacyCorePlatformApiByName(ctx.ModuleName())
+var legacyCorePlatformApiLookupKey = android.NewOnceKey("legacyCorePlatformApiLookup")
+
+func getLegacyCorePlatformApiLookup(config android.Config) map[string]struct{} {
+ return config.Once(legacyCorePlatformApiLookupKey, func() interface{} {
+ return legacyCorePlatformApiLookup
+ }).(map[string]struct{})
}
-func useLegacyCorePlatformApiByName(name string) bool {
- _, found := legacyCorePlatformApiLookup[name]
+// useLegacyCorePlatformApi checks to see whether the supplied module name is in the list of modules
+// that are able to use the legacy core platform API and returns true if it does, false otherwise.
+//
+// This method takes the module name separately from the context as this may be being called for a
+// module that is not the target of the supplied context.
+func useLegacyCorePlatformApi(ctx android.EarlyModuleContext, moduleName string) bool {
+ lookup := getLegacyCorePlatformApiLookup(ctx.Config())
+ _, found := lookup[moduleName]
return found
}
func corePlatformSystemModules(ctx android.EarlyModuleContext) string {
- if useLegacyCorePlatformApi(ctx) {
+ if useLegacyCorePlatformApi(ctx, ctx.ModuleName()) {
return config.LegacyCorePlatformSystemModules
} else {
return config.StableCorePlatformSystemModules
@@ -181,7 +191,7 @@
}
func corePlatformBootclasspathLibraries(ctx android.EarlyModuleContext) []string {
- if useLegacyCorePlatformApi(ctx) {
+ if useLegacyCorePlatformApi(ctx, ctx.ModuleName()) {
return config.LegacyCorePlatformBootclasspathLibraries
} else {
return config.StableCorePlatformBootclasspathLibraries
diff --git a/java/testing.go b/java/testing.go
index d8a77cf..a642753 100644
--- a/java/testing.go
+++ b/java/testing.go
@@ -229,6 +229,26 @@
)
}
+// FixtureUseLegacyCorePlatformApi prepares the fixture by setting the exception list of those
+// modules that are allowed to use the legacy core platform API to be the ones supplied.
+func FixtureUseLegacyCorePlatformApi(moduleNames ...string) android.FixturePreparer {
+ lookup := make(map[string]struct{})
+ for _, moduleName := range moduleNames {
+ lookup[moduleName] = struct{}{}
+ }
+ return android.FixtureModifyConfig(func(config android.Config) {
+ // Try and set the legacyCorePlatformApiLookup in the config, the returned value will be the
+ // actual value that is set.
+ cached := config.Once(legacyCorePlatformApiLookupKey, func() interface{} {
+ return lookup
+ })
+ // Make sure that the cached value is the one we need.
+ if !reflect.DeepEqual(cached, lookup) {
+ panic(fmt.Errorf("attempting to set legacyCorePlatformApiLookupKey to %q but it has already been set to %q", lookup, cached))
+ }
+ })
+}
+
// registerRequiredBuildComponentsForTest registers the build components used by
// PrepareForTestWithJavaDefaultModules.
//
diff --git a/mk2rbc/Android.bp b/mk2rbc/Android.bp
index 4fa3eb6..b18bfc7 100644
--- a/mk2rbc/Android.bp
+++ b/mk2rbc/Android.bp
@@ -38,6 +38,7 @@
"soong_variables.go",
"types.go",
"variable.go",
+ "version_defaults.go",
],
deps: ["androidmk-parser"],
}
diff --git a/mk2rbc/cmd/mk2rbc.go b/mk2rbc/cmd/mk2rbc.go
index 209e82b..7b5f298 100644
--- a/mk2rbc/cmd/mk2rbc.go
+++ b/mk2rbc/cmd/mk2rbc.go
@@ -81,6 +81,7 @@
var tracedVariables []string
var errorLogger = errorsByType{data: make(map[string]datum)}
var makefileFinder = &LinuxMakefileFinder{}
+var versionDefaultsMk = filepath.Join("build", "make", "core", "version_defaults.mk")
func main() {
flag.Usage = func() {
@@ -165,13 +166,24 @@
quit(fmt.Errorf("cannot generate configuration launcher for %s, it is not a known product",
product))
}
+ versionDefaults, err := generateVersionDefaults()
+ if err != nil {
+ quit(err)
+ }
ok = convertOne(path) && ok
- err := writeGenerated(*launcher, mk2rbc.Launcher(outputFilePath(path), mk2rbc.MakePath2ModuleName(path)))
+ versionDefaultsPath := outputFilePath(versionDefaultsMk)
+ err = writeGenerated(versionDefaultsPath, versionDefaults)
if err != nil {
fmt.Fprintf(os.Stderr, "%s:%s", path, err)
ok = false
}
+ err = writeGenerated(*launcher, mk2rbc.Launcher(outputFilePath(path), versionDefaultsPath,
+ mk2rbc.MakePath2ModuleName(path)))
+ if err != nil {
+ fmt.Fprintf(os.Stderr, "%s:%s", path, err)
+ ok = false
+ }
} else {
files := flag.Args()
if *allInSource {
@@ -194,6 +206,15 @@
}
}
+func generateVersionDefaults() (string, error) {
+ versionSettings, err := mk2rbc.ParseVersionDefaults(filepath.Join(*rootDir, versionDefaultsMk))
+ if err != nil {
+ return "", err
+ }
+ return mk2rbc.VersionDefaults(versionSettings), nil
+
+}
+
func quit(s interface{}) {
fmt.Fprintln(os.Stderr, s)
os.Exit(2)
diff --git a/mk2rbc/mk2rbc.go b/mk2rbc/mk2rbc.go
index b05d340..b9b7e2c 100644
--- a/mk2rbc/mk2rbc.go
+++ b/mk2rbc/mk2rbc.go
@@ -1618,12 +1618,12 @@
return starScript, nil
}
-func Launcher(path, name string) string {
+func Launcher(mainModuleUri, versionDefaultsUri, mainModuleName string) string {
var buf bytes.Buffer
fmt.Fprintf(&buf, "load(%q, %q)\n", baseUri, baseName)
- fmt.Fprintf(&buf, "load(%q, \"init\")\n", path)
- fmt.Fprintf(&buf, "g, config = %s(%q, init)\n", cfnMain, name)
- fmt.Fprintf(&buf, "%s(g, config)\n", cfnPrintVars)
+ fmt.Fprintf(&buf, "load(%q, \"version_defaults\")\n", versionDefaultsUri)
+ fmt.Fprintf(&buf, "load(%q, \"init\")\n", mainModuleUri)
+ fmt.Fprintf(&buf, "%s(%s(%q, init, version_defaults))\n", cfnPrintVars, cfnMain, mainModuleName)
return buf.String()
}
diff --git a/mk2rbc/test/version_defaults.mk.test b/mk2rbc/test/version_defaults.mk.test
new file mode 100644
index 0000000..1666392
--- /dev/null
+++ b/mk2rbc/test/version_defaults.mk.test
@@ -0,0 +1,22 @@
+INTERNAL_BUILD_ID_MAKEFILE := $(wildcard $(BUILD_SYSTEM)/build_id.mk)
+ifdef INTERNAL_BUILD_ID_MAKEFILE
+ include $(INTERNAL_BUILD_ID_MAKEFILE)
+endif
+
+DEFAULT_PLATFORM_VERSION := TP1A
+.KATI_READONLY := DEFAULT_PLATFORM_VERSION
+MIN_PLATFORM_VERSION := TP1A
+MAX_PLATFORM_VERSION := TP1A
+PLATFORM_VERSION_LAST_STABLE := 12
+PLATFORM_VERSION_CODENAME.SP2A := Sv2
+PLATFORM_VERSION_CODENAME.TP1A := Tiramisu
+ifndef PLATFORM_SDK_VERSION
+ PLATFORM_SDK_VERSION := 31
+endif
+.KATI_READONLY := PLATFORM_SDK_VERSION
+PLATFORM_SDK_EXTENSION_VERSION := 1
+PLATFORM_BASE_SDK_EXTENSION_VERSION := 0
+ifndef PLATFORM_SECURITY_PATCH
+ PLATFORM_SECURITY_PATCH := 2021-10-05
+endif
+include $(BUILD_SYSTEM)/version_util.mk
diff --git a/mk2rbc/version_defaults.go b/mk2rbc/version_defaults.go
new file mode 100644
index 0000000..27e8198
--- /dev/null
+++ b/mk2rbc/version_defaults.go
@@ -0,0 +1,109 @@
+// Copyright 2021 Google LLC
+//
+// 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 mk2rbc
+
+import (
+ mkparser "android/soong/androidmk/parser"
+ "bytes"
+ "fmt"
+ "io/ioutil"
+ "os"
+ "sort"
+ "strconv"
+ "strings"
+)
+
+const codenamePrefix = "PLATFORM_VERSION_CODENAME."
+
+// ParseVersionDefaults extracts version settings from the given file
+// and returns the map.
+func ParseVersionDefaults(path string) (map[string]string, error) {
+ contents, err := ioutil.ReadFile(path)
+ if err != nil {
+ return nil, err
+ }
+ parser := mkparser.NewParser(path, bytes.NewBuffer(contents))
+ nodes, errs := parser.Parse()
+ if len(errs) > 0 {
+ for _, e := range errs {
+ fmt.Fprintln(os.Stderr, "ERROR:", e)
+ }
+ return nil, fmt.Errorf("cannot parse %s", path)
+ }
+
+ result := map[string]string{
+ "DEFAULT_PLATFORM_VERSION": "",
+ "MAX_PLATFORM_VERSION": "",
+ "MIN_PLATFORM_VERSION": "A",
+ "PLATFORM_BASE_SDK_EXTENSION_VERSION": "",
+ "PLATFORM_SDK_EXTENSION_VERSION": "",
+ "PLATFORM_SDK_VERSION": "",
+ "PLATFORM_SECURITY_PATCH": "",
+ "PLATFORM_VERSION_LAST_STABLE": "",
+ }
+ for _, node := range nodes {
+ asgn, ok := node.(*mkparser.Assignment)
+ if !(ok && asgn.Name.Const()) {
+ continue
+ }
+ s := asgn.Name.Strings[0]
+ _, ok = result[s]
+ if !ok {
+ ok = strings.HasPrefix(s, codenamePrefix)
+ }
+ if !ok {
+ continue
+ }
+ v := asgn.Value
+ if !v.Const() {
+ return nil, fmt.Errorf("the value of %s should be constant", s)
+ }
+ result[s] = strings.TrimSpace(v.Strings[0])
+ }
+ return result, nil
+}
+
+func genericValue(s string) interface{} {
+ if ival, err := strconv.ParseInt(s, 0, 0); err == nil {
+ return ival
+ }
+ return s
+}
+
+// VersionDefaults generates the contents of the version_defaults.rbc file
+func VersionDefaults(values map[string]string) string {
+ var sink bytes.Buffer
+ var lines []string
+ var codenames []string
+ for name, value := range values {
+ if strings.HasPrefix(name, codenamePrefix) {
+ codenames = append(codenames,
+ fmt.Sprintf("%q: %q", strings.TrimPrefix(name, codenamePrefix), value))
+ } else {
+ // Print numbers as such
+ lines = append(lines, fmt.Sprintf(" %s = %#v,\n",
+ strings.ToLower(name), genericValue(value)))
+ }
+ }
+ sort.Strings(lines)
+ sink.WriteString("version_defaults = struct(\n")
+ for _, l := range lines {
+ sink.WriteString(l)
+ }
+ sink.WriteString(" codenames = { ")
+ sink.WriteString(strings.Join(codenames, ", "))
+ sink.WriteString(" }\n)\n")
+ return sink.String()
+}
diff --git a/mk2rbc/version_defaults_test.go b/mk2rbc/version_defaults_test.go
new file mode 100644
index 0000000..c78fa32
--- /dev/null
+++ b/mk2rbc/version_defaults_test.go
@@ -0,0 +1,60 @@
+package mk2rbc
+
+import (
+ "path/filepath"
+ "reflect"
+ "strings"
+ "testing"
+)
+
+func TestParseVersionDefaults(t *testing.T) {
+ testDir := getTestDirectory()
+ abspath := func(relPath string) string { return filepath.Join(testDir, relPath) }
+ actualProducts, err := ParseVersionDefaults(abspath("version_defaults.mk.test"))
+ if err != nil {
+ t.Fatal(err)
+ }
+ expectedProducts := map[string]string{
+ "DEFAULT_PLATFORM_VERSION": "TP1A",
+ "MAX_PLATFORM_VERSION": "TP1A",
+ "MIN_PLATFORM_VERSION": "TP1A",
+ "PLATFORM_BASE_SDK_EXTENSION_VERSION": "0",
+ "PLATFORM_SDK_EXTENSION_VERSION": "1",
+ "PLATFORM_SDK_VERSION": "31",
+ "PLATFORM_SECURITY_PATCH": "2021-10-05",
+ "PLATFORM_VERSION_LAST_STABLE": "12",
+ "PLATFORM_VERSION_CODENAME.SP2A": "Sv2",
+ "PLATFORM_VERSION_CODENAME.TP1A": "Tiramisu",
+ }
+ if !reflect.DeepEqual(actualProducts, expectedProducts) {
+ t.Errorf("\nExpected: %v\n Actual: %v", expectedProducts, actualProducts)
+ }
+}
+
+func TestVersionDefaults(t *testing.T) {
+ testDir := getTestDirectory()
+ abspath := func(relPath string) string { return filepath.Join(testDir, relPath) }
+ actualProducts, err := ParseVersionDefaults(abspath("version_defaults.mk.test"))
+ if err != nil {
+ t.Fatal(err)
+ }
+ expectedString := `version_defaults = struct(
+ default_platform_version = "TP1A",
+ max_platform_version = "TP1A",
+ min_platform_version = "TP1A",
+ platform_base_sdk_extension_version = 0,
+ platform_sdk_extension_version = 1,
+ platform_sdk_version = 31,
+ platform_security_patch = "2021-10-05",
+ platform_version_last_stable = 12,
+ codenames = { "SP2A": "Sv2", "TP1A": "Tiramisu" }
+)
+`
+ actualString := VersionDefaults(actualProducts)
+ if !reflect.DeepEqual(actualString, expectedString) {
+ t.Errorf("\nExpected: %v\nActual:\n%v",
+ strings.ReplaceAll(expectedString, "\n", "\n"),
+ strings.ReplaceAll(actualString, "\n", "\n"))
+ }
+
+}
diff --git a/rust/config/allowed_list.go b/rust/config/allowed_list.go
index 63a8f04..47ca3a7 100644
--- a/rust/config/allowed_list.go
+++ b/rust/config/allowed_list.go
@@ -28,6 +28,7 @@
"system/librustutils",
"system/logging/liblog",
"system/logging/rust",
+ "system/nfc",
"system/security",
"system/tools/aidl",
"tools/security/fuzzing/example_rust_fuzzer",