2020-11-26 01:06:39 +01:00
|
|
|
// Copyright 2020 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 bp2build
|
|
|
|
|
|
|
|
import (
|
2021-08-26 14:37:59 +02:00
|
|
|
"fmt"
|
2021-01-26 15:18:53 +01:00
|
|
|
"strings"
|
2020-11-26 01:06:39 +01:00
|
|
|
"testing"
|
2021-08-31 22:30:36 +02:00
|
|
|
|
|
|
|
"android/soong/android"
|
2022-03-28 21:53:03 +02:00
|
|
|
"android/soong/android/allowlists"
|
2023-04-26 04:56:37 +02:00
|
|
|
"android/soong/bazel"
|
2021-08-31 22:30:36 +02:00
|
|
|
"android/soong/python"
|
2020-11-26 01:06:39 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestGenerateSoongModuleTargets(t *testing.T) {
|
|
|
|
testCases := []struct {
|
2021-06-03 19:43:01 +02:00
|
|
|
description string
|
2020-11-26 01:06:39 +01:00
|
|
|
bp string
|
|
|
|
expectedBazelTarget string
|
|
|
|
}{
|
|
|
|
{
|
2021-06-03 19:43:01 +02:00
|
|
|
description: "only name",
|
2021-04-08 16:40:57 +02:00
|
|
|
bp: `custom { name: "foo" }
|
2021-06-03 19:43:01 +02:00
|
|
|
`,
|
2020-11-26 01:06:39 +01:00
|
|
|
expectedBazelTarget: `soong_module(
|
|
|
|
name = "foo",
|
2021-01-25 10:36:04 +01:00
|
|
|
soong_module_name = "foo",
|
|
|
|
soong_module_type = "custom",
|
|
|
|
soong_module_variant = "",
|
|
|
|
soong_module_deps = [
|
2020-11-26 01:06:39 +01:00
|
|
|
],
|
2021-06-03 19:43:01 +02:00
|
|
|
bool_prop = False,
|
2021-12-01 16:09:34 +01:00
|
|
|
string_prop = "",
|
2020-11-26 01:06:39 +01:00
|
|
|
)`,
|
|
|
|
},
|
|
|
|
{
|
2021-06-03 19:43:01 +02:00
|
|
|
description: "handles bool",
|
2020-11-26 01:06:39 +01:00
|
|
|
bp: `custom {
|
2021-06-03 19:43:01 +02:00
|
|
|
name: "foo",
|
|
|
|
bool_prop: true,
|
2020-11-26 01:06:39 +01:00
|
|
|
}
|
2021-06-03 19:43:01 +02:00
|
|
|
`,
|
2020-11-26 01:06:39 +01:00
|
|
|
expectedBazelTarget: `soong_module(
|
|
|
|
name = "foo",
|
2021-01-25 10:36:04 +01:00
|
|
|
soong_module_name = "foo",
|
|
|
|
soong_module_type = "custom",
|
|
|
|
soong_module_variant = "",
|
|
|
|
soong_module_deps = [
|
2020-11-26 01:06:39 +01:00
|
|
|
],
|
2021-06-03 19:43:01 +02:00
|
|
|
bool_prop = True,
|
2021-12-01 16:09:34 +01:00
|
|
|
string_prop = "",
|
2020-11-26 01:06:39 +01:00
|
|
|
)`,
|
|
|
|
},
|
|
|
|
{
|
2021-06-03 19:43:01 +02:00
|
|
|
description: "string escaping",
|
2020-11-26 01:06:39 +01:00
|
|
|
bp: `custom {
|
2021-06-03 19:43:01 +02:00
|
|
|
name: "foo",
|
|
|
|
owner: "a_string_with\"quotes\"_and_\\backslashes\\\\",
|
2020-11-26 01:06:39 +01:00
|
|
|
}
|
2021-06-03 19:43:01 +02:00
|
|
|
`,
|
2020-11-26 01:06:39 +01:00
|
|
|
expectedBazelTarget: `soong_module(
|
|
|
|
name = "foo",
|
2021-01-25 10:36:04 +01:00
|
|
|
soong_module_name = "foo",
|
|
|
|
soong_module_type = "custom",
|
|
|
|
soong_module_variant = "",
|
|
|
|
soong_module_deps = [
|
2020-11-26 01:06:39 +01:00
|
|
|
],
|
2021-06-03 19:43:01 +02:00
|
|
|
bool_prop = False,
|
2020-11-26 01:06:39 +01:00
|
|
|
owner = "a_string_with\"quotes\"_and_\\backslashes\\\\",
|
2021-12-01 16:09:34 +01:00
|
|
|
string_prop = "",
|
2020-11-26 01:06:39 +01:00
|
|
|
)`,
|
|
|
|
},
|
|
|
|
{
|
2021-06-03 19:43:01 +02:00
|
|
|
description: "single item string list",
|
2020-11-26 01:06:39 +01:00
|
|
|
bp: `custom {
|
2021-06-03 19:43:01 +02:00
|
|
|
name: "foo",
|
|
|
|
required: ["bar"],
|
2020-11-26 01:06:39 +01:00
|
|
|
}
|
2021-06-03 19:43:01 +02:00
|
|
|
`,
|
2020-11-26 01:06:39 +01:00
|
|
|
expectedBazelTarget: `soong_module(
|
|
|
|
name = "foo",
|
2021-01-25 10:36:04 +01:00
|
|
|
soong_module_name = "foo",
|
|
|
|
soong_module_type = "custom",
|
|
|
|
soong_module_variant = "",
|
|
|
|
soong_module_deps = [
|
2020-11-26 01:06:39 +01:00
|
|
|
],
|
2021-06-03 19:43:01 +02:00
|
|
|
bool_prop = False,
|
2021-04-08 16:40:57 +02:00
|
|
|
required = ["bar"],
|
2021-12-01 16:09:34 +01:00
|
|
|
string_prop = "",
|
2020-11-26 01:06:39 +01:00
|
|
|
)`,
|
|
|
|
},
|
|
|
|
{
|
2021-06-03 19:43:01 +02:00
|
|
|
description: "list of strings",
|
2020-11-26 01:06:39 +01:00
|
|
|
bp: `custom {
|
2021-06-03 19:43:01 +02:00
|
|
|
name: "foo",
|
|
|
|
target_required: ["qux", "bazqux"],
|
2020-11-26 01:06:39 +01:00
|
|
|
}
|
2021-06-03 19:43:01 +02:00
|
|
|
`,
|
2020-11-26 01:06:39 +01:00
|
|
|
expectedBazelTarget: `soong_module(
|
|
|
|
name = "foo",
|
2021-01-25 10:36:04 +01:00
|
|
|
soong_module_name = "foo",
|
|
|
|
soong_module_type = "custom",
|
|
|
|
soong_module_variant = "",
|
|
|
|
soong_module_deps = [
|
2020-11-26 01:06:39 +01:00
|
|
|
],
|
2021-06-03 19:43:01 +02:00
|
|
|
bool_prop = False,
|
2021-12-01 16:09:34 +01:00
|
|
|
string_prop = "",
|
2020-11-26 01:06:39 +01:00
|
|
|
target_required = [
|
|
|
|
"qux",
|
|
|
|
"bazqux",
|
|
|
|
],
|
|
|
|
)`,
|
|
|
|
},
|
|
|
|
{
|
2021-06-03 19:43:01 +02:00
|
|
|
description: "dist/dists",
|
2020-11-26 01:06:39 +01:00
|
|
|
bp: `custom {
|
2021-06-03 19:43:01 +02:00
|
|
|
name: "foo",
|
|
|
|
dist: {
|
|
|
|
targets: ["goal_foo"],
|
|
|
|
tag: ".foo",
|
|
|
|
},
|
|
|
|
dists: [{
|
|
|
|
targets: ["goal_bar"],
|
|
|
|
tag: ".bar",
|
|
|
|
}],
|
2020-11-26 01:06:39 +01:00
|
|
|
}
|
2021-06-03 19:43:01 +02:00
|
|
|
`,
|
2020-11-26 01:06:39 +01:00
|
|
|
expectedBazelTarget: `soong_module(
|
|
|
|
name = "foo",
|
2021-01-25 10:36:04 +01:00
|
|
|
soong_module_name = "foo",
|
|
|
|
soong_module_type = "custom",
|
|
|
|
soong_module_variant = "",
|
|
|
|
soong_module_deps = [
|
2020-11-26 01:06:39 +01:00
|
|
|
],
|
2021-06-03 19:43:01 +02:00
|
|
|
bool_prop = False,
|
2020-11-26 01:06:39 +01:00
|
|
|
dist = {
|
|
|
|
"tag": ".foo",
|
2021-04-08 16:40:57 +02:00
|
|
|
"targets": ["goal_foo"],
|
2020-11-26 01:06:39 +01:00
|
|
|
},
|
2021-04-08 16:40:57 +02:00
|
|
|
dists = [{
|
|
|
|
"tag": ".bar",
|
|
|
|
"targets": ["goal_bar"],
|
|
|
|
}],
|
2021-12-01 16:09:34 +01:00
|
|
|
string_prop = "",
|
2020-11-26 01:06:39 +01:00
|
|
|
)`,
|
|
|
|
},
|
|
|
|
{
|
2021-06-03 19:43:01 +02:00
|
|
|
description: "put it together",
|
2020-11-26 01:06:39 +01:00
|
|
|
bp: `custom {
|
2021-06-03 19:43:01 +02:00
|
|
|
name: "foo",
|
|
|
|
required: ["bar"],
|
|
|
|
target_required: ["qux", "bazqux"],
|
|
|
|
bool_prop: true,
|
|
|
|
owner: "custom_owner",
|
|
|
|
dists: [
|
|
|
|
{
|
|
|
|
tag: ".tag",
|
|
|
|
targets: ["my_goal"],
|
|
|
|
},
|
|
|
|
],
|
2020-11-26 01:06:39 +01:00
|
|
|
}
|
2021-06-03 19:43:01 +02:00
|
|
|
`,
|
2020-11-26 01:06:39 +01:00
|
|
|
expectedBazelTarget: `soong_module(
|
|
|
|
name = "foo",
|
2021-01-25 10:36:04 +01:00
|
|
|
soong_module_name = "foo",
|
|
|
|
soong_module_type = "custom",
|
|
|
|
soong_module_variant = "",
|
|
|
|
soong_module_deps = [
|
2020-11-26 01:06:39 +01:00
|
|
|
],
|
2021-06-03 19:43:01 +02:00
|
|
|
bool_prop = True,
|
2021-04-08 16:40:57 +02:00
|
|
|
dists = [{
|
|
|
|
"tag": ".tag",
|
|
|
|
"targets": ["my_goal"],
|
|
|
|
}],
|
2020-11-26 01:06:39 +01:00
|
|
|
owner = "custom_owner",
|
2021-04-08 16:40:57 +02:00
|
|
|
required = ["bar"],
|
2021-12-01 16:09:34 +01:00
|
|
|
string_prop = "",
|
2020-11-26 01:06:39 +01:00
|
|
|
target_required = [
|
|
|
|
"qux",
|
|
|
|
"bazqux",
|
|
|
|
],
|
|
|
|
)`,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
dir := "."
|
|
|
|
for _, testCase := range testCases {
|
2021-06-03 19:43:01 +02:00
|
|
|
t.Run(testCase.description, func(t *testing.T) {
|
|
|
|
config := android.TestConfig(buildDir, nil, testCase.bp, nil)
|
|
|
|
ctx := android.NewTestContext(config)
|
2021-02-19 06:48:40 +01:00
|
|
|
|
2022-05-13 23:20:20 +02:00
|
|
|
ctx.RegisterModuleType("custom", customModuleFactoryHostAndDevice)
|
2021-06-03 19:43:01 +02:00
|
|
|
ctx.Register()
|
2020-11-26 01:06:39 +01:00
|
|
|
|
2021-06-03 19:43:01 +02:00
|
|
|
_, errs := ctx.ParseFileList(dir, []string{"Android.bp"})
|
|
|
|
android.FailIfErrored(t, errs)
|
|
|
|
_, errs = ctx.PrepareBuildActions(config)
|
|
|
|
android.FailIfErrored(t, errs)
|
2020-11-26 01:06:39 +01:00
|
|
|
|
2022-11-09 03:14:01 +01:00
|
|
|
codegenCtx := NewCodegenContext(config, ctx.Context, QueryView, "")
|
2021-08-26 14:37:59 +02:00
|
|
|
bazelTargets, err := generateBazelTargetsForDir(codegenCtx, dir)
|
|
|
|
android.FailIfErrored(t, err)
|
2021-06-03 19:43:01 +02:00
|
|
|
if actualCount, expectedCount := len(bazelTargets), 1; actualCount != expectedCount {
|
|
|
|
t.Fatalf("Expected %d bazel target, got %d", expectedCount, actualCount)
|
|
|
|
}
|
2020-11-26 01:06:39 +01:00
|
|
|
|
2021-06-03 19:43:01 +02:00
|
|
|
actualBazelTarget := bazelTargets[0]
|
|
|
|
if actualBazelTarget.content != testCase.expectedBazelTarget {
|
|
|
|
t.Errorf(
|
|
|
|
"Expected generated Bazel target to be '%s', got '%s'",
|
|
|
|
testCase.expectedBazelTarget,
|
|
|
|
actualBazelTarget.content,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
})
|
2020-12-14 14:25:34 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGenerateBazelTargetModules(t *testing.T) {
|
2022-06-21 21:28:33 +02:00
|
|
|
testCases := []Bp2buildTestCase{
|
2022-06-23 18:02:44 +02:00
|
|
|
{
|
|
|
|
Description: "string prop empty",
|
|
|
|
Blueprint: `custom {
|
|
|
|
name: "foo",
|
|
|
|
string_literal_prop: "",
|
|
|
|
bazel_module: { bp2build_available: true },
|
|
|
|
}`,
|
|
|
|
ExpectedBazelTargets: []string{
|
2022-08-31 20:28:19 +02:00
|
|
|
MakeBazelTarget("custom", "foo", AttrNameToString{
|
2022-06-23 18:02:44 +02:00
|
|
|
"string_literal_prop": `""`,
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Description: `string prop "PROP"`,
|
|
|
|
Blueprint: `custom {
|
|
|
|
name: "foo",
|
|
|
|
string_literal_prop: "PROP",
|
|
|
|
bazel_module: { bp2build_available: true },
|
|
|
|
}`,
|
|
|
|
ExpectedBazelTargets: []string{
|
2022-08-31 20:28:19 +02:00
|
|
|
MakeBazelTarget("custom", "foo", AttrNameToString{
|
2022-06-23 18:02:44 +02:00
|
|
|
"string_literal_prop": `"PROP"`,
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Description: `string prop arch variant`,
|
|
|
|
Blueprint: `custom {
|
|
|
|
name: "foo",
|
|
|
|
arch: {
|
|
|
|
arm: { string_literal_prop: "ARM" },
|
|
|
|
arm64: { string_literal_prop: "ARM64" },
|
|
|
|
},
|
|
|
|
bazel_module: { bp2build_available: true },
|
|
|
|
}`,
|
|
|
|
ExpectedBazelTargets: []string{
|
2022-08-31 20:28:19 +02:00
|
|
|
MakeBazelTarget("custom", "foo", AttrNameToString{
|
2022-06-23 18:02:44 +02:00
|
|
|
"string_literal_prop": `select({
|
2023-10-11 12:51:28 +02:00
|
|
|
"//build/bazel_common_rules/platforms/arch:arm": "ARM",
|
|
|
|
"//build/bazel_common_rules/platforms/arch:arm64": "ARM64",
|
2022-06-23 18:02:44 +02:00
|
|
|
"//conditions:default": None,
|
|
|
|
})`,
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
},
|
2021-12-01 16:09:34 +01:00
|
|
|
{
|
2022-06-21 21:28:33 +02:00
|
|
|
Description: "string ptr props",
|
|
|
|
Blueprint: `custom {
|
2021-12-01 16:09:34 +01:00
|
|
|
name: "foo",
|
|
|
|
string_ptr_prop: "",
|
|
|
|
bazel_module: { bp2build_available: true },
|
|
|
|
}`,
|
2022-06-21 21:28:33 +02:00
|
|
|
ExpectedBazelTargets: []string{
|
2022-08-31 20:28:19 +02:00
|
|
|
MakeBazelTarget("custom", "foo", AttrNameToString{
|
2021-12-01 16:09:34 +01:00
|
|
|
"string_ptr_prop": `""`,
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
},
|
2020-12-14 14:25:34 +01:00
|
|
|
{
|
2022-06-23 18:02:44 +02:00
|
|
|
Description: "string list props",
|
2022-06-21 21:28:33 +02:00
|
|
|
Blueprint: `custom {
|
2021-11-01 20:32:43 +01:00
|
|
|
name: "foo",
|
2020-12-14 14:25:34 +01:00
|
|
|
string_list_prop: ["a", "b"],
|
2021-12-01 16:09:34 +01:00
|
|
|
string_ptr_prop: "a",
|
2021-02-05 09:03:24 +01:00
|
|
|
bazel_module: { bp2build_available: true },
|
2020-12-14 14:25:34 +01:00
|
|
|
}`,
|
2022-06-21 21:28:33 +02:00
|
|
|
ExpectedBazelTargets: []string{
|
2022-08-31 20:28:19 +02:00
|
|
|
MakeBazelTarget("custom", "foo", AttrNameToString{
|
2021-11-08 18:56:31 +01:00
|
|
|
"string_list_prop": `[
|
2020-12-14 14:25:34 +01:00
|
|
|
"a",
|
|
|
|
"b",
|
2021-11-08 18:56:31 +01:00
|
|
|
]`,
|
2021-12-01 16:09:34 +01:00
|
|
|
"string_ptr_prop": `"a"`,
|
2021-11-08 18:56:31 +01:00
|
|
|
}),
|
2021-04-22 00:15:34 +02:00
|
|
|
},
|
2021-03-30 15:08:36 +02:00
|
|
|
},
|
|
|
|
{
|
2022-06-21 21:28:33 +02:00
|
|
|
Description: "control characters",
|
|
|
|
Blueprint: `custom {
|
2021-11-08 18:56:31 +01:00
|
|
|
name: "foo",
|
2021-03-30 15:08:36 +02:00
|
|
|
string_list_prop: ["\t", "\n"],
|
2021-12-01 16:09:34 +01:00
|
|
|
string_ptr_prop: "a\t\n\r",
|
2021-03-30 15:08:36 +02:00
|
|
|
bazel_module: { bp2build_available: true },
|
|
|
|
}`,
|
2022-06-21 21:28:33 +02:00
|
|
|
ExpectedBazelTargets: []string{
|
2022-08-31 20:28:19 +02:00
|
|
|
MakeBazelTarget("custom", "foo", AttrNameToString{
|
2021-11-08 18:56:31 +01:00
|
|
|
"string_list_prop": `[
|
2021-03-30 15:08:36 +02:00
|
|
|
"\t",
|
|
|
|
"\n",
|
2021-11-08 18:56:31 +01:00
|
|
|
]`,
|
2021-12-01 16:09:34 +01:00
|
|
|
"string_ptr_prop": `"a\t\n\r"`,
|
2021-11-08 18:56:31 +01:00
|
|
|
}),
|
2021-04-22 00:15:34 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2022-06-21 21:28:33 +02:00
|
|
|
Description: "handles dep",
|
|
|
|
Blueprint: `custom {
|
2021-04-22 00:15:34 +02:00
|
|
|
name: "has_dep",
|
|
|
|
arch_paths: [":dep"],
|
|
|
|
bazel_module: { bp2build_available: true },
|
|
|
|
}
|
|
|
|
|
|
|
|
custom {
|
|
|
|
name: "dep",
|
|
|
|
arch_paths: ["abc"],
|
|
|
|
bazel_module: { bp2build_available: true },
|
|
|
|
}`,
|
2022-06-21 21:28:33 +02:00
|
|
|
ExpectedBazelTargets: []string{
|
2022-08-31 20:28:19 +02:00
|
|
|
MakeBazelTarget("custom", "dep", AttrNameToString{
|
2021-11-08 18:56:31 +01:00
|
|
|
"arch_paths": `["abc"]`,
|
|
|
|
}),
|
2022-08-31 20:28:19 +02:00
|
|
|
MakeBazelTarget("custom", "has_dep", AttrNameToString{
|
2021-11-08 18:56:31 +01:00
|
|
|
"arch_paths": `[":dep"]`,
|
|
|
|
}),
|
2021-04-22 00:15:34 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2022-06-21 21:28:33 +02:00
|
|
|
Description: "arch-variant srcs",
|
|
|
|
Blueprint: `custom {
|
2021-04-22 00:15:34 +02:00
|
|
|
name: "arch_paths",
|
|
|
|
arch: {
|
2021-10-11 21:41:03 +02:00
|
|
|
x86: { arch_paths: ["x86.txt"] },
|
|
|
|
x86_64: { arch_paths: ["x86_64.txt"] },
|
|
|
|
arm: { arch_paths: ["arm.txt"] },
|
|
|
|
arm64: { arch_paths: ["arm64.txt"] },
|
2022-07-15 03:10:34 +02:00
|
|
|
riscv64: { arch_paths: ["riscv64.txt"] },
|
2021-10-11 21:41:03 +02:00
|
|
|
},
|
|
|
|
target: {
|
|
|
|
linux: { arch_paths: ["linux.txt"] },
|
|
|
|
bionic: { arch_paths: ["bionic.txt"] },
|
|
|
|
host: { arch_paths: ["host.txt"] },
|
|
|
|
not_windows: { arch_paths: ["not_windows.txt"] },
|
|
|
|
android: { arch_paths: ["android.txt"] },
|
|
|
|
linux_musl: { arch_paths: ["linux_musl.txt"] },
|
|
|
|
musl: { arch_paths: ["musl.txt"] },
|
|
|
|
linux_glibc: { arch_paths: ["linux_glibc.txt"] },
|
|
|
|
glibc: { arch_paths: ["glibc.txt"] },
|
|
|
|
linux_bionic: { arch_paths: ["linux_bionic.txt"] },
|
|
|
|
darwin: { arch_paths: ["darwin.txt"] },
|
|
|
|
windows: { arch_paths: ["windows.txt"] },
|
|
|
|
},
|
|
|
|
multilib: {
|
|
|
|
lib32: { arch_paths: ["lib32.txt"] },
|
|
|
|
lib64: { arch_paths: ["lib64.txt"] },
|
2021-04-22 00:15:34 +02:00
|
|
|
},
|
|
|
|
bazel_module: { bp2build_available: true },
|
|
|
|
}`,
|
2022-06-21 21:28:33 +02:00
|
|
|
ExpectedBazelTargets: []string{
|
2022-08-31 20:28:19 +02:00
|
|
|
MakeBazelTarget("custom", "arch_paths", AttrNameToString{
|
2021-11-08 18:56:31 +01:00
|
|
|
"arch_paths": `select({
|
2023-10-11 12:51:28 +02:00
|
|
|
"//build/bazel_common_rules/platforms/arch:arm": [
|
2021-10-11 21:41:03 +02:00
|
|
|
"arm.txt",
|
|
|
|
"lib32.txt",
|
|
|
|
],
|
2023-10-11 12:51:28 +02:00
|
|
|
"//build/bazel_common_rules/platforms/arch:arm64": [
|
2021-10-11 21:41:03 +02:00
|
|
|
"arm64.txt",
|
|
|
|
"lib64.txt",
|
|
|
|
],
|
2023-10-11 12:51:28 +02:00
|
|
|
"//build/bazel_common_rules/platforms/arch:riscv64": [
|
2022-07-15 03:10:34 +02:00
|
|
|
"riscv64.txt",
|
|
|
|
"lib64.txt",
|
|
|
|
],
|
2023-10-11 12:51:28 +02:00
|
|
|
"//build/bazel_common_rules/platforms/arch:x86": [
|
2021-10-11 21:41:03 +02:00
|
|
|
"x86.txt",
|
|
|
|
"lib32.txt",
|
|
|
|
],
|
2023-10-11 12:51:28 +02:00
|
|
|
"//build/bazel_common_rules/platforms/arch:x86_64": [
|
2021-10-11 21:41:03 +02:00
|
|
|
"x86_64.txt",
|
|
|
|
"lib64.txt",
|
|
|
|
],
|
|
|
|
"//conditions:default": [],
|
|
|
|
}) + select({
|
2023-10-11 12:51:28 +02:00
|
|
|
"//build/bazel_common_rules/platforms/os:android": [
|
2021-10-11 21:41:03 +02:00
|
|
|
"linux.txt",
|
|
|
|
"bionic.txt",
|
|
|
|
"android.txt",
|
|
|
|
],
|
2023-10-11 12:51:28 +02:00
|
|
|
"//build/bazel_common_rules/platforms/os:darwin": [
|
2021-10-11 21:41:03 +02:00
|
|
|
"host.txt",
|
|
|
|
"darwin.txt",
|
|
|
|
"not_windows.txt",
|
|
|
|
],
|
2023-10-11 12:51:28 +02:00
|
|
|
"//build/bazel_common_rules/platforms/os:linux_bionic": [
|
2021-10-11 21:41:03 +02:00
|
|
|
"host.txt",
|
|
|
|
"linux.txt",
|
2022-12-21 00:29:31 +01:00
|
|
|
"bionic.txt",
|
|
|
|
"linux_bionic.txt",
|
2021-10-11 21:41:03 +02:00
|
|
|
"not_windows.txt",
|
|
|
|
],
|
2023-10-11 12:51:28 +02:00
|
|
|
"//build/bazel_common_rules/platforms/os:linux_glibc": [
|
2021-10-11 21:41:03 +02:00
|
|
|
"host.txt",
|
|
|
|
"linux.txt",
|
2022-12-21 00:29:31 +01:00
|
|
|
"glibc.txt",
|
|
|
|
"linux_glibc.txt",
|
2021-10-11 21:41:03 +02:00
|
|
|
"not_windows.txt",
|
|
|
|
],
|
2023-10-11 12:51:28 +02:00
|
|
|
"//build/bazel_common_rules/platforms/os:linux_musl": [
|
2021-10-11 21:41:03 +02:00
|
|
|
"host.txt",
|
|
|
|
"linux.txt",
|
|
|
|
"musl.txt",
|
|
|
|
"linux_musl.txt",
|
|
|
|
"not_windows.txt",
|
|
|
|
],
|
2023-10-11 12:51:28 +02:00
|
|
|
"//build/bazel_common_rules/platforms/os:windows": [
|
2021-10-11 21:41:03 +02:00
|
|
|
"host.txt",
|
|
|
|
"windows.txt",
|
|
|
|
],
|
2021-04-22 00:15:34 +02:00
|
|
|
"//conditions:default": [],
|
2021-11-08 18:56:31 +01:00
|
|
|
})`,
|
|
|
|
}),
|
2021-04-22 00:15:34 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2022-06-21 21:28:33 +02:00
|
|
|
Description: "arch-variant deps",
|
|
|
|
Blueprint: `custom {
|
2021-04-22 00:15:34 +02:00
|
|
|
name: "has_dep",
|
|
|
|
arch: {
|
|
|
|
x86: {
|
|
|
|
arch_paths: [":dep"],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
bazel_module: { bp2build_available: true },
|
|
|
|
}
|
|
|
|
|
|
|
|
custom {
|
|
|
|
name: "dep",
|
|
|
|
arch_paths: ["abc"],
|
|
|
|
bazel_module: { bp2build_available: true },
|
|
|
|
}`,
|
2022-06-21 21:28:33 +02:00
|
|
|
ExpectedBazelTargets: []string{
|
2022-08-31 20:28:19 +02:00
|
|
|
MakeBazelTarget("custom", "dep", AttrNameToString{
|
2021-11-08 18:56:31 +01:00
|
|
|
"arch_paths": `["abc"]`,
|
|
|
|
}),
|
2022-08-31 20:28:19 +02:00
|
|
|
MakeBazelTarget("custom", "has_dep", AttrNameToString{
|
2021-11-08 18:56:31 +01:00
|
|
|
"arch_paths": `select({
|
2023-10-11 12:51:28 +02:00
|
|
|
"//build/bazel_common_rules/platforms/arch:x86": [":dep"],
|
2021-04-22 00:15:34 +02:00
|
|
|
"//conditions:default": [],
|
2021-11-08 18:56:31 +01:00
|
|
|
})`,
|
|
|
|
}),
|
2021-09-14 17:17:21 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2022-06-21 21:28:33 +02:00
|
|
|
Description: "embedded props",
|
|
|
|
Blueprint: `custom {
|
2021-09-14 17:17:21 +02:00
|
|
|
name: "embedded_props",
|
|
|
|
embedded_prop: "abc",
|
|
|
|
bazel_module: { bp2build_available: true },
|
|
|
|
}`,
|
2022-06-21 21:28:33 +02:00
|
|
|
ExpectedBazelTargets: []string{
|
2022-08-31 20:28:19 +02:00
|
|
|
MakeBazelTarget("custom", "embedded_props", AttrNameToString{
|
2021-11-08 18:56:31 +01:00
|
|
|
"embedded_attr": `"abc"`,
|
|
|
|
}),
|
2021-09-14 17:17:21 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2022-06-21 21:28:33 +02:00
|
|
|
Description: "ptr to embedded props",
|
|
|
|
Blueprint: `custom {
|
2021-09-14 17:17:21 +02:00
|
|
|
name: "ptr_to_embedded_props",
|
|
|
|
other_embedded_prop: "abc",
|
|
|
|
bazel_module: { bp2build_available: true },
|
|
|
|
}`,
|
2022-06-21 21:28:33 +02:00
|
|
|
ExpectedBazelTargets: []string{
|
2022-08-31 20:28:19 +02:00
|
|
|
MakeBazelTarget("custom", "ptr_to_embedded_props", AttrNameToString{
|
2021-11-08 18:56:31 +01:00
|
|
|
"other_embedded_attr": `"abc"`,
|
|
|
|
}),
|
2021-04-22 00:15:34 +02:00
|
|
|
},
|
2020-12-14 14:25:34 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
dir := "."
|
|
|
|
for _, testCase := range testCases {
|
2022-06-21 21:28:33 +02:00
|
|
|
t.Run(testCase.Description, func(t *testing.T) {
|
|
|
|
config := android.TestConfig(buildDir, nil, testCase.Blueprint, nil)
|
2021-10-11 21:41:03 +02:00
|
|
|
ctx := android.NewTestContext(config)
|
2021-02-19 06:48:40 +01:00
|
|
|
|
2021-10-11 21:41:03 +02:00
|
|
|
registerCustomModuleForBp2buildConversion(ctx)
|
2020-12-14 14:25:34 +01:00
|
|
|
|
2021-10-11 21:41:03 +02:00
|
|
|
_, errs := ctx.ParseFileList(dir, []string{"Android.bp"})
|
|
|
|
if errored(t, testCase, errs) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
_, errs = ctx.ResolveDependencies(config)
|
|
|
|
if errored(t, testCase, errs) {
|
|
|
|
return
|
|
|
|
}
|
2020-12-14 14:25:34 +01:00
|
|
|
|
2022-11-09 03:14:01 +01:00
|
|
|
codegenCtx := NewCodegenContext(config, ctx.Context, Bp2Build, "")
|
2021-10-11 21:41:03 +02:00
|
|
|
bazelTargets, err := generateBazelTargetsForDir(codegenCtx, dir)
|
|
|
|
android.FailIfErrored(t, err)
|
2021-02-19 06:48:40 +01:00
|
|
|
|
2022-06-21 21:28:33 +02:00
|
|
|
if actualCount, expectedCount := len(bazelTargets), len(testCase.ExpectedBazelTargets); actualCount != expectedCount {
|
|
|
|
t.Errorf("Expected %d bazel target (%s),\ngot %d (%s)", expectedCount, testCase.ExpectedBazelTargets, actualCount, bazelTargets)
|
2021-10-11 21:41:03 +02:00
|
|
|
} else {
|
2022-06-21 21:28:33 +02:00
|
|
|
for i, expectedBazelTarget := range testCase.ExpectedBazelTargets {
|
2021-10-11 21:41:03 +02:00
|
|
|
actualBazelTarget := bazelTargets[i]
|
|
|
|
if actualBazelTarget.content != expectedBazelTarget {
|
|
|
|
t.Errorf(
|
|
|
|
"Expected generated Bazel target to be '%s', got '%s'",
|
|
|
|
expectedBazelTarget,
|
|
|
|
actualBazelTarget.content,
|
|
|
|
)
|
|
|
|
}
|
2021-04-22 00:15:34 +02:00
|
|
|
}
|
2021-01-26 15:18:53 +01:00
|
|
|
}
|
2021-10-11 21:41:03 +02:00
|
|
|
})
|
2020-11-26 01:06:39 +01:00
|
|
|
}
|
|
|
|
}
|
2021-01-21 09:20:18 +01:00
|
|
|
|
2022-05-13 23:20:20 +02:00
|
|
|
func TestBp2buildHostAndDevice(t *testing.T) {
|
2022-06-21 21:28:33 +02:00
|
|
|
testCases := []Bp2buildTestCase{
|
2022-05-13 23:20:20 +02:00
|
|
|
{
|
2022-06-21 21:28:33 +02:00
|
|
|
Description: "host and device, device only",
|
|
|
|
ModuleTypeUnderTest: "custom",
|
|
|
|
ModuleTypeUnderTestFactory: customModuleFactoryHostAndDevice,
|
|
|
|
Blueprint: `custom {
|
2022-05-13 23:20:20 +02:00
|
|
|
name: "foo",
|
|
|
|
bazel_module: { bp2build_available: true },
|
|
|
|
}`,
|
2022-06-21 21:28:33 +02:00
|
|
|
ExpectedBazelTargets: []string{
|
|
|
|
makeBazelTargetHostOrDevice("custom", "foo", AttrNameToString{}, android.DeviceSupported),
|
2022-05-13 23:20:20 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2022-06-21 21:28:33 +02:00
|
|
|
Description: "host and device, both",
|
|
|
|
ModuleTypeUnderTest: "custom",
|
|
|
|
ModuleTypeUnderTestFactory: customModuleFactoryHostAndDevice,
|
|
|
|
Blueprint: `custom {
|
2022-05-13 23:20:20 +02:00
|
|
|
name: "foo",
|
|
|
|
host_supported: true,
|
|
|
|
bazel_module: { bp2build_available: true },
|
|
|
|
}`,
|
2022-06-21 21:28:33 +02:00
|
|
|
ExpectedBazelTargets: []string{
|
|
|
|
MakeBazelTargetNoRestrictions("custom", "foo", AttrNameToString{}),
|
2022-05-13 23:20:20 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2022-06-21 21:28:33 +02:00
|
|
|
Description: "host and device, host explicitly disabled",
|
|
|
|
ModuleTypeUnderTest: "custom",
|
|
|
|
ModuleTypeUnderTestFactory: customModuleFactoryHostAndDevice,
|
|
|
|
Blueprint: `custom {
|
2022-05-13 23:20:20 +02:00
|
|
|
name: "foo",
|
|
|
|
host_supported: false,
|
|
|
|
bazel_module: { bp2build_available: true },
|
|
|
|
}`,
|
2022-06-21 21:28:33 +02:00
|
|
|
ExpectedBazelTargets: []string{
|
|
|
|
makeBazelTargetHostOrDevice("custom", "foo", AttrNameToString{}, android.DeviceSupported),
|
2022-05-13 23:20:20 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2022-06-21 21:28:33 +02:00
|
|
|
Description: "host and device, neither",
|
|
|
|
ModuleTypeUnderTest: "custom",
|
|
|
|
ModuleTypeUnderTestFactory: customModuleFactoryHostAndDevice,
|
|
|
|
Blueprint: `custom {
|
2022-05-13 23:20:20 +02:00
|
|
|
name: "foo",
|
|
|
|
host_supported: false,
|
|
|
|
device_supported: false,
|
|
|
|
bazel_module: { bp2build_available: true },
|
|
|
|
}`,
|
2022-06-21 21:28:33 +02:00
|
|
|
ExpectedBazelTargets: []string{
|
|
|
|
MakeBazelTargetNoRestrictions("custom", "foo", AttrNameToString{
|
2022-05-13 23:20:20 +02:00
|
|
|
"target_compatible_with": `["@platforms//:incompatible"]`,
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2022-06-21 21:28:33 +02:00
|
|
|
Description: "host and device, neither, cannot override with product_var",
|
|
|
|
ModuleTypeUnderTest: "custom",
|
|
|
|
ModuleTypeUnderTestFactory: customModuleFactoryHostAndDevice,
|
|
|
|
Blueprint: `custom {
|
2022-05-13 23:20:20 +02:00
|
|
|
name: "foo",
|
|
|
|
host_supported: false,
|
|
|
|
device_supported: false,
|
|
|
|
product_variables: { unbundled_build: { enabled: true } },
|
|
|
|
bazel_module: { bp2build_available: true },
|
|
|
|
}`,
|
2022-06-21 21:28:33 +02:00
|
|
|
ExpectedBazelTargets: []string{
|
|
|
|
MakeBazelTargetNoRestrictions("custom", "foo", AttrNameToString{
|
2022-05-13 23:20:20 +02:00
|
|
|
"target_compatible_with": `["@platforms//:incompatible"]`,
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2022-06-21 21:28:33 +02:00
|
|
|
Description: "host and device, both, disabled overrided with product_var",
|
|
|
|
ModuleTypeUnderTest: "custom",
|
|
|
|
ModuleTypeUnderTestFactory: customModuleFactoryHostAndDevice,
|
|
|
|
Blueprint: `custom {
|
2022-05-13 23:20:20 +02:00
|
|
|
name: "foo",
|
|
|
|
host_supported: true,
|
|
|
|
device_supported: true,
|
|
|
|
enabled: false,
|
|
|
|
product_variables: { unbundled_build: { enabled: true } },
|
|
|
|
bazel_module: { bp2build_available: true },
|
|
|
|
}`,
|
2022-06-21 21:28:33 +02:00
|
|
|
ExpectedBazelTargets: []string{
|
|
|
|
MakeBazelTargetNoRestrictions("custom", "foo", AttrNameToString{
|
2023-07-31 21:10:12 +02:00
|
|
|
"target_compatible_with": `select({
|
|
|
|
"//build/bazel/product_config/config_settings:unbundled_build": [],
|
|
|
|
"//conditions:default": ["@platforms//:incompatible"],
|
|
|
|
})`,
|
2022-05-13 23:20:20 +02:00
|
|
|
}),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2022-06-21 21:28:33 +02:00
|
|
|
Description: "host and device, neither, cannot override with arch enabled",
|
|
|
|
ModuleTypeUnderTest: "custom",
|
|
|
|
ModuleTypeUnderTestFactory: customModuleFactoryHostAndDevice,
|
|
|
|
Blueprint: `custom {
|
2022-05-13 23:20:20 +02:00
|
|
|
name: "foo",
|
|
|
|
host_supported: false,
|
|
|
|
device_supported: false,
|
|
|
|
arch: { x86: { enabled: true } },
|
|
|
|
bazel_module: { bp2build_available: true },
|
|
|
|
}`,
|
2022-06-21 21:28:33 +02:00
|
|
|
ExpectedBazelTargets: []string{
|
|
|
|
MakeBazelTargetNoRestrictions("custom", "foo", AttrNameToString{
|
2022-05-13 23:20:20 +02:00
|
|
|
"target_compatible_with": `["@platforms//:incompatible"]`,
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2022-06-21 21:28:33 +02:00
|
|
|
Description: "host and device, host only",
|
|
|
|
ModuleTypeUnderTest: "custom",
|
|
|
|
ModuleTypeUnderTestFactory: customModuleFactoryHostAndDevice,
|
|
|
|
Blueprint: `custom {
|
2022-05-13 23:20:20 +02:00
|
|
|
name: "foo",
|
|
|
|
host_supported: true,
|
|
|
|
device_supported: false,
|
|
|
|
bazel_module: { bp2build_available: true },
|
|
|
|
}`,
|
2022-06-21 21:28:33 +02:00
|
|
|
ExpectedBazelTargets: []string{
|
|
|
|
makeBazelTargetHostOrDevice("custom", "foo", AttrNameToString{}, android.HostSupported),
|
2022-05-13 23:20:20 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2022-06-21 21:28:33 +02:00
|
|
|
Description: "host only",
|
|
|
|
ModuleTypeUnderTest: "custom",
|
|
|
|
ModuleTypeUnderTestFactory: customModuleFactoryHostSupported,
|
|
|
|
Blueprint: `custom {
|
2022-05-13 23:20:20 +02:00
|
|
|
name: "foo",
|
|
|
|
bazel_module: { bp2build_available: true },
|
|
|
|
}`,
|
2022-06-21 21:28:33 +02:00
|
|
|
ExpectedBazelTargets: []string{
|
|
|
|
makeBazelTargetHostOrDevice("custom", "foo", AttrNameToString{}, android.HostSupported),
|
2022-05-13 23:20:20 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2022-06-21 21:28:33 +02:00
|
|
|
Description: "device only",
|
|
|
|
ModuleTypeUnderTest: "custom",
|
|
|
|
ModuleTypeUnderTestFactory: customModuleFactoryDeviceSupported,
|
|
|
|
Blueprint: `custom {
|
2022-05-13 23:20:20 +02:00
|
|
|
name: "foo",
|
|
|
|
bazel_module: { bp2build_available: true },
|
|
|
|
}`,
|
2022-06-21 21:28:33 +02:00
|
|
|
ExpectedBazelTargets: []string{
|
|
|
|
makeBazelTargetHostOrDevice("custom", "foo", AttrNameToString{}, android.DeviceSupported),
|
2022-05-13 23:20:20 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2022-06-21 21:28:33 +02:00
|
|
|
Description: "host and device default, default",
|
|
|
|
ModuleTypeUnderTest: "custom",
|
|
|
|
ModuleTypeUnderTestFactory: customModuleFactoryHostAndDeviceDefault,
|
|
|
|
Blueprint: `custom {
|
2022-05-13 23:20:20 +02:00
|
|
|
name: "foo",
|
|
|
|
bazel_module: { bp2build_available: true },
|
|
|
|
}`,
|
2022-06-21 21:28:33 +02:00
|
|
|
ExpectedBazelTargets: []string{
|
|
|
|
MakeBazelTargetNoRestrictions("custom", "foo", AttrNameToString{}),
|
2022-05-13 23:20:20 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2022-06-21 21:28:33 +02:00
|
|
|
Description: "host and device default, device only",
|
|
|
|
ModuleTypeUnderTest: "custom",
|
|
|
|
ModuleTypeUnderTestFactory: customModuleFactoryHostAndDeviceDefault,
|
|
|
|
Blueprint: `custom {
|
2022-05-13 23:20:20 +02:00
|
|
|
name: "foo",
|
|
|
|
host_supported: false,
|
|
|
|
bazel_module: { bp2build_available: true },
|
|
|
|
}`,
|
2022-06-21 21:28:33 +02:00
|
|
|
ExpectedBazelTargets: []string{
|
|
|
|
makeBazelTargetHostOrDevice("custom", "foo", AttrNameToString{}, android.DeviceSupported),
|
2022-05-13 23:20:20 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2022-06-21 21:28:33 +02:00
|
|
|
Description: "host and device default, host only",
|
|
|
|
ModuleTypeUnderTest: "custom",
|
|
|
|
ModuleTypeUnderTestFactory: customModuleFactoryHostAndDeviceDefault,
|
|
|
|
Blueprint: `custom {
|
2022-05-13 23:20:20 +02:00
|
|
|
name: "foo",
|
|
|
|
device_supported: false,
|
|
|
|
bazel_module: { bp2build_available: true },
|
|
|
|
}`,
|
2022-06-21 21:28:33 +02:00
|
|
|
ExpectedBazelTargets: []string{
|
|
|
|
makeBazelTargetHostOrDevice("custom", "foo", AttrNameToString{}, android.HostSupported),
|
2022-05-13 23:20:20 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2022-06-21 21:28:33 +02:00
|
|
|
Description: "host and device default, neither",
|
|
|
|
ModuleTypeUnderTest: "custom",
|
|
|
|
ModuleTypeUnderTestFactory: customModuleFactoryHostAndDeviceDefault,
|
|
|
|
Blueprint: `custom {
|
2022-05-13 23:20:20 +02:00
|
|
|
name: "foo",
|
|
|
|
host_supported: false,
|
|
|
|
device_supported: false,
|
|
|
|
bazel_module: { bp2build_available: true },
|
|
|
|
}`,
|
2022-06-21 21:28:33 +02:00
|
|
|
ExpectedBazelTargets: []string{
|
|
|
|
MakeBazelTargetNoRestrictions("custom", "foo", AttrNameToString{
|
2022-05-13 23:20:20 +02:00
|
|
|
"target_compatible_with": `["@platforms//:incompatible"]`,
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
2022-06-21 21:28:33 +02:00
|
|
|
t.Run(tc.Description, func(t *testing.T) {
|
2022-09-01 20:57:01 +02:00
|
|
|
RunBp2BuildTestCaseSimple(t, tc)
|
2022-05-13 23:20:20 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-27 03:58:43 +01:00
|
|
|
func TestLoadStatements(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
bazelTargets BazelTargets
|
|
|
|
expectedLoadStatements string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
bazelTargets: BazelTargets{
|
|
|
|
BazelTarget{
|
2023-09-15 00:16:58 +02:00
|
|
|
name: "foo",
|
|
|
|
ruleClass: "cc_library",
|
|
|
|
loads: []BazelLoad{{
|
|
|
|
file: "//build/bazel/rules:cc.bzl",
|
|
|
|
symbols: []BazelLoadSymbol{{symbol: "cc_library"}},
|
|
|
|
}},
|
2021-01-27 03:58:43 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_library")`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
bazelTargets: BazelTargets{
|
|
|
|
BazelTarget{
|
2023-09-15 00:16:58 +02:00
|
|
|
name: "foo",
|
|
|
|
ruleClass: "cc_library",
|
|
|
|
loads: []BazelLoad{{
|
|
|
|
file: "//build/bazel/rules:cc.bzl",
|
|
|
|
symbols: []BazelLoadSymbol{{symbol: "cc_library"}},
|
|
|
|
}},
|
2021-01-27 03:58:43 +01:00
|
|
|
},
|
|
|
|
BazelTarget{
|
2023-09-15 00:16:58 +02:00
|
|
|
name: "bar",
|
|
|
|
ruleClass: "cc_library",
|
|
|
|
loads: []BazelLoad{{
|
|
|
|
file: "//build/bazel/rules:cc.bzl",
|
|
|
|
symbols: []BazelLoadSymbol{{symbol: "cc_library"}},
|
|
|
|
}},
|
2021-01-27 03:58:43 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_library")`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
bazelTargets: BazelTargets{
|
|
|
|
BazelTarget{
|
2023-09-15 00:16:58 +02:00
|
|
|
name: "foo",
|
|
|
|
ruleClass: "cc_library",
|
|
|
|
loads: []BazelLoad{{
|
|
|
|
file: "//build/bazel/rules:cc.bzl",
|
|
|
|
symbols: []BazelLoadSymbol{{symbol: "cc_library"}},
|
|
|
|
}},
|
2021-01-27 03:58:43 +01:00
|
|
|
},
|
|
|
|
BazelTarget{
|
2023-09-15 00:16:58 +02:00
|
|
|
name: "bar",
|
|
|
|
ruleClass: "cc_binary",
|
|
|
|
loads: []BazelLoad{{
|
|
|
|
file: "//build/bazel/rules:cc.bzl",
|
|
|
|
symbols: []BazelLoadSymbol{{symbol: "cc_binary"}},
|
|
|
|
}},
|
2021-01-27 03:58:43 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_binary", "cc_library")`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
bazelTargets: BazelTargets{
|
|
|
|
BazelTarget{
|
2023-09-15 00:16:58 +02:00
|
|
|
name: "foo",
|
|
|
|
ruleClass: "cc_library",
|
|
|
|
loads: []BazelLoad{{
|
|
|
|
file: "//build/bazel/rules:cc.bzl",
|
|
|
|
symbols: []BazelLoadSymbol{{symbol: "cc_library"}},
|
|
|
|
}},
|
2021-01-27 03:58:43 +01:00
|
|
|
},
|
|
|
|
BazelTarget{
|
2023-09-15 00:16:58 +02:00
|
|
|
name: "bar",
|
|
|
|
ruleClass: "cc_binary",
|
|
|
|
loads: []BazelLoad{{
|
|
|
|
file: "//build/bazel/rules:cc.bzl",
|
|
|
|
symbols: []BazelLoadSymbol{{symbol: "cc_binary"}},
|
|
|
|
}},
|
2021-01-27 03:58:43 +01:00
|
|
|
},
|
|
|
|
BazelTarget{
|
2023-09-15 00:16:58 +02:00
|
|
|
name: "baz",
|
|
|
|
ruleClass: "java_binary",
|
|
|
|
loads: []BazelLoad{{
|
|
|
|
file: "//build/bazel/rules:java.bzl",
|
|
|
|
symbols: []BazelLoadSymbol{{symbol: "java_binary"}},
|
|
|
|
}},
|
2021-01-27 03:58:43 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_binary", "cc_library")
|
|
|
|
load("//build/bazel/rules:java.bzl", "java_binary")`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
bazelTargets: BazelTargets{
|
|
|
|
BazelTarget{
|
2023-09-15 00:16:58 +02:00
|
|
|
name: "foo",
|
|
|
|
ruleClass: "cc_binary",
|
|
|
|
loads: []BazelLoad{{
|
|
|
|
file: "//build/bazel/rules:cc.bzl",
|
|
|
|
symbols: []BazelLoadSymbol{{symbol: "cc_binary"}},
|
|
|
|
}},
|
2021-01-27 03:58:43 +01:00
|
|
|
},
|
|
|
|
BazelTarget{
|
2023-09-15 00:16:58 +02:00
|
|
|
name: "bar",
|
|
|
|
ruleClass: "java_binary",
|
|
|
|
loads: []BazelLoad{{
|
|
|
|
file: "//build/bazel/rules:java.bzl",
|
|
|
|
symbols: []BazelLoadSymbol{{symbol: "java_binary"}},
|
|
|
|
}},
|
2021-01-27 03:58:43 +01:00
|
|
|
},
|
|
|
|
BazelTarget{
|
|
|
|
name: "baz",
|
|
|
|
ruleClass: "genrule",
|
2023-09-15 00:16:58 +02:00
|
|
|
// Note: no loads for native rules
|
2021-01-27 03:58:43 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_binary")
|
|
|
|
load("//build/bazel/rules:java.bzl", "java_binary")`,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, testCase := range testCases {
|
|
|
|
actual := testCase.bazelTargets.LoadStatements()
|
|
|
|
expected := testCase.expectedLoadStatements
|
|
|
|
if actual != expected {
|
|
|
|
t.Fatalf("Expected load statements to be %s, got %s", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGenerateBazelTargetModules_OneToMany_LoadedFromStarlark(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
bp string
|
|
|
|
expectedBazelTarget string
|
|
|
|
expectedBazelTargetCount int
|
|
|
|
expectedLoadStatements string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
bp: `custom {
|
|
|
|
name: "bar",
|
2022-05-13 23:20:20 +02:00
|
|
|
host_supported: true,
|
2021-11-01 20:32:43 +01:00
|
|
|
one_to_many_prop: true,
|
2021-02-05 09:03:24 +01:00
|
|
|
bazel_module: { bp2build_available: true },
|
2021-01-27 03:58:43 +01:00
|
|
|
}`,
|
|
|
|
expectedBazelTarget: `my_library(
|
|
|
|
name = "bar",
|
|
|
|
)
|
|
|
|
|
|
|
|
proto_library(
|
|
|
|
name = "bar_proto_library_deps",
|
2021-08-11 06:17:36 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
my_proto_library(
|
|
|
|
name = "bar_my_proto_library_deps",
|
2021-01-27 03:58:43 +01:00
|
|
|
)`,
|
|
|
|
expectedBazelTargetCount: 3,
|
|
|
|
expectedLoadStatements: `load("//build/bazel/rules:proto.bzl", "my_proto_library", "proto_library")
|
|
|
|
load("//build/bazel/rules:rules.bzl", "my_library")`,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
dir := "."
|
|
|
|
for _, testCase := range testCases {
|
|
|
|
config := android.TestConfig(buildDir, nil, testCase.bp, nil)
|
|
|
|
ctx := android.NewTestContext(config)
|
2022-05-13 23:20:20 +02:00
|
|
|
ctx.RegisterModuleType("custom", customModuleFactoryHostAndDevice)
|
2021-01-27 03:58:43 +01:00
|
|
|
ctx.RegisterForBazelConversion()
|
|
|
|
|
|
|
|
_, errs := ctx.ParseFileList(dir, []string{"Android.bp"})
|
|
|
|
android.FailIfErrored(t, errs)
|
|
|
|
_, errs = ctx.ResolveDependencies(config)
|
|
|
|
android.FailIfErrored(t, errs)
|
|
|
|
|
2022-11-09 03:14:01 +01:00
|
|
|
codegenCtx := NewCodegenContext(config, ctx.Context, Bp2Build, "")
|
2021-08-26 14:37:59 +02:00
|
|
|
bazelTargets, err := generateBazelTargetsForDir(codegenCtx, dir)
|
|
|
|
android.FailIfErrored(t, err)
|
2021-01-27 03:58:43 +01:00
|
|
|
if actualCount := len(bazelTargets); actualCount != testCase.expectedBazelTargetCount {
|
|
|
|
t.Fatalf("Expected %d bazel target, got %d", testCase.expectedBazelTargetCount, actualCount)
|
|
|
|
}
|
|
|
|
|
|
|
|
actualBazelTargets := bazelTargets.String()
|
|
|
|
if actualBazelTargets != testCase.expectedBazelTarget {
|
|
|
|
t.Errorf(
|
|
|
|
"Expected generated Bazel target to be '%s', got '%s'",
|
|
|
|
testCase.expectedBazelTarget,
|
|
|
|
actualBazelTargets,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
actualLoadStatements := bazelTargets.LoadStatements()
|
|
|
|
if actualLoadStatements != testCase.expectedLoadStatements {
|
|
|
|
t.Errorf(
|
|
|
|
"Expected generated load statements to be '%s', got '%s'",
|
|
|
|
testCase.expectedLoadStatements,
|
|
|
|
actualLoadStatements,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-21 09:20:18 +01:00
|
|
|
func TestModuleTypeBp2Build(t *testing.T) {
|
2022-06-21 21:28:33 +02:00
|
|
|
testCases := []Bp2buildTestCase{
|
2021-02-16 21:00:05 +01:00
|
|
|
{
|
2022-06-21 21:28:33 +02:00
|
|
|
Description: "filegroup with does not specify srcs",
|
|
|
|
ModuleTypeUnderTest: "filegroup",
|
|
|
|
ModuleTypeUnderTestFactory: android.FileGroupFactory,
|
|
|
|
Blueprint: `filegroup {
|
2021-02-16 21:00:05 +01:00
|
|
|
name: "fg_foo",
|
|
|
|
bazel_module: { bp2build_available: true },
|
|
|
|
}`,
|
2022-06-21 21:28:33 +02:00
|
|
|
ExpectedBazelTargets: []string{
|
|
|
|
MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{}),
|
2021-02-16 21:00:05 +01:00
|
|
|
},
|
|
|
|
},
|
2021-01-21 09:20:18 +01:00
|
|
|
{
|
2022-06-21 21:28:33 +02:00
|
|
|
Description: "filegroup with no srcs",
|
|
|
|
ModuleTypeUnderTest: "filegroup",
|
|
|
|
ModuleTypeUnderTestFactory: android.FileGroupFactory,
|
|
|
|
Blueprint: `filegroup {
|
2021-01-26 15:18:53 +01:00
|
|
|
name: "fg_foo",
|
|
|
|
srcs: [],
|
2021-02-05 09:03:24 +01:00
|
|
|
bazel_module: { bp2build_available: true },
|
2021-01-21 09:20:18 +01:00
|
|
|
}`,
|
2022-06-21 21:28:33 +02:00
|
|
|
ExpectedBazelTargets: []string{
|
|
|
|
MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{}),
|
2021-01-26 15:18:53 +01:00
|
|
|
},
|
2021-01-21 09:20:18 +01:00
|
|
|
},
|
|
|
|
{
|
2022-06-21 21:28:33 +02:00
|
|
|
Description: "filegroup with srcs",
|
|
|
|
ModuleTypeUnderTest: "filegroup",
|
|
|
|
ModuleTypeUnderTestFactory: android.FileGroupFactory,
|
|
|
|
Blueprint: `filegroup {
|
2021-01-26 15:18:53 +01:00
|
|
|
name: "fg_foo",
|
|
|
|
srcs: ["a", "b"],
|
2021-02-05 09:03:24 +01:00
|
|
|
bazel_module: { bp2build_available: true },
|
2022-09-23 22:46:38 +02:00
|
|
|
}`,
|
|
|
|
ExpectedBazelTargets: []string{
|
|
|
|
MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{
|
|
|
|
"srcs": `[
|
|
|
|
"a",
|
|
|
|
"b",
|
|
|
|
]`,
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Description: "filegroup with dot-slash-prefixed srcs",
|
|
|
|
ModuleTypeUnderTest: "filegroup",
|
|
|
|
ModuleTypeUnderTestFactory: android.FileGroupFactory,
|
|
|
|
Blueprint: `filegroup {
|
|
|
|
name: "fg_foo",
|
|
|
|
srcs: ["./a", "./b"],
|
|
|
|
bazel_module: { bp2build_available: true },
|
2021-01-21 09:20:18 +01:00
|
|
|
}`,
|
2022-06-21 21:28:33 +02:00
|
|
|
ExpectedBazelTargets: []string{
|
|
|
|
MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{
|
2021-11-08 18:56:31 +01:00
|
|
|
"srcs": `[
|
2021-01-21 09:20:18 +01:00
|
|
|
"a",
|
|
|
|
"b",
|
2021-11-08 18:56:31 +01:00
|
|
|
]`,
|
|
|
|
}),
|
2021-01-26 15:18:53 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2022-06-21 21:28:33 +02:00
|
|
|
Description: "filegroup with excludes srcs",
|
|
|
|
ModuleTypeUnderTest: "filegroup",
|
|
|
|
ModuleTypeUnderTestFactory: android.FileGroupFactory,
|
|
|
|
Blueprint: `filegroup {
|
2021-01-26 15:18:53 +01:00
|
|
|
name: "fg_foo",
|
|
|
|
srcs: ["a", "b"],
|
|
|
|
exclude_srcs: ["a"],
|
2021-02-05 09:03:24 +01:00
|
|
|
bazel_module: { bp2build_available: true },
|
2021-01-26 15:18:53 +01:00
|
|
|
}`,
|
2022-06-21 21:28:33 +02:00
|
|
|
ExpectedBazelTargets: []string{
|
|
|
|
MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{
|
2021-11-08 18:56:31 +01:00
|
|
|
"srcs": `["b"]`,
|
|
|
|
}),
|
2021-01-26 15:18:53 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2022-06-21 21:28:33 +02:00
|
|
|
Description: "depends_on_other_dir_module",
|
|
|
|
ModuleTypeUnderTest: "filegroup",
|
|
|
|
ModuleTypeUnderTestFactory: android.FileGroupFactory,
|
|
|
|
Blueprint: `filegroup {
|
2021-11-08 18:56:31 +01:00
|
|
|
name: "fg_foo",
|
2021-01-26 15:18:53 +01:00
|
|
|
srcs: [
|
2021-04-08 16:40:57 +02:00
|
|
|
":foo",
|
2021-01-26 15:18:53 +01:00
|
|
|
"c",
|
|
|
|
],
|
2021-02-05 09:03:24 +01:00
|
|
|
bazel_module: { bp2build_available: true },
|
2021-01-26 15:18:53 +01:00
|
|
|
}`,
|
2022-06-21 21:28:33 +02:00
|
|
|
Filesystem: map[string]string{
|
2021-01-26 15:18:53 +01:00
|
|
|
"other/Android.bp": `filegroup {
|
|
|
|
name: "foo",
|
|
|
|
srcs: ["a", "b"],
|
2021-08-26 14:37:59 +02:00
|
|
|
bazel_module: { bp2build_available: true },
|
|
|
|
}`,
|
|
|
|
},
|
2022-06-21 21:28:33 +02:00
|
|
|
ExpectedBazelTargets: []string{
|
|
|
|
MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{
|
2021-11-08 18:56:31 +01:00
|
|
|
"srcs": `[
|
|
|
|
"//other:foo",
|
|
|
|
"c",
|
|
|
|
]`,
|
|
|
|
}),
|
|
|
|
},
|
2021-08-26 14:37:59 +02:00
|
|
|
},
|
2021-01-21 09:20:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, testCase := range testCases {
|
2022-06-21 21:28:33 +02:00
|
|
|
t.Run(testCase.Description, func(t *testing.T) {
|
|
|
|
RunBp2BuildTestCase(t, func(ctx android.RegistrationContext) {}, testCase)
|
2021-08-26 14:37:59 +02:00
|
|
|
})
|
2021-01-26 15:18:53 +01:00
|
|
|
}
|
|
|
|
}
|
2021-01-21 09:20:18 +01:00
|
|
|
|
2021-03-10 08:05:59 +01:00
|
|
|
func TestAllowlistingBp2buildTargetsExplicitly(t *testing.T) {
|
2021-02-05 09:03:24 +01:00
|
|
|
testCases := []struct {
|
2021-11-01 20:32:43 +01:00
|
|
|
moduleTypeUnderTest string
|
|
|
|
moduleTypeUnderTestFactory android.ModuleFactory
|
|
|
|
bp string
|
|
|
|
expectedCount int
|
|
|
|
description string
|
2021-02-05 09:03:24 +01:00
|
|
|
}{
|
|
|
|
{
|
2021-11-01 20:32:43 +01:00
|
|
|
description: "explicitly unavailable",
|
|
|
|
moduleTypeUnderTest: "filegroup",
|
|
|
|
moduleTypeUnderTestFactory: android.FileGroupFactory,
|
2021-02-05 09:03:24 +01:00
|
|
|
bp: `filegroup {
|
|
|
|
name: "foo",
|
|
|
|
srcs: ["a", "b"],
|
|
|
|
bazel_module: { bp2build_available: false },
|
|
|
|
}`,
|
|
|
|
expectedCount: 0,
|
|
|
|
},
|
|
|
|
{
|
2021-11-01 20:32:43 +01:00
|
|
|
description: "implicitly unavailable",
|
|
|
|
moduleTypeUnderTest: "filegroup",
|
|
|
|
moduleTypeUnderTestFactory: android.FileGroupFactory,
|
2021-02-05 09:03:24 +01:00
|
|
|
bp: `filegroup {
|
|
|
|
name: "foo",
|
|
|
|
srcs: ["a", "b"],
|
|
|
|
}`,
|
|
|
|
expectedCount: 0,
|
|
|
|
},
|
|
|
|
{
|
2021-11-01 20:32:43 +01:00
|
|
|
description: "explicitly available",
|
|
|
|
moduleTypeUnderTest: "filegroup",
|
|
|
|
moduleTypeUnderTestFactory: android.FileGroupFactory,
|
2021-02-05 09:03:24 +01:00
|
|
|
bp: `filegroup {
|
|
|
|
name: "foo",
|
|
|
|
srcs: ["a", "b"],
|
|
|
|
bazel_module: { bp2build_available: true },
|
|
|
|
}`,
|
|
|
|
expectedCount: 1,
|
|
|
|
},
|
|
|
|
{
|
2021-11-01 20:32:43 +01:00
|
|
|
description: "generates more than 1 target if needed",
|
|
|
|
moduleTypeUnderTest: "custom",
|
2022-05-13 23:20:20 +02:00
|
|
|
moduleTypeUnderTestFactory: customModuleFactoryHostAndDevice,
|
2021-02-05 09:03:24 +01:00
|
|
|
bp: `custom {
|
|
|
|
name: "foo",
|
2021-11-01 20:32:43 +01:00
|
|
|
one_to_many_prop: true,
|
2021-02-05 09:03:24 +01:00
|
|
|
bazel_module: { bp2build_available: true },
|
|
|
|
}`,
|
|
|
|
expectedCount: 3,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
dir := "."
|
|
|
|
for _, testCase := range testCases {
|
2021-08-11 06:17:36 +02:00
|
|
|
t.Run(testCase.description, func(t *testing.T) {
|
|
|
|
config := android.TestConfig(buildDir, nil, testCase.bp, nil)
|
|
|
|
ctx := android.NewTestContext(config)
|
|
|
|
ctx.RegisterModuleType(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestFactory)
|
|
|
|
ctx.RegisterForBazelConversion()
|
2021-02-05 09:03:24 +01:00
|
|
|
|
2021-08-11 06:17:36 +02:00
|
|
|
_, errs := ctx.ParseFileList(dir, []string{"Android.bp"})
|
|
|
|
android.FailIfErrored(t, errs)
|
|
|
|
_, errs = ctx.ResolveDependencies(config)
|
|
|
|
android.FailIfErrored(t, errs)
|
2021-02-05 09:03:24 +01:00
|
|
|
|
2022-11-09 03:14:01 +01:00
|
|
|
codegenCtx := NewCodegenContext(config, ctx.Context, Bp2Build, "")
|
2021-08-26 14:37:59 +02:00
|
|
|
bazelTargets, err := generateBazelTargetsForDir(codegenCtx, dir)
|
|
|
|
android.FailIfErrored(t, err)
|
2021-08-11 06:17:36 +02:00
|
|
|
if actualCount := len(bazelTargets); actualCount != testCase.expectedCount {
|
|
|
|
t.Fatalf("%s: Expected %d bazel target, got %d", testCase.description, testCase.expectedCount, actualCount)
|
|
|
|
}
|
|
|
|
})
|
2021-02-05 09:03:24 +01:00
|
|
|
}
|
|
|
|
}
|
2021-02-17 19:22:03 +01:00
|
|
|
|
2021-03-10 08:05:59 +01:00
|
|
|
func TestAllowlistingBp2buildTargetsWithConfig(t *testing.T) {
|
|
|
|
testCases := []struct {
|
2021-11-01 20:32:43 +01:00
|
|
|
moduleTypeUnderTest string
|
|
|
|
moduleTypeUnderTestFactory android.ModuleFactory
|
|
|
|
expectedCount map[string]int
|
|
|
|
description string
|
2022-03-28 21:53:03 +02:00
|
|
|
bp2buildConfig allowlists.Bp2BuildConfig
|
2021-11-01 20:32:43 +01:00
|
|
|
checkDir string
|
|
|
|
fs map[string]string
|
2023-03-16 20:15:19 +01:00
|
|
|
forceEnabledModules []string
|
|
|
|
expectedErrorMessages []string
|
2021-03-10 08:05:59 +01:00
|
|
|
}{
|
|
|
|
{
|
2021-11-01 20:32:43 +01:00
|
|
|
description: "test bp2build config package and subpackages config",
|
|
|
|
moduleTypeUnderTest: "filegroup",
|
|
|
|
moduleTypeUnderTestFactory: android.FileGroupFactory,
|
2021-03-10 08:05:59 +01:00
|
|
|
expectedCount: map[string]int{
|
|
|
|
"migrated": 1,
|
|
|
|
"migrated/but_not_really": 0,
|
|
|
|
"migrated/but_not_really/but_really": 1,
|
|
|
|
"not_migrated": 0,
|
|
|
|
"also_not_migrated": 0,
|
|
|
|
},
|
2022-03-28 21:53:03 +02:00
|
|
|
bp2buildConfig: allowlists.Bp2BuildConfig{
|
|
|
|
"migrated": allowlists.Bp2BuildDefaultTrueRecursively,
|
|
|
|
"migrated/but_not_really": allowlists.Bp2BuildDefaultFalse,
|
|
|
|
"not_migrated": allowlists.Bp2BuildDefaultFalse,
|
2021-03-10 08:05:59 +01:00
|
|
|
},
|
|
|
|
fs: map[string]string{
|
|
|
|
"migrated/Android.bp": `filegroup { name: "a" }`,
|
|
|
|
"migrated/but_not_really/Android.bp": `filegroup { name: "b" }`,
|
|
|
|
"migrated/but_not_really/but_really/Android.bp": `filegroup { name: "c" }`,
|
|
|
|
"not_migrated/Android.bp": `filegroup { name: "d" }`,
|
|
|
|
"also_not_migrated/Android.bp": `filegroup { name: "e" }`,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2021-11-01 20:32:43 +01:00
|
|
|
description: "test bp2build config opt-in and opt-out",
|
|
|
|
moduleTypeUnderTest: "filegroup",
|
|
|
|
moduleTypeUnderTestFactory: android.FileGroupFactory,
|
2021-03-10 08:05:59 +01:00
|
|
|
expectedCount: map[string]int{
|
|
|
|
"package-opt-in": 2,
|
|
|
|
"package-opt-in/subpackage": 0,
|
|
|
|
"package-opt-out": 1,
|
|
|
|
"package-opt-out/subpackage": 0,
|
|
|
|
},
|
2022-03-28 21:53:03 +02:00
|
|
|
bp2buildConfig: allowlists.Bp2BuildConfig{
|
|
|
|
"package-opt-in": allowlists.Bp2BuildDefaultFalse,
|
|
|
|
"package-opt-out": allowlists.Bp2BuildDefaultTrueRecursively,
|
2021-03-10 08:05:59 +01:00
|
|
|
},
|
|
|
|
fs: map[string]string{
|
|
|
|
"package-opt-in/Android.bp": `
|
|
|
|
filegroup { name: "opt-in-a" }
|
|
|
|
filegroup { name: "opt-in-b", bazel_module: { bp2build_available: true } }
|
|
|
|
filegroup { name: "opt-in-c", bazel_module: { bp2build_available: true } }
|
|
|
|
`,
|
|
|
|
|
|
|
|
"package-opt-in/subpackage/Android.bp": `
|
|
|
|
filegroup { name: "opt-in-d" } // parent package not configured to DefaultTrueRecursively
|
|
|
|
`,
|
|
|
|
|
|
|
|
"package-opt-out/Android.bp": `
|
|
|
|
filegroup { name: "opt-out-a" }
|
|
|
|
filegroup { name: "opt-out-b", bazel_module: { bp2build_available: false } }
|
|
|
|
filegroup { name: "opt-out-c", bazel_module: { bp2build_available: false } }
|
|
|
|
`,
|
|
|
|
|
|
|
|
"package-opt-out/subpackage/Android.bp": `
|
|
|
|
filegroup { name: "opt-out-g", bazel_module: { bp2build_available: false } }
|
|
|
|
filegroup { name: "opt-out-h", bazel_module: { bp2build_available: false } }
|
|
|
|
`,
|
|
|
|
},
|
|
|
|
},
|
2023-03-16 20:15:19 +01:00
|
|
|
{
|
|
|
|
description: "test force-enabled errors out",
|
|
|
|
moduleTypeUnderTest: "filegroup",
|
|
|
|
moduleTypeUnderTestFactory: android.FileGroupFactory,
|
|
|
|
expectedCount: map[string]int{
|
|
|
|
"migrated": 0,
|
|
|
|
"not_migrated": 0,
|
|
|
|
},
|
|
|
|
bp2buildConfig: allowlists.Bp2BuildConfig{
|
|
|
|
"migrated/but_not_really": allowlists.Bp2BuildDefaultFalse,
|
|
|
|
"not_migrated": allowlists.Bp2BuildDefaultFalse,
|
|
|
|
},
|
|
|
|
fs: map[string]string{
|
|
|
|
"migrated/Android.bp": `filegroup { name: "a" }`,
|
|
|
|
},
|
|
|
|
forceEnabledModules: []string{"a"},
|
|
|
|
expectedErrorMessages: []string{"Force Enabled Module a not converted"},
|
|
|
|
},
|
2021-03-10 08:05:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
dir := "."
|
|
|
|
for _, testCase := range testCases {
|
|
|
|
fs := make(map[string][]byte)
|
|
|
|
toParse := []string{
|
|
|
|
"Android.bp",
|
|
|
|
}
|
|
|
|
for f, content := range testCase.fs {
|
|
|
|
if strings.HasSuffix(f, "Android.bp") {
|
|
|
|
toParse = append(toParse, f)
|
|
|
|
}
|
|
|
|
fs[f] = []byte(content)
|
|
|
|
}
|
|
|
|
config := android.TestConfig(buildDir, nil, "", fs)
|
2023-03-16 20:15:19 +01:00
|
|
|
config.AddForceEnabledModules(testCase.forceEnabledModules)
|
2021-03-10 08:05:59 +01:00
|
|
|
ctx := android.NewTestContext(config)
|
|
|
|
ctx.RegisterModuleType(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestFactory)
|
2022-03-28 21:53:03 +02:00
|
|
|
allowlist := android.NewBp2BuildAllowlist().SetDefaultConfig(testCase.bp2buildConfig)
|
|
|
|
ctx.RegisterBp2BuildConfig(allowlist)
|
2021-03-10 08:05:59 +01:00
|
|
|
ctx.RegisterForBazelConversion()
|
|
|
|
|
|
|
|
_, errs := ctx.ParseFileList(dir, toParse)
|
|
|
|
android.FailIfErrored(t, errs)
|
|
|
|
_, errs = ctx.ResolveDependencies(config)
|
|
|
|
android.FailIfErrored(t, errs)
|
|
|
|
|
2022-11-09 03:14:01 +01:00
|
|
|
codegenCtx := NewCodegenContext(config, ctx.Context, Bp2Build, "")
|
2021-03-10 08:05:59 +01:00
|
|
|
|
|
|
|
// For each directory, test that the expected number of generated targets is correct.
|
|
|
|
for dir, expectedCount := range testCase.expectedCount {
|
2021-08-26 14:37:59 +02:00
|
|
|
bazelTargets, err := generateBazelTargetsForDir(codegenCtx, dir)
|
2023-03-16 20:15:19 +01:00
|
|
|
android.CheckErrorsAgainstExpectations(t, err, testCase.expectedErrorMessages)
|
2021-03-10 08:05:59 +01:00
|
|
|
if actualCount := len(bazelTargets); actualCount != expectedCount {
|
|
|
|
t.Fatalf(
|
|
|
|
"%s: Expected %d bazel target for %s package, got %d",
|
|
|
|
testCase.description,
|
|
|
|
expectedCount,
|
|
|
|
dir,
|
|
|
|
actualCount)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-17 19:22:03 +01:00
|
|
|
func TestCombineBuildFilesBp2buildTargets(t *testing.T) {
|
2022-06-21 21:28:33 +02:00
|
|
|
testCases := []Bp2buildTestCase{
|
2021-02-17 19:22:03 +01:00
|
|
|
{
|
2022-06-21 21:28:33 +02:00
|
|
|
Description: "filegroup bazel_module.label",
|
|
|
|
ModuleTypeUnderTest: "filegroup",
|
|
|
|
ModuleTypeUnderTestFactory: android.FileGroupFactory,
|
|
|
|
Blueprint: `filegroup {
|
2021-02-17 19:22:03 +01:00
|
|
|
name: "fg_foo",
|
|
|
|
bazel_module: { label: "//other:fg_foo" },
|
|
|
|
}`,
|
2022-08-31 23:48:26 +02:00
|
|
|
ExpectedBazelTargets: []string{},
|
2022-06-21 21:28:33 +02:00
|
|
|
Filesystem: map[string]string{
|
2021-02-17 19:22:03 +01:00
|
|
|
"other/BUILD.bazel": `// BUILD file`,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2022-06-21 21:28:33 +02:00
|
|
|
Description: "multiple bazel_module.label same BUILD",
|
|
|
|
ModuleTypeUnderTest: "filegroup",
|
|
|
|
ModuleTypeUnderTestFactory: android.FileGroupFactory,
|
|
|
|
Blueprint: `filegroup {
|
2021-11-01 20:32:43 +01:00
|
|
|
name: "fg_foo",
|
|
|
|
bazel_module: { label: "//other:fg_foo" },
|
|
|
|
}
|
2021-02-17 19:22:03 +01:00
|
|
|
|
2021-11-01 20:32:43 +01:00
|
|
|
filegroup {
|
|
|
|
name: "foo",
|
|
|
|
bazel_module: { label: "//other:foo" },
|
|
|
|
}`,
|
2022-08-31 23:48:26 +02:00
|
|
|
ExpectedBazelTargets: []string{},
|
2022-06-21 21:28:33 +02:00
|
|
|
Filesystem: map[string]string{
|
2021-02-17 19:22:03 +01:00
|
|
|
"other/BUILD.bazel": `// BUILD file`,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2022-06-21 21:28:33 +02:00
|
|
|
Description: "filegroup bazel_module.label and bp2build in subdir",
|
|
|
|
ModuleTypeUnderTest: "filegroup",
|
|
|
|
ModuleTypeUnderTestFactory: android.FileGroupFactory,
|
|
|
|
Dir: "other",
|
|
|
|
Blueprint: ``,
|
|
|
|
Filesystem: map[string]string{
|
2021-06-17 07:43:19 +02:00
|
|
|
"other/Android.bp": `filegroup {
|
2021-11-01 20:32:43 +01:00
|
|
|
name: "fg_foo",
|
|
|
|
bazel_module: {
|
|
|
|
bp2build_available: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
filegroup {
|
|
|
|
name: "fg_bar",
|
|
|
|
bazel_module: {
|
|
|
|
label: "//other:fg_bar"
|
|
|
|
},
|
|
|
|
}`,
|
2021-06-17 07:43:19 +02:00
|
|
|
"other/BUILD.bazel": `// definition for fg_bar`,
|
|
|
|
},
|
2022-06-21 21:28:33 +02:00
|
|
|
ExpectedBazelTargets: []string{
|
|
|
|
MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{}),
|
2021-02-17 19:22:03 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2022-06-21 21:28:33 +02:00
|
|
|
Description: "filegroup bazel_module.label and filegroup bp2build",
|
|
|
|
ModuleTypeUnderTest: "filegroup",
|
|
|
|
ModuleTypeUnderTestFactory: android.FileGroupFactory,
|
2021-11-01 20:32:43 +01:00
|
|
|
|
2022-06-21 21:28:33 +02:00
|
|
|
Filesystem: map[string]string{
|
2021-11-08 18:56:31 +01:00
|
|
|
"other/BUILD.bazel": `// BUILD file`,
|
|
|
|
},
|
2022-06-21 21:28:33 +02:00
|
|
|
Blueprint: `filegroup {
|
2021-11-01 20:32:43 +01:00
|
|
|
name: "fg_foo",
|
|
|
|
bazel_module: {
|
|
|
|
label: "//other:fg_foo",
|
|
|
|
},
|
|
|
|
}
|
2021-02-17 19:22:03 +01:00
|
|
|
|
2021-11-01 20:32:43 +01:00
|
|
|
filegroup {
|
|
|
|
name: "fg_bar",
|
|
|
|
bazel_module: {
|
|
|
|
bp2build_available: true,
|
|
|
|
},
|
|
|
|
}`,
|
2022-06-21 21:28:33 +02:00
|
|
|
ExpectedBazelTargets: []string{
|
|
|
|
MakeBazelTargetNoRestrictions("filegroup", "fg_bar", map[string]string{}),
|
2021-02-17 19:22:03 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
dir := "."
|
|
|
|
for _, testCase := range testCases {
|
2022-06-21 21:28:33 +02:00
|
|
|
t.Run(testCase.Description, func(t *testing.T) {
|
2021-05-25 07:16:48 +02:00
|
|
|
fs := make(map[string][]byte)
|
|
|
|
toParse := []string{
|
|
|
|
"Android.bp",
|
2021-02-17 19:22:03 +01:00
|
|
|
}
|
2022-06-21 21:28:33 +02:00
|
|
|
for f, content := range testCase.Filesystem {
|
2021-05-25 07:16:48 +02:00
|
|
|
if strings.HasSuffix(f, "Android.bp") {
|
|
|
|
toParse = append(toParse, f)
|
|
|
|
}
|
|
|
|
fs[f] = []byte(content)
|
|
|
|
}
|
2022-06-21 21:28:33 +02:00
|
|
|
config := android.TestConfig(buildDir, nil, testCase.Blueprint, fs)
|
2021-05-25 07:16:48 +02:00
|
|
|
ctx := android.NewTestContext(config)
|
2022-06-21 21:28:33 +02:00
|
|
|
ctx.RegisterModuleType(testCase.ModuleTypeUnderTest, testCase.ModuleTypeUnderTestFactory)
|
2021-05-25 07:16:48 +02:00
|
|
|
ctx.RegisterForBazelConversion()
|
2021-04-27 11:47:02 +02:00
|
|
|
|
2021-05-25 07:16:48 +02:00
|
|
|
_, errs := ctx.ParseFileList(dir, toParse)
|
2021-09-02 13:44:42 +02:00
|
|
|
if errored(t, testCase, errs) {
|
2021-05-25 07:16:48 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
_, errs = ctx.ResolveDependencies(config)
|
2021-09-02 13:44:42 +02:00
|
|
|
if errored(t, testCase, errs) {
|
2021-05-25 07:16:48 +02:00
|
|
|
return
|
|
|
|
}
|
2021-04-27 11:47:02 +02:00
|
|
|
|
2021-05-25 07:16:48 +02:00
|
|
|
checkDir := dir
|
2022-06-21 21:28:33 +02:00
|
|
|
if testCase.Dir != "" {
|
|
|
|
checkDir = testCase.Dir
|
2021-05-25 07:16:48 +02:00
|
|
|
}
|
2022-11-09 03:14:01 +01:00
|
|
|
codegenCtx := NewCodegenContext(config, ctx.Context, Bp2Build, "")
|
2021-08-26 14:37:59 +02:00
|
|
|
bazelTargets, err := generateBazelTargetsForDir(codegenCtx, checkDir)
|
|
|
|
android.FailIfErrored(t, err)
|
2021-05-25 07:16:48 +02:00
|
|
|
bazelTargets.sort()
|
|
|
|
actualCount := len(bazelTargets)
|
2022-06-21 21:28:33 +02:00
|
|
|
expectedCount := len(testCase.ExpectedBazelTargets)
|
2021-05-25 07:16:48 +02:00
|
|
|
if actualCount != expectedCount {
|
|
|
|
t.Errorf("Expected %d bazel target, got %d\n%s", expectedCount, actualCount, bazelTargets)
|
|
|
|
}
|
2021-04-27 11:47:02 +02:00
|
|
|
for i, target := range bazelTargets {
|
2021-05-25 07:16:48 +02:00
|
|
|
actualContent := target.content
|
2022-06-21 21:28:33 +02:00
|
|
|
expectedContent := testCase.ExpectedBazelTargets[i]
|
2021-05-25 07:16:48 +02:00
|
|
|
if expectedContent != actualContent {
|
2021-04-27 11:47:02 +02:00
|
|
|
t.Errorf(
|
2021-05-25 07:16:48 +02:00
|
|
|
"Expected generated Bazel target to be '%s', got '%s'",
|
|
|
|
expectedContent,
|
|
|
|
actualContent,
|
2021-04-27 11:47:02 +02:00
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
2021-05-25 07:16:48 +02:00
|
|
|
})
|
2021-04-27 11:47:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-21 12:27:42 +02:00
|
|
|
func TestGlob(t *testing.T) {
|
|
|
|
testCases := []Bp2buildTestCase{
|
|
|
|
{
|
|
|
|
Description: "filegroup with glob",
|
|
|
|
ModuleTypeUnderTest: "filegroup",
|
|
|
|
ModuleTypeUnderTestFactory: android.FileGroupFactory,
|
|
|
|
Blueprint: `filegroup {
|
|
|
|
name: "fg_foo",
|
|
|
|
srcs: ["**/*.txt"],
|
|
|
|
bazel_module: { bp2build_available: true },
|
|
|
|
}`,
|
|
|
|
ExpectedBazelTargets: []string{
|
|
|
|
MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{
|
|
|
|
"srcs": `[
|
|
|
|
"other/a.txt",
|
|
|
|
"other/b.txt",
|
|
|
|
"other/subdir/a.txt",
|
|
|
|
]`,
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
Filesystem: map[string]string{
|
|
|
|
"other/a.txt": "",
|
|
|
|
"other/b.txt": "",
|
|
|
|
"other/subdir/a.txt": "",
|
|
|
|
"other/file": "",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Description: "filegroup with glob in subdir",
|
|
|
|
ModuleTypeUnderTest: "filegroup",
|
|
|
|
ModuleTypeUnderTestFactory: android.FileGroupFactory,
|
|
|
|
Dir: "other",
|
|
|
|
Filesystem: map[string]string{
|
|
|
|
"other/Android.bp": `filegroup {
|
|
|
|
name: "fg_foo",
|
|
|
|
srcs: ["**/*.txt"],
|
|
|
|
bazel_module: { bp2build_available: true },
|
|
|
|
}`,
|
|
|
|
"other/a.txt": "",
|
|
|
|
"other/b.txt": "",
|
|
|
|
"other/subdir/a.txt": "",
|
|
|
|
"other/file": "",
|
|
|
|
},
|
|
|
|
ExpectedBazelTargets: []string{
|
|
|
|
MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{
|
|
|
|
"srcs": `[
|
|
|
|
"a.txt",
|
|
|
|
"b.txt",
|
|
|
|
"subdir/a.txt",
|
|
|
|
]`,
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Description: "filegroup with glob with no kept BUILD files",
|
|
|
|
ModuleTypeUnderTest: "filegroup",
|
|
|
|
ModuleTypeUnderTestFactory: android.FileGroupFactory,
|
|
|
|
KeepBuildFileForDirs: []string{
|
|
|
|
// empty
|
|
|
|
},
|
|
|
|
Blueprint: `filegroup {
|
|
|
|
name: "fg_foo",
|
|
|
|
srcs: ["**/*.txt"],
|
|
|
|
bazel_module: { bp2build_available: true },
|
|
|
|
}`,
|
|
|
|
Filesystem: map[string]string{
|
|
|
|
"a.txt": "",
|
|
|
|
"b.txt": "",
|
|
|
|
"foo/BUILD": "",
|
|
|
|
"foo/a.txt": "",
|
|
|
|
"foo/bar/BUILD": "",
|
|
|
|
"foo/bar/b.txt": "",
|
|
|
|
},
|
|
|
|
ExpectedBazelTargets: []string{
|
|
|
|
MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{
|
|
|
|
"srcs": `[
|
|
|
|
"a.txt",
|
|
|
|
"b.txt",
|
|
|
|
"foo/a.txt",
|
|
|
|
"foo/bar/b.txt",
|
|
|
|
]`,
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Description: "filegroup with glob with kept BUILD file",
|
|
|
|
ModuleTypeUnderTest: "filegroup",
|
|
|
|
ModuleTypeUnderTestFactory: android.FileGroupFactory,
|
|
|
|
KeepBuildFileForDirs: []string{
|
|
|
|
"foo",
|
|
|
|
},
|
|
|
|
Blueprint: `filegroup {
|
|
|
|
name: "fg_foo",
|
|
|
|
srcs: ["**/*.txt"],
|
|
|
|
bazel_module: { bp2build_available: true },
|
|
|
|
}`,
|
|
|
|
Filesystem: map[string]string{
|
|
|
|
"a.txt": "",
|
|
|
|
"b.txt": "",
|
|
|
|
"foo/BUILD": "",
|
|
|
|
"foo/a.txt": "",
|
|
|
|
"foo/bar/BUILD": "",
|
|
|
|
"foo/bar/b.txt": "",
|
|
|
|
},
|
|
|
|
ExpectedBazelTargets: []string{
|
|
|
|
MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{
|
|
|
|
"srcs": `[
|
|
|
|
"a.txt",
|
|
|
|
"b.txt",
|
|
|
|
"//foo:a.txt",
|
|
|
|
"//foo:bar/b.txt",
|
|
|
|
]`,
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Description: "filegroup with glob with kept BUILD.bazel file",
|
|
|
|
ModuleTypeUnderTest: "filegroup",
|
|
|
|
ModuleTypeUnderTestFactory: android.FileGroupFactory,
|
|
|
|
KeepBuildFileForDirs: []string{
|
|
|
|
"foo",
|
|
|
|
},
|
|
|
|
Blueprint: `filegroup {
|
|
|
|
name: "fg_foo",
|
|
|
|
srcs: ["**/*.txt"],
|
|
|
|
bazel_module: { bp2build_available: true },
|
|
|
|
}`,
|
|
|
|
Filesystem: map[string]string{
|
|
|
|
"a.txt": "",
|
|
|
|
"b.txt": "",
|
|
|
|
"foo/BUILD.bazel": "",
|
|
|
|
"foo/a.txt": "",
|
|
|
|
"foo/bar/BUILD.bazel": "",
|
|
|
|
"foo/bar/b.txt": "",
|
|
|
|
},
|
|
|
|
ExpectedBazelTargets: []string{
|
|
|
|
MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{
|
|
|
|
"srcs": `[
|
|
|
|
"a.txt",
|
|
|
|
"b.txt",
|
|
|
|
"//foo:a.txt",
|
|
|
|
"//foo:bar/b.txt",
|
|
|
|
]`,
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Description: "filegroup with glob with Android.bp file as boundary",
|
|
|
|
ModuleTypeUnderTest: "filegroup",
|
|
|
|
ModuleTypeUnderTestFactory: android.FileGroupFactory,
|
|
|
|
Blueprint: `filegroup {
|
|
|
|
name: "fg_foo",
|
|
|
|
srcs: ["**/*.txt"],
|
|
|
|
bazel_module: { bp2build_available: true },
|
|
|
|
}`,
|
|
|
|
Filesystem: map[string]string{
|
|
|
|
"a.txt": "",
|
|
|
|
"b.txt": "",
|
|
|
|
"foo/Android.bp": "",
|
|
|
|
"foo/a.txt": "",
|
|
|
|
"foo/bar/Android.bp": "",
|
|
|
|
"foo/bar/b.txt": "",
|
|
|
|
},
|
|
|
|
ExpectedBazelTargets: []string{
|
|
|
|
MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{
|
|
|
|
"srcs": `[
|
|
|
|
"a.txt",
|
|
|
|
"b.txt",
|
|
|
|
"//foo:a.txt",
|
|
|
|
"//foo/bar:b.txt",
|
|
|
|
]`,
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Description: "filegroup with glob in subdir with kept BUILD and BUILD.bazel file",
|
|
|
|
ModuleTypeUnderTest: "filegroup",
|
|
|
|
ModuleTypeUnderTestFactory: android.FileGroupFactory,
|
|
|
|
Dir: "other",
|
|
|
|
KeepBuildFileForDirs: []string{
|
|
|
|
"other/foo",
|
|
|
|
"other/foo/bar",
|
|
|
|
// deliberately not other/foo/baz/BUILD.
|
|
|
|
},
|
|
|
|
Filesystem: map[string]string{
|
|
|
|
"other/Android.bp": `filegroup {
|
|
|
|
name: "fg_foo",
|
|
|
|
srcs: ["**/*.txt"],
|
|
|
|
bazel_module: { bp2build_available: true },
|
|
|
|
}`,
|
|
|
|
"other/a.txt": "",
|
|
|
|
"other/b.txt": "",
|
|
|
|
"other/foo/BUILD": "",
|
|
|
|
"other/foo/a.txt": "",
|
|
|
|
"other/foo/bar/BUILD.bazel": "",
|
|
|
|
"other/foo/bar/b.txt": "",
|
|
|
|
"other/foo/baz/BUILD": "",
|
|
|
|
"other/foo/baz/c.txt": "",
|
|
|
|
},
|
|
|
|
ExpectedBazelTargets: []string{
|
|
|
|
MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{
|
|
|
|
"srcs": `[
|
|
|
|
"a.txt",
|
|
|
|
"b.txt",
|
|
|
|
"//other/foo:a.txt",
|
|
|
|
"//other/foo/bar:b.txt",
|
|
|
|
"//other/foo:baz/c.txt",
|
|
|
|
]`,
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, testCase := range testCases {
|
|
|
|
t.Run(testCase.Description, func(t *testing.T) {
|
|
|
|
RunBp2BuildTestCaseSimple(t, testCase)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-27 11:47:02 +02:00
|
|
|
func TestGlobExcludeSrcs(t *testing.T) {
|
2022-06-21 21:28:33 +02:00
|
|
|
testCases := []Bp2buildTestCase{
|
2021-04-27 11:47:02 +02:00
|
|
|
{
|
2022-06-21 21:28:33 +02:00
|
|
|
Description: "filegroup top level exclude_srcs",
|
|
|
|
ModuleTypeUnderTest: "filegroup",
|
|
|
|
ModuleTypeUnderTestFactory: android.FileGroupFactory,
|
|
|
|
Blueprint: `filegroup {
|
2021-04-27 11:47:02 +02:00
|
|
|
name: "fg_foo",
|
|
|
|
srcs: ["**/*.txt"],
|
|
|
|
exclude_srcs: ["c.txt"],
|
|
|
|
bazel_module: { bp2build_available: true },
|
|
|
|
}`,
|
2022-06-21 21:28:33 +02:00
|
|
|
Filesystem: map[string]string{
|
2021-04-27 11:47:02 +02:00
|
|
|
"a.txt": "",
|
|
|
|
"b.txt": "",
|
|
|
|
"c.txt": "",
|
|
|
|
"dir/Android.bp": "",
|
|
|
|
"dir/e.txt": "",
|
|
|
|
"dir/f.txt": "",
|
|
|
|
},
|
2022-06-21 21:28:33 +02:00
|
|
|
ExpectedBazelTargets: []string{
|
|
|
|
MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{
|
2021-11-08 18:56:31 +01:00
|
|
|
"srcs": `[
|
|
|
|
"a.txt",
|
|
|
|
"b.txt",
|
|
|
|
"//dir:e.txt",
|
|
|
|
"//dir:f.txt",
|
|
|
|
]`,
|
|
|
|
}),
|
|
|
|
},
|
2021-04-27 11:47:02 +02:00
|
|
|
},
|
|
|
|
{
|
2022-06-21 21:28:33 +02:00
|
|
|
Description: "filegroup in subdir exclude_srcs",
|
|
|
|
ModuleTypeUnderTest: "filegroup",
|
|
|
|
ModuleTypeUnderTestFactory: android.FileGroupFactory,
|
|
|
|
Blueprint: "",
|
|
|
|
Dir: "dir",
|
|
|
|
Filesystem: map[string]string{
|
2021-04-27 11:47:02 +02:00
|
|
|
"dir/Android.bp": `filegroup {
|
|
|
|
name: "fg_foo",
|
|
|
|
srcs: ["**/*.txt"],
|
|
|
|
exclude_srcs: ["b.txt"],
|
|
|
|
bazel_module: { bp2build_available: true },
|
|
|
|
}
|
|
|
|
`,
|
|
|
|
"dir/a.txt": "",
|
|
|
|
"dir/b.txt": "",
|
|
|
|
"dir/subdir/Android.bp": "",
|
|
|
|
"dir/subdir/e.txt": "",
|
|
|
|
"dir/subdir/f.txt": "",
|
|
|
|
},
|
2022-06-21 21:28:33 +02:00
|
|
|
ExpectedBazelTargets: []string{
|
|
|
|
MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{
|
2021-11-08 18:56:31 +01:00
|
|
|
"srcs": `[
|
2021-05-21 14:37:59 +02:00
|
|
|
"a.txt",
|
2021-04-27 11:47:02 +02:00
|
|
|
"//dir/subdir:e.txt",
|
|
|
|
"//dir/subdir:f.txt",
|
2021-11-08 18:56:31 +01:00
|
|
|
]`,
|
|
|
|
}),
|
2021-04-27 11:47:02 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, testCase := range testCases {
|
2022-06-21 21:28:33 +02:00
|
|
|
t.Run(testCase.Description, func(t *testing.T) {
|
2022-09-01 20:57:01 +02:00
|
|
|
RunBp2BuildTestCaseSimple(t, testCase)
|
2021-11-08 18:56:31 +01:00
|
|
|
})
|
2021-02-17 19:22:03 +01:00
|
|
|
}
|
|
|
|
}
|
2021-08-31 22:30:36 +02:00
|
|
|
|
|
|
|
func TestCommonBp2BuildModuleAttrs(t *testing.T) {
|
2022-06-21 21:28:33 +02:00
|
|
|
testCases := []Bp2buildTestCase{
|
2021-08-31 22:30:36 +02:00
|
|
|
{
|
2022-06-21 21:28:33 +02:00
|
|
|
Description: "Required into data test",
|
|
|
|
ModuleTypeUnderTest: "filegroup",
|
|
|
|
ModuleTypeUnderTestFactory: android.FileGroupFactory,
|
2023-09-19 03:12:48 +02:00
|
|
|
StubbedBuildDefinitions: []string{"reqd"},
|
|
|
|
Blueprint: simpleModule("filegroup", "reqd") + `
|
2021-08-31 22:30:36 +02:00
|
|
|
filegroup {
|
|
|
|
name: "fg_foo",
|
|
|
|
required: ["reqd"],
|
|
|
|
bazel_module: { bp2build_available: true },
|
2022-09-21 11:05:37 +02:00
|
|
|
}`,
|
|
|
|
ExpectedBazelTargets: []string{
|
|
|
|
MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{
|
|
|
|
"data": `[":reqd"]`,
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Description: "Required into data test, cyclic self reference is filtered out",
|
|
|
|
ModuleTypeUnderTest: "filegroup",
|
|
|
|
ModuleTypeUnderTestFactory: android.FileGroupFactory,
|
2023-09-19 03:12:48 +02:00
|
|
|
StubbedBuildDefinitions: []string{"reqd"},
|
|
|
|
Blueprint: simpleModule("filegroup", "reqd") + `
|
2022-09-21 11:05:37 +02:00
|
|
|
filegroup {
|
|
|
|
name: "fg_foo",
|
|
|
|
required: ["reqd", "fg_foo"],
|
|
|
|
bazel_module: { bp2build_available: true },
|
2021-08-31 22:30:36 +02:00
|
|
|
}`,
|
2022-06-21 21:28:33 +02:00
|
|
|
ExpectedBazelTargets: []string{
|
|
|
|
MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{
|
2021-11-08 18:56:31 +01:00
|
|
|
"data": `[":reqd"]`,
|
|
|
|
}),
|
2021-08-31 22:30:36 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2022-06-21 21:28:33 +02:00
|
|
|
Description: "Required via arch into data test",
|
|
|
|
ModuleTypeUnderTest: "python_library",
|
|
|
|
ModuleTypeUnderTestFactory: python.PythonLibraryFactory,
|
2023-09-19 03:12:48 +02:00
|
|
|
StubbedBuildDefinitions: []string{"reqdx86", "reqdarm"},
|
|
|
|
Blueprint: simpleModule("python_library", "reqdx86") +
|
|
|
|
simpleModule("python_library", "reqdarm") + `
|
2021-08-31 22:30:36 +02:00
|
|
|
python_library {
|
|
|
|
name: "fg_foo",
|
|
|
|
arch: {
|
2021-11-01 20:32:43 +01:00
|
|
|
arm: {
|
|
|
|
required: ["reqdarm"],
|
|
|
|
},
|
|
|
|
x86: {
|
|
|
|
required: ["reqdx86"],
|
|
|
|
},
|
2021-08-31 22:30:36 +02:00
|
|
|
},
|
|
|
|
bazel_module: { bp2build_available: true },
|
|
|
|
}`,
|
2022-06-21 21:28:33 +02:00
|
|
|
ExpectedBazelTargets: []string{
|
2022-08-31 20:28:19 +02:00
|
|
|
MakeBazelTarget("py_library", "fg_foo", map[string]string{
|
2021-11-08 18:56:31 +01:00
|
|
|
"data": `select({
|
2023-10-11 12:51:28 +02:00
|
|
|
"//build/bazel_common_rules/platforms/arch:arm": [":reqdarm"],
|
|
|
|
"//build/bazel_common_rules/platforms/arch:x86": [":reqdx86"],
|
2021-08-31 22:30:36 +02:00
|
|
|
"//conditions:default": [],
|
2021-11-08 18:56:31 +01:00
|
|
|
})`,
|
|
|
|
"srcs_version": `"PY3"`,
|
2022-05-18 19:57:33 +02:00
|
|
|
"imports": `["."]`,
|
2021-11-08 18:56:31 +01:00
|
|
|
}),
|
2021-08-31 22:30:36 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2022-06-21 21:28:33 +02:00
|
|
|
Description: "Required appended to data test",
|
|
|
|
ModuleTypeUnderTest: "python_library",
|
|
|
|
ModuleTypeUnderTestFactory: python.PythonLibraryFactory,
|
|
|
|
Filesystem: map[string]string{
|
2021-11-08 18:56:31 +01:00
|
|
|
"data.bin": "",
|
|
|
|
"src.py": "",
|
|
|
|
},
|
2023-09-19 03:12:48 +02:00
|
|
|
StubbedBuildDefinitions: []string{"reqd"},
|
|
|
|
Blueprint: simpleModule("python_library", "reqd") + `
|
2021-08-31 22:30:36 +02:00
|
|
|
python_library {
|
|
|
|
name: "fg_foo",
|
|
|
|
data: ["data.bin"],
|
|
|
|
required: ["reqd"],
|
|
|
|
bazel_module: { bp2build_available: true },
|
|
|
|
}`,
|
2022-06-21 21:28:33 +02:00
|
|
|
ExpectedBazelTargets: []string{
|
2022-08-31 20:28:19 +02:00
|
|
|
MakeBazelTarget("py_library", "fg_foo", map[string]string{
|
2021-11-08 18:56:31 +01:00
|
|
|
"data": `[
|
2021-08-31 22:30:36 +02:00
|
|
|
"data.bin",
|
|
|
|
":reqd",
|
2021-11-08 18:56:31 +01:00
|
|
|
]`,
|
|
|
|
"srcs_version": `"PY3"`,
|
2022-05-18 19:57:33 +02:00
|
|
|
"imports": `["."]`,
|
2021-11-08 18:56:31 +01:00
|
|
|
}),
|
2021-08-31 22:30:36 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2022-06-21 21:28:33 +02:00
|
|
|
Description: "All props-to-attrs at once together test",
|
|
|
|
ModuleTypeUnderTest: "filegroup",
|
|
|
|
ModuleTypeUnderTestFactory: android.FileGroupFactory,
|
2023-09-19 03:12:48 +02:00
|
|
|
StubbedBuildDefinitions: []string{"reqd"},
|
|
|
|
Blueprint: simpleModule("filegroup", "reqd") + `
|
2021-08-31 22:30:36 +02:00
|
|
|
filegroup {
|
|
|
|
name: "fg_foo",
|
|
|
|
required: ["reqd"],
|
|
|
|
bazel_module: { bp2build_available: true },
|
|
|
|
}`,
|
2022-06-21 21:28:33 +02:00
|
|
|
ExpectedBazelTargets: []string{
|
|
|
|
MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{
|
2021-11-08 18:56:31 +01:00
|
|
|
"data": `[":reqd"]`,
|
|
|
|
}),
|
2021-08-31 22:30:36 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2021-11-08 18:56:31 +01:00
|
|
|
for _, tc := range testCases {
|
2022-06-21 21:28:33 +02:00
|
|
|
t.Run(tc.Description, func(t *testing.T) {
|
2022-09-01 20:57:01 +02:00
|
|
|
RunBp2BuildTestCaseSimple(t, tc)
|
2021-11-08 18:56:31 +01:00
|
|
|
})
|
2021-08-31 22:30:36 +02:00
|
|
|
}
|
|
|
|
}
|
2022-09-27 03:15:45 +02:00
|
|
|
|
|
|
|
func TestLicensesAttrConversion(t *testing.T) {
|
|
|
|
RunBp2BuildTestCase(t,
|
|
|
|
func(ctx android.RegistrationContext) {
|
|
|
|
ctx.RegisterModuleType("license", android.LicenseFactory)
|
|
|
|
},
|
|
|
|
Bp2buildTestCase{
|
|
|
|
Description: "Test that licenses: attribute is converted",
|
|
|
|
ModuleTypeUnderTest: "filegroup",
|
|
|
|
ModuleTypeUnderTestFactory: android.FileGroupFactory,
|
|
|
|
Blueprint: `
|
|
|
|
license {
|
|
|
|
name: "my_license",
|
|
|
|
}
|
|
|
|
filegroup {
|
|
|
|
name: "my_filegroup",
|
|
|
|
licenses: ["my_license"],
|
|
|
|
}
|
|
|
|
`,
|
|
|
|
ExpectedBazelTargets: []string{
|
|
|
|
MakeBazelTargetNoRestrictions("filegroup", "my_filegroup", AttrNameToString{
|
|
|
|
"applicable_licenses": `[":my_license"]`,
|
|
|
|
}),
|
|
|
|
MakeBazelTargetNoRestrictions("android_license", "my_license", AttrNameToString{}),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
2022-09-28 22:43:08 +02:00
|
|
|
|
2023-04-19 19:36:12 +02:00
|
|
|
func TestGenerateConfigSetting(t *testing.T) {
|
|
|
|
bp := `
|
|
|
|
custom {
|
|
|
|
name: "foo",
|
|
|
|
test_config_setting: true,
|
|
|
|
}
|
|
|
|
`
|
|
|
|
expectedBazelTargets := []string{
|
|
|
|
MakeBazelTargetNoRestrictions(
|
|
|
|
"config_setting",
|
|
|
|
"foo_config_setting",
|
|
|
|
AttrNameToString{
|
|
|
|
"flag_values": `{
|
|
|
|
"//build/bazel/rules/my_string_setting": "foo",
|
|
|
|
}`,
|
|
|
|
},
|
|
|
|
),
|
|
|
|
MakeBazelTarget(
|
|
|
|
"custom",
|
|
|
|
"foo",
|
|
|
|
AttrNameToString{},
|
|
|
|
),
|
|
|
|
}
|
|
|
|
registerCustomModule := func(ctx android.RegistrationContext) {
|
|
|
|
ctx.RegisterModuleType("custom", customModuleFactoryHostAndDevice)
|
|
|
|
}
|
|
|
|
RunBp2BuildTestCase(t, registerCustomModule, Bp2buildTestCase{
|
|
|
|
Blueprint: bp,
|
|
|
|
ExpectedBazelTargets: expectedBazelTargets,
|
|
|
|
Description: "Generating API contribution Bazel targets for custom module",
|
|
|
|
})
|
|
|
|
}
|
2023-04-26 04:56:37 +02:00
|
|
|
|
|
|
|
// If values of all keys in an axis are equal to //conditions:default, drop the axis and print the common value
|
|
|
|
func TestPrettyPrintSelectMapEqualValues(t *testing.T) {
|
|
|
|
lla := bazel.LabelListAttribute{
|
|
|
|
Value: bazel.LabelList{},
|
|
|
|
}
|
|
|
|
libFooImplLabel := bazel.Label{
|
|
|
|
Label: ":libfoo.impl",
|
|
|
|
}
|
2023-04-18 08:20:40 +02:00
|
|
|
lla.SetSelectValue(bazel.OsAndInApexAxis, bazel.AndroidPlatform, bazel.MakeLabelList([]bazel.Label{libFooImplLabel}))
|
2023-04-26 04:56:37 +02:00
|
|
|
lla.SetSelectValue(bazel.OsAndInApexAxis, bazel.ConditionsDefaultConfigKey, bazel.MakeLabelList([]bazel.Label{libFooImplLabel}))
|
|
|
|
actual, _ := prettyPrintAttribute(lla, 0)
|
|
|
|
android.AssertStringEquals(t, "Print the common value if all keys in an axis have the same value", `[":libfoo.impl"]`, actual)
|
|
|
|
}
|
2023-08-04 00:33:47 +02:00
|
|
|
|
2023-09-14 01:33:20 +02:00
|
|
|
func TestAlreadyPresentBuildTarget(t *testing.T) {
|
|
|
|
bp := `
|
|
|
|
custom {
|
|
|
|
name: "foo",
|
|
|
|
}
|
|
|
|
custom {
|
|
|
|
name: "bar",
|
|
|
|
}
|
|
|
|
`
|
|
|
|
alreadyPresentBuildFile :=
|
|
|
|
MakeBazelTarget(
|
|
|
|
"custom",
|
|
|
|
"foo",
|
|
|
|
AttrNameToString{},
|
|
|
|
)
|
|
|
|
expectedBazelTargets := []string{
|
|
|
|
MakeBazelTarget(
|
|
|
|
"custom",
|
|
|
|
"bar",
|
|
|
|
AttrNameToString{},
|
|
|
|
),
|
|
|
|
}
|
|
|
|
registerCustomModule := func(ctx android.RegistrationContext) {
|
|
|
|
ctx.RegisterModuleType("custom", customModuleFactoryHostAndDevice)
|
|
|
|
}
|
|
|
|
RunBp2BuildTestCase(t, registerCustomModule, Bp2buildTestCase{
|
|
|
|
AlreadyExistingBuildContents: alreadyPresentBuildFile,
|
|
|
|
Blueprint: bp,
|
|
|
|
ExpectedBazelTargets: expectedBazelTargets,
|
|
|
|
Description: "Not duplicating work for an already-present BUILD target.",
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-09-21 22:36:35 +02:00
|
|
|
func TestAlreadyPresentOneToManyBuildTarget(t *testing.T) {
|
|
|
|
bp := `
|
|
|
|
custom {
|
|
|
|
name: "foo",
|
|
|
|
one_to_many_prop: true,
|
|
|
|
}
|
|
|
|
custom {
|
|
|
|
name: "bar",
|
|
|
|
}
|
|
|
|
`
|
|
|
|
alreadyPresentBuildFile :=
|
|
|
|
MakeBazelTarget(
|
|
|
|
"custom",
|
|
|
|
// one_to_many_prop ensures that foo generates "foo_proto_library_deps".
|
|
|
|
"foo_proto_library_deps",
|
|
|
|
AttrNameToString{},
|
|
|
|
)
|
|
|
|
expectedBazelTargets := []string{
|
|
|
|
MakeBazelTarget(
|
|
|
|
"custom",
|
|
|
|
"bar",
|
|
|
|
AttrNameToString{},
|
|
|
|
),
|
|
|
|
}
|
|
|
|
registerCustomModule := func(ctx android.RegistrationContext) {
|
|
|
|
ctx.RegisterModuleType("custom", customModuleFactoryHostAndDevice)
|
|
|
|
}
|
|
|
|
RunBp2BuildTestCase(t, registerCustomModule, Bp2buildTestCase{
|
|
|
|
AlreadyExistingBuildContents: alreadyPresentBuildFile,
|
|
|
|
Blueprint: bp,
|
|
|
|
ExpectedBazelTargets: expectedBazelTargets,
|
|
|
|
Description: "Not duplicating work for an already-present BUILD target (different generated name)",
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-09-14 01:33:20 +02:00
|
|
|
// Verifies that if a module is defined in pkg1/Android.bp, that a target present
|
|
|
|
// in pkg2/BUILD.bazel does not result in the module being labeled "already defined
|
|
|
|
// in a BUILD file".
|
|
|
|
func TestBuildTargetPresentOtherDirectory(t *testing.T) {
|
|
|
|
bp := `
|
|
|
|
custom {
|
|
|
|
name: "foo",
|
|
|
|
}
|
|
|
|
`
|
|
|
|
expectedBazelTargets := []string{
|
|
|
|
MakeBazelTarget(
|
|
|
|
"custom",
|
|
|
|
"foo",
|
|
|
|
AttrNameToString{},
|
|
|
|
),
|
|
|
|
}
|
|
|
|
registerCustomModule := func(ctx android.RegistrationContext) {
|
|
|
|
ctx.RegisterModuleType("custom", customModuleFactoryHostAndDevice)
|
|
|
|
}
|
|
|
|
RunBp2BuildTestCase(t, registerCustomModule, Bp2buildTestCase{
|
|
|
|
KeepBuildFileForDirs: []string{"other_pkg"},
|
|
|
|
Filesystem: map[string]string{
|
|
|
|
"other_pkg/BUILD.bazel": MakeBazelTarget("custom", "foo", AttrNameToString{}),
|
|
|
|
},
|
|
|
|
Blueprint: bp,
|
|
|
|
ExpectedBazelTargets: expectedBazelTargets,
|
|
|
|
Description: "Not treating a BUILD target as the bazel definition for a module in another package",
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-08-04 00:33:47 +02:00
|
|
|
// If CommonAttributes.Dir is set, the bazel target should be created in that dir
|
|
|
|
func TestCreateBazelTargetInDifferentDir(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
bp := `
|
|
|
|
custom {
|
|
|
|
name: "foo",
|
|
|
|
dir: "subdir",
|
|
|
|
}
|
|
|
|
`
|
|
|
|
registerCustomModule := func(ctx android.RegistrationContext) {
|
|
|
|
ctx.RegisterModuleType("custom", customModuleFactoryHostAndDevice)
|
|
|
|
}
|
|
|
|
// Check that foo is not created in root dir
|
|
|
|
RunBp2BuildTestCase(t, registerCustomModule, Bp2buildTestCase{
|
|
|
|
Description: "foo is not created in root dir because it sets dir explicitly",
|
|
|
|
Blueprint: bp,
|
|
|
|
Filesystem: map[string]string{
|
|
|
|
"subdir/Android.bp": "",
|
|
|
|
},
|
|
|
|
ExpectedBazelTargets: []string{},
|
|
|
|
})
|
|
|
|
// Check that foo is created in `subdir`
|
|
|
|
RunBp2BuildTestCase(t, registerCustomModule, Bp2buildTestCase{
|
|
|
|
Description: "foo is created in `subdir` because it sets dir explicitly",
|
|
|
|
Blueprint: bp,
|
|
|
|
Filesystem: map[string]string{
|
|
|
|
"subdir/Android.bp": "",
|
|
|
|
},
|
|
|
|
Dir: "subdir",
|
|
|
|
ExpectedBazelTargets: []string{
|
|
|
|
MakeBazelTarget("custom", "foo", AttrNameToString{}),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
// Check that we cannot create target in different dir if it is does not an Android.bp
|
|
|
|
RunBp2BuildTestCase(t, registerCustomModule, Bp2buildTestCase{
|
|
|
|
Description: "foo cannot be created in `subdir` because it does not contain an Android.bp file",
|
|
|
|
Blueprint: bp,
|
|
|
|
Dir: "subdir",
|
|
|
|
ExpectedErr: fmt.Errorf("Cannot use ca.Dir to create a BazelTarget in dir: subdir since it does not contain an Android.bp file"),
|
|
|
|
})
|
|
|
|
|
|
|
|
}
|
2023-09-28 22:41:03 +02:00
|
|
|
|
|
|
|
func TestBp2buildDepsMutator_missingTransitiveDep(t *testing.T) {
|
|
|
|
bp := `
|
|
|
|
custom {
|
|
|
|
name: "foo",
|
|
|
|
}
|
|
|
|
|
|
|
|
custom {
|
|
|
|
name: "has_deps",
|
|
|
|
arch_paths: [":has_missing_dep", ":foo"],
|
|
|
|
}
|
|
|
|
|
|
|
|
custom {
|
|
|
|
name: "has_missing_dep",
|
|
|
|
arch_paths: [":missing"],
|
|
|
|
}
|
|
|
|
`
|
|
|
|
expectedBazelTargets := []string{
|
|
|
|
MakeBazelTarget(
|
|
|
|
"custom",
|
|
|
|
"foo",
|
|
|
|
AttrNameToString{},
|
|
|
|
),
|
|
|
|
}
|
|
|
|
registerCustomModule := func(ctx android.RegistrationContext) {
|
|
|
|
ctx.RegisterModuleType("custom", customModuleFactoryHostAndDevice)
|
|
|
|
}
|
|
|
|
RunBp2BuildTestCase(t, registerCustomModule, Bp2buildTestCase{
|
|
|
|
Blueprint: bp,
|
|
|
|
ExpectedBazelTargets: expectedBazelTargets,
|
|
|
|
Description: "Skipping conversion of a target with missing transitive dep",
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBp2buildDepsMutator_missingDirectDep(t *testing.T) {
|
|
|
|
bp := `
|
|
|
|
custom {
|
|
|
|
name: "foo",
|
|
|
|
arch_paths: [":exists"],
|
|
|
|
}
|
|
|
|
custom {
|
|
|
|
name: "exists",
|
|
|
|
}
|
|
|
|
|
|
|
|
custom {
|
|
|
|
name: "has_missing_dep",
|
|
|
|
arch_paths: [":missing"],
|
|
|
|
}
|
|
|
|
`
|
|
|
|
expectedBazelTargets := []string{
|
|
|
|
MakeBazelTarget(
|
|
|
|
"custom",
|
|
|
|
"foo",
|
|
|
|
AttrNameToString{"arch_paths": `[":exists"]`},
|
|
|
|
),
|
|
|
|
MakeBazelTarget(
|
|
|
|
"custom",
|
|
|
|
"exists",
|
|
|
|
AttrNameToString{},
|
|
|
|
),
|
|
|
|
}
|
|
|
|
registerCustomModule := func(ctx android.RegistrationContext) {
|
|
|
|
ctx.RegisterModuleType("custom", customModuleFactoryHostAndDevice)
|
|
|
|
}
|
|
|
|
RunBp2BuildTestCase(t, registerCustomModule, Bp2buildTestCase{
|
|
|
|
Blueprint: bp,
|
|
|
|
ExpectedBazelTargets: expectedBazelTargets,
|
|
|
|
Description: "Skipping conversion of a target with missing direct dep",
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBp2buildDepsMutator_unconvertedDirectDep(t *testing.T) {
|
|
|
|
bp := `
|
|
|
|
custom {
|
|
|
|
name: "has_unconverted_dep",
|
|
|
|
arch_paths: [":unconvertible"],
|
|
|
|
}
|
|
|
|
|
|
|
|
custom {
|
|
|
|
name: "unconvertible",
|
|
|
|
does_not_convert_to_bazel: true
|
|
|
|
}
|
|
|
|
`
|
|
|
|
registerCustomModule := func(ctx android.RegistrationContext) {
|
|
|
|
ctx.RegisterModuleType("custom", customModuleFactoryHostAndDevice)
|
|
|
|
}
|
|
|
|
RunBp2BuildTestCase(t, registerCustomModule, Bp2buildTestCase{
|
|
|
|
Blueprint: bp,
|
|
|
|
ExpectedBazelTargets: []string{},
|
|
|
|
Description: "Skipping conversion of a target with unconverted direct dep",
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBp2buildDepsMutator_unconvertedTransitiveDep(t *testing.T) {
|
|
|
|
bp := `
|
|
|
|
custom {
|
|
|
|
name: "foo",
|
|
|
|
arch_paths: [":has_unconverted_dep", ":bar"],
|
|
|
|
}
|
|
|
|
|
|
|
|
custom {
|
|
|
|
name: "bar",
|
|
|
|
}
|
|
|
|
|
|
|
|
custom {
|
|
|
|
name: "has_unconverted_dep",
|
|
|
|
arch_paths: [":unconvertible"],
|
|
|
|
}
|
|
|
|
|
|
|
|
custom {
|
|
|
|
name: "unconvertible",
|
|
|
|
does_not_convert_to_bazel: true
|
|
|
|
}
|
|
|
|
`
|
|
|
|
expectedBazelTargets := []string{
|
|
|
|
MakeBazelTarget(
|
|
|
|
"custom",
|
|
|
|
"bar",
|
|
|
|
AttrNameToString{},
|
|
|
|
),
|
|
|
|
}
|
|
|
|
registerCustomModule := func(ctx android.RegistrationContext) {
|
|
|
|
ctx.RegisterModuleType("custom", customModuleFactoryHostAndDevice)
|
|
|
|
}
|
|
|
|
RunBp2BuildTestCase(t, registerCustomModule, Bp2buildTestCase{
|
|
|
|
Blueprint: bp,
|
|
|
|
ExpectedBazelTargets: expectedBazelTargets,
|
|
|
|
Description: "Skipping conversion of a target with unconverted transitive dep",
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBp2buildDepsMutator_alreadyExistsBuildDeps(t *testing.T) {
|
|
|
|
bp := `
|
|
|
|
custom {
|
|
|
|
name: "foo",
|
|
|
|
arch_paths: [":bar"],
|
|
|
|
}
|
|
|
|
custom {
|
|
|
|
name: "bar",
|
|
|
|
arch_paths: [":checked_in"],
|
|
|
|
}
|
|
|
|
custom {
|
|
|
|
name: "checked_in",
|
|
|
|
arch_paths: [":checked_in"],
|
|
|
|
does_not_convert_to_bazel: true
|
|
|
|
}
|
|
|
|
`
|
|
|
|
expectedBazelTargets := []string{
|
|
|
|
MakeBazelTarget(
|
|
|
|
"custom",
|
|
|
|
"foo",
|
|
|
|
AttrNameToString{"arch_paths": `[":bar"]`},
|
|
|
|
),
|
|
|
|
MakeBazelTarget(
|
|
|
|
"custom",
|
|
|
|
"bar",
|
|
|
|
AttrNameToString{"arch_paths": `[":checked_in"]`},
|
|
|
|
),
|
|
|
|
}
|
|
|
|
registerCustomModule := func(ctx android.RegistrationContext) {
|
|
|
|
ctx.RegisterModuleType("custom", customModuleFactoryHostAndDevice)
|
|
|
|
}
|
|
|
|
RunBp2BuildTestCase(t, registerCustomModule, Bp2buildTestCase{
|
|
|
|
StubbedBuildDefinitions: []string{"checked_in"},
|
|
|
|
Blueprint: bp,
|
|
|
|
ExpectedBazelTargets: expectedBazelTargets,
|
|
|
|
Description: "Convert target with already-existing build dep",
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tests that deps of libc are always considered valid for libc. This circumvents
|
|
|
|
// an issue that, in a variantless graph (such as bp2build's), libc has the
|
|
|
|
// unique predicament that it depends on itself.
|
|
|
|
func TestBp2buildDepsMutator_depOnLibc(t *testing.T) {
|
|
|
|
bp := `
|
|
|
|
custom {
|
|
|
|
name: "foo",
|
|
|
|
arch_paths: [":libc"],
|
|
|
|
}
|
|
|
|
custom {
|
|
|
|
name: "libc",
|
|
|
|
arch_paths: [":libc_dep"],
|
|
|
|
}
|
|
|
|
custom {
|
|
|
|
name: "libc_dep",
|
|
|
|
does_not_convert_to_bazel: true
|
|
|
|
}
|
|
|
|
`
|
|
|
|
expectedBazelTargets := []string{
|
|
|
|
MakeBazelTarget(
|
|
|
|
"custom",
|
|
|
|
"foo",
|
|
|
|
AttrNameToString{"arch_paths": `[":libc"]`},
|
|
|
|
),
|
|
|
|
MakeBazelTarget(
|
|
|
|
"custom",
|
|
|
|
"libc",
|
|
|
|
AttrNameToString{"arch_paths": `[":libc_dep"]`},
|
|
|
|
),
|
|
|
|
}
|
|
|
|
registerCustomModule := func(ctx android.RegistrationContext) {
|
|
|
|
ctx.RegisterModuleType("custom", customModuleFactoryHostAndDevice)
|
|
|
|
}
|
|
|
|
RunBp2BuildTestCase(t, registerCustomModule, Bp2buildTestCase{
|
|
|
|
StubbedBuildDefinitions: []string{"checked_in"},
|
|
|
|
Blueprint: bp,
|
|
|
|
ExpectedBazelTargets: expectedBazelTargets,
|
|
|
|
Description: "Convert target with dep on libc",
|
|
|
|
})
|
|
|
|
}
|