2021-08-23 19:05:17 +02:00
|
|
|
// Copyright 2021 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.
|
|
|
|
|
2020-11-26 01:06:39 +01:00
|
|
|
package bp2build
|
|
|
|
|
2021-08-23 19:04:20 +02:00
|
|
|
/*
|
|
|
|
For shareable/common bp2build testing functionality and dumping ground for
|
|
|
|
specific-but-shared functionality among tests in package
|
|
|
|
*/
|
|
|
|
|
2020-11-26 01:06:39 +01:00
|
|
|
import (
|
2021-09-22 21:52:58 +02:00
|
|
|
"fmt"
|
2023-09-14 01:33:20 +02:00
|
|
|
"path/filepath"
|
2023-09-19 03:12:48 +02:00
|
|
|
"regexp"
|
2023-03-08 01:48:19 +01:00
|
|
|
"sort"
|
2021-08-23 18:10:00 +02:00
|
|
|
"strings"
|
2021-05-19 15:14:26 +02:00
|
|
|
"testing"
|
|
|
|
|
2023-09-25 21:01:40 +02:00
|
|
|
"android/soong/ui/metrics/bp2build_metrics_proto"
|
2023-10-13 20:32:14 +02:00
|
|
|
|
2022-09-28 22:43:08 +02:00
|
|
|
"github.com/google/blueprint/proptools"
|
|
|
|
|
2020-11-26 01:06:39 +01:00
|
|
|
"android/soong/android"
|
2022-03-28 21:53:03 +02:00
|
|
|
"android/soong/android/allowlists"
|
2020-12-14 14:25:34 +01:00
|
|
|
"android/soong/bazel"
|
2020-11-26 01:06:39 +01:00
|
|
|
)
|
|
|
|
|
Add os/target configurable selects for label list attributes.
This CL is pretty large, so I recommend starting with reading the newly
added tests for the expected behavior.
This change works in conjunction with the linked CLs in the Gerrit topic.
Those CLs add support for new platform() definitions for OS targets
specified in Soong's arch.go, which are configurable through
Android.bp's `target {}` property. It works similary to previous CLs
adding support for the `arch {}` property.
These configurable props are keyed by the OS: android, linux_bionic,
windows, and so on. They map to `select` statements in label list
attributes, which this CL enables for cc_library_headers' header_libs
and export_header_lib_headers props.
This enables //bionic/libc:libc_headers to be generated correctly, from:
cc_library_headers {
name: "libc_headers",
target: {
android: {
header_libs: ["libc_headers_arch"],
export_header_lib_headers: ["libc_headers_arch"],
},
linux_bionic: {
header_libs: ["libc_headers_arch"],
export_header_lib_headers: ["libc_headers_arch"],
},
},
// omitted props
}
to:
cc_library_headers(
name = "libc_headers",
deps = [] + select({
"//build/bazel/platforms/os:android": [
":libc_headers_arch",
],
"//build/bazel/platforms/os:linux_bionic": [
":libc_headers_arch",
],
"//conditions:default": [],
}),
)
Test: TH
Test: Verify generated //bionic/libc:libc_headers
Fixes: 183597786
Change-Id: I01016cc2cc9a71449f02300d747f01decebf3f6e
2021-03-24 07:18:33 +01:00
|
|
|
var (
|
2021-05-19 15:14:26 +02:00
|
|
|
buildDir string
|
Add os/target configurable selects for label list attributes.
This CL is pretty large, so I recommend starting with reading the newly
added tests for the expected behavior.
This change works in conjunction with the linked CLs in the Gerrit topic.
Those CLs add support for new platform() definitions for OS targets
specified in Soong's arch.go, which are configurable through
Android.bp's `target {}` property. It works similary to previous CLs
adding support for the `arch {}` property.
These configurable props are keyed by the OS: android, linux_bionic,
windows, and so on. They map to `select` statements in label list
attributes, which this CL enables for cc_library_headers' header_libs
and export_header_lib_headers props.
This enables //bionic/libc:libc_headers to be generated correctly, from:
cc_library_headers {
name: "libc_headers",
target: {
android: {
header_libs: ["libc_headers_arch"],
export_header_lib_headers: ["libc_headers_arch"],
},
linux_bionic: {
header_libs: ["libc_headers_arch"],
export_header_lib_headers: ["libc_headers_arch"],
},
},
// omitted props
}
to:
cc_library_headers(
name = "libc_headers",
deps = [] + select({
"//build/bazel/platforms/os:android": [
":libc_headers_arch",
],
"//build/bazel/platforms/os:linux_bionic": [
":libc_headers_arch",
],
"//conditions:default": [],
}),
)
Test: TH
Test: Verify generated //bionic/libc:libc_headers
Fixes: 183597786
Change-Id: I01016cc2cc9a71449f02300d747f01decebf3f6e
2021-03-24 07:18:33 +01:00
|
|
|
)
|
|
|
|
|
2023-09-19 03:12:48 +02:00
|
|
|
var labelRegex = regexp.MustCompile(`^//([^: ]+):([^ ]+)$`)
|
|
|
|
var simpleModuleNameRegex = regexp.MustCompile(`^[^: /]+$`)
|
|
|
|
|
2021-09-02 13:44:42 +02:00
|
|
|
func checkError(t *testing.T, errs []error, expectedErr error) bool {
|
2021-08-23 18:10:00 +02:00
|
|
|
t.Helper()
|
2021-09-02 13:44:42 +02:00
|
|
|
|
|
|
|
if len(errs) != 1 {
|
2021-08-26 14:37:59 +02:00
|
|
|
return false
|
2021-09-02 13:44:42 +02:00
|
|
|
}
|
2021-12-14 18:21:22 +01:00
|
|
|
if strings.Contains(errs[0].Error(), expectedErr.Error()) {
|
2021-09-02 13:44:42 +02:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2022-06-21 21:28:33 +02:00
|
|
|
func errored(t *testing.T, tc Bp2buildTestCase, errs []error) bool {
|
2021-09-02 13:44:42 +02:00
|
|
|
t.Helper()
|
2022-06-21 21:28:33 +02:00
|
|
|
if tc.ExpectedErr != nil {
|
2021-09-02 13:44:42 +02:00
|
|
|
// Rely on checkErrors, as this test case is expected to have an error.
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2021-08-23 18:10:00 +02:00
|
|
|
if len(errs) > 0 {
|
|
|
|
for _, err := range errs {
|
2022-06-21 21:28:33 +02:00
|
|
|
t.Errorf("%s: %s", tc.Description, err)
|
2021-08-23 18:10:00 +02:00
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
2021-09-02 13:44:42 +02:00
|
|
|
|
|
|
|
// All good, continue execution.
|
2021-08-23 18:10:00 +02:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2022-09-01 20:57:01 +02:00
|
|
|
func RunBp2BuildTestCaseSimple(t *testing.T, tc Bp2buildTestCase) {
|
2021-08-23 18:10:00 +02:00
|
|
|
t.Helper()
|
2022-06-21 21:28:33 +02:00
|
|
|
RunBp2BuildTestCase(t, func(ctx android.RegistrationContext) {}, tc)
|
2021-08-23 18:10:00 +02:00
|
|
|
}
|
|
|
|
|
2022-06-21 21:28:33 +02:00
|
|
|
type Bp2buildTestCase struct {
|
|
|
|
Description string
|
|
|
|
ModuleTypeUnderTest string
|
|
|
|
ModuleTypeUnderTestFactory android.ModuleFactory
|
2023-06-12 21:44:03 +02:00
|
|
|
// Text to add to the toplevel, root Android.bp file. If Dir is not set, all
|
|
|
|
// ExpectedBazelTargets are assumed to be generated by this file.
|
2023-06-08 16:28:51 +02:00
|
|
|
Blueprint string
|
2023-06-12 21:44:03 +02:00
|
|
|
// ExpectedBazelTargets compares the BazelTargets generated in `Dir` (if not empty).
|
|
|
|
// Otherwise, it checks the BazelTargets generated by `Blueprint` in the root directory.
|
|
|
|
ExpectedBazelTargets []string
|
2023-09-25 21:01:40 +02:00
|
|
|
// ExpectedConvertedModules asserts that modules in this list are labeled as "converted
|
|
|
|
// by bp2build" in the metrics reported by bp2build.
|
|
|
|
ExpectedConvertedModules []string
|
|
|
|
// ExpectedHandcraftedModules asserts that modules in this list are labeled as "handcrafted
|
|
|
|
// in build files" in the metrics reported by bp2build. Such modules are either explicitly
|
|
|
|
// defined in a BUILD file (by name), or registered as "otherwise implicitly handled"
|
|
|
|
// by bp2build (for example, by macros owned by other modules).
|
|
|
|
ExpectedHandcraftedModules []string
|
|
|
|
|
2023-09-14 01:33:20 +02:00
|
|
|
// AlreadyExistingBuildContents, if non-empty, simulates an already-present source BUILD file
|
|
|
|
// in the directory under test. The BUILD file has the given contents. This BUILD file
|
|
|
|
// will also be treated as "BUILD file to keep" by the simulated bp2build environment.
|
|
|
|
AlreadyExistingBuildContents string
|
2023-09-19 03:12:48 +02:00
|
|
|
|
|
|
|
// StubbedBuildDefinitions, if non-empty, adds stub definitions to already-present source
|
|
|
|
// BUILD files for each bazel label given. The BUILD files with these stub definitions
|
|
|
|
// are added to the BUILD file given in AlreadyExistingBuildContents.
|
|
|
|
// Labels may be of the form //pkg/to:target_name (which would be defined in pkg/to/BUILD.bazel)
|
|
|
|
// or `target_name` (which would be defined in ./BUILD.bazel).
|
2023-09-14 01:33:20 +02:00
|
|
|
StubbedBuildDefinitions []string
|
|
|
|
|
|
|
|
Filesystem map[string]string
|
2023-06-12 21:44:03 +02:00
|
|
|
// Dir sets the directory which will be compared against the targets in ExpectedBazelTargets.
|
|
|
|
// This should used in conjunction with the Filesystem property to check for targets
|
|
|
|
// generated from a directory that is not the root.
|
|
|
|
// If not set, all ExpectedBazelTargets are assumed to be generated by the text in the
|
|
|
|
// Blueprint property.
|
|
|
|
Dir string
|
2022-04-07 22:36:39 +02:00
|
|
|
// An error with a string contained within the string of the expected error
|
2022-06-21 21:28:33 +02:00
|
|
|
ExpectedErr error
|
|
|
|
UnconvertedDepsMode unconvertedDepsMode
|
2022-09-21 12:27:42 +02:00
|
|
|
|
|
|
|
// For every directory listed here, the BUILD file for that directory will
|
|
|
|
// be merged with the generated BUILD file. This allows custom BUILD targets
|
|
|
|
// to be used in tests, or use BUILD files to draw package boundaries.
|
|
|
|
KeepBuildFileForDirs []string
|
2023-10-13 20:32:14 +02:00
|
|
|
|
|
|
|
// An extra FixturePreparer to use when running the test. If you need multiple extra
|
|
|
|
// FixturePreparers, use android.GroupFixturePreparers()
|
|
|
|
ExtraFixturePreparer android.FixturePreparer
|
|
|
|
|
|
|
|
// If bp2build_product_config.go should run as part of the test.
|
|
|
|
RunBp2buildProductConfig bool
|
2021-08-23 18:10:00 +02:00
|
|
|
}
|
|
|
|
|
2023-10-13 20:32:14 +02:00
|
|
|
func RunBp2BuildTestCase(t *testing.T, registerModuleTypes func(ctx android.RegistrationContext), tc Bp2buildTestCase) {
|
2023-09-25 21:01:40 +02:00
|
|
|
t.Helper()
|
2023-09-28 22:41:03 +02:00
|
|
|
preparers := []android.FixturePreparer{
|
2023-09-25 21:01:40 +02:00
|
|
|
android.FixtureRegisterWithContext(registerModuleTypes),
|
2023-09-28 22:41:03 +02:00
|
|
|
}
|
2023-10-13 20:32:14 +02:00
|
|
|
if tc.ExtraFixturePreparer != nil {
|
|
|
|
preparers = append(preparers, tc.ExtraFixturePreparer)
|
2023-09-28 22:41:03 +02:00
|
|
|
}
|
2023-10-13 20:32:14 +02:00
|
|
|
preparers = append(preparers, android.FixtureSetTestRunner(&bazelTestRunner{generateProductConfigTargets: tc.RunBp2buildProductConfig}))
|
2023-09-28 22:41:03 +02:00
|
|
|
bp2buildSetup := android.GroupFixturePreparers(
|
|
|
|
preparers...,
|
2023-09-25 21:01:40 +02:00
|
|
|
)
|
|
|
|
runBp2BuildTestCaseWithSetup(t, bp2buildSetup, tc)
|
|
|
|
}
|
|
|
|
|
2022-10-29 18:48:00 +02:00
|
|
|
func runBp2BuildTestCaseWithSetup(t *testing.T, extraPreparer android.FixturePreparer, tc Bp2buildTestCase) {
|
2021-08-23 18:10:00 +02:00
|
|
|
t.Helper()
|
2023-09-19 03:12:48 +02:00
|
|
|
if tc.Filesystem == nil {
|
|
|
|
tc.Filesystem = map[string]string{}
|
|
|
|
}
|
2023-09-14 01:33:20 +02:00
|
|
|
checkDir := "."
|
|
|
|
if tc.Dir != "" {
|
|
|
|
checkDir = tc.Dir
|
|
|
|
}
|
|
|
|
keepExistingBuildDirs := tc.KeepBuildFileForDirs
|
|
|
|
buildFilesToParse := []string{}
|
2023-09-19 03:12:48 +02:00
|
|
|
|
2023-09-14 01:33:20 +02:00
|
|
|
if len(tc.StubbedBuildDefinitions) > 0 {
|
2023-09-19 03:12:48 +02:00
|
|
|
for _, buildDef := range tc.StubbedBuildDefinitions {
|
|
|
|
globalLabelMatch := labelRegex.FindStringSubmatch(buildDef)
|
|
|
|
var dir, targetName string
|
|
|
|
if len(globalLabelMatch) > 0 {
|
|
|
|
dir = globalLabelMatch[1]
|
|
|
|
targetName = globalLabelMatch[2]
|
|
|
|
} else {
|
|
|
|
if !simpleModuleNameRegex.MatchString(buildDef) {
|
|
|
|
t.Errorf("Stubbed build definition '%s' must be either a simple module name or of global target syntax (//foo/bar:baz).", buildDef)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
dir = "."
|
|
|
|
targetName = buildDef
|
|
|
|
}
|
|
|
|
buildFilePath := filepath.Join(dir, "BUILD")
|
|
|
|
tc.Filesystem[buildFilePath] +=
|
|
|
|
MakeBazelTarget(
|
|
|
|
"bp2build_test_stub",
|
|
|
|
targetName,
|
|
|
|
AttrNameToString{})
|
|
|
|
keepExistingBuildDirs = append(keepExistingBuildDirs, dir)
|
|
|
|
buildFilesToParse = append(buildFilesToParse, buildFilePath)
|
2023-09-14 01:33:20 +02:00
|
|
|
}
|
|
|
|
}
|
2023-09-19 03:12:48 +02:00
|
|
|
if len(tc.AlreadyExistingBuildContents) > 0 {
|
2023-09-14 01:33:20 +02:00
|
|
|
buildFilePath := filepath.Join(checkDir, "BUILD")
|
2023-09-19 03:12:48 +02:00
|
|
|
tc.Filesystem[buildFilePath] += tc.AlreadyExistingBuildContents
|
2023-09-14 01:33:20 +02:00
|
|
|
keepExistingBuildDirs = append(keepExistingBuildDirs, checkDir)
|
|
|
|
buildFilesToParse = append(buildFilesToParse, buildFilePath)
|
|
|
|
}
|
2023-09-19 03:12:48 +02:00
|
|
|
filesystem := make(map[string][]byte)
|
|
|
|
for f, content := range tc.Filesystem {
|
|
|
|
filesystem[f] = []byte(content)
|
|
|
|
}
|
2022-10-29 18:48:00 +02:00
|
|
|
preparers := []android.FixturePreparer{
|
|
|
|
extraPreparer,
|
|
|
|
android.FixtureMergeMockFs(filesystem),
|
|
|
|
android.FixtureWithRootAndroidBp(tc.Blueprint),
|
|
|
|
android.FixtureRegisterWithContext(func(ctx android.RegistrationContext) {
|
|
|
|
ctx.RegisterModuleType(tc.ModuleTypeUnderTest, tc.ModuleTypeUnderTestFactory)
|
|
|
|
}),
|
2023-09-14 01:33:20 +02:00
|
|
|
android.FixtureModifyContextWithMockFs(func(ctx *android.TestContext) {
|
2022-10-29 18:48:00 +02:00
|
|
|
// A default configuration for tests to not have to specify bp2build_available on top level
|
|
|
|
// targets.
|
|
|
|
bp2buildConfig := android.NewBp2BuildAllowlist().SetDefaultConfig(
|
|
|
|
allowlists.Bp2BuildConfig{
|
|
|
|
android.Bp2BuildTopLevel: allowlists.Bp2BuildDefaultTrueRecursively,
|
|
|
|
},
|
|
|
|
)
|
2023-09-14 01:33:20 +02:00
|
|
|
for _, f := range keepExistingBuildDirs {
|
2022-10-29 18:48:00 +02:00
|
|
|
bp2buildConfig.SetKeepExistingBuildFile(map[string]bool{
|
|
|
|
f: /*recursive=*/ false,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
ctx.RegisterBp2BuildConfig(bp2buildConfig)
|
2023-06-08 16:28:51 +02:00
|
|
|
// This setting is added to bp2build invocations. It prevents bp2build
|
|
|
|
// from cloning modules to their original state after mutators run. This
|
|
|
|
// would lose some data intentionally set by these mutators.
|
|
|
|
ctx.SkipCloneModulesAfterMutators = true
|
2023-09-14 01:33:20 +02:00
|
|
|
err := ctx.RegisterExistingBazelTargets(".", buildFilesToParse)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("error parsing build files in test setup: %s", err)
|
|
|
|
}
|
2022-10-29 18:48:00 +02:00
|
|
|
}),
|
|
|
|
android.FixtureModifyEnv(func(env map[string]string) {
|
|
|
|
if tc.UnconvertedDepsMode == errorModulesUnconvertedDeps {
|
|
|
|
env["BP2BUILD_ERROR_UNCONVERTED"] = "true"
|
|
|
|
}
|
|
|
|
}),
|
2022-09-21 12:27:42 +02:00
|
|
|
}
|
2021-08-23 18:10:00 +02:00
|
|
|
|
2022-10-29 18:48:00 +02:00
|
|
|
preparer := android.GroupFixturePreparers(preparers...)
|
|
|
|
if tc.ExpectedErr != nil {
|
|
|
|
pattern := "\\Q" + tc.ExpectedErr.Error() + "\\E"
|
|
|
|
preparer = preparer.ExtendWithErrorHandler(android.FixtureExpectsOneErrorPattern(pattern))
|
2021-09-02 13:44:42 +02:00
|
|
|
}
|
2022-10-29 18:48:00 +02:00
|
|
|
result := preparer.RunTestWithCustomResult(t).(*BazelTestResult)
|
|
|
|
if len(result.Errs) > 0 {
|
2021-08-23 18:10:00 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-10-29 18:48:00 +02:00
|
|
|
expectedTargets := map[string][]string{
|
|
|
|
checkDir: tc.ExpectedBazelTargets,
|
|
|
|
}
|
|
|
|
|
2023-09-25 21:01:40 +02:00
|
|
|
result.CompareAllBazelTargets(t, tc, expectedTargets, true)
|
2022-10-29 18:48:00 +02:00
|
|
|
}
|
|
|
|
|
2023-06-20 23:46:57 +02:00
|
|
|
// bazelTestRunner customizes the test fixture mechanism to run tests of the bp2build build mode.
|
2023-10-13 20:32:14 +02:00
|
|
|
type bazelTestRunner struct {
|
|
|
|
generateProductConfigTargets bool
|
|
|
|
}
|
2022-10-29 18:48:00 +02:00
|
|
|
|
|
|
|
func (b *bazelTestRunner) FinalPreparer(result *android.TestResult) android.CustomTestResult {
|
|
|
|
ctx := result.TestContext
|
2023-06-20 23:46:57 +02:00
|
|
|
ctx.RegisterForBazelConversion()
|
2022-10-29 18:48:00 +02:00
|
|
|
|
|
|
|
return &BazelTestResult{TestResult: result}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *bazelTestRunner) PostParseProcessor(result android.CustomTestResult) {
|
|
|
|
bazelResult := result.(*BazelTestResult)
|
|
|
|
ctx := bazelResult.TestContext
|
|
|
|
config := bazelResult.Config
|
|
|
|
_, errs := ctx.ResolveDependencies(config)
|
|
|
|
if bazelResult.CollateErrs(errs) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-06-20 23:46:57 +02:00
|
|
|
codegenCtx := NewCodegenContext(config, ctx.Context, Bp2Build, "")
|
2022-10-29 18:48:00 +02:00
|
|
|
res, errs := GenerateBazelTargets(codegenCtx, false)
|
|
|
|
if bazelResult.CollateErrs(errs) {
|
|
|
|
return
|
|
|
|
}
|
2023-10-13 20:32:14 +02:00
|
|
|
if b.generateProductConfigTargets {
|
|
|
|
productConfig, err := createProductConfigFiles(codegenCtx, res.moduleNameToPartition, res.metrics.convertedModulePathMap)
|
|
|
|
if err != nil {
|
|
|
|
bazelResult.CollateErrs([]error{err})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for k, v := range productConfig.bp2buildTargets {
|
|
|
|
res.buildFileToTargets[k] = append(res.buildFileToTargets[k], v...)
|
|
|
|
}
|
|
|
|
}
|
2022-10-29 18:48:00 +02:00
|
|
|
|
|
|
|
// Store additional data for access by tests.
|
|
|
|
bazelResult.conversionResults = res
|
|
|
|
}
|
|
|
|
|
|
|
|
// BazelTestResult is a wrapper around android.TestResult to provide type safe access to the bazel
|
|
|
|
// specific data stored by the bazelTestRunner.
|
|
|
|
type BazelTestResult struct {
|
|
|
|
*android.TestResult
|
|
|
|
|
|
|
|
// The result returned by the GenerateBazelTargets function.
|
|
|
|
conversionResults
|
|
|
|
}
|
|
|
|
|
|
|
|
// CompareAllBazelTargets compares the BazelTargets produced by the test for all the directories
|
|
|
|
// with the supplied set of expected targets.
|
|
|
|
//
|
|
|
|
// If ignoreUnexpected=false then this enforces an exact match where every BazelTarget produced must
|
|
|
|
// have a corresponding expected BazelTarget.
|
|
|
|
//
|
|
|
|
// If ignoreUnexpected=true then it will ignore directories for which there are no expected targets.
|
2023-09-25 21:01:40 +02:00
|
|
|
func (b BazelTestResult) CompareAllBazelTargets(t *testing.T, tc Bp2buildTestCase, expectedTargets map[string][]string, ignoreUnexpected bool) {
|
2023-03-23 16:51:49 +01:00
|
|
|
t.Helper()
|
2022-10-29 18:48:00 +02:00
|
|
|
actualTargets := b.buildFileToTargets
|
|
|
|
|
|
|
|
// Generate the sorted set of directories to check.
|
2023-03-01 01:02:16 +01:00
|
|
|
dirsToCheck := android.SortedKeys(expectedTargets)
|
2022-10-29 18:48:00 +02:00
|
|
|
if !ignoreUnexpected {
|
|
|
|
// This needs to perform an exact match so add the directories in which targets were
|
|
|
|
// produced to the list of directories to check.
|
2023-03-01 01:02:16 +01:00
|
|
|
dirsToCheck = append(dirsToCheck, android.SortedKeys(actualTargets)...)
|
2022-10-29 18:48:00 +02:00
|
|
|
dirsToCheck = android.SortedUniqueStrings(dirsToCheck)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, dir := range dirsToCheck {
|
|
|
|
expected := expectedTargets[dir]
|
|
|
|
actual := actualTargets[dir]
|
|
|
|
|
|
|
|
if expected == nil {
|
|
|
|
if actual != nil {
|
|
|
|
t.Errorf("did not expect any bazel modules in %q but found %d", dir, len(actual))
|
|
|
|
}
|
|
|
|
} else if actual == nil {
|
|
|
|
expectedCount := len(expected)
|
|
|
|
if expectedCount > 0 {
|
|
|
|
t.Errorf("expected %d bazel modules in %q but did not find any", expectedCount, dir)
|
|
|
|
}
|
2021-12-14 18:21:22 +01:00
|
|
|
} else {
|
2023-09-25 21:01:40 +02:00
|
|
|
b.CompareBazelTargets(t, tc.Description, expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, module := range tc.ExpectedConvertedModules {
|
|
|
|
if _, found := b.metrics.convertedModulePathMap[module]; !found {
|
|
|
|
t.Errorf("expected %s to be generated by bp2build, but was not. Map of converted modules: %s", module, b.metrics.convertedModulePathMap)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, module := range tc.ExpectedHandcraftedModules {
|
|
|
|
if reason, found := b.metrics.serialized.UnconvertedModules[module]; !found {
|
|
|
|
t.Errorf("expected %s to be marked 'unconverted' by bp2build, but was not found. Full list: %s",
|
|
|
|
module, b.metrics.serialized.UnconvertedModules)
|
|
|
|
} else {
|
|
|
|
if reason.Type != bp2build_metrics_proto.UnconvertedReasonType_DEFINED_IN_BUILD_FILE {
|
|
|
|
t.Errorf("expected %s to be marked 'handcrafted' by bp2build, but was disabled for another reason: %s", module, reason)
|
|
|
|
}
|
2021-12-14 18:21:22 +01:00
|
|
|
}
|
2021-08-26 14:37:59 +02:00
|
|
|
}
|
2022-10-29 18:48:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (b BazelTestResult) CompareBazelTargets(t *testing.T, description string, expectedContents []string, actualTargets BazelTargets) {
|
2023-01-25 18:07:43 +01:00
|
|
|
t.Helper()
|
2022-10-29 18:48:00 +02:00
|
|
|
if actualCount, expectedCount := len(actualTargets), len(expectedContents); actualCount != expectedCount {
|
2022-08-04 22:28:38 +02:00
|
|
|
t.Errorf("%s: Expected %d bazel target (%s), got %d (%s)",
|
2022-10-29 18:48:00 +02:00
|
|
|
description, expectedCount, expectedContents, actualCount, actualTargets)
|
2021-08-23 18:10:00 +02:00
|
|
|
} else {
|
2023-03-08 01:48:19 +01:00
|
|
|
sort.SliceStable(actualTargets, func(i, j int) bool {
|
|
|
|
return actualTargets[i].name < actualTargets[j].name
|
|
|
|
})
|
|
|
|
sort.SliceStable(expectedContents, func(i, j int) bool {
|
|
|
|
return getTargetName(expectedContents[i]) < getTargetName(expectedContents[j])
|
|
|
|
})
|
2022-10-29 18:48:00 +02:00
|
|
|
for i, actualTarget := range actualTargets {
|
|
|
|
if w, g := expectedContents[i], actualTarget.content; w != g {
|
2021-08-23 18:10:00 +02:00
|
|
|
t.Errorf(
|
2022-10-29 18:48:00 +02:00
|
|
|
"%s[%d]: Expected generated Bazel target to be `%s`, got `%s`",
|
|
|
|
description, i, w, g)
|
2021-08-23 18:10:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-26 01:06:39 +01:00
|
|
|
type nestedProps struct {
|
2021-12-01 16:09:34 +01:00
|
|
|
Nested_prop *string
|
2020-11-26 01:06:39 +01:00
|
|
|
}
|
|
|
|
|
2021-09-14 17:17:21 +02:00
|
|
|
type EmbeddedProps struct {
|
2021-12-01 16:09:34 +01:00
|
|
|
Embedded_prop *string
|
2021-09-14 17:17:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type OtherEmbeddedProps struct {
|
2021-12-01 16:09:34 +01:00
|
|
|
Other_embedded_prop *string
|
2021-09-14 17:17:21 +02:00
|
|
|
}
|
|
|
|
|
2020-11-26 01:06:39 +01:00
|
|
|
type customProps struct {
|
2021-09-14 17:17:21 +02:00
|
|
|
EmbeddedProps
|
|
|
|
*OtherEmbeddedProps
|
|
|
|
|
2020-11-26 01:06:39 +01:00
|
|
|
Bool_prop bool
|
|
|
|
Bool_ptr_prop *bool
|
|
|
|
// Ensure that properties tagged `blueprint:mutated` are omitted
|
2022-06-23 18:02:44 +02:00
|
|
|
Int_prop int `blueprint:"mutated"`
|
|
|
|
Int64_ptr_prop *int64
|
|
|
|
String_prop string
|
|
|
|
String_literal_prop *string `android:"arch_variant"`
|
|
|
|
String_ptr_prop *string
|
|
|
|
String_list_prop []string
|
2020-11-26 01:06:39 +01:00
|
|
|
|
|
|
|
Nested_props nestedProps
|
|
|
|
Nested_props_ptr *nestedProps
|
2021-04-22 00:15:34 +02:00
|
|
|
|
2021-08-04 21:17:02 +02:00
|
|
|
Arch_paths []string `android:"path,arch_variant"`
|
|
|
|
Arch_paths_exclude []string `android:"path,arch_variant"`
|
2021-11-01 20:32:43 +01:00
|
|
|
|
|
|
|
// Prop used to indicate this conversion should be 1 module -> multiple targets
|
|
|
|
One_to_many_prop *bool
|
2022-09-28 22:43:08 +02:00
|
|
|
|
2023-09-28 22:41:03 +02:00
|
|
|
// Prop used to simulate an unsupported property in bp2build conversion. If this
|
|
|
|
// is true, this module should be treated as "unconvertible" via bp2build.
|
|
|
|
Does_not_convert_to_bazel *bool
|
|
|
|
|
2022-09-28 22:43:08 +02:00
|
|
|
Api *string // File describing the APIs of this module
|
2023-04-19 19:36:12 +02:00
|
|
|
|
|
|
|
Test_config_setting *bool // Used to test generation of config_setting targets
|
2023-08-04 00:33:47 +02:00
|
|
|
|
|
|
|
Dir *string // Dir in which the Bazel Target will be created
|
2020-11-26 01:06:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type customModule struct {
|
|
|
|
android.ModuleBase
|
2021-02-17 16:17:28 +01:00
|
|
|
android.BazelModuleBase
|
2020-11-26 01:06:39 +01:00
|
|
|
|
|
|
|
props customProps
|
|
|
|
}
|
|
|
|
|
|
|
|
// OutputFiles is needed because some instances of this module use dist with a
|
|
|
|
// tag property which requires the module implements OutputFileProducer.
|
|
|
|
func (m *customModule) OutputFiles(tag string) (android.Paths, error) {
|
|
|
|
return android.PathsForTesting("path" + tag), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *customModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
|
|
|
|
// nothing for now.
|
|
|
|
}
|
|
|
|
|
|
|
|
func customModuleFactoryBase() android.Module {
|
|
|
|
module := &customModule{}
|
|
|
|
module.AddProperties(&module.props)
|
2021-02-17 16:17:28 +01:00
|
|
|
android.InitBazelModule(module)
|
2020-11-26 01:06:39 +01:00
|
|
|
return module
|
|
|
|
}
|
|
|
|
|
2022-05-13 23:20:20 +02:00
|
|
|
func customModuleFactoryHostAndDevice() android.Module {
|
2020-11-26 01:06:39 +01:00
|
|
|
m := customModuleFactoryBase()
|
2021-04-22 00:15:34 +02:00
|
|
|
android.InitAndroidArchModule(m, android.HostAndDeviceSupported, android.MultilibBoth)
|
2020-11-26 01:06:39 +01:00
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
2022-05-13 23:20:20 +02:00
|
|
|
func customModuleFactoryDeviceSupported() android.Module {
|
|
|
|
m := customModuleFactoryBase()
|
|
|
|
android.InitAndroidArchModule(m, android.DeviceSupported, android.MultilibBoth)
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
|
|
|
func customModuleFactoryHostSupported() android.Module {
|
|
|
|
m := customModuleFactoryBase()
|
|
|
|
android.InitAndroidArchModule(m, android.HostSupported, android.MultilibBoth)
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
|
|
|
func customModuleFactoryHostAndDeviceDefault() android.Module {
|
|
|
|
m := customModuleFactoryBase()
|
|
|
|
android.InitAndroidArchModule(m, android.HostAndDeviceDefault, android.MultilibBoth)
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
|
|
|
func customModuleFactoryNeitherHostNorDeviceSupported() android.Module {
|
|
|
|
m := customModuleFactoryBase()
|
|
|
|
android.InitAndroidArchModule(m, android.NeitherHostNorDeviceSupported, android.MultilibBoth)
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
2020-11-26 01:06:39 +01:00
|
|
|
type testProps struct {
|
|
|
|
Test_prop struct {
|
|
|
|
Test_string_prop string
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type customTestModule struct {
|
|
|
|
android.ModuleBase
|
|
|
|
|
|
|
|
props customProps
|
|
|
|
test_props testProps
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *customTestModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
|
|
|
|
// nothing for now.
|
|
|
|
}
|
|
|
|
|
|
|
|
func customTestModuleFactoryBase() android.Module {
|
|
|
|
m := &customTestModule{}
|
|
|
|
m.AddProperties(&m.props)
|
|
|
|
m.AddProperties(&m.test_props)
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
|
|
|
func customTestModuleFactory() android.Module {
|
|
|
|
m := customTestModuleFactoryBase()
|
|
|
|
android.InitAndroidModule(m)
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
|
|
|
type customDefaultsModule struct {
|
|
|
|
android.ModuleBase
|
|
|
|
android.DefaultsModuleBase
|
|
|
|
}
|
|
|
|
|
|
|
|
func customDefaultsModuleFactoryBase() android.DefaultsModule {
|
|
|
|
module := &customDefaultsModule{}
|
|
|
|
module.AddProperties(&customProps{})
|
|
|
|
return module
|
|
|
|
}
|
|
|
|
|
|
|
|
func customDefaultsModuleFactoryBasic() android.Module {
|
|
|
|
return customDefaultsModuleFactoryBase()
|
|
|
|
}
|
|
|
|
|
|
|
|
func customDefaultsModuleFactory() android.Module {
|
|
|
|
m := customDefaultsModuleFactoryBase()
|
|
|
|
android.InitDefaultsModule(m)
|
|
|
|
return m
|
|
|
|
}
|
2020-12-14 14:25:34 +01:00
|
|
|
|
2021-09-14 17:17:21 +02:00
|
|
|
type EmbeddedAttr struct {
|
2021-12-01 16:09:34 +01:00
|
|
|
Embedded_attr *string
|
2021-09-14 17:17:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type OtherEmbeddedAttr struct {
|
2021-12-01 16:09:34 +01:00
|
|
|
Other_embedded_attr *string
|
2021-09-14 17:17:21 +02:00
|
|
|
}
|
|
|
|
|
2020-12-14 14:25:34 +01:00
|
|
|
type customBazelModuleAttributes struct {
|
2021-09-14 17:17:21 +02:00
|
|
|
EmbeddedAttr
|
|
|
|
*OtherEmbeddedAttr
|
2022-06-23 18:02:44 +02:00
|
|
|
String_literal_prop bazel.StringAttribute
|
|
|
|
String_ptr_prop *string
|
|
|
|
String_list_prop []string
|
|
|
|
Arch_paths bazel.LabelListAttribute
|
2022-09-28 22:43:08 +02:00
|
|
|
Api bazel.LabelAttribute
|
2020-12-14 14:25:34 +01:00
|
|
|
}
|
|
|
|
|
2023-08-04 00:33:47 +02:00
|
|
|
func (m *customModule) dir() *string {
|
|
|
|
return m.props.Dir
|
|
|
|
}
|
|
|
|
|
2023-09-19 22:09:00 +02:00
|
|
|
func (m *customModule) ConvertWithBp2build(ctx android.Bp2buildMutatorContext) {
|
2023-09-28 22:41:03 +02:00
|
|
|
if p := m.props.Does_not_convert_to_bazel; p != nil && *p {
|
|
|
|
ctx.MarkBp2buildUnconvertible(bp2build_metrics_proto.UnconvertedReasonType_PROPERTY_UNSUPPORTED, "")
|
|
|
|
return
|
|
|
|
}
|
2021-11-01 20:32:43 +01:00
|
|
|
if p := m.props.One_to_many_prop; p != nil && *p {
|
|
|
|
customBp2buildOneToMany(ctx, m)
|
|
|
|
return
|
|
|
|
}
|
2021-04-22 00:15:34 +02:00
|
|
|
|
2022-06-23 18:02:44 +02:00
|
|
|
paths := bazel.LabelListAttribute{}
|
|
|
|
strAttr := bazel.StringAttribute{}
|
2021-11-01 20:32:43 +01:00
|
|
|
for axis, configToProps := range m.GetArchVariantProperties(ctx, &customProps{}) {
|
|
|
|
for config, props := range configToProps {
|
2022-06-23 18:02:44 +02:00
|
|
|
if custProps, ok := props.(*customProps); ok {
|
|
|
|
if custProps.Arch_paths != nil {
|
|
|
|
paths.SetSelectValue(axis, config, android.BazelLabelForModuleSrcExcludes(ctx, custProps.Arch_paths, custProps.Arch_paths_exclude))
|
|
|
|
}
|
|
|
|
if custProps.String_literal_prop != nil {
|
|
|
|
strAttr.SetSelectValue(axis, config, custProps.String_literal_prop)
|
|
|
|
}
|
2021-04-22 00:15:34 +02:00
|
|
|
}
|
|
|
|
}
|
2021-11-01 20:32:43 +01:00
|
|
|
}
|
2023-09-19 14:41:14 +02:00
|
|
|
productVariableProps, errs := android.ProductVariableProperties(ctx, ctx.Module())
|
|
|
|
for _, err := range errs {
|
|
|
|
ctx.ModuleErrorf("ProductVariableProperties error: %s", err)
|
|
|
|
}
|
2022-12-21 20:51:37 +01:00
|
|
|
if props, ok := productVariableProps["String_literal_prop"]; ok {
|
|
|
|
for c, p := range props {
|
|
|
|
if val, ok := p.(*string); ok {
|
|
|
|
strAttr.SetSelectValue(c.ConfigurationAxis(), c.SelectKey(), val)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-04-22 00:15:34 +02:00
|
|
|
|
2021-11-01 20:32:43 +01:00
|
|
|
paths.ResolveExcludes()
|
2021-02-05 09:01:50 +01:00
|
|
|
|
2021-11-01 20:32:43 +01:00
|
|
|
attrs := &customBazelModuleAttributes{
|
2022-06-23 18:02:44 +02:00
|
|
|
String_literal_prop: strAttr,
|
|
|
|
String_ptr_prop: m.props.String_ptr_prop,
|
|
|
|
String_list_prop: m.props.String_list_prop,
|
|
|
|
Arch_paths: paths,
|
2021-11-01 20:32:43 +01:00
|
|
|
}
|
2022-06-23 18:02:44 +02:00
|
|
|
|
2021-11-01 20:32:43 +01:00
|
|
|
attrs.Embedded_attr = m.props.Embedded_prop
|
|
|
|
if m.props.OtherEmbeddedProps != nil {
|
|
|
|
attrs.OtherEmbeddedAttr = &OtherEmbeddedAttr{Other_embedded_attr: m.props.OtherEmbeddedProps.Other_embedded_prop}
|
|
|
|
}
|
2021-02-05 09:01:50 +01:00
|
|
|
|
2021-11-01 20:32:43 +01:00
|
|
|
props := bazel.BazelTargetModuleProperties{
|
|
|
|
Rule_class: "custom",
|
2020-12-14 14:25:34 +01:00
|
|
|
}
|
2021-11-01 20:32:43 +01:00
|
|
|
|
2023-08-04 00:33:47 +02:00
|
|
|
ctx.CreateBazelTargetModule(props, android.CommonAttributes{Name: m.Name(), Dir: m.dir()}, attrs)
|
2023-04-19 19:36:12 +02:00
|
|
|
|
|
|
|
if proptools.Bool(m.props.Test_config_setting) {
|
|
|
|
m.createConfigSetting(ctx)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-09-19 22:09:00 +02:00
|
|
|
func (m *customModule) createConfigSetting(ctx android.Bp2buildMutatorContext) {
|
2023-04-19 19:36:12 +02:00
|
|
|
csa := bazel.ConfigSettingAttributes{
|
|
|
|
Flag_values: bazel.StringMapAttribute{
|
|
|
|
"//build/bazel/rules/my_string_setting": m.Name(),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
ca := android.CommonAttributes{
|
|
|
|
Name: m.Name() + "_config_setting",
|
|
|
|
}
|
|
|
|
ctx.CreateBazelConfigSetting(
|
|
|
|
csa,
|
|
|
|
ca,
|
|
|
|
ctx.ModuleDir(),
|
|
|
|
)
|
2020-12-14 14:25:34 +01:00
|
|
|
}
|
2021-01-27 03:58:43 +01:00
|
|
|
|
|
|
|
// A bp2build mutator that uses load statements and creates a 1:M mapping from
|
|
|
|
// module to target.
|
2023-09-19 22:09:00 +02:00
|
|
|
func customBp2buildOneToMany(ctx android.Bp2buildMutatorContext, m *customModule) {
|
2021-02-05 09:03:24 +01:00
|
|
|
|
2021-11-01 20:32:43 +01:00
|
|
|
baseName := m.Name()
|
|
|
|
attrs := &customBazelModuleAttributes{}
|
2021-02-05 09:01:50 +01:00
|
|
|
|
2021-11-01 20:32:43 +01:00
|
|
|
myLibraryProps := bazel.BazelTargetModuleProperties{
|
|
|
|
Rule_class: "my_library",
|
|
|
|
Bzl_load_location: "//build/bazel/rules:rules.bzl",
|
|
|
|
}
|
|
|
|
ctx.CreateBazelTargetModule(myLibraryProps, android.CommonAttributes{Name: baseName}, attrs)
|
2021-02-19 17:06:17 +01:00
|
|
|
|
2021-11-01 20:32:43 +01:00
|
|
|
protoLibraryProps := bazel.BazelTargetModuleProperties{
|
|
|
|
Rule_class: "proto_library",
|
|
|
|
Bzl_load_location: "//build/bazel/rules:proto.bzl",
|
|
|
|
}
|
|
|
|
ctx.CreateBazelTargetModule(protoLibraryProps, android.CommonAttributes{Name: baseName + "_proto_library_deps"}, attrs)
|
2021-02-19 17:06:17 +01:00
|
|
|
|
2021-11-01 20:32:43 +01:00
|
|
|
myProtoLibraryProps := bazel.BazelTargetModuleProperties{
|
|
|
|
Rule_class: "my_proto_library",
|
|
|
|
Bzl_load_location: "//build/bazel/rules:proto.bzl",
|
2021-01-27 03:58:43 +01:00
|
|
|
}
|
2021-11-01 20:32:43 +01:00
|
|
|
ctx.CreateBazelTargetModule(myProtoLibraryProps, android.CommonAttributes{Name: baseName + "_my_proto_library_deps"}, attrs)
|
2021-01-27 03:58:43 +01:00
|
|
|
}
|
2021-02-22 16:19:34 +01:00
|
|
|
|
|
|
|
// Helper method for tests to easily access the targets in a dir.
|
2021-08-26 14:37:59 +02:00
|
|
|
func generateBazelTargetsForDir(codegenCtx *CodegenContext, dir string) (BazelTargets, []error) {
|
2021-04-21 13:10:09 +02:00
|
|
|
// TODO: Set generateFilegroups to true and/or remove the generateFilegroups argument completely
|
2021-08-26 14:37:59 +02:00
|
|
|
res, err := GenerateBazelTargets(codegenCtx, false)
|
2022-08-16 22:37:33 +02:00
|
|
|
if err != nil {
|
|
|
|
return BazelTargets{}, err
|
|
|
|
}
|
2021-08-26 14:37:59 +02:00
|
|
|
return res.buildFileToTargets[dir], err
|
2021-02-22 16:19:34 +01:00
|
|
|
}
|
2021-08-04 21:17:02 +02:00
|
|
|
|
|
|
|
func registerCustomModuleForBp2buildConversion(ctx *android.TestContext) {
|
2022-05-13 23:20:20 +02:00
|
|
|
ctx.RegisterModuleType("custom", customModuleFactoryHostAndDevice)
|
2021-08-04 21:17:02 +02:00
|
|
|
ctx.RegisterForBazelConversion()
|
|
|
|
}
|
2021-09-22 21:52:58 +02:00
|
|
|
|
2023-09-19 03:12:48 +02:00
|
|
|
func simpleModule(typ, name string) string {
|
2021-09-22 21:52:58 +02:00
|
|
|
return fmt.Sprintf(`
|
|
|
|
%s {
|
|
|
|
name: "%s",
|
|
|
|
}`, typ, name)
|
|
|
|
}
|
2021-11-08 18:56:31 +01:00
|
|
|
|
2022-06-21 21:28:33 +02:00
|
|
|
type AttrNameToString map[string]string
|
2021-11-08 18:56:31 +01:00
|
|
|
|
2022-06-21 21:28:33 +02:00
|
|
|
func (a AttrNameToString) clone() AttrNameToString {
|
|
|
|
newAttrs := make(AttrNameToString, len(a))
|
2022-05-13 23:20:20 +02:00
|
|
|
for k, v := range a {
|
|
|
|
newAttrs[k] = v
|
|
|
|
}
|
|
|
|
return newAttrs
|
|
|
|
}
|
|
|
|
|
|
|
|
// makeBazelTargetNoRestrictions returns bazel target build file definition that can be host or
|
|
|
|
// device specific, or independent of host/device.
|
2022-06-21 21:28:33 +02:00
|
|
|
func makeBazelTargetHostOrDevice(typ, name string, attrs AttrNameToString, hod android.HostOrDeviceSupported) string {
|
2022-05-13 23:20:20 +02:00
|
|
|
if _, ok := attrs["target_compatible_with"]; !ok {
|
|
|
|
switch hod {
|
|
|
|
case android.HostSupported:
|
|
|
|
attrs["target_compatible_with"] = `select({
|
2023-10-11 12:51:28 +02:00
|
|
|
"//build/bazel_common_rules/platforms/os:android": ["@platforms//:incompatible"],
|
2022-05-13 23:20:20 +02:00
|
|
|
"//conditions:default": [],
|
|
|
|
})`
|
|
|
|
case android.DeviceSupported:
|
2023-10-11 12:51:28 +02:00
|
|
|
attrs["target_compatible_with"] = `["//build/bazel_common_rules/platforms/os:android"]`
|
2022-05-13 23:20:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-08 18:56:31 +01:00
|
|
|
attrStrings := make([]string, 0, len(attrs)+1)
|
2022-08-04 20:13:27 +02:00
|
|
|
if name != "" {
|
|
|
|
attrStrings = append(attrStrings, fmt.Sprintf(` name = "%s",`, name))
|
|
|
|
}
|
2023-03-01 01:02:16 +01:00
|
|
|
for _, k := range android.SortedKeys(attrs) {
|
2021-11-08 18:56:31 +01:00
|
|
|
attrStrings = append(attrStrings, fmt.Sprintf(" %s = %s,", k, attrs[k]))
|
|
|
|
}
|
|
|
|
return fmt.Sprintf(`%s(
|
|
|
|
%s
|
|
|
|
)`, typ, strings.Join(attrStrings, "\n"))
|
|
|
|
}
|
2022-05-13 23:20:20 +02:00
|
|
|
|
2022-06-21 21:28:33 +02:00
|
|
|
// MakeBazelTargetNoRestrictions returns bazel target build file definition that does not add a
|
2022-05-13 23:20:20 +02:00
|
|
|
// target_compatible_with. This is useful for module types like filegroup and genrule that arch not
|
|
|
|
// arch variant
|
2022-06-21 21:28:33 +02:00
|
|
|
func MakeBazelTargetNoRestrictions(typ, name string, attrs AttrNameToString) string {
|
2022-05-13 23:20:20 +02:00
|
|
|
return makeBazelTargetHostOrDevice(typ, name, attrs, android.HostAndDeviceDefault)
|
|
|
|
}
|
|
|
|
|
|
|
|
// makeBazelTargetNoRestrictions returns bazel target build file definition that is device specific
|
|
|
|
// as this is the most common default in Soong.
|
2022-08-31 20:28:19 +02:00
|
|
|
func MakeBazelTarget(typ, name string, attrs AttrNameToString) string {
|
2022-05-13 23:20:20 +02:00
|
|
|
return makeBazelTargetHostOrDevice(typ, name, attrs, android.DeviceSupported)
|
|
|
|
}
|
2022-08-04 22:28:38 +02:00
|
|
|
|
|
|
|
type ExpectedRuleTarget struct {
|
|
|
|
Rule string
|
|
|
|
Name string
|
|
|
|
Attrs AttrNameToString
|
|
|
|
Hod android.HostOrDeviceSupported
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ebr ExpectedRuleTarget) String() string {
|
|
|
|
return makeBazelTargetHostOrDevice(ebr.Rule, ebr.Name, ebr.Attrs, ebr.Hod)
|
|
|
|
}
|
2022-09-16 17:36:10 +02:00
|
|
|
|
|
|
|
func makeCcStubSuiteTargets(name string, attrs AttrNameToString) string {
|
|
|
|
if _, hasStubs := attrs["stubs_symbol_file"]; !hasStubs {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
STUB_SUITE_ATTRS := map[string]string{
|
2023-09-14 01:59:05 +02:00
|
|
|
"api_surface": "api_surface",
|
2023-03-15 23:06:18 +01:00
|
|
|
"stubs_symbol_file": "symbol_file",
|
|
|
|
"stubs_versions": "versions",
|
|
|
|
"soname": "soname",
|
|
|
|
"source_library_label": "source_library_label",
|
2022-09-16 17:36:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
stubSuiteAttrs := AttrNameToString{}
|
|
|
|
for key, _ := range attrs {
|
|
|
|
if _, stubSuiteAttr := STUB_SUITE_ATTRS[key]; stubSuiteAttr {
|
|
|
|
stubSuiteAttrs[STUB_SUITE_ATTRS[key]] = attrs[key]
|
2023-03-15 23:06:18 +01:00
|
|
|
} else {
|
|
|
|
panic(fmt.Sprintf("unused cc_stub_suite attr %q\n", key))
|
2022-09-16 17:36:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return MakeBazelTarget("cc_stub_suite", name+"_stub_libs", stubSuiteAttrs)
|
|
|
|
}
|
2022-10-31 20:08:18 +01:00
|
|
|
|
|
|
|
func MakeNeverlinkDuplicateTarget(moduleType string, name string) string {
|
2023-08-07 19:38:18 +02:00
|
|
|
return MakeNeverlinkDuplicateTargetWithAttrs(moduleType, name, AttrNameToString{
|
|
|
|
"sdk_version": `"current"`, // use as default
|
|
|
|
})
|
2023-02-24 18:07:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func MakeNeverlinkDuplicateTargetWithAttrs(moduleType string, name string, extraAttrs AttrNameToString) string {
|
|
|
|
attrs := extraAttrs
|
|
|
|
attrs["neverlink"] = `True`
|
|
|
|
attrs["exports"] = `[":` + name + `"]`
|
|
|
|
return MakeBazelTarget(moduleType, name+"-neverlink", attrs)
|
2022-10-31 20:08:18 +01:00
|
|
|
}
|
2023-03-08 01:48:19 +01:00
|
|
|
|
|
|
|
func getTargetName(targetContent string) string {
|
|
|
|
data := strings.Split(targetContent, "name = \"")
|
|
|
|
if len(data) < 2 {
|
|
|
|
return ""
|
|
|
|
} else {
|
|
|
|
endIndex := strings.Index(data[1], "\"")
|
|
|
|
return data[1][:endIndex]
|
|
|
|
}
|
|
|
|
}
|