Merge "Refactor bp2build tests"

This commit is contained in:
Treehugger Robot
2021-11-15 23:21:10 +00:00
committed by Gerrit Code Review
19 changed files with 1443 additions and 1751 deletions

View File

@@ -42,8 +42,9 @@ android_app_certificate {
certificate: "chamber_of_secrets_dir", certificate: "chamber_of_secrets_dir",
} }
`, `,
expectedBazelTargets: []string{`android_app_certificate( expectedBazelTargets: []string{
name = "com.android.apogee.cert", makeBazelTarget("android_app_certificate", "com.android.apogee.cert", attrNameToString{
certificate = "chamber_of_secrets_dir", "certificate": `"chamber_of_secrets_dir"`,
)`}}) }),
}})
} }

View File

@@ -113,29 +113,30 @@ apex {
], ],
} }
`, `,
expectedBazelTargets: []string{`apex( expectedBazelTargets: []string{
name = "com.android.apogee", makeBazelTarget("apex", "com.android.apogee", attrNameToString{
android_manifest = "ApogeeAndroidManifest.xml", "android_manifest": `"ApogeeAndroidManifest.xml"`,
binaries = [ "binaries": `[
"binary_1", "binary_1",
"binary_2", "binary_2",
], ]`,
certificate = ":com.android.apogee.certificate", "certificate": `":com.android.apogee.certificate"`,
file_contexts = ":com.android.apogee-file_contexts", "file_contexts": `":com.android.apogee-file_contexts"`,
installable = False, "installable": "False",
key = ":com.android.apogee.key", "key": `":com.android.apogee.key"`,
manifest = "apogee_manifest.json", "manifest": `"apogee_manifest.json"`,
min_sdk_version = "29", "min_sdk_version": `"29"`,
native_shared_libs = [ "native_shared_libs": `[
":native_shared_lib_1", ":native_shared_lib_1",
":native_shared_lib_2", ":native_shared_lib_2",
], ]`,
prebuilts = [ "prebuilts": `[
":pretend_prebuilt_1", ":pretend_prebuilt_1",
":pretend_prebuilt_2", ":pretend_prebuilt_2",
], ]`,
updatable = False, "updatable": "False",
)`}}) }),
}})
} }
func TestApexBundleDefaultPropertyValues(t *testing.T) { func TestApexBundleDefaultPropertyValues(t *testing.T) {
@@ -151,10 +152,10 @@ apex {
manifest: "apogee_manifest.json", manifest: "apogee_manifest.json",
} }
`, `,
expectedBazelTargets: []string{`apex( expectedBazelTargets: []string{makeBazelTarget("apex", "com.android.apogee", attrNameToString{
name = "com.android.apogee", "manifest": `"apogee_manifest.json"`,
manifest = "apogee_manifest.json", }),
)`}}) }})
} }
func TestApexBundleHasBazelModuleProps(t *testing.T) { func TestApexBundleHasBazelModuleProps(t *testing.T) {
@@ -171,8 +172,8 @@ apex {
bazel_module: { bp2build_available: true }, bazel_module: { bp2build_available: true },
} }
`, `,
expectedBazelTargets: []string{`apex( expectedBazelTargets: []string{makeBazelTarget("apex", "apogee", attrNameToString{
name = "apogee", "manifest": `"manifest.json"`,
manifest = "manifest.json", }),
)`}}) }})
} }

View File

@@ -43,9 +43,9 @@ apex_key {
private_key: "com.android.apogee.pem", private_key: "com.android.apogee.pem",
} }
`, `,
expectedBazelTargets: []string{`apex_key( expectedBazelTargets: []string{makeBazelTarget("apex_key", "com.android.apogee.key", attrNameToString{
name = "com.android.apogee.key", "private_key": `"com.android.apogee.pem"`,
private_key = "com.android.apogee.pem", "public_key": `"com.android.apogee.avbpubkey"`,
public_key = "com.android.apogee.avbpubkey", }),
)`}}) }})
} }

View File

@@ -230,32 +230,32 @@ func TestGenerateBazelTargetModules(t *testing.T) {
string_prop: "a", string_prop: "a",
bazel_module: { bp2build_available: true }, bazel_module: { bp2build_available: true },
}`, }`,
expectedBazelTargets: []string{`custom( expectedBazelTargets: []string{
name = "foo", makeBazelTarget("custom", "foo", attrNameToString{
string_list_prop = [ "string_list_prop": `[
"a", "a",
"b", "b",
], ]`,
string_prop = "a", "string_prop": `"a"`,
)`, }),
}, },
}, },
{ {
description: "control characters", description: "control characters",
blueprint: `custom { blueprint: `custom {
name: "control_characters", name: "foo",
string_list_prop: ["\t", "\n"], string_list_prop: ["\t", "\n"],
string_prop: "a\t\n\r", string_prop: "a\t\n\r",
bazel_module: { bp2build_available: true }, bazel_module: { bp2build_available: true },
}`, }`,
expectedBazelTargets: []string{`custom( expectedBazelTargets: []string{
name = "control_characters", makeBazelTarget("custom", "foo", attrNameToString{
string_list_prop = [ "string_list_prop": `[
"\t", "\t",
"\n", "\n",
], ]`,
string_prop = "a\t\n\r", "string_prop": `"a\t\n\r"`,
)`, }),
}, },
}, },
{ {
@@ -271,14 +271,13 @@ custom {
arch_paths: ["abc"], arch_paths: ["abc"],
bazel_module: { bp2build_available: true }, bazel_module: { bp2build_available: true },
}`, }`,
expectedBazelTargets: []string{`custom( expectedBazelTargets: []string{
name = "dep", makeBazelTarget("custom", "dep", attrNameToString{
arch_paths = ["abc"], "arch_paths": `["abc"]`,
)`, }),
`custom( makeBazelTarget("custom", "has_dep", attrNameToString{
name = "has_dep", "arch_paths": `[":dep"]`,
arch_paths = [":dep"], }),
)`,
}, },
}, },
{ {
@@ -311,9 +310,9 @@ custom {
}, },
bazel_module: { bp2build_available: true }, bazel_module: { bp2build_available: true },
}`, }`,
expectedBazelTargets: []string{`custom( expectedBazelTargets: []string{
name = "arch_paths", makeBazelTarget("custom", "arch_paths", attrNameToString{
arch_paths = select({ "arch_paths": `select({
"//build/bazel/platforms/arch:arm": [ "//build/bazel/platforms/arch:arm": [
"arm.txt", "arm.txt",
"lib32.txt", "lib32.txt",
@@ -368,8 +367,8 @@ custom {
"windows.txt", "windows.txt",
], ],
"//conditions:default": [], "//conditions:default": [],
}), })`,
)`, }),
}, },
}, },
{ {
@@ -389,17 +388,16 @@ custom {
arch_paths: ["abc"], arch_paths: ["abc"],
bazel_module: { bp2build_available: true }, bazel_module: { bp2build_available: true },
}`, }`,
expectedBazelTargets: []string{`custom( expectedBazelTargets: []string{
name = "dep", makeBazelTarget("custom", "dep", attrNameToString{
arch_paths = ["abc"], "arch_paths": `["abc"]`,
)`, }),
`custom( makeBazelTarget("custom", "has_dep", attrNameToString{
name = "has_dep", "arch_paths": `select({
arch_paths = select({
"//build/bazel/platforms/arch:x86": [":dep"], "//build/bazel/platforms/arch:x86": [":dep"],
"//conditions:default": [], "//conditions:default": [],
}), })`,
)`, }),
}, },
}, },
{ {
@@ -409,10 +407,10 @@ custom {
embedded_prop: "abc", embedded_prop: "abc",
bazel_module: { bp2build_available: true }, bazel_module: { bp2build_available: true },
}`, }`,
expectedBazelTargets: []string{`custom( expectedBazelTargets: []string{
name = "embedded_props", makeBazelTarget("custom", "embedded_props", attrNameToString{
embedded_attr = "abc", "embedded_attr": `"abc"`,
)`, }),
}, },
}, },
{ {
@@ -422,10 +420,10 @@ custom {
other_embedded_prop: "abc", other_embedded_prop: "abc",
bazel_module: { bp2build_available: true }, bazel_module: { bp2build_available: true },
}`, }`,
expectedBazelTargets: []string{`custom( expectedBazelTargets: []string{
name = "ptr_to_embedded_props", makeBazelTarget("custom", "ptr_to_embedded_props", attrNameToString{
other_embedded_attr = "abc", "other_embedded_attr": `"abc"`,
)`, }),
}, },
}, },
} }
@@ -649,9 +647,7 @@ func TestModuleTypeBp2Build(t *testing.T) {
bazel_module: { bp2build_available: true }, bazel_module: { bp2build_available: true },
}`, }`,
expectedBazelTargets: []string{ expectedBazelTargets: []string{
`filegroup( makeBazelTarget("filegroup", "fg_foo", map[string]string{}),
name = "fg_foo",
)`,
}, },
}, },
{ {
@@ -665,9 +661,7 @@ func TestModuleTypeBp2Build(t *testing.T) {
bazel_module: { bp2build_available: true }, bazel_module: { bp2build_available: true },
}`, }`,
expectedBazelTargets: []string{ expectedBazelTargets: []string{
`filegroup( makeBazelTarget("filegroup", "fg_foo", map[string]string{}),
name = "fg_foo",
)`,
}, },
}, },
{ {
@@ -680,13 +674,13 @@ func TestModuleTypeBp2Build(t *testing.T) {
srcs: ["a", "b"], srcs: ["a", "b"],
bazel_module: { bp2build_available: true }, bazel_module: { bp2build_available: true },
}`, }`,
expectedBazelTargets: []string{`filegroup( expectedBazelTargets: []string{
name = "fg_foo", makeBazelTarget("filegroup", "fg_foo", map[string]string{
srcs = [ "srcs": `[
"a", "a",
"b", "b",
], ]`,
)`, }),
}, },
}, },
{ {
@@ -700,10 +694,10 @@ func TestModuleTypeBp2Build(t *testing.T) {
exclude_srcs: ["a"], exclude_srcs: ["a"],
bazel_module: { bp2build_available: true }, bazel_module: { bp2build_available: true },
}`, }`,
expectedBazelTargets: []string{`filegroup( expectedBazelTargets: []string{
name = "fg_foo", makeBazelTarget("filegroup", "fg_foo", map[string]string{
srcs = ["b"], "srcs": `["b"]`,
)`, }),
}, },
}, },
{ {
@@ -712,18 +706,18 @@ func TestModuleTypeBp2Build(t *testing.T) {
moduleTypeUnderTestFactory: android.FileGroupFactory, moduleTypeUnderTestFactory: android.FileGroupFactory,
moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build, moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
blueprint: `filegroup { blueprint: `filegroup {
name: "foo", name: "fg_foo",
srcs: ["**/*.txt"], srcs: ["**/*.txt"],
bazel_module: { bp2build_available: true }, bazel_module: { bp2build_available: true },
}`, }`,
expectedBazelTargets: []string{`filegroup( expectedBazelTargets: []string{
name = "foo", makeBazelTarget("filegroup", "fg_foo", map[string]string{
srcs = [ "srcs": `[
"other/a.txt", "other/a.txt",
"other/b.txt", "other/b.txt",
"other/subdir/a.txt", "other/subdir/a.txt",
], ]`,
)`, }),
}, },
filesystem: map[string]string{ filesystem: map[string]string{
"other/a.txt": "", "other/a.txt": "",
@@ -737,21 +731,8 @@ func TestModuleTypeBp2Build(t *testing.T) {
moduleTypeUnderTest: "filegroup", moduleTypeUnderTest: "filegroup",
moduleTypeUnderTestFactory: android.FileGroupFactory, moduleTypeUnderTestFactory: android.FileGroupFactory,
moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build, moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
blueprint: `filegroup { blueprint: ``,
name: "foo", dir: "other",
srcs: ["a.txt"],
bazel_module: { bp2build_available: true },
}`,
dir: "other",
expectedBazelTargets: []string{`filegroup(
name = "fg_foo",
srcs = [
"a.txt",
"b.txt",
"subdir/a.txt",
],
)`,
},
filesystem: map[string]string{ filesystem: map[string]string{
"other/Android.bp": `filegroup { "other/Android.bp": `filegroup {
name: "fg_foo", name: "fg_foo",
@@ -763,6 +744,15 @@ func TestModuleTypeBp2Build(t *testing.T) {
"other/subdir/a.txt": "", "other/subdir/a.txt": "",
"other/file": "", "other/file": "",
}, },
expectedBazelTargets: []string{
makeBazelTarget("filegroup", "fg_foo", map[string]string{
"srcs": `[
"a.txt",
"b.txt",
"subdir/a.txt",
]`,
}),
},
}, },
{ {
description: "depends_on_other_dir_module", description: "depends_on_other_dir_module",
@@ -770,21 +760,13 @@ func TestModuleTypeBp2Build(t *testing.T) {
moduleTypeUnderTestFactory: android.FileGroupFactory, moduleTypeUnderTestFactory: android.FileGroupFactory,
moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build, moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
blueprint: `filegroup { blueprint: `filegroup {
name: "foobar", name: "fg_foo",
srcs: [ srcs: [
":foo", ":foo",
"c", "c",
], ],
bazel_module: { bp2build_available: true }, bazel_module: { bp2build_available: true },
}`, }`,
expectedBazelTargets: []string{`filegroup(
name = "foobar",
srcs = [
"//other:foo",
"c",
],
)`,
},
filesystem: map[string]string{ filesystem: map[string]string{
"other/Android.bp": `filegroup { "other/Android.bp": `filegroup {
name: "foo", name: "foo",
@@ -792,6 +774,14 @@ func TestModuleTypeBp2Build(t *testing.T) {
bazel_module: { bp2build_available: true }, bazel_module: { bp2build_available: true },
}`, }`,
}, },
expectedBazelTargets: []string{
makeBazelTarget("filegroup", "fg_foo", map[string]string{
"srcs": `[
"//other:foo",
"c",
]`,
}),
},
}, },
{ {
description: "depends_on_other_unconverted_module_error", description: "depends_on_other_unconverted_module_error",
@@ -799,21 +789,21 @@ func TestModuleTypeBp2Build(t *testing.T) {
moduleTypeUnderTestFactory: android.FileGroupFactory, moduleTypeUnderTestFactory: android.FileGroupFactory,
moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build, moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
unconvertedDepsMode: errorModulesUnconvertedDeps, unconvertedDepsMode: errorModulesUnconvertedDeps,
blueprint: `filegroup {
name: "foobar",
srcs: [
":foo",
"c",
],
bazel_module: { bp2build_available: true },
}`,
expectedErr: fmt.Errorf(`"foobar" depends on unconverted modules: foo`),
filesystem: map[string]string{ filesystem: map[string]string{
"other/Android.bp": `filegroup { "other/Android.bp": `filegroup {
name: "foo", name: "foo",
srcs: ["a", "b"], srcs: ["a", "b"],
}`, }`,
}, },
blueprint: `filegroup {
name: "fg_foo",
srcs: [
":foo",
"c",
],
bazel_module: { bp2build_available: true },
}`,
expectedErr: fmt.Errorf(`"fg_foo" depends on unconverted modules: foo`),
}, },
} }
@@ -1088,9 +1078,8 @@ func TestCombineBuildFilesBp2buildTargets(t *testing.T) {
"other/BUILD.bazel": `// definition for fg_bar`, "other/BUILD.bazel": `// definition for fg_bar`,
}, },
expectedBazelTargets: []string{ expectedBazelTargets: []string{
`filegroup( makeBazelTarget("filegroup", "fg_foo", map[string]string{}),
name = "fg_foo", `// definition for fg_bar`,
)`, `// definition for fg_bar`,
}, },
}, },
{ {
@@ -1098,6 +1087,9 @@ func TestCombineBuildFilesBp2buildTargets(t *testing.T) {
moduleTypeUnderTest: "filegroup", moduleTypeUnderTest: "filegroup",
moduleTypeUnderTestFactory: android.FileGroupFactory, moduleTypeUnderTestFactory: android.FileGroupFactory,
moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build, moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
filesystem: map[string]string{
"other/BUILD.bazel": `// BUILD file`,
},
blueprint: `filegroup { blueprint: `filegroup {
name: "fg_foo", name: "fg_foo",
bazel_module: { bazel_module: {
@@ -1112,14 +1104,9 @@ func TestCombineBuildFilesBp2buildTargets(t *testing.T) {
}, },
}`, }`,
expectedBazelTargets: []string{ expectedBazelTargets: []string{
`filegroup( makeBazelTarget("filegroup", "fg_bar", map[string]string{}),
name = "fg_bar",
)`,
`// BUILD file`, `// BUILD file`,
}, },
filesystem: map[string]string{
"other/BUILD.bazel": `// BUILD file`,
},
}, },
} }
@@ -1195,16 +1182,6 @@ func TestGlobExcludeSrcs(t *testing.T) {
exclude_srcs: ["c.txt"], exclude_srcs: ["c.txt"],
bazel_module: { bp2build_available: true }, bazel_module: { bp2build_available: true },
}`, }`,
expectedBazelTargets: []string{`filegroup(
name = "fg_foo",
srcs = [
"a.txt",
"b.txt",
"//dir:e.txt",
"//dir:f.txt",
],
)`,
},
filesystem: map[string]string{ filesystem: map[string]string{
"a.txt": "", "a.txt": "",
"b.txt": "", "b.txt": "",
@@ -1213,6 +1190,16 @@ func TestGlobExcludeSrcs(t *testing.T) {
"dir/e.txt": "", "dir/e.txt": "",
"dir/f.txt": "", "dir/f.txt": "",
}, },
expectedBazelTargets: []string{
makeBazelTarget("filegroup", "fg_foo", map[string]string{
"srcs": `[
"a.txt",
"b.txt",
"//dir:e.txt",
"//dir:f.txt",
]`,
}),
},
}, },
{ {
description: "filegroup in subdir exclude_srcs", description: "filegroup in subdir exclude_srcs",
@@ -1235,66 +1222,22 @@ func TestGlobExcludeSrcs(t *testing.T) {
"dir/subdir/e.txt": "", "dir/subdir/e.txt": "",
"dir/subdir/f.txt": "", "dir/subdir/f.txt": "",
}, },
expectedBazelTargets: []string{`filegroup( expectedBazelTargets: []string{
name = "fg_foo", makeBazelTarget("filegroup", "fg_foo", map[string]string{
srcs = [ "srcs": `[
"a.txt", "a.txt",
"//dir/subdir:e.txt", "//dir/subdir:e.txt",
"//dir/subdir:f.txt", "//dir/subdir:f.txt",
], ]`,
)`, }),
}, },
}, },
} }
dir := "."
for _, testCase := range testCases { for _, testCase := range testCases {
fs := make(map[string][]byte) t.Run(testCase.description, func(t *testing.T) {
toParse := []string{ runBp2BuildTestCaseSimple(t, testCase)
"Android.bp", })
}
for f, content := range testCase.filesystem {
if strings.HasSuffix(f, "Android.bp") {
toParse = append(toParse, f)
}
fs[f] = []byte(content)
}
config := android.TestConfig(buildDir, nil, testCase.blueprint, fs)
ctx := android.NewTestContext(config)
ctx.RegisterModuleType(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestFactory)
ctx.RegisterBp2BuildMutator(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestBp2BuildMutator)
ctx.RegisterForBazelConversion()
_, errs := ctx.ParseFileList(dir, toParse)
if errored(t, testCase, errs) {
continue
}
_, errs = ctx.ResolveDependencies(config)
if errored(t, testCase, errs) {
continue
}
checkDir := dir
if testCase.dir != "" {
checkDir = testCase.dir
}
codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build)
bazelTargets, err := generateBazelTargetsForDir(codegenCtx, checkDir)
android.FailIfErrored(t, err)
if actualCount, expectedCount := len(bazelTargets), len(testCase.expectedBazelTargets); actualCount != expectedCount {
t.Errorf("%s: Expected %d bazel target, got %d\n%s", testCase.description, expectedCount, actualCount, bazelTargets)
} else {
for i, target := range bazelTargets {
if w, g := testCase.expectedBazelTargets[i], target.content; w != g {
t.Errorf(
"%s: Expected generated Bazel target to be '%s', got '%s'",
testCase.description,
w,
g,
)
}
}
}
} }
} }
@@ -1305,22 +1248,16 @@ func TestCommonBp2BuildModuleAttrs(t *testing.T) {
moduleTypeUnderTest: "filegroup", moduleTypeUnderTest: "filegroup",
moduleTypeUnderTestFactory: android.FileGroupFactory, moduleTypeUnderTestFactory: android.FileGroupFactory,
moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build, moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
blueprint: `filegroup { blueprint: simpleModuleDoNotConvertBp2build("filegroup", "reqd") + `
name: "reqd",
}
filegroup { filegroup {
name: "fg_foo", name: "fg_foo",
required: ["reqd"], required: ["reqd"],
bazel_module: { bp2build_available: true }, bazel_module: { bp2build_available: true },
}`, }`,
expectedBazelTargets: []string{`filegroup( expectedBazelTargets: []string{
name = "fg_foo", makeBazelTarget("filegroup", "fg_foo", map[string]string{
data = [":reqd"], "data": `[":reqd"]`,
)`, }),
`filegroup(
name = "reqd",
)`,
}, },
}, },
{ {
@@ -1328,16 +1265,8 @@ filegroup {
moduleTypeUnderTest: "python_library", moduleTypeUnderTest: "python_library",
moduleTypeUnderTestFactory: python.PythonLibraryFactory, moduleTypeUnderTestFactory: python.PythonLibraryFactory,
moduleTypeUnderTestBp2BuildMutator: python.PythonLibraryBp2Build, moduleTypeUnderTestBp2BuildMutator: python.PythonLibraryBp2Build,
blueprint: `python_library { blueprint: simpleModuleDoNotConvertBp2build("python_library", "reqdx86") +
name: "reqdx86", simpleModuleDoNotConvertBp2build("python_library", "reqdarm") + `
bazel_module: { bp2build_available: false, },
}
python_library {
name: "reqdarm",
bazel_module: { bp2build_available: false, },
}
python_library { python_library {
name: "fg_foo", name: "fg_foo",
arch: { arch: {
@@ -1350,15 +1279,15 @@ python_library {
}, },
bazel_module: { bp2build_available: true }, bazel_module: { bp2build_available: true },
}`, }`,
expectedBazelTargets: []string{`py_library( expectedBazelTargets: []string{
name = "fg_foo", makeBazelTarget("py_library", "fg_foo", map[string]string{
data = select({ "data": `select({
"//build/bazel/platforms/arch:arm": [":reqdarm"], "//build/bazel/platforms/arch:arm": [":reqdarm"],
"//build/bazel/platforms/arch:x86": [":reqdx86"], "//build/bazel/platforms/arch:x86": [":reqdx86"],
"//conditions:default": [], "//conditions:default": [],
}), })`,
srcs_version = "PY3", "srcs_version": `"PY3"`,
)`, }),
}, },
}, },
{ {
@@ -1366,11 +1295,11 @@ python_library {
moduleTypeUnderTest: "python_library", moduleTypeUnderTest: "python_library",
moduleTypeUnderTestFactory: python.PythonLibraryFactory, moduleTypeUnderTestFactory: python.PythonLibraryFactory,
moduleTypeUnderTestBp2BuildMutator: python.PythonLibraryBp2Build, moduleTypeUnderTestBp2BuildMutator: python.PythonLibraryBp2Build,
blueprint: `python_library { filesystem: map[string]string{
name: "reqd", "data.bin": "",
srcs: ["src.py"], "src.py": "",
} },
blueprint: simpleModuleDoNotConvertBp2build("python_library", "reqd") + `
python_library { python_library {
name: "fg_foo", name: "fg_foo",
data: ["data.bin"], data: ["data.bin"],
@@ -1378,23 +1307,13 @@ python_library {
bazel_module: { bp2build_available: true }, bazel_module: { bp2build_available: true },
}`, }`,
expectedBazelTargets: []string{ expectedBazelTargets: []string{
`py_library( makeBazelTarget("py_library", "fg_foo", map[string]string{
name = "fg_foo", "data": `[
data = [
"data.bin", "data.bin",
":reqd", ":reqd",
], ]`,
srcs_version = "PY3", "srcs_version": `"PY3"`,
)`, }),
`py_library(
name = "reqd",
srcs = ["src.py"],
srcs_version = "PY3",
)`,
},
filesystem: map[string]string{
"data.bin": "",
"src.py": "",
}, },
}, },
{ {
@@ -1402,28 +1321,23 @@ python_library {
moduleTypeUnderTest: "filegroup", moduleTypeUnderTest: "filegroup",
moduleTypeUnderTestFactory: android.FileGroupFactory, moduleTypeUnderTestFactory: android.FileGroupFactory,
moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build, moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
blueprint: `filegroup { blueprint: simpleModuleDoNotConvertBp2build("filegroup", "reqd") + `
name: "reqd"
}
filegroup { filegroup {
name: "fg_foo", name: "fg_foo",
required: ["reqd"], required: ["reqd"],
bazel_module: { bp2build_available: true }, bazel_module: { bp2build_available: true },
}`, }`,
expectedBazelTargets: []string{ expectedBazelTargets: []string{
`filegroup( makeBazelTarget("filegroup", "fg_foo", map[string]string{
name = "fg_foo", "data": `[":reqd"]`,
data = [":reqd"], }),
)`,
`filegroup(
name = "reqd",
)`,
}, },
filesystem: map[string]string{},
}, },
} }
for _, test := range testCases { for _, tc := range testCases {
runBp2BuildTestCaseSimple(t, test) t.Run(tc.description, func(t *testing.T) {
runBp2BuildTestCaseSimple(t, tc)
})
} }
} }

View File

@@ -28,6 +28,26 @@ const (
compatibleWithPlaceHolder = "{target_compatible_with}" compatibleWithPlaceHolder = "{target_compatible_with}"
) )
type testBazelTarget struct {
typ string
name string
attrs attrNameToString
}
func generateBazelTargetsForTest(targets []testBazelTarget) []string {
ret := make([]string, 0, len(targets))
for _, t := range targets {
ret = append(ret, makeBazelTarget(t.typ, t.name, t.attrs))
}
return ret
}
type ccBinaryBp2buildTestCase struct {
description string
blueprint string
targets []testBazelTarget
}
func registerCcBinaryModuleTypes(ctx android.RegistrationContext) { func registerCcBinaryModuleTypes(ctx android.RegistrationContext) {
cc.RegisterCCBuildComponents(ctx) cc.RegisterCCBuildComponents(ctx)
ctx.RegisterModuleType("filegroup", android.FileGroupFactory) ctx.RegisterModuleType("filegroup", android.FileGroupFactory)
@@ -36,55 +56,56 @@ func registerCcBinaryModuleTypes(ctx android.RegistrationContext) {
ctx.RegisterModuleType("genrule", genrule.GenRuleFactory) ctx.RegisterModuleType("genrule", genrule.GenRuleFactory)
} }
var binaryReplacer = strings.NewReplacer(ccBinaryTypePlaceHolder, "cc_binary", compatibleWithPlaceHolder, "") var binaryReplacer = strings.NewReplacer(ccBinaryTypePlaceHolder, "cc_binary")
var hostBinaryReplacer = strings.NewReplacer(ccBinaryTypePlaceHolder, "cc_binary_host", compatibleWithPlaceHolder, ` var hostBinaryReplacer = strings.NewReplacer(ccBinaryTypePlaceHolder, "cc_binary_host")
target_compatible_with = select({
"//build/bazel/platforms/os:android": ["@platforms//:incompatible"],
"//conditions:default": [],
}),`)
func runCcBinaryTests(t *testing.T, tc bp2buildTestCase) { func runCcBinaryTests(t *testing.T, tc ccBinaryBp2buildTestCase) {
t.Helper() t.Helper()
runCcBinaryTestCase(t, tc) runCcBinaryTestCase(t, tc)
runCcHostBinaryTestCase(t, tc) runCcHostBinaryTestCase(t, tc)
} }
func runCcBinaryTestCase(t *testing.T, tc bp2buildTestCase) { func runCcBinaryTestCase(t *testing.T, tc ccBinaryBp2buildTestCase) {
t.Helper() t.Helper()
testCase := tc moduleTypeUnderTest := "cc_binary"
testCase.expectedBazelTargets = append([]string{}, tc.expectedBazelTargets...) testCase := bp2buildTestCase{
testCase.moduleTypeUnderTest = "cc_binary" expectedBazelTargets: generateBazelTargetsForTest(tc.targets),
testCase.moduleTypeUnderTestFactory = cc.BinaryFactory moduleTypeUnderTest: moduleTypeUnderTest,
testCase.moduleTypeUnderTestBp2BuildMutator = cc.BinaryBp2build moduleTypeUnderTestFactory: cc.BinaryFactory,
testCase.description = fmt.Sprintf("%s %s", testCase.moduleTypeUnderTest, testCase.description) moduleTypeUnderTestBp2BuildMutator: cc.BinaryBp2build,
testCase.blueprint = binaryReplacer.Replace(testCase.blueprint) description: fmt.Sprintf("%s %s", moduleTypeUnderTest, tc.description),
for i, et := range testCase.expectedBazelTargets { blueprint: binaryReplacer.Replace(tc.blueprint),
testCase.expectedBazelTargets[i] = binaryReplacer.Replace(et)
} }
t.Run(testCase.description, func(t *testing.T) { t.Run(testCase.description, func(t *testing.T) {
runBp2BuildTestCase(t, registerCcBinaryModuleTypes, testCase) runBp2BuildTestCase(t, registerCcBinaryModuleTypes, testCase)
}) })
} }
func runCcHostBinaryTestCase(t *testing.T, tc bp2buildTestCase) { func runCcHostBinaryTestCase(t *testing.T, tc ccBinaryBp2buildTestCase) {
t.Helper() t.Helper()
testCase := tc testCase := tc
testCase.expectedBazelTargets = append([]string{}, tc.expectedBazelTargets...) for i, t := range testCase.targets {
testCase.moduleTypeUnderTest = "cc_binary_host" t.attrs["target_compatible_with"] = `select({
testCase.moduleTypeUnderTestFactory = cc.BinaryHostFactory "//build/bazel/platforms/os:android": ["@platforms//:incompatible"],
testCase.moduleTypeUnderTestBp2BuildMutator = cc.BinaryHostBp2build "//conditions:default": [],
testCase.description = fmt.Sprintf("%s %s", testCase.moduleTypeUnderTest, testCase.description) })`
testCase.blueprint = hostBinaryReplacer.Replace(testCase.blueprint) testCase.targets[i] = t
for i, et := range testCase.expectedBazelTargets {
testCase.expectedBazelTargets[i] = hostBinaryReplacer.Replace(et)
} }
moduleTypeUnderTest := "cc_binary_host"
t.Run(testCase.description, func(t *testing.T) { t.Run(testCase.description, func(t *testing.T) {
runBp2BuildTestCase(t, registerCcBinaryModuleTypes, testCase) runBp2BuildTestCase(t, registerCcBinaryModuleTypes, bp2buildTestCase{
expectedBazelTargets: generateBazelTargetsForTest(testCase.targets),
moduleTypeUnderTest: moduleTypeUnderTest,
moduleTypeUnderTestFactory: cc.BinaryHostFactory,
moduleTypeUnderTestBp2BuildMutator: cc.BinaryHostBp2build,
description: fmt.Sprintf("%s %s", moduleTypeUnderTest, tc.description),
blueprint: hostBinaryReplacer.Replace(testCase.blueprint),
})
}) })
} }
func TestBasicCcBinary(t *testing.T) { func TestBasicCcBinary(t *testing.T) {
runCcBinaryTests(t, bp2buildTestCase{ runCcBinaryTests(t, ccBinaryBp2buildTestCase{
description: "basic -- properties -> attrs with little/no transformation", description: "basic -- properties -> attrs with little/no transformation",
blueprint: ` blueprint: `
{rule_name} { {rule_name} {
@@ -107,33 +128,35 @@ func TestBasicCcBinary(t *testing.T) {
}, },
} }
`, `,
expectedBazelTargets: []string{`cc_binary( targets: []testBazelTarget{
name = "foo", {"cc_binary", "foo", attrNameToString{
absolute_includes = ["absolute_dir"], "absolute_includes": `["absolute_dir"]`,
asflags = ["-Dasflag"], "asflags": `["-Dasflag"]`,
conlyflags = ["-Dconlyflag"], "conlyflags": `["-Dconlyflag"]`,
copts = ["-Dcopt"], "copts": `["-Dcopt"]`,
cppflags = ["-Dcppflag"], "cppflags": `["-Dcppflag"]`,
linkopts = ["ld-flag"], "linkopts": `["ld-flag"]`,
local_includes = [ "local_includes": `[
"dir", "dir",
".", ".",
], ]`,
rtti = True, "rtti": `True`,
srcs = ["a.cc"], "srcs": `["a.cc"]`,
strip = { "strip": `{
"all": True, "all": True,
"keep_symbols": True, "keep_symbols": True,
"keep_symbols_and_debug_frame": True, "keep_symbols_and_debug_frame": True,
"keep_symbols_list": ["symbol"], "keep_symbols_list": ["symbol"],
"none": True, "none": True,
},{target_compatible_with} }`,
)`}, },
},
},
}) })
} }
func TestCcBinaryWithSharedLdflagDisableFeature(t *testing.T) { func TestCcBinaryWithSharedLdflagDisableFeature(t *testing.T) {
runCcBinaryTests(t, bp2buildTestCase{ runCcBinaryTests(t, ccBinaryBp2buildTestCase{
description: `ldflag "-shared" disables static_flag feature`, description: `ldflag "-shared" disables static_flag feature`,
blueprint: ` blueprint: `
{rule_name} { {rule_name} {
@@ -142,16 +165,18 @@ func TestCcBinaryWithSharedLdflagDisableFeature(t *testing.T) {
include_build_directory: false, include_build_directory: false,
} }
`, `,
expectedBazelTargets: []string{`cc_binary( targets: []testBazelTarget{
name = "foo", {"cc_binary", "foo", attrNameToString{
features = ["-static_flag"], "features": `["-static_flag"]`,
linkopts = ["-shared"],{target_compatible_with} "linkopts": `["-shared"]`,
)`}, },
},
},
}) })
} }
func TestCcBinaryWithLinkStatic(t *testing.T) { func TestCcBinaryWithLinkStatic(t *testing.T) {
runCcBinaryTests(t, bp2buildTestCase{ runCcBinaryTests(t, ccBinaryBp2buildTestCase{
description: "link static", description: "link static",
blueprint: ` blueprint: `
{rule_name} { {rule_name} {
@@ -160,15 +185,17 @@ func TestCcBinaryWithLinkStatic(t *testing.T) {
include_build_directory: false, include_build_directory: false,
} }
`, `,
expectedBazelTargets: []string{`cc_binary( targets: []testBazelTarget{
name = "foo", {"cc_binary", "foo", attrNameToString{
linkshared = False,{target_compatible_with} "linkshared": `False`,
)`}, },
},
},
}) })
} }
func TestCcBinaryVersionScript(t *testing.T) { func TestCcBinaryVersionScript(t *testing.T) {
runCcBinaryTests(t, bp2buildTestCase{ runCcBinaryTests(t, ccBinaryBp2buildTestCase{
description: `version script`, description: `version script`,
blueprint: ` blueprint: `
{rule_name} { {rule_name} {
@@ -177,16 +204,18 @@ func TestCcBinaryVersionScript(t *testing.T) {
version_script: "vs", version_script: "vs",
} }
`, `,
expectedBazelTargets: []string{`cc_binary( targets: []testBazelTarget{
name = "foo", {"cc_binary", "foo", attrNameToString{
additional_linker_inputs = ["vs"], "additional_linker_inputs": `["vs"]`,
linkopts = ["-Wl,--version-script,$(location vs)"],{target_compatible_with} "linkopts": `["-Wl,--version-script,$(location vs)"]`,
)`}, },
},
},
}) })
} }
func TestCcBinarySplitSrcsByLang(t *testing.T) { func TestCcBinarySplitSrcsByLang(t *testing.T) {
runCcHostBinaryTestCase(t, bp2buildTestCase{ runCcHostBinaryTestCase(t, ccBinaryBp2buildTestCase{
description: "split srcs by lang", description: "split srcs by lang",
blueprint: ` blueprint: `
{rule_name} { {rule_name} {
@@ -200,26 +229,28 @@ func TestCcBinarySplitSrcsByLang(t *testing.T) {
include_build_directory: false, include_build_directory: false,
} }
` + simpleModuleDoNotConvertBp2build("filegroup", "fg_foo"), ` + simpleModuleDoNotConvertBp2build("filegroup", "fg_foo"),
expectedBazelTargets: []string{`cc_binary( targets: []testBazelTarget{
name = "foo", {"cc_binary", "foo", attrNameToString{
srcs = [ "srcs": `[
"cpponly.cpp", "cpponly.cpp",
":fg_foo_cpp_srcs", ":fg_foo_cpp_srcs",
], ]`,
srcs_as = [ "srcs_as": `[
"asonly.S", "asonly.S",
":fg_foo_as_srcs", ":fg_foo_as_srcs",
], ]`,
srcs_c = [ "srcs_c": `[
"conly.c", "conly.c",
":fg_foo_c_srcs", ":fg_foo_c_srcs",
],{target_compatible_with} ]`,
)`}, },
},
},
}) })
} }
func TestCcBinaryDoNotDistinguishBetweenDepsAndImplementationDeps(t *testing.T) { func TestCcBinaryDoNotDistinguishBetweenDepsAndImplementationDeps(t *testing.T) {
runCcBinaryTestCase(t, bp2buildTestCase{ runCcBinaryTestCase(t, ccBinaryBp2buildTestCase{
description: "no implementation deps", description: "no implementation deps",
blueprint: ` blueprint: `
genrule { genrule {
@@ -251,26 +282,28 @@ genrule {
simpleModuleDoNotConvertBp2build("cc_library_static", "not_explicitly_exported_whole_static_dep") + simpleModuleDoNotConvertBp2build("cc_library_static", "not_explicitly_exported_whole_static_dep") +
simpleModuleDoNotConvertBp2build("cc_library", "shared_dep") + simpleModuleDoNotConvertBp2build("cc_library", "shared_dep") +
simpleModuleDoNotConvertBp2build("cc_library", "implementation_shared_dep"), simpleModuleDoNotConvertBp2build("cc_library", "implementation_shared_dep"),
expectedBazelTargets: []string{`cc_binary( targets: []testBazelTarget{
name = "foo", {"cc_binary", "foo", attrNameToString{
deps = [ "deps": `[
":implementation_static_dep", ":implementation_static_dep",
":static_dep", ":static_dep",
], ]`,
dynamic_deps = [ "dynamic_deps": `[
":implementation_shared_dep", ":implementation_shared_dep",
":shared_dep", ":shared_dep",
], ]`,
srcs = [ "srcs": `[
"foo.cpp", "foo.cpp",
":generated_hdr", ":generated_hdr",
":export_generated_hdr", ":export_generated_hdr",
],{target_compatible_with} ]`,
whole_archive_deps = [ "whole_archive_deps": `[
":not_explicitly_exported_whole_static_dep", ":not_explicitly_exported_whole_static_dep",
":whole_static_dep", ":whole_static_dep",
], ]`,
)`}, },
},
},
}) })
} }
@@ -278,19 +311,21 @@ func TestCcBinaryNocrtTests(t *testing.T) {
baseTestCases := []struct { baseTestCases := []struct {
description string description string
soongProperty string soongProperty string
bazelAttr string bazelAttr attrNameToString
}{ }{
{ {
description: "nocrt: true", description: "nocrt: true",
soongProperty: `nocrt: true,`, soongProperty: `nocrt: true,`,
bazelAttr: ` link_crt = False,`, bazelAttr: attrNameToString{"link_crt": `False`},
}, },
{ {
description: "nocrt: false", description: "nocrt: false",
soongProperty: `nocrt: false,`, soongProperty: `nocrt: false,`,
bazelAttr: attrNameToString{},
}, },
{ {
description: "nocrt: not set", description: "nocrt: not set",
bazelAttr: attrNameToString{},
}, },
} }
@@ -300,24 +335,16 @@ func TestCcBinaryNocrtTests(t *testing.T) {
} }
` `
baseBazelTarget := `cc_binary(
name = "foo",%s{target_compatible_with}
)`
for _, btc := range baseTestCases { for _, btc := range baseTestCases {
prop := btc.soongProperty prop := btc.soongProperty
if len(prop) > 0 { if len(prop) > 0 {
prop = "\n" + prop prop = "\n" + prop
} }
attr := btc.bazelAttr runCcBinaryTests(t, ccBinaryBp2buildTestCase{
if len(attr) > 0 {
attr = "\n" + attr
}
runCcBinaryTests(t, bp2buildTestCase{
description: btc.description, description: btc.description,
blueprint: fmt.Sprintf(baseBlueprint, prop), blueprint: fmt.Sprintf(baseBlueprint, prop),
expectedBazelTargets: []string{ targets: []testBazelTarget{
fmt.Sprintf(baseBazelTarget, attr), {"cc_binary", "foo", btc.bazelAttr},
}, },
}) })
} }
@@ -327,20 +354,21 @@ func TestCcBinaryNo_libcrtTests(t *testing.T) {
baseTestCases := []struct { baseTestCases := []struct {
description string description string
soongProperty string soongProperty string
bazelAttr string bazelAttr attrNameToString
}{ }{
{ {
description: "no_libcrt: true", description: "no_libcrt: true",
soongProperty: `no_libcrt: true,`, soongProperty: `no_libcrt: true,`,
bazelAttr: ` use_libcrt = False,`, bazelAttr: attrNameToString{"use_libcrt": `False`},
}, },
{ {
description: "no_libcrt: false", description: "no_libcrt: false",
soongProperty: `no_libcrt: false,`, soongProperty: `no_libcrt: false,`,
bazelAttr: ` use_libcrt = True,`, bazelAttr: attrNameToString{"use_libcrt": `True`},
}, },
{ {
description: "no_libcrt: not set", description: "no_libcrt: not set",
bazelAttr: attrNameToString{},
}, },
} }
@@ -350,24 +378,16 @@ func TestCcBinaryNo_libcrtTests(t *testing.T) {
} }
` `
baseBazelTarget := `cc_binary(
name = "foo",{target_compatible_with}%s
)`
for _, btc := range baseTestCases { for _, btc := range baseTestCases {
prop := btc.soongProperty prop := btc.soongProperty
if len(prop) > 0 { if len(prop) > 0 {
prop = "\n" + prop prop = "\n" + prop
} }
attr := btc.bazelAttr runCcBinaryTests(t, ccBinaryBp2buildTestCase{
if len(attr) > 0 {
attr = "\n" + attr
}
runCcBinaryTests(t, bp2buildTestCase{
description: btc.description, description: btc.description,
blueprint: fmt.Sprintf(baseBlueprint, prop), blueprint: fmt.Sprintf(baseBlueprint, prop),
expectedBazelTargets: []string{ targets: []testBazelTarget{
fmt.Sprintf(baseBazelTarget, attr), {"cc_binary", "foo", btc.bazelAttr},
}, },
}) })
} }
@@ -377,31 +397,35 @@ func TestCcBinaryPropertiesToFeatures(t *testing.T) {
baseTestCases := []struct { baseTestCases := []struct {
description string description string
soongProperty string soongProperty string
bazelAttr string bazelAttr attrNameToString
}{ }{
{ {
description: "pack_relocation: true", description: "pack_relocation: true",
soongProperty: `pack_relocations: true,`, soongProperty: `pack_relocations: true,`,
bazelAttr: attrNameToString{},
}, },
{ {
description: "pack_relocations: false", description: "pack_relocations: false",
soongProperty: `pack_relocations: false,`, soongProperty: `pack_relocations: false,`,
bazelAttr: ` features = ["disable_pack_relocations"],`, bazelAttr: attrNameToString{"features": `["disable_pack_relocations"]`},
}, },
{ {
description: "pack_relocations: not set", description: "pack_relocations: not set",
bazelAttr: attrNameToString{},
}, },
{ {
description: "pack_relocation: true", description: "pack_relocation: true",
soongProperty: `allow_undefined_symbols: true,`, soongProperty: `allow_undefined_symbols: true,`,
bazelAttr: ` features = ["-no_undefined_symbols"],`, bazelAttr: attrNameToString{"features": `["-no_undefined_symbols"]`},
}, },
{ {
description: "allow_undefined_symbols: false", description: "allow_undefined_symbols: false",
soongProperty: `allow_undefined_symbols: false,`, soongProperty: `allow_undefined_symbols: false,`,
bazelAttr: attrNameToString{},
}, },
{ {
description: "allow_undefined_symbols: not set", description: "allow_undefined_symbols: not set",
bazelAttr: attrNameToString{},
}, },
} }
@@ -410,25 +434,16 @@ func TestCcBinaryPropertiesToFeatures(t *testing.T) {
include_build_directory: false, include_build_directory: false,
} }
` `
baseBazelTarget := `cc_binary(
name = "foo",%s{target_compatible_with}
)`
for _, btc := range baseTestCases { for _, btc := range baseTestCases {
prop := btc.soongProperty prop := btc.soongProperty
if len(prop) > 0 { if len(prop) > 0 {
prop = "\n" + prop prop = "\n" + prop
} }
attr := btc.bazelAttr runCcBinaryTests(t, ccBinaryBp2buildTestCase{
if len(attr) > 0 {
attr = "\n" + attr
}
runCcBinaryTests(t, bp2buildTestCase{
description: btc.description, description: btc.description,
blueprint: fmt.Sprintf(baseBlueprint, prop), blueprint: fmt.Sprintf(baseBlueprint, prop),
expectedBazelTargets: []string{ targets: []testBazelTarget{
fmt.Sprintf(baseBazelTarget, attr), {"cc_binary", "foo", btc.bazelAttr},
}, },
}) })
} }

View File

@@ -39,15 +39,15 @@ cc_genrule {
func runCcGenruleTestCase(t *testing.T, tc bp2buildTestCase) { func runCcGenruleTestCase(t *testing.T, tc bp2buildTestCase) {
t.Helper() t.Helper()
(&tc).moduleTypeUnderTest = "cc_genrule"
(&tc).moduleTypeUnderTestFactory = cc.GenRuleFactory
(&tc).moduleTypeUnderTestBp2BuildMutator = genrule.CcGenruleBp2Build
runBp2BuildTestCase(t, func(ctx android.RegistrationContext) {}, tc) runBp2BuildTestCase(t, func(ctx android.RegistrationContext) {}, tc)
} }
func TestCliVariableReplacement(t *testing.T) { func TestCliVariableReplacement(t *testing.T) {
runCcGenruleTestCase(t, bp2buildTestCase{ runCcGenruleTestCase(t, bp2buildTestCase{
description: "cc_genrule with command line variable replacements", description: "cc_genrule with command line variable replacements",
moduleTypeUnderTest: "cc_genrule",
moduleTypeUnderTestFactory: cc.GenRuleFactory,
moduleTypeUnderTestBp2BuildMutator: genrule.CcGenruleBp2Build,
blueprint: `cc_genrule { blueprint: `cc_genrule {
name: "foo.tool", name: "foo.tool",
out: ["foo_tool.out"], out: ["foo_tool.out"],
@@ -65,29 +65,24 @@ cc_genrule {
bazel_module: { bp2build_available: true }, bazel_module: { bp2build_available: true },
}`, }`,
expectedBazelTargets: []string{ expectedBazelTargets: []string{
`genrule( makeBazelTarget("genrule", "foo", attrNameToString{
name = "foo", "cmd": `"$(location :foo.tool) --genDir=$(RULEDIR) arg $(SRCS) $(OUTS)"`,
cmd = "$(location :foo.tool) --genDir=$(RULEDIR) arg $(SRCS) $(OUTS)", "outs": `["foo.out"]`,
outs = ["foo.out"], "srcs": `["foo.in"]`,
srcs = ["foo.in"], "tools": `[":foo.tool"]`,
tools = [":foo.tool"], }),
)`, makeBazelTarget("genrule", "foo.tool", attrNameToString{
`genrule( "cmd": `"cp $(SRCS) $(OUTS)"`,
name = "foo.tool", "outs": `["foo_tool.out"]`,
cmd = "cp $(SRCS) $(OUTS)", "srcs": `["foo_tool.in"]`,
outs = ["foo_tool.out"], }),
srcs = ["foo_tool.in"],
)`,
}, },
}) })
} }
func TestUsingLocationsLabel(t *testing.T) { func TestUsingLocationsLabel(t *testing.T) {
runCcGenruleTestCase(t, bp2buildTestCase{ runCcGenruleTestCase(t, bp2buildTestCase{
description: "cc_genrule using $(locations :label)", description: "cc_genrule using $(locations :label)",
moduleTypeUnderTest: "cc_genrule",
moduleTypeUnderTestFactory: cc.GenRuleFactory,
moduleTypeUnderTestBp2BuildMutator: genrule.CcGenruleBp2Build,
blueprint: `cc_genrule { blueprint: `cc_genrule {
name: "foo.tools", name: "foo.tools",
out: ["foo_tool.out", "foo_tool2.out"], out: ["foo_tool.out", "foo_tool2.out"],
@@ -104,32 +99,28 @@ cc_genrule {
cmd: "$(locations :foo.tools) -s $(out) $(in)", cmd: "$(locations :foo.tools) -s $(out) $(in)",
bazel_module: { bp2build_available: true }, bazel_module: { bp2build_available: true },
}`, }`,
expectedBazelTargets: []string{`genrule( expectedBazelTargets: []string{
name = "foo", makeBazelTarget("genrule", "foo", attrNameToString{
cmd = "$(locations :foo.tools) -s $(OUTS) $(SRCS)", "cmd": `"$(locations :foo.tools) -s $(OUTS) $(SRCS)"`,
outs = ["foo.out"], "outs": `["foo.out"]`,
srcs = ["foo.in"], "srcs": `["foo.in"]`,
tools = [":foo.tools"], "tools": `[":foo.tools"]`,
)`, }),
`genrule( makeBazelTarget("genrule", "foo.tools", attrNameToString{
name = "foo.tools", "cmd": `"cp $(SRCS) $(OUTS)"`,
cmd = "cp $(SRCS) $(OUTS)", "outs": `[
outs = [
"foo_tool.out", "foo_tool.out",
"foo_tool2.out", "foo_tool2.out",
], ]`,
srcs = ["foo_tool.in"], "srcs": `["foo_tool.in"]`,
)`, }),
}, },
}) })
} }
func TestUsingLocationsAbsoluteLabel(t *testing.T) { func TestUsingLocationsAbsoluteLabel(t *testing.T) {
runCcGenruleTestCase(t, bp2buildTestCase{ runCcGenruleTestCase(t, bp2buildTestCase{
description: "cc_genrule using $(locations //absolute:label)", description: "cc_genrule using $(locations //absolute:label)",
moduleTypeUnderTest: "cc_genrule",
moduleTypeUnderTestFactory: cc.GenRuleFactory,
moduleTypeUnderTestBp2BuildMutator: genrule.CcGenruleBp2Build,
blueprint: `cc_genrule { blueprint: `cc_genrule {
name: "foo", name: "foo",
out: ["foo.out"], out: ["foo.out"],
@@ -138,24 +129,21 @@ func TestUsingLocationsAbsoluteLabel(t *testing.T) {
cmd: "$(locations :foo.tool) -s $(out) $(in)", cmd: "$(locations :foo.tool) -s $(out) $(in)",
bazel_module: { bp2build_available: true }, bazel_module: { bp2build_available: true },
}`, }`,
expectedBazelTargets: []string{`genrule(
name = "foo",
cmd = "$(locations //other:foo.tool) -s $(OUTS) $(SRCS)",
outs = ["foo.out"],
srcs = ["foo.in"],
tools = ["//other:foo.tool"],
)`,
},
filesystem: otherCcGenruleBp, filesystem: otherCcGenruleBp,
expectedBazelTargets: []string{
makeBazelTarget("genrule", "foo", attrNameToString{
"cmd": `"$(locations //other:foo.tool) -s $(OUTS) $(SRCS)"`,
"outs": `["foo.out"]`,
"srcs": `["foo.in"]`,
"tools": `["//other:foo.tool"]`,
}),
},
}) })
} }
func TestSrcsUsingAbsoluteLabel(t *testing.T) { func TestSrcsUsingAbsoluteLabel(t *testing.T) {
runCcGenruleTestCase(t, bp2buildTestCase{ runCcGenruleTestCase(t, bp2buildTestCase{
description: "cc_genrule srcs using $(locations //absolute:label)", description: "cc_genrule srcs using $(locations //absolute:label)",
moduleTypeUnderTest: "cc_genrule",
moduleTypeUnderTestFactory: cc.GenRuleFactory,
moduleTypeUnderTestBp2BuildMutator: genrule.CcGenruleBp2Build,
blueprint: `cc_genrule { blueprint: `cc_genrule {
name: "foo", name: "foo",
out: ["foo.out"], out: ["foo.out"],
@@ -164,24 +152,21 @@ func TestSrcsUsingAbsoluteLabel(t *testing.T) {
cmd: "$(locations :foo.tool) -s $(out) $(location :other.tool)", cmd: "$(locations :foo.tool) -s $(out) $(location :other.tool)",
bazel_module: { bp2build_available: true }, bazel_module: { bp2build_available: true },
}`, }`,
expectedBazelTargets: []string{`genrule(
name = "foo",
cmd = "$(locations //other:foo.tool) -s $(OUTS) $(location //other:other.tool)",
outs = ["foo.out"],
srcs = ["//other:other.tool"],
tools = ["//other:foo.tool"],
)`,
},
filesystem: otherCcGenruleBp, filesystem: otherCcGenruleBp,
expectedBazelTargets: []string{
makeBazelTarget("genrule", "foo", attrNameToString{
"cmd": `"$(locations //other:foo.tool) -s $(OUTS) $(location //other:other.tool)"`,
"outs": `["foo.out"]`,
"srcs": `["//other:other.tool"]`,
"tools": `["//other:foo.tool"]`,
}),
},
}) })
} }
func TestLocationsLabelUsesFirstToolFile(t *testing.T) { func TestLocationsLabelUsesFirstToolFile(t *testing.T) {
runCcGenruleTestCase(t, bp2buildTestCase{ runCcGenruleTestCase(t, bp2buildTestCase{
description: "cc_genrule using $(location) label should substitute first tool label automatically", description: "cc_genrule using $(location) label should substitute first tool label automatically",
moduleTypeUnderTest: "cc_genrule",
moduleTypeUnderTestFactory: cc.GenRuleFactory,
moduleTypeUnderTestBp2BuildMutator: genrule.CcGenruleBp2Build,
blueprint: `cc_genrule { blueprint: `cc_genrule {
name: "foo", name: "foo",
out: ["foo.out"], out: ["foo.out"],
@@ -190,27 +175,24 @@ func TestLocationsLabelUsesFirstToolFile(t *testing.T) {
cmd: "$(location) -s $(out) $(in)", cmd: "$(location) -s $(out) $(in)",
bazel_module: { bp2build_available: true }, bazel_module: { bp2build_available: true },
}`, }`,
expectedBazelTargets: []string{`genrule( filesystem: otherCcGenruleBp,
name = "foo", expectedBazelTargets: []string{
cmd = "$(location //other:foo.tool) -s $(OUTS) $(SRCS)", makeBazelTarget("genrule", "foo", attrNameToString{
outs = ["foo.out"], "cmd": `"$(location //other:foo.tool) -s $(OUTS) $(SRCS)"`,
srcs = ["foo.in"], "outs": `["foo.out"]`,
tools = [ "srcs": `["foo.in"]`,
"tools": `[
"//other:foo.tool", "//other:foo.tool",
"//other:other.tool", "//other:other.tool",
], ]`,
)`, }),
}, },
filesystem: otherCcGenruleBp,
}) })
} }
func TestLocationsLabelUsesFirstTool(t *testing.T) { func TestLocationsLabelUsesFirstTool(t *testing.T) {
runCcGenruleTestCase(t, bp2buildTestCase{ runCcGenruleTestCase(t, bp2buildTestCase{
description: "cc_genrule using $(locations) label should substitute first tool label automatically", description: "cc_genrule using $(locations) label should substitute first tool label automatically",
moduleTypeUnderTest: "cc_genrule",
moduleTypeUnderTestFactory: cc.GenRuleFactory,
moduleTypeUnderTestBp2BuildMutator: genrule.CcGenruleBp2Build,
blueprint: `cc_genrule { blueprint: `cc_genrule {
name: "foo", name: "foo",
out: ["foo.out"], out: ["foo.out"],
@@ -219,27 +201,24 @@ func TestLocationsLabelUsesFirstTool(t *testing.T) {
cmd: "$(locations) -s $(out) $(in)", cmd: "$(locations) -s $(out) $(in)",
bazel_module: { bp2build_available: true }, bazel_module: { bp2build_available: true },
}`, }`,
expectedBazelTargets: []string{`genrule( filesystem: otherCcGenruleBp,
name = "foo", expectedBazelTargets: []string{
cmd = "$(locations //other:foo.tool) -s $(OUTS) $(SRCS)", makeBazelTarget("genrule", "foo", attrNameToString{
outs = ["foo.out"], "cmd": `"$(locations //other:foo.tool) -s $(OUTS) $(SRCS)"`,
srcs = ["foo.in"], "outs": `["foo.out"]`,
tools = [ "srcs": `["foo.in"]`,
"tools": `[
"//other:foo.tool", "//other:foo.tool",
"//other:other.tool", "//other:other.tool",
], ]`,
)`, }),
}, },
filesystem: otherCcGenruleBp,
}) })
} }
func TestWithoutToolsOrToolFiles(t *testing.T) { func TestWithoutToolsOrToolFiles(t *testing.T) {
runCcGenruleTestCase(t, bp2buildTestCase{ runCcGenruleTestCase(t, bp2buildTestCase{
description: "cc_genrule without tools or tool_files can convert successfully", description: "cc_genrule without tools or tool_files can convert successfully",
moduleTypeUnderTest: "cc_genrule",
moduleTypeUnderTestFactory: cc.GenRuleFactory,
moduleTypeUnderTestBp2BuildMutator: genrule.CcGenruleBp2Build,
blueprint: `cc_genrule { blueprint: `cc_genrule {
name: "foo", name: "foo",
out: ["foo.out"], out: ["foo.out"],
@@ -247,12 +226,12 @@ func TestWithoutToolsOrToolFiles(t *testing.T) {
cmd: "cp $(in) $(out)", cmd: "cp $(in) $(out)",
bazel_module: { bp2build_available: true }, bazel_module: { bp2build_available: true },
}`, }`,
expectedBazelTargets: []string{`genrule( expectedBazelTargets: []string{
name = "foo", makeBazelTarget("genrule", "foo", attrNameToString{
cmd = "cp $(SRCS) $(OUTS)", "cmd": `"cp $(SRCS) $(OUTS)"`,
outs = ["foo.out"], "outs": `["foo.out"]`,
srcs = ["foo.in"], "srcs": `["foo.in"]`,
)`, }),
}, },
}) })
} }

View File

@@ -81,8 +81,8 @@ func TestCcLibrarySimple(t *testing.T) {
"x86_64.cpp": "", "x86_64.cpp": "",
"foo-dir/a.h": "", "foo-dir/a.h": "",
}, },
blueprint: soongCcLibraryPreamble + ` blueprint: soongCcLibraryPreamble +
cc_library_headers { name: "some-headers" } simpleModuleDoNotConvertBp2build("cc_library_headers", "some-headers") + `
cc_library { cc_library {
name: "foo-lib", name: "foo-lib",
srcs: ["impl.cpp"], srcs: ["impl.cpp"],
@@ -117,17 +117,17 @@ cc_library {
include_build_directory: false, include_build_directory: false,
} }
`, `,
expectedBazelTargets: []string{`cc_library( expectedBazelTargets: []string{
name = "foo-lib", makeBazelTarget("cc_library", "foo-lib", attrNameToString{
copts = ["-Wall"], "copts": `["-Wall"]`,
export_includes = ["foo-dir"], "export_includes": `["foo-dir"]`,
implementation_deps = [":some-headers"], "implementation_deps": `[":some-headers"]`,
linkopts = ["-Wl,--exclude-libs=bar.a"] + select({ "linkopts": `["-Wl,--exclude-libs=bar.a"] + select({
"//build/bazel/platforms/arch:x86": ["-Wl,--exclude-libs=baz.a"], "//build/bazel/platforms/arch:x86": ["-Wl,--exclude-libs=baz.a"],
"//build/bazel/platforms/arch:x86_64": ["-Wl,--exclude-libs=qux.a"], "//build/bazel/platforms/arch:x86_64": ["-Wl,--exclude-libs=qux.a"],
"//conditions:default": [], "//conditions:default": [],
}), })`,
srcs = ["impl.cpp"] + select({ "srcs": `["impl.cpp"] + select({
"//build/bazel/platforms/arch:x86": ["x86.cpp"], "//build/bazel/platforms/arch:x86": ["x86.cpp"],
"//build/bazel/platforms/arch:x86_64": ["x86_64.cpp"], "//build/bazel/platforms/arch:x86_64": ["x86_64.cpp"],
"//conditions:default": [], "//conditions:default": [],
@@ -140,8 +140,10 @@ cc_library {
"//build/bazel/platforms/os:linux": ["linux.cpp"], "//build/bazel/platforms/os:linux": ["linux.cpp"],
"//build/bazel/platforms/os:linux_bionic": ["bionic.cpp"], "//build/bazel/platforms/os:linux_bionic": ["bionic.cpp"],
"//conditions:default": [], "//conditions:default": [],
}), })`,
)`}}) }),
},
})
} }
func TestCcLibraryTrimmedLdAndroid(t *testing.T) { func TestCcLibraryTrimmedLdAndroid(t *testing.T) {
@@ -188,16 +190,17 @@ cc_library {
include_build_directory: false, include_build_directory: false,
} }
`, `,
expectedBazelTargets: []string{`cc_library( expectedBazelTargets: []string{
name = "fake-ld-android", makeBazelTarget("cc_library", "fake-ld-android", attrNameToString{
copts = [ "srcs": `["ld_android.cpp"]`,
"copts": `[
"-Wall", "-Wall",
"-Wextra", "-Wextra",
"-Wunused", "-Wunused",
"-Werror", "-Werror",
], ]`,
implementation_deps = [":libc_headers"], "implementation_deps": `[":libc_headers"]`,
linkopts = [ "linkopts": `[
"-Wl,--exclude-libs=libgcc.a", "-Wl,--exclude-libs=libgcc.a",
"-Wl,--exclude-libs=libgcc_stripped.a", "-Wl,--exclude-libs=libgcc_stripped.a",
"-Wl,--exclude-libs=libclang_rt.builtins-arm-android.a", "-Wl,--exclude-libs=libclang_rt.builtins-arm-android.a",
@@ -208,9 +211,9 @@ cc_library {
"//build/bazel/platforms/arch:x86": ["-Wl,--exclude-libs=libgcc_eh.a"], "//build/bazel/platforms/arch:x86": ["-Wl,--exclude-libs=libgcc_eh.a"],
"//build/bazel/platforms/arch:x86_64": ["-Wl,--exclude-libs=libgcc_eh.a"], "//build/bazel/platforms/arch:x86_64": ["-Wl,--exclude-libs=libgcc_eh.a"],
"//conditions:default": [], "//conditions:default": [],
}), })`,
srcs = ["ld_android.cpp"], }),
)`}, },
}) })
} }
@@ -255,15 +258,16 @@ cc_library {
`, `,
}, },
blueprint: soongCcLibraryPreamble, blueprint: soongCcLibraryPreamble,
expectedBazelTargets: []string{`cc_library( expectedBazelTargets: []string{
name = "fake-libarm-optimized-routines-math", makeBazelTarget("cc_library", "fake-libarm-optimized-routines-math", attrNameToString{
copts = select({ "copts": `select({
"//build/bazel/platforms/arch:arm64": ["-DHAVE_FAST_FMA=1"], "//build/bazel/platforms/arch:arm64": ["-DHAVE_FAST_FMA=1"],
"//conditions:default": [], "//conditions:default": [],
}), })`,
local_includes = ["."], "local_includes": `["."]`,
srcs_c = ["math/cosf.c"], "srcs_c": `["math/cosf.c"]`,
)`}, }),
},
}) })
} }
@@ -348,28 +352,29 @@ cc_library {
bazel_module: { bp2build_available: false }, bazel_module: { bp2build_available: false },
} }
`, `,
expectedBazelTargets: []string{`cc_library( expectedBazelTargets: []string{
name = "a", makeBazelTarget("cc_library", "a", attrNameToString{
copts = ["bothflag"], "copts": `["bothflag"]`,
implementation_deps = [":static_dep_for_both"], "implementation_deps": `[":static_dep_for_both"]`,
implementation_dynamic_deps = [":shared_dep_for_both"], "implementation_dynamic_deps": `[":shared_dep_for_both"]`,
shared = { "shared": `{
"copts": ["sharedflag"], "copts": ["sharedflag"],
"implementation_deps": [":static_dep_for_shared"], "implementation_deps": [":static_dep_for_shared"],
"implementation_dynamic_deps": [":shared_dep_for_shared"], "implementation_dynamic_deps": [":shared_dep_for_shared"],
"srcs": ["sharedonly.cpp"], "srcs": ["sharedonly.cpp"],
"whole_archive_deps": [":whole_static_lib_for_shared"], "whole_archive_deps": [":whole_static_lib_for_shared"],
}, }`,
srcs = ["both.cpp"], "srcs": `["both.cpp"]`,
static = { "static": `{
"copts": ["staticflag"], "copts": ["staticflag"],
"implementation_deps": [":static_dep_for_static"], "implementation_deps": [":static_dep_for_static"],
"implementation_dynamic_deps": [":shared_dep_for_static"], "implementation_dynamic_deps": [":shared_dep_for_static"],
"srcs": ["staticonly.cpp"], "srcs": ["staticonly.cpp"],
"whole_archive_deps": [":whole_static_lib_for_static"], "whole_archive_deps": [":whole_static_lib_for_static"],
}, }`,
whole_archive_deps = [":whole_static_lib_for_both"], "whole_archive_deps": `[":whole_static_lib_for_both"]`,
)`}, }),
},
}) })
} }
@@ -432,14 +437,14 @@ cc_library {
simpleModuleDoNotConvertBp2build("cc_library", "implementation_shared_dep_for_static") + simpleModuleDoNotConvertBp2build("cc_library", "implementation_shared_dep_for_static") +
simpleModuleDoNotConvertBp2build("cc_library", "shared_dep_for_both") + simpleModuleDoNotConvertBp2build("cc_library", "shared_dep_for_both") +
simpleModuleDoNotConvertBp2build("cc_library", "implementation_shared_dep_for_both"), simpleModuleDoNotConvertBp2build("cc_library", "implementation_shared_dep_for_both"),
expectedBazelTargets: []string{`cc_library( expectedBazelTargets: []string{
name = "a", makeBazelTarget("cc_library", "a", attrNameToString{
copts = ["bothflag"], "copts": `["bothflag"]`,
deps = [":static_dep_for_both"], "deps": `[":static_dep_for_both"]`,
dynamic_deps = [":shared_dep_for_both"], "dynamic_deps": `[":shared_dep_for_both"]`,
implementation_deps = [":implementation_static_dep_for_both"], "implementation_deps": `[":implementation_static_dep_for_both"]`,
implementation_dynamic_deps = [":implementation_shared_dep_for_both"], "implementation_dynamic_deps": `[":implementation_shared_dep_for_both"]`,
shared = { "shared": `{
"copts": ["sharedflag"], "copts": ["sharedflag"],
"deps": [":static_dep_for_shared"], "deps": [":static_dep_for_shared"],
"dynamic_deps": [":shared_dep_for_shared"], "dynamic_deps": [":shared_dep_for_shared"],
@@ -450,9 +455,9 @@ cc_library {
":not_explicitly_exported_whole_static_dep_for_shared", ":not_explicitly_exported_whole_static_dep_for_shared",
":whole_static_dep_for_shared", ":whole_static_dep_for_shared",
], ],
}, }`,
srcs = ["both.cpp"], "srcs": `["both.cpp"]`,
static = { "static": `{
"copts": ["staticflag"], "copts": ["staticflag"],
"deps": [":static_dep_for_static"], "deps": [":static_dep_for_static"],
"dynamic_deps": [":shared_dep_for_static"], "dynamic_deps": [":shared_dep_for_static"],
@@ -463,12 +468,13 @@ cc_library {
":not_explicitly_exported_whole_static_dep_for_static", ":not_explicitly_exported_whole_static_dep_for_static",
":whole_static_dep_for_static", ":whole_static_dep_for_static",
], ],
}, }`,
whole_archive_deps = [ "whole_archive_deps": `[
":not_explicitly_exported_whole_static_dep_for_both", ":not_explicitly_exported_whole_static_dep_for_both",
":whole_static_dep_for_both", ":whole_static_dep_for_both",
], ]`,
)`}, }),
},
}) })
} }
@@ -501,16 +507,17 @@ cc_prebuilt_library_static { name: "whole_static_lib_for_both" }
`, `,
}, },
blueprint: soongCcLibraryPreamble, blueprint: soongCcLibraryPreamble,
expectedBazelTargets: []string{`cc_library( expectedBazelTargets: []string{
name = "a", makeBazelTarget("cc_library", "a", attrNameToString{
shared = { "shared": `{
"whole_archive_deps": [":whole_static_lib_for_shared_alwayslink"], "whole_archive_deps": [":whole_static_lib_for_shared_alwayslink"],
}, }`,
static = { "static": `{
"whole_archive_deps": [":whole_static_lib_for_static_alwayslink"], "whole_archive_deps": [":whole_static_lib_for_static_alwayslink"],
}, }`,
whole_archive_deps = [":whole_static_lib_for_both_alwayslink"], "whole_archive_deps": `[":whole_static_lib_for_both_alwayslink"]`,
)`}, }),
},
}) })
} }
@@ -591,12 +598,12 @@ cc_library_static { name: "android_dep_for_shared" }
`, `,
}, },
blueprint: soongCcLibraryPreamble, blueprint: soongCcLibraryPreamble,
expectedBazelTargets: []string{`cc_library( expectedBazelTargets: []string{
name = "a", makeBazelTarget("cc_library", "a", attrNameToString{
copts = ["bothflag"], "copts": `["bothflag"]`,
implementation_deps = [":static_dep_for_both"], "implementation_deps": `[":static_dep_for_both"]`,
local_includes = ["."], "local_includes": `["."]`,
shared = { "shared": `{
"copts": ["sharedflag"] + select({ "copts": ["sharedflag"] + select({
"//build/bazel/platforms/arch:arm": ["-DARM_SHARED"], "//build/bazel/platforms/arch:arm": ["-DARM_SHARED"],
"//conditions:default": [], "//conditions:default": [],
@@ -629,9 +636,9 @@ cc_library_static { name: "android_dep_for_shared" }
"//build/bazel/platforms/arch:arm": [":arm_whole_static_dep_for_shared"], "//build/bazel/platforms/arch:arm": [":arm_whole_static_dep_for_shared"],
"//conditions:default": [], "//conditions:default": [],
}), }),
}, }`,
srcs = ["both.cpp"], "srcs": `["both.cpp"]`,
static = { "static": `{
"copts": ["staticflag"] + select({ "copts": ["staticflag"] + select({
"//build/bazel/platforms/arch:x86": ["-DX86_STATIC"], "//build/bazel/platforms/arch:x86": ["-DX86_STATIC"],
"//conditions:default": [], "//conditions:default": [],
@@ -644,8 +651,9 @@ cc_library_static { name: "android_dep_for_shared" }
"//build/bazel/platforms/arch:x86": ["x86_static.cpp"], "//build/bazel/platforms/arch:x86": ["x86_static.cpp"],
"//conditions:default": [], "//conditions:default": [],
}), }),
}, }`,
)`}, }),
},
}) })
} }
@@ -729,10 +737,10 @@ filegroup {
`, `,
}, },
blueprint: soongCcLibraryPreamble, blueprint: soongCcLibraryPreamble,
expectedBazelTargets: []string{`cc_library( expectedBazelTargets: []string{
name = "a", makeBazelTarget("cc_library", "a", attrNameToString{
local_includes = ["."], "local_includes": `["."]`,
shared = { "shared": `{
"srcs": [ "srcs": [
"shared_source.cpp", "shared_source.cpp",
"shared_source.cc", "shared_source.cc",
@@ -747,22 +755,22 @@ filegroup {
"shared_source.c", "shared_source.c",
":shared_filegroup_c_srcs", ":shared_filegroup_c_srcs",
], ],
}, }`,
srcs = [ "srcs": `[
"both_source.cpp", "both_source.cpp",
"both_source.cc", "both_source.cc",
":both_filegroup_cpp_srcs", ":both_filegroup_cpp_srcs",
], ]`,
srcs_as = [ "srcs_as": `[
"both_source.s", "both_source.s",
"both_source.S", "both_source.S",
":both_filegroup_as_srcs", ":both_filegroup_as_srcs",
], ]`,
srcs_c = [ "srcs_c": `[
"both_source.c", "both_source.c",
":both_filegroup_c_srcs", ":both_filegroup_c_srcs",
], ]`,
static = { "static": `{
"srcs": [ "srcs": [
"static_source.cpp", "static_source.cpp",
"static_source.cc", "static_source.cc",
@@ -777,8 +785,9 @@ filegroup {
"static_source.c", "static_source.c",
":static_filegroup_c_srcs", ":static_filegroup_c_srcs",
], ],
}, }`,
)`}, }),
},
}) })
} }
@@ -801,12 +810,13 @@ cc_library {
`, `,
}, },
blueprint: soongCcLibraryPreamble, blueprint: soongCcLibraryPreamble,
expectedBazelTargets: []string{`cc_library( expectedBazelTargets: []string{
name = "a", makeBazelTarget("cc_library", "a", attrNameToString{
additional_linker_inputs = ["v.map"], "additional_linker_inputs": `["v.map"]`,
linkopts = ["-Wl,--version-script,$(location v.map)"], "linkopts": `["-Wl,--version-script,$(location v.map)"]`,
srcs = ["a.cpp"], "srcs": `["a.cpp"]`,
)`}, }),
},
}) })
} }
@@ -837,20 +847,21 @@ cc_library {
`, `,
}, },
blueprint: soongCcLibraryPreamble, blueprint: soongCcLibraryPreamble,
expectedBazelTargets: []string{`cc_library( expectedBazelTargets: []string{
name = "a", makeBazelTarget("cc_library", "a", attrNameToString{
additional_linker_inputs = select({ "additional_linker_inputs": `select({
"//build/bazel/platforms/arch:arm": ["arm.map"], "//build/bazel/platforms/arch:arm": ["arm.map"],
"//build/bazel/platforms/arch:arm64": ["arm64.map"], "//build/bazel/platforms/arch:arm64": ["arm64.map"],
"//conditions:default": [], "//conditions:default": [],
}), })`,
linkopts = select({ "linkopts": `select({
"//build/bazel/platforms/arch:arm": ["-Wl,--version-script,$(location arm.map)"], "//build/bazel/platforms/arch:arm": ["-Wl,--version-script,$(location arm.map)"],
"//build/bazel/platforms/arch:arm64": ["-Wl,--version-script,$(location arm64.map)"], "//build/bazel/platforms/arch:arm64": ["-Wl,--version-script,$(location arm64.map)"],
"//conditions:default": [], "//conditions:default": [],
}), })`,
srcs = ["a.cpp"], "srcs": `["a.cpp"]`,
)`}, }),
},
}) })
} }
@@ -872,10 +883,11 @@ cc_library {
include_build_directory: false, include_build_directory: false,
} }
`, `,
expectedBazelTargets: []string{`cc_library( expectedBazelTargets: []string{
name = "a", makeBazelTarget("cc_library", "a", attrNameToString{
implementation_dynamic_deps = [":mylib"], "implementation_dynamic_deps": `[":mylib"]`,
)`}, }),
},
}) })
} }
@@ -917,34 +929,33 @@ cc_library {
}, },
include_build_directory: false, include_build_directory: false,
}`, }`,
expectedBazelTargets: []string{`cc_library( expectedBazelTargets: []string{
name = "a", makeBazelTarget("cc_library", "a", attrNameToString{
features = [ "features": `[
"disable_pack_relocations", "disable_pack_relocations",
"-no_undefined_symbols", "-no_undefined_symbols",
], ]`,
srcs = ["a.cpp"], "srcs": `["a.cpp"]`,
)`, `cc_library( }), makeBazelTarget("cc_library", "b", attrNameToString{
name = "b", "features": `select({
features = select({
"//build/bazel/platforms/arch:x86_64": [ "//build/bazel/platforms/arch:x86_64": [
"disable_pack_relocations", "disable_pack_relocations",
"-no_undefined_symbols", "-no_undefined_symbols",
], ],
"//conditions:default": [], "//conditions:default": [],
}), })`,
srcs = ["b.cpp"], "srcs": `["b.cpp"]`,
)`, `cc_library( }), makeBazelTarget("cc_library", "c", attrNameToString{
name = "c", "features": `select({
features = select({
"//build/bazel/platforms/os:darwin": [ "//build/bazel/platforms/os:darwin": [
"disable_pack_relocations", "disable_pack_relocations",
"-no_undefined_symbols", "-no_undefined_symbols",
], ],
"//conditions:default": [], "//conditions:default": [],
}), })`,
srcs = ["c.cpp"], "srcs": `["c.cpp"]`,
)`}, }),
},
}) })
} }
@@ -961,13 +972,14 @@ cc_library {
include_build_directory: false, include_build_directory: false,
} }
`, `,
expectedBazelTargets: []string{`cc_library( expectedBazelTargets: []string{
name = "a", makeBazelTarget("cc_library", "a", attrNameToString{
copts = [ "copts": `[
"-include", "-include",
"header.h", "header.h",
], ]`,
)`}, }),
},
}) })
} }
@@ -998,10 +1010,10 @@ func TestCcLibraryCppFlagsGoesIntoCopts(t *testing.T) {
include_build_directory: false, include_build_directory: false,
} }
`, `,
expectedBazelTargets: []string{`cc_library( expectedBazelTargets: []string{
name = "a", makeBazelTarget("cc_library", "a", attrNameToString{
copts = ["-Wall"], "copts": `["-Wall"]`,
cppflags = [ "cppflags": `[
"-fsigned-char", "-fsigned-char",
"-pedantic", "-pedantic",
] + select({ ] + select({
@@ -1010,9 +1022,10 @@ func TestCcLibraryCppFlagsGoesIntoCopts(t *testing.T) {
}) + select({ }) + select({
"//build/bazel/platforms/os:android": ["-DANDROID=1"], "//build/bazel/platforms/os:android": ["-DANDROID=1"],
"//conditions:default": [], "//conditions:default": [],
}), })`,
srcs = ["a.cpp"], "srcs": `["a.cpp"]`,
)`}, }),
},
}) })
} }
@@ -1097,31 +1110,30 @@ cc_library {
} }
`, `,
expectedBazelTargets: []string{ expectedBazelTargets: []string{
`cc_library( makeBazelTarget("cc_library", "foo_static", attrNameToString{
name = "foo_static", "implementation_deps": `select({
implementation_deps = select({
"//build/bazel/platforms/arch:arm": [], "//build/bazel/platforms/arch:arm": [],
"//conditions:default": [":arm_static_lib_excludes_bp2build_cc_library_static"], "//conditions:default": [":arm_static_lib_excludes_bp2build_cc_library_static"],
}) + select({ }) + select({
"//build/bazel/product_variables:malloc_not_svelte": [], "//build/bazel/product_variables:malloc_not_svelte": [],
"//conditions:default": [":malloc_not_svelte_static_lib_excludes_bp2build_cc_library_static"], "//conditions:default": [":malloc_not_svelte_static_lib_excludes_bp2build_cc_library_static"],
}), })`,
implementation_dynamic_deps = select({ "implementation_dynamic_deps": `select({
"//build/bazel/platforms/arch:arm": [], "//build/bazel/platforms/arch:arm": [],
"//conditions:default": [":arm_shared_lib_excludes"], "//conditions:default": [":arm_shared_lib_excludes"],
}) + select({ }) + select({
"//build/bazel/product_variables:malloc_not_svelte": [":malloc_not_svelte_shared_lib"], "//build/bazel/product_variables:malloc_not_svelte": [":malloc_not_svelte_shared_lib"],
"//conditions:default": [], "//conditions:default": [],
}), })`,
srcs_c = ["common.c"], "srcs_c": `["common.c"]`,
whole_archive_deps = select({ "whole_archive_deps": `select({
"//build/bazel/platforms/arch:arm": [], "//build/bazel/platforms/arch:arm": [],
"//conditions:default": [":arm_whole_static_lib_excludes_bp2build_cc_library_static"], "//conditions:default": [":arm_whole_static_lib_excludes_bp2build_cc_library_static"],
}) + select({ }) + select({
"//build/bazel/product_variables:malloc_not_svelte": [":malloc_not_svelte_whole_static_lib_bp2build_cc_library_static"], "//build/bazel/product_variables:malloc_not_svelte": [":malloc_not_svelte_whole_static_lib_bp2build_cc_library_static"],
"//conditions:default": [":malloc_not_svelte_whole_static_lib_excludes_bp2build_cc_library_static"], "//conditions:default": [":malloc_not_svelte_whole_static_lib_excludes_bp2build_cc_library_static"],
}), })`,
)`, }),
}, },
}) })
} }
@@ -1143,11 +1155,13 @@ cc_library {
include_build_directory: false, include_build_directory: false,
} }
`, `,
expectedBazelTargets: []string{`cc_library( expectedBazelTargets: []string{
name = "foo-lib", makeBazelTarget("cc_library", "foo-lib", attrNameToString{
link_crt = False, "link_crt": `False`,
srcs = ["impl.cpp"], "srcs": `["impl.cpp"]`,
)`}}) }),
},
})
} }
func TestCCLibraryNoCrtFalse(t *testing.T) { func TestCCLibraryNoCrtFalse(t *testing.T) {
@@ -1167,10 +1181,12 @@ cc_library {
include_build_directory: false, include_build_directory: false,
} }
`, `,
expectedBazelTargets: []string{`cc_library( expectedBazelTargets: []string{
name = "foo-lib", makeBazelTarget("cc_library", "foo-lib", attrNameToString{
srcs = ["impl.cpp"], "srcs": `["impl.cpp"]`,
)`}}) }),
},
})
} }
func TestCCLibraryNoCrtArchVariant(t *testing.T) { func TestCCLibraryNoCrtArchVariant(t *testing.T) {
@@ -1219,11 +1235,12 @@ cc_library {
include_build_directory: false, include_build_directory: false,
} }
`, `,
expectedBazelTargets: []string{`cc_library( expectedBazelTargets: []string{
name = "foo-lib", makeBazelTarget("cc_library", "foo-lib", attrNameToString{
srcs = ["impl.cpp"], "srcs": `["impl.cpp"]`,
use_libcrt = False, "use_libcrt": `False`,
)`}}) }),
}})
} }
func TestCCLibraryNoLibCrtFalse(t *testing.T) { func TestCCLibraryNoLibCrtFalse(t *testing.T) {
@@ -1243,11 +1260,12 @@ cc_library {
include_build_directory: false, include_build_directory: false,
} }
`, `,
expectedBazelTargets: []string{`cc_library( expectedBazelTargets: []string{
name = "foo-lib", makeBazelTarget("cc_library", "foo-lib", attrNameToString{
srcs = ["impl.cpp"], "srcs": `["impl.cpp"]`,
use_libcrt = True, "use_libcrt": `True`,
)`}}) }),
}})
} }
func TestCCLibraryNoLibCrtArchVariant(t *testing.T) { func TestCCLibraryNoLibCrtArchVariant(t *testing.T) {
@@ -1273,15 +1291,16 @@ cc_library {
include_build_directory: false, include_build_directory: false,
} }
`, `,
expectedBazelTargets: []string{`cc_library( expectedBazelTargets: []string{
name = "foo-lib", makeBazelTarget("cc_library", "foo-lib", attrNameToString{
srcs = ["impl.cpp"], "srcs": `["impl.cpp"]`,
use_libcrt = select({ "use_libcrt": `select({
"//build/bazel/platforms/arch:arm": False, "//build/bazel/platforms/arch:arm": False,
"//build/bazel/platforms/arch:x86": False, "//build/bazel/platforms/arch:x86": False,
"//conditions:default": None, "//conditions:default": None,
}), })`,
)`}}) }),
}})
} }
func TestCcLibraryStrip(t *testing.T) { func TestCcLibraryStrip(t *testing.T) {
@@ -1331,34 +1350,29 @@ cc_library {
include_build_directory: false, include_build_directory: false,
} }
`, `,
expectedBazelTargets: []string{`cc_library( expectedBazelTargets: []string{
name = "all", makeBazelTarget("cc_library", "all", attrNameToString{
strip = { "strip": `{
"all": True, "all": True,
}, }`,
)`, `cc_library( }), makeBazelTarget("cc_library", "keep_symbols", attrNameToString{
name = "keep_symbols", "strip": `{
strip = {
"keep_symbols": True, "keep_symbols": True,
}, }`,
)`, `cc_library( }), makeBazelTarget("cc_library", "keep_symbols_and_debug_frame", attrNameToString{
name = "keep_symbols_and_debug_frame", "strip": `{
strip = {
"keep_symbols_and_debug_frame": True, "keep_symbols_and_debug_frame": True,
}, }`,
)`, `cc_library( }), makeBazelTarget("cc_library", "keep_symbols_list", attrNameToString{
name = "keep_symbols_list", "strip": `{
strip = {
"keep_symbols_list": ["symbol"], "keep_symbols_list": ["symbol"],
}, }`,
)`, `cc_library( }), makeBazelTarget("cc_library", "none", attrNameToString{
name = "none", "strip": `{
strip = {
"none": True, "none": True,
}, }`,
)`, `cc_library( }), makeBazelTarget("cc_library", "nothing", attrNameToString{}),
name = "nothing", },
)`},
}) })
} }
@@ -1393,9 +1407,9 @@ cc_library {
include_build_directory: false, include_build_directory: false,
} }
`, `,
expectedBazelTargets: []string{`cc_library( expectedBazelTargets: []string{
name = "multi-arch", makeBazelTarget("cc_library", "multi-arch", attrNameToString{
strip = { "strip": `{
"keep_symbols": select({ "keep_symbols": select({
"//build/bazel/platforms/arch:arm64": True, "//build/bazel/platforms/arch:arm64": True,
"//conditions:default": None, "//conditions:default": None,
@@ -1411,8 +1425,9 @@ cc_library {
], ],
"//conditions:default": [], "//conditions:default": [],
}), }),
}, }`,
)`}, }),
},
}) })
} }
@@ -1429,10 +1444,11 @@ cc_library {
include_build_directory: false, include_build_directory: false,
} }
`, `,
expectedBazelTargets: []string{`cc_library( expectedBazelTargets: []string{
name = "root_empty", makeBazelTarget("cc_library", "root_empty", attrNameToString{
system_dynamic_deps = [], "system_dynamic_deps": `[]`,
)`}, }),
},
}) })
} }
@@ -1451,12 +1467,13 @@ cc_library {
include_build_directory: false, include_build_directory: false,
} }
`, `,
expectedBazelTargets: []string{`cc_library( expectedBazelTargets: []string{
name = "static_empty", makeBazelTarget("cc_library", "static_empty", attrNameToString{
static = { "static": `{
"system_dynamic_deps": [], "system_dynamic_deps": [],
}, }`,
)`}, }),
},
}) })
} }
@@ -1475,12 +1492,13 @@ cc_library {
include_build_directory: false, include_build_directory: false,
} }
`, `,
expectedBazelTargets: []string{`cc_library( expectedBazelTargets: []string{
name = "shared_empty", makeBazelTarget("cc_library", "shared_empty", attrNameToString{
shared = { "shared": `{
"system_dynamic_deps": [], "system_dynamic_deps": [],
}, }`,
)`}, }),
},
}) })
} }
@@ -1503,12 +1521,13 @@ cc_library {
include_build_directory: false, include_build_directory: false,
} }
`, `,
expectedBazelTargets: []string{`cc_library( expectedBazelTargets: []string{
name = "shared_empty", makeBazelTarget("cc_library", "shared_empty", attrNameToString{
shared = { "shared": `{
"system_dynamic_deps": [], "system_dynamic_deps": [],
}, }`,
)`}, }),
},
}) })
} }
@@ -1533,10 +1552,11 @@ cc_library {
include_build_directory: false, include_build_directory: false,
} }
`, `,
expectedBazelTargets: []string{`cc_library( expectedBazelTargets: []string{
name = "target_linux_bionic_empty", makeBazelTarget("cc_library", "target_linux_bionic_empty", attrNameToString{
system_dynamic_deps = [], "system_dynamic_deps": `[]`,
)`}, }),
},
}) })
} }
@@ -1557,10 +1577,11 @@ cc_library {
include_build_directory: false, include_build_directory: false,
} }
`, `,
expectedBazelTargets: []string{`cc_library( expectedBazelTargets: []string{
name = "target_bionic_empty", makeBazelTarget("cc_library", "target_bionic_empty", attrNameToString{
system_dynamic_deps = [], "system_dynamic_deps": `[]`,
)`}, }),
},
}) })
} }
@@ -1589,13 +1610,14 @@ cc_library {
include_build_directory: false, include_build_directory: false,
} }
`, `,
expectedBazelTargets: []string{`cc_library( expectedBazelTargets: []string{
name = "foo", makeBazelTarget("cc_library", "foo", attrNameToString{
shared = { "shared": `{
"system_dynamic_deps": [":libm"], "system_dynamic_deps": [":libm"],
}, }`,
system_dynamic_deps = [":libc"], "system_dynamic_deps": `[":libc"]`,
)`}, }),
},
}) })
} }
@@ -1637,9 +1659,9 @@ cc_library {
include_build_directory: false, include_build_directory: false,
} }
`, `,
expectedBazelTargets: []string{`cc_library( expectedBazelTargets: []string{
name = "foo-lib", makeBazelTarget("cc_library", "foo-lib", attrNameToString{
srcs = ["base.cpp"] + select({ "srcs": `["base.cpp"] + select({
"//build/bazel/platforms/os:android": [ "//build/bazel/platforms/os:android": [
"linux.cpp", "linux.cpp",
"bionic.cpp", "bionic.cpp",
@@ -1660,9 +1682,10 @@ cc_library {
], ],
"//build/bazel/platforms/os:windows": ["windows.cpp"], "//build/bazel/platforms/os:windows": ["windows.cpp"],
"//conditions:default": [], "//conditions:default": [],
}), })`,
)`}}) }),
},
})
} }
func TestCcLibraryCppStdWithGnuExtensions_ConvertsToFeatureAttr(t *testing.T) { func TestCcLibraryCppStdWithGnuExtensions_ConvertsToFeatureAttr(t *testing.T) {
@@ -1714,17 +1737,17 @@ func TestCcLibraryCppStdWithGnuExtensions_ConvertsToFeatureAttr(t *testing.T) {
{cpp_std: "gnu++17", gnu_extensions: "true", bazel_cpp_std: "gnu++17"}, {cpp_std: "gnu++17", gnu_extensions: "true", bazel_cpp_std: "gnu++17"},
} }
for _, tc := range testCases { for _, tc := range testCases {
cppStdAttr := "" cppStdProp := ""
if tc.cpp_std != "" { if tc.cpp_std != "" {
cppStdAttr = fmt.Sprintf(" cpp_std: \"%s\",", tc.cpp_std) cppStdProp = fmt.Sprintf(" cpp_std: \"%s\",", tc.cpp_std)
} }
gnuExtensionsAttr := "" gnuExtensionsProp := ""
if tc.gnu_extensions != "" { if tc.gnu_extensions != "" {
gnuExtensionsAttr = fmt.Sprintf(" gnu_extensions: %s,", tc.gnu_extensions) gnuExtensionsProp = fmt.Sprintf(" gnu_extensions: %s,", tc.gnu_extensions)
} }
bazelCppStdAttr := "" attrs := attrNameToString{}
if tc.bazel_cpp_std != "" { if tc.bazel_cpp_std != "" {
bazelCppStdAttr = fmt.Sprintf("\n cpp_std = \"%s\",", tc.bazel_cpp_std) attrs["cpp_std"] = fmt.Sprintf(`"%s"`, tc.bazel_cpp_std)
} }
runCcLibraryTestCase(t, bp2buildTestCase{ runCcLibraryTestCase(t, bp2buildTestCase{
@@ -1740,10 +1763,10 @@ cc_library {
%s // gnu_extensions: *bool %s // gnu_extensions: *bool
include_build_directory: false, include_build_directory: false,
} }
`, cppStdAttr, gnuExtensionsAttr), `, cppStdProp, gnuExtensionsProp),
expectedBazelTargets: []string{fmt.Sprintf(`cc_library( expectedBazelTargets: []string{
name = "a",%s makeBazelTarget("cc_library", "a", attrs),
)`, bazelCppStdAttr)}, },
}) })
runCcLibraryStaticTestCase(t, bp2buildTestCase{ runCcLibraryStaticTestCase(t, bp2buildTestCase{
@@ -1759,10 +1782,10 @@ cc_library_static {
%s // gnu_extensions: *bool %s // gnu_extensions: *bool
include_build_directory: false, include_build_directory: false,
} }
`, cppStdAttr, gnuExtensionsAttr), `, cppStdProp, gnuExtensionsProp),
expectedBazelTargets: []string{fmt.Sprintf(`cc_library_static( expectedBazelTargets: []string{
name = "a",%s makeBazelTarget("cc_library_static", "a", attrs),
)`, bazelCppStdAttr)}, },
}) })
runCcLibrarySharedTestCase(t, bp2buildTestCase{ runCcLibrarySharedTestCase(t, bp2buildTestCase{
@@ -1778,10 +1801,10 @@ cc_library_shared {
%s // gnu_extensions: *bool %s // gnu_extensions: *bool
include_build_directory: false, include_build_directory: false,
} }
`, cppStdAttr, gnuExtensionsAttr), `, cppStdProp, gnuExtensionsProp),
expectedBazelTargets: []string{fmt.Sprintf(`cc_library_shared( expectedBazelTargets: []string{
name = "a",%s makeBazelTarget("cc_library_shared", "a", attrs),
)`, bazelCppStdAttr)}, },
}) })
} }
} }

View File

@@ -128,9 +128,9 @@ cc_library_headers {
// TODO: Also support export_header_lib_headers // TODO: Also support export_header_lib_headers
}`, }`,
expectedBazelTargets: []string{`cc_library_headers( expectedBazelTargets: []string{
name = "foo_headers", makeBazelTarget("cc_library_headers", "foo_headers", attrNameToString{
export_includes = [ "export_includes": `[
"dir-1", "dir-1",
"dir-2", "dir-2",
] + select({ ] + select({
@@ -138,12 +138,13 @@ cc_library_headers {
"//build/bazel/platforms/arch:x86": ["arch_x86_exported_include_dir"], "//build/bazel/platforms/arch:x86": ["arch_x86_exported_include_dir"],
"//build/bazel/platforms/arch:x86_64": ["arch_x86_64_exported_include_dir"], "//build/bazel/platforms/arch:x86_64": ["arch_x86_64_exported_include_dir"],
"//conditions:default": [], "//conditions:default": [],
}), })`,
implementation_deps = [ "implementation_deps": `[
":lib-1", ":lib-1",
":lib-2", ":lib-2",
], ]`,
)`}, }),
},
}) })
} }
@@ -191,17 +192,18 @@ cc_library_headers {
}, },
include_build_directory: false, include_build_directory: false,
}`, }`,
expectedBazelTargets: []string{`cc_library_headers( expectedBazelTargets: []string{
name = "foo_headers", makeBazelTarget("cc_library_headers", "foo_headers", attrNameToString{
implementation_deps = [":base-lib"] + select({ "implementation_deps": `[":base-lib"] + select({
"//build/bazel/platforms/os:android": [":android-lib"], "//build/bazel/platforms/os:android": [":android-lib"],
"//build/bazel/platforms/os:darwin": [":darwin-lib"], "//build/bazel/platforms/os:darwin": [":darwin-lib"],
"//build/bazel/platforms/os:linux": [":linux-lib"], "//build/bazel/platforms/os:linux": [":linux-lib"],
"//build/bazel/platforms/os:linux_bionic": [":linux_bionic-lib"], "//build/bazel/platforms/os:linux_bionic": [":linux_bionic-lib"],
"//build/bazel/platforms/os:windows": [":windows-lib"], "//build/bazel/platforms/os:windows": [":windows-lib"],
"//conditions:default": [], "//conditions:default": [],
}), })`,
)`}, }),
},
}) })
} }
@@ -231,17 +233,18 @@ cc_library_headers {
}, },
include_build_directory: false, include_build_directory: false,
}`, }`,
expectedBazelTargets: []string{`cc_library_headers( expectedBazelTargets: []string{
name = "foo_headers", makeBazelTarget("cc_library_headers", "foo_headers", attrNameToString{
deps = select({ "deps": `select({
"//build/bazel/platforms/os:android": [":exported-lib"], "//build/bazel/platforms/os:android": [":exported-lib"],
"//conditions:default": [], "//conditions:default": [],
}), })`,
implementation_deps = select({ "implementation_deps": `select({
"//build/bazel/platforms/os:android": [":android-lib"], "//build/bazel/platforms/os:android": [":android-lib"],
"//conditions:default": [], "//conditions:default": [],
}), })`,
)`}, }),
},
}) })
} }
@@ -288,9 +291,9 @@ func TestCcLibraryHeadersArchAndTargetExportSystemIncludes(t *testing.T) {
}, },
include_build_directory: false, include_build_directory: false,
}`, }`,
expectedBazelTargets: []string{`cc_library_headers( expectedBazelTargets: []string{
name = "foo_headers", makeBazelTarget("cc_library_headers", "foo_headers", attrNameToString{
export_system_includes = ["shared_include_dir"] + select({ "export_system_includes": `["shared_include_dir"] + select({
"//build/bazel/platforms/arch:arm": ["arm_include_dir"], "//build/bazel/platforms/arch:arm": ["arm_include_dir"],
"//build/bazel/platforms/arch:x86_64": ["x86_64_include_dir"], "//build/bazel/platforms/arch:x86_64": ["x86_64_include_dir"],
"//conditions:default": [], "//conditions:default": [],
@@ -299,8 +302,9 @@ func TestCcLibraryHeadersArchAndTargetExportSystemIncludes(t *testing.T) {
"//build/bazel/platforms/os:darwin": ["darwin_include_dir"], "//build/bazel/platforms/os:darwin": ["darwin_include_dir"],
"//build/bazel/platforms/os:linux": ["linux_include_dir"], "//build/bazel/platforms/os:linux": ["linux_include_dir"],
"//conditions:default": [], "//conditions:default": [],
}), })`,
)`}, }),
},
}) })
} }
@@ -330,9 +334,10 @@ cc_library_headers {
no_libcrt: true, no_libcrt: true,
include_build_directory: false, include_build_directory: false,
}`, }`,
expectedBazelTargets: []string{`cc_library_headers( expectedBazelTargets: []string{
name = "lib-1", makeBazelTarget("cc_library_headers", "lib-1", attrNameToString{
export_includes = ["lib-1"], "export_includes": `["lib-1"]`,
)`}, }),
},
}) })
} }

View File

@@ -37,15 +37,15 @@ func registerCcLibrarySharedModuleTypes(ctx android.RegistrationContext) {
func runCcLibrarySharedTestCase(t *testing.T, tc bp2buildTestCase) { func runCcLibrarySharedTestCase(t *testing.T, tc bp2buildTestCase) {
t.Helper() t.Helper()
(&tc).moduleTypeUnderTest = "cc_library_shared"
(&tc).moduleTypeUnderTestFactory = cc.LibrarySharedFactory
(&tc).moduleTypeUnderTestBp2BuildMutator = cc.CcLibrarySharedBp2Build
runBp2BuildTestCase(t, registerCcLibrarySharedModuleTypes, tc) runBp2BuildTestCase(t, registerCcLibrarySharedModuleTypes, tc)
} }
func TestCcLibrarySharedSimple(t *testing.T) { func TestCcLibrarySharedSimple(t *testing.T) {
runCcLibrarySharedTestCase(t, bp2buildTestCase{ runCcLibrarySharedTestCase(t, bp2buildTestCase{
description: "cc_library_shared simple overall test", description: "cc_library_shared simple overall test",
moduleTypeUnderTest: "cc_library_shared",
moduleTypeUnderTestFactory: cc.LibrarySharedFactory,
moduleTypeUnderTestBp2BuildMutator: cc.CcLibrarySharedBp2Build,
filesystem: map[string]string{ filesystem: map[string]string{
// NOTE: include_dir headers *should not* appear in Bazel hdrs later (?) // NOTE: include_dir headers *should not* appear in Bazel hdrs later (?)
"include_dir_1/include_dir_1_a.h": "", "include_dir_1/include_dir_1_a.h": "",
@@ -140,52 +140,50 @@ cc_library_shared {
// TODO: Also support export_header_lib_headers // TODO: Also support export_header_lib_headers
}`, }`,
expectedBazelTargets: []string{`cc_library_shared( expectedBazelTargets: []string{
name = "foo_shared", makeBazelTarget("cc_library_shared", "foo_shared", attrNameToString{
absolute_includes = [ "absolute_includes": `[
"include_dir_1", "include_dir_1",
"include_dir_2", "include_dir_2",
], ]`,
copts = [ "copts": `[
"-Dflag1", "-Dflag1",
"-Dflag2", "-Dflag2",
], ]`,
export_includes = [ "export_includes": `[
"export_include_dir_1", "export_include_dir_1",
"export_include_dir_2", "export_include_dir_2",
], ]`,
implementation_deps = [ "implementation_deps": `[
":header_lib_1", ":header_lib_1",
":header_lib_2", ":header_lib_2",
], ]`,
implementation_dynamic_deps = [ "implementation_dynamic_deps": `[
":shared_lib_1", ":shared_lib_1",
":shared_lib_2", ":shared_lib_2",
], ]`,
local_includes = [ "local_includes": `[
"local_include_dir_1", "local_include_dir_1",
"local_include_dir_2", "local_include_dir_2",
".", ".",
], ]`,
srcs = [ "srcs": `[
"foo_shared1.cc", "foo_shared1.cc",
"foo_shared2.cc", "foo_shared2.cc",
], ]`,
whole_archive_deps = [ "whole_archive_deps": `[
":whole_static_lib_1", ":whole_static_lib_1",
":whole_static_lib_2", ":whole_static_lib_2",
], ]`,
)`}, }),
},
}) })
} }
func TestCcLibrarySharedArchSpecificSharedLib(t *testing.T) { func TestCcLibrarySharedArchSpecificSharedLib(t *testing.T) {
runCcLibrarySharedTestCase(t, bp2buildTestCase{ runCcLibrarySharedTestCase(t, bp2buildTestCase{
description: "cc_library_shared arch-specific shared_libs with whole_static_libs", description: "cc_library_shared arch-specific shared_libs with whole_static_libs",
moduleTypeUnderTest: "cc_library_shared", filesystem: map[string]string{},
moduleTypeUnderTestFactory: cc.LibrarySharedFactory,
moduleTypeUnderTestBp2BuildMutator: cc.CcLibrarySharedBp2Build,
filesystem: map[string]string{},
blueprint: soongCcLibrarySharedPreamble + ` blueprint: soongCcLibrarySharedPreamble + `
cc_library_static { cc_library_static {
name: "static_dep", name: "static_dep",
@@ -200,27 +198,25 @@ cc_library_shared {
arch: { arm64: { shared_libs: ["shared_dep"], whole_static_libs: ["static_dep"] } }, arch: { arm64: { shared_libs: ["shared_dep"], whole_static_libs: ["static_dep"] } },
include_build_directory: false, include_build_directory: false,
}`, }`,
expectedBazelTargets: []string{`cc_library_shared( expectedBazelTargets: []string{
name = "foo_shared", makeBazelTarget("cc_library_shared", "foo_shared", attrNameToString{
implementation_dynamic_deps = select({ "implementation_dynamic_deps": `select({
"//build/bazel/platforms/arch:arm64": [":shared_dep"], "//build/bazel/platforms/arch:arm64": [":shared_dep"],
"//conditions:default": [], "//conditions:default": [],
}), })`,
whole_archive_deps = select({ "whole_archive_deps": `select({
"//build/bazel/platforms/arch:arm64": [":static_dep"], "//build/bazel/platforms/arch:arm64": [":static_dep"],
"//conditions:default": [], "//conditions:default": [],
}), })`,
)`}, }),
},
}) })
} }
func TestCcLibrarySharedOsSpecificSharedLib(t *testing.T) { func TestCcLibrarySharedOsSpecificSharedLib(t *testing.T) {
runCcLibraryStaticTestCase(t, bp2buildTestCase{ runCcLibrarySharedTestCase(t, bp2buildTestCase{
description: "cc_library_shared os-specific shared_libs", description: "cc_library_shared os-specific shared_libs",
moduleTypeUnderTest: "cc_library_shared", filesystem: map[string]string{},
moduleTypeUnderTestFactory: cc.LibrarySharedFactory,
moduleTypeUnderTestBp2BuildMutator: cc.CcLibrarySharedBp2Build,
filesystem: map[string]string{},
blueprint: soongCcLibrarySharedPreamble + ` blueprint: soongCcLibrarySharedPreamble + `
cc_library_shared { cc_library_shared {
name: "shared_dep", name: "shared_dep",
@@ -231,23 +227,21 @@ cc_library_shared {
target: { android: { shared_libs: ["shared_dep"], } }, target: { android: { shared_libs: ["shared_dep"], } },
include_build_directory: false, include_build_directory: false,
}`, }`,
expectedBazelTargets: []string{`cc_library_shared( expectedBazelTargets: []string{
name = "foo_shared", makeBazelTarget("cc_library_shared", "foo_shared", attrNameToString{
implementation_dynamic_deps = select({ "implementation_dynamic_deps": `select({
"//build/bazel/platforms/os:android": [":shared_dep"], "//build/bazel/platforms/os:android": [":shared_dep"],
"//conditions:default": [], "//conditions:default": [],
}), })`,
)`}, }),
},
}) })
} }
func TestCcLibrarySharedBaseArchOsSpecificSharedLib(t *testing.T) { func TestCcLibrarySharedBaseArchOsSpecificSharedLib(t *testing.T) {
runCcLibrarySharedTestCase(t, bp2buildTestCase{ runCcLibrarySharedTestCase(t, bp2buildTestCase{
description: "cc_library_shared base, arch, and os-specific shared_libs", description: "cc_library_shared base, arch, and os-specific shared_libs",
moduleTypeUnderTest: "cc_library_shared", filesystem: map[string]string{},
moduleTypeUnderTestFactory: cc.LibrarySharedFactory,
moduleTypeUnderTestBp2BuildMutator: cc.CcLibrarySharedBp2Build,
filesystem: map[string]string{},
blueprint: soongCcLibrarySharedPreamble + ` blueprint: soongCcLibrarySharedPreamble + `
cc_library_shared { cc_library_shared {
name: "shared_dep", name: "shared_dep",
@@ -268,25 +262,23 @@ cc_library_shared {
arch: { arm64: { shared_libs: ["shared_dep3"] } }, arch: { arm64: { shared_libs: ["shared_dep3"] } },
include_build_directory: false, include_build_directory: false,
}`, }`,
expectedBazelTargets: []string{`cc_library_shared( expectedBazelTargets: []string{
name = "foo_shared", makeBazelTarget("cc_library_shared", "foo_shared", attrNameToString{
implementation_dynamic_deps = [":shared_dep"] + select({ "implementation_dynamic_deps": `[":shared_dep"] + select({
"//build/bazel/platforms/arch:arm64": [":shared_dep3"], "//build/bazel/platforms/arch:arm64": [":shared_dep3"],
"//conditions:default": [], "//conditions:default": [],
}) + select({ }) + select({
"//build/bazel/platforms/os:android": [":shared_dep2"], "//build/bazel/platforms/os:android": [":shared_dep2"],
"//conditions:default": [], "//conditions:default": [],
}), })`,
)`}, }),
},
}) })
} }
func TestCcLibrarySharedSimpleExcludeSrcs(t *testing.T) { func TestCcLibrarySharedSimpleExcludeSrcs(t *testing.T) {
runCcLibrarySharedTestCase(t, bp2buildTestCase{ runCcLibrarySharedTestCase(t, bp2buildTestCase{
description: "cc_library_shared simple exclude_srcs", description: "cc_library_shared simple exclude_srcs",
moduleTypeUnderTest: "cc_library_shared",
moduleTypeUnderTestFactory: cc.LibrarySharedFactory,
moduleTypeUnderTestBp2BuildMutator: cc.CcLibrarySharedBp2Build,
filesystem: map[string]string{ filesystem: map[string]string{
"common.c": "", "common.c": "",
"foo-a.c": "", "foo-a.c": "",
@@ -299,23 +291,21 @@ cc_library_shared {
exclude_srcs: ["foo-excluded.c"], exclude_srcs: ["foo-excluded.c"],
include_build_directory: false, include_build_directory: false,
}`, }`,
expectedBazelTargets: []string{`cc_library_shared( expectedBazelTargets: []string{
name = "foo_shared", makeBazelTarget("cc_library_shared", "foo_shared", attrNameToString{
srcs_c = [ "srcs_c": `[
"common.c", "common.c",
"foo-a.c", "foo-a.c",
], ]`,
)`}, }),
},
}) })
} }
func TestCcLibrarySharedStrip(t *testing.T) { func TestCcLibrarySharedStrip(t *testing.T) {
runCcLibrarySharedTestCase(t, bp2buildTestCase{ runCcLibrarySharedTestCase(t, bp2buildTestCase{
description: "cc_library_shared stripping", description: "cc_library_shared stripping",
moduleTypeUnderTest: "cc_library_shared", filesystem: map[string]string{},
moduleTypeUnderTestFactory: cc.LibrarySharedFactory,
moduleTypeUnderTestBp2BuildMutator: cc.CcLibrarySharedBp2Build,
filesystem: map[string]string{},
blueprint: soongCcLibrarySharedPreamble + ` blueprint: soongCcLibrarySharedPreamble + `
cc_library_shared { cc_library_shared {
name: "foo_shared", name: "foo_shared",
@@ -328,9 +318,9 @@ cc_library_shared {
}, },
include_build_directory: false, include_build_directory: false,
}`, }`,
expectedBazelTargets: []string{`cc_library_shared( expectedBazelTargets: []string{
name = "foo_shared", makeBazelTarget("cc_library_shared", "foo_shared", attrNameToString{
strip = { "strip": `{
"all": True, "all": True,
"keep_symbols": False, "keep_symbols": False,
"keep_symbols_and_debug_frame": True, "keep_symbols_and_debug_frame": True,
@@ -339,17 +329,15 @@ cc_library_shared {
"sym2", "sym2",
], ],
"none": False, "none": False,
}, }`,
)`}, }),
},
}) })
} }
func TestCcLibrarySharedVersionScript(t *testing.T) { func TestCcLibrarySharedVersionScript(t *testing.T) {
runCcLibrarySharedTestCase(t, bp2buildTestCase{ runCcLibrarySharedTestCase(t, bp2buildTestCase{
description: "cc_library_shared version script", description: "cc_library_shared version script",
moduleTypeUnderTest: "cc_library_shared",
moduleTypeUnderTestFactory: cc.LibrarySharedFactory,
moduleTypeUnderTestBp2BuildMutator: cc.CcLibrarySharedBp2Build,
filesystem: map[string]string{ filesystem: map[string]string{
"version_script": "", "version_script": "",
}, },
@@ -359,20 +347,18 @@ cc_library_shared {
version_script: "version_script", version_script: "version_script",
include_build_directory: false, include_build_directory: false,
}`, }`,
expectedBazelTargets: []string{`cc_library_shared( expectedBazelTargets: []string{
name = "foo_shared", makeBazelTarget("cc_library_shared", "foo_shared", attrNameToString{
additional_linker_inputs = ["version_script"], "additional_linker_inputs": `["version_script"]`,
linkopts = ["-Wl,--version-script,$(location version_script)"], "linkopts": `["-Wl,--version-script,$(location version_script)"]`,
)`}, }),
},
}) })
} }
func TestCcLibrarySharedNoCrtTrue(t *testing.T) { func TestCcLibrarySharedNoCrtTrue(t *testing.T) {
runCcLibrarySharedTestCase(t, bp2buildTestCase{ runCcLibrarySharedTestCase(t, bp2buildTestCase{
description: "cc_library_shared - nocrt: true emits attribute", description: "cc_library_shared - nocrt: true emits attribute",
moduleTypeUnderTest: "cc_library_shared",
moduleTypeUnderTestFactory: cc.LibrarySharedFactory,
moduleTypeUnderTestBp2BuildMutator: cc.CcLibrarySharedBp2Build,
filesystem: map[string]string{ filesystem: map[string]string{
"impl.cpp": "", "impl.cpp": "",
}, },
@@ -384,19 +370,18 @@ cc_library_shared {
include_build_directory: false, include_build_directory: false,
} }
`, `,
expectedBazelTargets: []string{`cc_library_shared( expectedBazelTargets: []string{
name = "foo_shared", makeBazelTarget("cc_library_shared", "foo_shared", attrNameToString{
link_crt = False, "link_crt": `False`,
srcs = ["impl.cpp"], "srcs": `["impl.cpp"]`,
)`}}) }),
},
})
} }
func TestCcLibrarySharedNoCrtFalse(t *testing.T) { func TestCcLibrarySharedNoCrtFalse(t *testing.T) {
runCcLibrarySharedTestCase(t, bp2buildTestCase{ runCcLibrarySharedTestCase(t, bp2buildTestCase{
description: "cc_library_shared - nocrt: false doesn't emit attribute", description: "cc_library_shared - nocrt: false doesn't emit attribute",
moduleTypeUnderTest: "cc_library_shared",
moduleTypeUnderTestFactory: cc.LibrarySharedFactory,
moduleTypeUnderTestBp2BuildMutator: cc.CcLibrarySharedBp2Build,
filesystem: map[string]string{ filesystem: map[string]string{
"impl.cpp": "", "impl.cpp": "",
}, },
@@ -408,18 +393,17 @@ cc_library_shared {
include_build_directory: false, include_build_directory: false,
} }
`, `,
expectedBazelTargets: []string{`cc_library_shared( expectedBazelTargets: []string{
name = "foo_shared", makeBazelTarget("cc_library_shared", "foo_shared", attrNameToString{
srcs = ["impl.cpp"], "srcs": `["impl.cpp"]`,
)`}}) }),
},
})
} }
func TestCcLibrarySharedNoCrtArchVariant(t *testing.T) { func TestCcLibrarySharedNoCrtArchVariant(t *testing.T) {
runCcLibrarySharedTestCase(t, bp2buildTestCase{ runCcLibrarySharedTestCase(t, bp2buildTestCase{
description: "cc_library_shared - nocrt in select", description: "cc_library_shared - nocrt in select",
moduleTypeUnderTest: "cc_library_shared",
moduleTypeUnderTestFactory: cc.LibrarySharedFactory,
moduleTypeUnderTestBp2BuildMutator: cc.CcLibrarySharedBp2Build,
filesystem: map[string]string{ filesystem: map[string]string{
"impl.cpp": "", "impl.cpp": "",
}, },

File diff suppressed because it is too large Load Diff

View File

@@ -28,15 +28,15 @@ func registerCcObjectModuleTypes(ctx android.RegistrationContext) {
func runCcObjectTestCase(t *testing.T, tc bp2buildTestCase) { func runCcObjectTestCase(t *testing.T, tc bp2buildTestCase) {
t.Helper() t.Helper()
(&tc).moduleTypeUnderTest = "cc_object"
(&tc).moduleTypeUnderTestFactory = cc.ObjectFactory
(&tc).moduleTypeUnderTestBp2BuildMutator = cc.ObjectBp2Build
runBp2BuildTestCase(t, registerCcObjectModuleTypes, tc) runBp2BuildTestCase(t, registerCcObjectModuleTypes, tc)
} }
func TestCcObjectSimple(t *testing.T) { func TestCcObjectSimple(t *testing.T) {
runCcObjectTestCase(t, bp2buildTestCase{ runCcObjectTestCase(t, bp2buildTestCase{
description: "simple cc_object generates cc_object with include header dep", description: "simple cc_object generates cc_object with include header dep",
moduleTypeUnderTest: "cc_object",
moduleTypeUnderTestFactory: cc.ObjectFactory,
moduleTypeUnderTestBp2BuildMutator: cc.ObjectBp2Build,
filesystem: map[string]string{ filesystem: map[string]string{
"a/b/foo.h": "", "a/b/foo.h": "",
"a/b/bar.h": "", "a/b/bar.h": "",
@@ -58,30 +58,27 @@ func TestCcObjectSimple(t *testing.T) {
exclude_srcs: ["a/b/exclude.c"], exclude_srcs: ["a/b/exclude.c"],
} }
`, `,
expectedBazelTargets: []string{`cc_object( expectedBazelTargets: []string{
name = "foo", makeBazelTarget("cc_object", "foo", attrNameToString{
copts = [ "copts": `[
"-fno-addrsig", "-fno-addrsig",
"-Wno-gcc-compat", "-Wno-gcc-compat",
"-Wall", "-Wall",
"-Werror", "-Werror",
], ]`,
local_includes = [ "local_includes": `[
"include", "include",
".", ".",
], ]`,
srcs = ["a/b/c.c"], "srcs": `["a/b/c.c"]`,
system_dynamic_deps = [], "system_dynamic_deps": `[]`,
)`, }),
}, },
}) })
} }
func TestCcObjectDefaults(t *testing.T) { func TestCcObjectDefaults(t *testing.T) {
runCcObjectTestCase(t, bp2buildTestCase{ runCcObjectTestCase(t, bp2buildTestCase{
moduleTypeUnderTest: "cc_object",
moduleTypeUnderTestFactory: cc.ObjectFactory,
moduleTypeUnderTestBp2BuildMutator: cc.ObjectBp2Build,
blueprint: `cc_object { blueprint: `cc_object {
name: "foo", name: "foo",
system_shared_libs: [], system_shared_libs: [],
@@ -101,33 +98,26 @@ cc_defaults {
cc_defaults { cc_defaults {
name: "foo_bar_defaults", name: "foo_bar_defaults",
cflags: [ cflags: [
"-Wno-gcc-compat",
"-Wall",
"-Werror", "-Werror",
], ],
} }
`, `,
expectedBazelTargets: []string{`cc_object( expectedBazelTargets: []string{
name = "foo", makeBazelTarget("cc_object", "foo", attrNameToString{
copts = [ "copts": `[
"-Wno-gcc-compat",
"-Wall",
"-Werror", "-Werror",
"-fno-addrsig", "-fno-addrsig",
], ]`,
local_includes = ["."], "local_includes": `["."]`,
srcs = ["a/b/c.c"], "srcs": `["a/b/c.c"]`,
system_dynamic_deps = [], "system_dynamic_deps": `[]`,
)`, }),
}}) }})
} }
func TestCcObjectCcObjetDepsInObjs(t *testing.T) { func TestCcObjectCcObjetDepsInObjs(t *testing.T) {
runCcObjectTestCase(t, bp2buildTestCase{ runCcObjectTestCase(t, bp2buildTestCase{
description: "cc_object with cc_object deps in objs props", description: "cc_object with cc_object deps in objs props",
moduleTypeUnderTest: "cc_object",
moduleTypeUnderTestFactory: cc.ObjectFactory,
moduleTypeUnderTestBp2BuildMutator: cc.ObjectBp2Build,
filesystem: map[string]string{ filesystem: map[string]string{
"a/b/c.c": "", "a/b/c.c": "",
"x/y/z.c": "", "x/y/z.c": "",
@@ -147,28 +137,24 @@ cc_object {
include_build_directory: false, include_build_directory: false,
} }
`, `,
expectedBazelTargets: []string{`cc_object( expectedBazelTargets: []string{
name = "bar", makeBazelTarget("cc_object", "bar", attrNameToString{
copts = ["-fno-addrsig"], "copts": `["-fno-addrsig"]`,
srcs = ["x/y/z.c"], "srcs": `["x/y/z.c"]`,
system_dynamic_deps = [], "system_dynamic_deps": `[]`,
)`, `cc_object( }), makeBazelTarget("cc_object", "foo", attrNameToString{
name = "foo", "copts": `["-fno-addrsig"]`,
copts = ["-fno-addrsig"], "deps": `[":bar"]`,
deps = [":bar"], "srcs": `["a/b/c.c"]`,
srcs = ["a/b/c.c"], "system_dynamic_deps": `[]`,
system_dynamic_deps = [], }),
)`,
}, },
}) })
} }
func TestCcObjectIncludeBuildDirFalse(t *testing.T) { func TestCcObjectIncludeBuildDirFalse(t *testing.T) {
runCcObjectTestCase(t, bp2buildTestCase{ runCcObjectTestCase(t, bp2buildTestCase{
description: "cc_object with include_build_dir: false", description: "cc_object with include_build_dir: false",
moduleTypeUnderTest: "cc_object",
moduleTypeUnderTestFactory: cc.ObjectFactory,
moduleTypeUnderTestBp2BuildMutator: cc.ObjectBp2Build,
filesystem: map[string]string{ filesystem: map[string]string{
"a/b/c.c": "", "a/b/c.c": "",
"x/y/z.c": "", "x/y/z.c": "",
@@ -180,22 +166,19 @@ func TestCcObjectIncludeBuildDirFalse(t *testing.T) {
include_build_directory: false, include_build_directory: false,
} }
`, `,
expectedBazelTargets: []string{`cc_object( expectedBazelTargets: []string{
name = "foo", makeBazelTarget("cc_object", "foo", attrNameToString{
copts = ["-fno-addrsig"], "copts": `["-fno-addrsig"]`,
srcs = ["a/b/c.c"], "srcs": `["a/b/c.c"]`,
system_dynamic_deps = [], "system_dynamic_deps": `[]`,
)`, }),
}, },
}) })
} }
func TestCcObjectProductVariable(t *testing.T) { func TestCcObjectProductVariable(t *testing.T) {
runCcObjectTestCase(t, bp2buildTestCase{ runCcObjectTestCase(t, bp2buildTestCase{
description: "cc_object with product variable", description: "cc_object with product variable",
moduleTypeUnderTest: "cc_object",
moduleTypeUnderTestFactory: cc.ObjectFactory,
moduleTypeUnderTestBp2BuildMutator: cc.ObjectBp2Build,
blueprint: `cc_object { blueprint: `cc_object {
name: "foo", name: "foo",
system_shared_libs: [], system_shared_libs: [],
@@ -208,26 +191,23 @@ func TestCcObjectProductVariable(t *testing.T) {
srcs: ["src.S"], srcs: ["src.S"],
} }
`, `,
expectedBazelTargets: []string{`cc_object( expectedBazelTargets: []string{
name = "foo", makeBazelTarget("cc_object", "foo", attrNameToString{
asflags = select({ "asflags": `select({
"//build/bazel/product_variables:platform_sdk_version": ["-DPLATFORM_SDK_VERSION=$(Platform_sdk_version)"], "//build/bazel/product_variables:platform_sdk_version": ["-DPLATFORM_SDK_VERSION=$(Platform_sdk_version)"],
"//conditions:default": [], "//conditions:default": [],
}), })`,
copts = ["-fno-addrsig"], "copts": `["-fno-addrsig"]`,
srcs_as = ["src.S"], "srcs_as": `["src.S"]`,
system_dynamic_deps = [], "system_dynamic_deps": `[]`,
)`, }),
}, },
}) })
} }
func TestCcObjectCflagsOneArch(t *testing.T) { func TestCcObjectCflagsOneArch(t *testing.T) {
runCcObjectTestCase(t, bp2buildTestCase{ runCcObjectTestCase(t, bp2buildTestCase{
description: "cc_object setting cflags for one arch", description: "cc_object setting cflags for one arch",
moduleTypeUnderTest: "cc_object",
moduleTypeUnderTestFactory: cc.ObjectFactory,
moduleTypeUnderTestBp2BuildMutator: cc.ObjectBp2Build,
blueprint: `cc_object { blueprint: `cc_object {
name: "foo", name: "foo",
system_shared_libs: [], system_shared_libs: [],
@@ -244,28 +224,24 @@ func TestCcObjectCflagsOneArch(t *testing.T) {
} }
`, `,
expectedBazelTargets: []string{ expectedBazelTargets: []string{
`cc_object( makeBazelTarget("cc_object", "foo", attrNameToString{
name = "foo", "copts": `["-fno-addrsig"] + select({
copts = ["-fno-addrsig"] + select({
"//build/bazel/platforms/arch:x86": ["-fPIC"], "//build/bazel/platforms/arch:x86": ["-fPIC"],
"//conditions:default": [], "//conditions:default": [],
}), })`,
srcs = ["a.cpp"] + select({ "srcs": `["a.cpp"] + select({
"//build/bazel/platforms/arch:arm": ["arch/arm/file.cpp"], "//build/bazel/platforms/arch:arm": ["arch/arm/file.cpp"],
"//conditions:default": [], "//conditions:default": [],
}), })`,
system_dynamic_deps = [], "system_dynamic_deps": `[]`,
)`, }),
}, },
}) })
} }
func TestCcObjectCflagsFourArch(t *testing.T) { func TestCcObjectCflagsFourArch(t *testing.T) {
runCcObjectTestCase(t, bp2buildTestCase{ runCcObjectTestCase(t, bp2buildTestCase{
description: "cc_object setting cflags for 4 architectures", description: "cc_object setting cflags for 4 architectures",
moduleTypeUnderTest: "cc_object",
moduleTypeUnderTestFactory: cc.ObjectFactory,
moduleTypeUnderTestBp2BuildMutator: cc.ObjectBp2Build,
blueprint: `cc_object { blueprint: `cc_object {
name: "foo", name: "foo",
system_shared_libs: [], system_shared_libs: [],
@@ -292,34 +268,30 @@ func TestCcObjectCflagsFourArch(t *testing.T) {
} }
`, `,
expectedBazelTargets: []string{ expectedBazelTargets: []string{
`cc_object( makeBazelTarget("cc_object", "foo", attrNameToString{
name = "foo", "copts": `["-fno-addrsig"] + select({
copts = ["-fno-addrsig"] + select({
"//build/bazel/platforms/arch:arm": ["-Wall"], "//build/bazel/platforms/arch:arm": ["-Wall"],
"//build/bazel/platforms/arch:arm64": ["-Wall"], "//build/bazel/platforms/arch:arm64": ["-Wall"],
"//build/bazel/platforms/arch:x86": ["-fPIC"], "//build/bazel/platforms/arch:x86": ["-fPIC"],
"//build/bazel/platforms/arch:x86_64": ["-fPIC"], "//build/bazel/platforms/arch:x86_64": ["-fPIC"],
"//conditions:default": [], "//conditions:default": [],
}), })`,
srcs = ["base.cpp"] + select({ "srcs": `["base.cpp"] + select({
"//build/bazel/platforms/arch:arm": ["arm.cpp"], "//build/bazel/platforms/arch:arm": ["arm.cpp"],
"//build/bazel/platforms/arch:arm64": ["arm64.cpp"], "//build/bazel/platforms/arch:arm64": ["arm64.cpp"],
"//build/bazel/platforms/arch:x86": ["x86.cpp"], "//build/bazel/platforms/arch:x86": ["x86.cpp"],
"//build/bazel/platforms/arch:x86_64": ["x86_64.cpp"], "//build/bazel/platforms/arch:x86_64": ["x86_64.cpp"],
"//conditions:default": [], "//conditions:default": [],
}), })`,
system_dynamic_deps = [], "system_dynamic_deps": `[]`,
)`, }),
}, },
}) })
} }
func TestCcObjectLinkerScript(t *testing.T) { func TestCcObjectLinkerScript(t *testing.T) {
runCcObjectTestCase(t, bp2buildTestCase{ runCcObjectTestCase(t, bp2buildTestCase{
description: "cc_object setting linker_script", description: "cc_object setting linker_script",
moduleTypeUnderTest: "cc_object",
moduleTypeUnderTestFactory: cc.ObjectFactory,
moduleTypeUnderTestBp2BuildMutator: cc.ObjectBp2Build,
blueprint: `cc_object { blueprint: `cc_object {
name: "foo", name: "foo",
srcs: ["base.cpp"], srcs: ["base.cpp"],
@@ -328,22 +300,18 @@ func TestCcObjectLinkerScript(t *testing.T) {
} }
`, `,
expectedBazelTargets: []string{ expectedBazelTargets: []string{
`cc_object( makeBazelTarget("cc_object", "foo", attrNameToString{
name = "foo", "copts": `["-fno-addrsig"]`,
copts = ["-fno-addrsig"], "linker_script": `"bunny.lds"`,
linker_script = "bunny.lds", "srcs": `["base.cpp"]`,
srcs = ["base.cpp"], }),
)`,
}, },
}) })
} }
func TestCcObjectDepsAndLinkerScriptSelects(t *testing.T) { func TestCcObjectDepsAndLinkerScriptSelects(t *testing.T) {
runCcObjectTestCase(t, bp2buildTestCase{ runCcObjectTestCase(t, bp2buildTestCase{
description: "cc_object setting deps and linker_script across archs", description: "cc_object setting deps and linker_script across archs",
moduleTypeUnderTest: "cc_object",
moduleTypeUnderTestFactory: cc.ObjectFactory,
moduleTypeUnderTestBp2BuildMutator: cc.ObjectBp2Build,
blueprint: `cc_object { blueprint: `cc_object {
name: "foo", name: "foo",
srcs: ["base.cpp"], srcs: ["base.cpp"],
@@ -389,33 +357,29 @@ cc_object {
} }
`, `,
expectedBazelTargets: []string{ expectedBazelTargets: []string{
`cc_object( makeBazelTarget("cc_object", "foo", attrNameToString{
name = "foo", "copts": `["-fno-addrsig"]`,
copts = ["-fno-addrsig"], "deps": `select({
deps = select({
"//build/bazel/platforms/arch:arm": [":arm_obj"], "//build/bazel/platforms/arch:arm": [":arm_obj"],
"//build/bazel/platforms/arch:x86": [":x86_obj"], "//build/bazel/platforms/arch:x86": [":x86_obj"],
"//build/bazel/platforms/arch:x86_64": [":x86_64_obj"], "//build/bazel/platforms/arch:x86_64": [":x86_64_obj"],
"//conditions:default": [], "//conditions:default": [],
}), })`,
linker_script = select({ "linker_script": `select({
"//build/bazel/platforms/arch:arm": "arm.lds", "//build/bazel/platforms/arch:arm": "arm.lds",
"//build/bazel/platforms/arch:x86": "x86.lds", "//build/bazel/platforms/arch:x86": "x86.lds",
"//build/bazel/platforms/arch:x86_64": "x86_64.lds", "//build/bazel/platforms/arch:x86_64": "x86_64.lds",
"//conditions:default": None, "//conditions:default": None,
}), })`,
srcs = ["base.cpp"], "srcs": `["base.cpp"]`,
)`, }),
}, },
}) })
} }
func TestCcObjectSelectOnLinuxAndBionicArchs(t *testing.T) { func TestCcObjectSelectOnLinuxAndBionicArchs(t *testing.T) {
runCcObjectTestCase(t, bp2buildTestCase{ runCcObjectTestCase(t, bp2buildTestCase{
description: "cc_object setting srcs based on linux and bionic archs", description: "cc_object setting srcs based on linux and bionic archs",
moduleTypeUnderTest: "cc_object",
moduleTypeUnderTestFactory: cc.ObjectFactory,
moduleTypeUnderTestBp2BuildMutator: cc.ObjectBp2Build,
blueprint: `cc_object { blueprint: `cc_object {
name: "foo", name: "foo",
srcs: ["base.cpp"], srcs: ["base.cpp"],
@@ -434,10 +398,9 @@ func TestCcObjectSelectOnLinuxAndBionicArchs(t *testing.T) {
} }
`, `,
expectedBazelTargets: []string{ expectedBazelTargets: []string{
`cc_object( makeBazelTarget("cc_object", "foo", attrNameToString{
name = "foo", "copts": `["-fno-addrsig"]`,
copts = ["-fno-addrsig"], "srcs": `["base.cpp"] + select({
srcs = ["base.cpp"] + select({
"//build/bazel/platforms/os_arch:android_arm64": [ "//build/bazel/platforms/os_arch:android_arm64": [
"linux_arm64.cpp", "linux_arm64.cpp",
"bionic_arm64.cpp", "bionic_arm64.cpp",
@@ -450,8 +413,8 @@ func TestCcObjectSelectOnLinuxAndBionicArchs(t *testing.T) {
"//build/bazel/platforms/os_arch:linux_glibc_x86": ["linux_x86.cpp"], "//build/bazel/platforms/os_arch:linux_glibc_x86": ["linux_x86.cpp"],
"//build/bazel/platforms/os_arch:linux_musl_x86": ["linux_x86.cpp"], "//build/bazel/platforms/os_arch:linux_musl_x86": ["linux_x86.cpp"],
"//conditions:default": [], "//conditions:default": [],
}), })`,
)`, }),
}, },
}) })
} }

View File

@@ -23,10 +23,9 @@ cc_prebuilt_library_shared {
bazel_module: { bp2build_available: true }, bazel_module: { bp2build_available: true },
}`, }`,
expectedBazelTargets: []string{ expectedBazelTargets: []string{
`prebuilt_library_shared( makeBazelTarget("prebuilt_library_shared", "libtest", attrNameToString{
name = "libtest", "shared_library": `"libf.so"`,
shared_library = "libf.so", }),
)`,
}, },
}) })
} }
@@ -52,14 +51,13 @@ cc_prebuilt_library_shared {
bazel_module: { bp2build_available: true }, bazel_module: { bp2build_available: true },
}`, }`,
expectedBazelTargets: []string{ expectedBazelTargets: []string{
`prebuilt_library_shared( makeBazelTarget("prebuilt_library_shared", "libtest", attrNameToString{
name = "libtest", "shared_library": `select({
shared_library = select({
"//build/bazel/platforms/arch:arm": "libg.so", "//build/bazel/platforms/arch:arm": "libg.so",
"//build/bazel/platforms/arch:arm64": "libf.so", "//build/bazel/platforms/arch:arm64": "libf.so",
"//conditions:default": None, "//conditions:default": None,
}), })`,
)`, }),
}, },
}) })
} }

View File

@@ -23,6 +23,9 @@ import (
func runFilegroupTestCase(t *testing.T, tc bp2buildTestCase) { func runFilegroupTestCase(t *testing.T, tc bp2buildTestCase) {
t.Helper() t.Helper()
(&tc).moduleTypeUnderTest = "filegroup"
(&tc).moduleTypeUnderTestFactory = android.FileGroupFactory
(&tc).moduleTypeUnderTestBp2BuildMutator = android.FilegroupBp2Build
runBp2BuildTestCase(t, registerFilegroupModuleTypes, tc) runBp2BuildTestCase(t, registerFilegroupModuleTypes, tc)
} }
@@ -30,11 +33,8 @@ func registerFilegroupModuleTypes(ctx android.RegistrationContext) {}
func TestFilegroupSameNameAsFile_OneFile(t *testing.T) { func TestFilegroupSameNameAsFile_OneFile(t *testing.T) {
runFilegroupTestCase(t, bp2buildTestCase{ runFilegroupTestCase(t, bp2buildTestCase{
description: "filegroup - same name as file, with one file", description: "filegroup - same name as file, with one file",
moduleTypeUnderTest: "filegroup", filesystem: map[string]string{},
moduleTypeUnderTestFactory: android.FileGroupFactory,
moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
filesystem: map[string]string{},
blueprint: ` blueprint: `
filegroup { filegroup {
name: "foo", name: "foo",
@@ -46,11 +46,8 @@ filegroup {
func TestFilegroupSameNameAsFile_MultipleFiles(t *testing.T) { func TestFilegroupSameNameAsFile_MultipleFiles(t *testing.T) {
runFilegroupTestCase(t, bp2buildTestCase{ runFilegroupTestCase(t, bp2buildTestCase{
description: "filegroup - same name as file, with multiple files", description: "filegroup - same name as file, with multiple files",
moduleTypeUnderTest: "filegroup", filesystem: map[string]string{},
moduleTypeUnderTestFactory: android.FileGroupFactory,
moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
filesystem: map[string]string{},
blueprint: ` blueprint: `
filegroup { filegroup {
name: "foo", name: "foo",

View File

@@ -17,10 +17,21 @@ package bp2build
import ( import (
"android/soong/android" "android/soong/android"
"android/soong/genrule" "android/soong/genrule"
"strings"
"testing" "testing"
) )
func registerGenruleModuleTypes(ctx android.RegistrationContext) {
ctx.RegisterModuleType("genrule_defaults", func() android.Module { return genrule.DefaultsFactory() })
}
func runGenruleTestCase(t *testing.T, tc bp2buildTestCase) {
t.Helper()
(&tc).moduleTypeUnderTest = "genrule"
(&tc).moduleTypeUnderTestFactory = genrule.GenRuleFactory
(&tc).moduleTypeUnderTestBp2BuildMutator = genrule.GenruleBp2Build
runBp2BuildTestCase(t, registerGenruleModuleTypes, tc)
}
func TestGenruleBp2Build(t *testing.T) { func TestGenruleBp2Build(t *testing.T) {
otherGenruleBp := map[string]string{ otherGenruleBp := map[string]string{
"other/Android.bp": `genrule { "other/Android.bp": `genrule {
@@ -39,10 +50,7 @@ genrule {
testCases := []bp2buildTestCase{ testCases := []bp2buildTestCase{
{ {
description: "genrule with command line variable replacements", description: "genrule with command line variable replacements",
moduleTypeUnderTest: "genrule",
moduleTypeUnderTestFactory: genrule.GenRuleFactory,
moduleTypeUnderTestBp2BuildMutator: genrule.GenruleBp2Build,
blueprint: `genrule { blueprint: `genrule {
name: "foo.tool", name: "foo.tool",
out: ["foo_tool.out"], out: ["foo_tool.out"],
@@ -60,26 +68,21 @@ genrule {
bazel_module: { bp2build_available: true }, bazel_module: { bp2build_available: true },
}`, }`,
expectedBazelTargets: []string{ expectedBazelTargets: []string{
`genrule( makeBazelTarget("genrule", "foo", attrNameToString{
name = "foo", "cmd": `"$(location :foo.tool) --genDir=$(GENDIR) arg $(SRCS) $(OUTS)"`,
cmd = "$(location :foo.tool) --genDir=$(GENDIR) arg $(SRCS) $(OUTS)", "outs": `["foo.out"]`,
outs = ["foo.out"], "srcs": `["foo.in"]`,
srcs = ["foo.in"], "tools": `[":foo.tool"]`,
tools = [":foo.tool"], }),
)`, makeBazelTarget("genrule", "foo.tool", attrNameToString{
`genrule( "cmd": `"cp $(SRCS) $(OUTS)"`,
name = "foo.tool", "outs": `["foo_tool.out"]`,
cmd = "cp $(SRCS) $(OUTS)", "srcs": `["foo_tool.in"]`,
outs = ["foo_tool.out"], }),
srcs = ["foo_tool.in"],
)`,
}, },
}, },
{ {
description: "genrule using $(locations :label)", description: "genrule using $(locations :label)",
moduleTypeUnderTest: "genrule",
moduleTypeUnderTestFactory: genrule.GenRuleFactory,
moduleTypeUnderTestBp2BuildMutator: genrule.GenruleBp2Build,
blueprint: `genrule { blueprint: `genrule {
name: "foo.tools", name: "foo.tools",
out: ["foo_tool.out", "foo_tool2.out"], out: ["foo_tool.out", "foo_tool2.out"],
@@ -96,29 +99,25 @@ genrule {
cmd: "$(locations :foo.tools) -s $(out) $(in)", cmd: "$(locations :foo.tools) -s $(out) $(in)",
bazel_module: { bp2build_available: true }, bazel_module: { bp2build_available: true },
}`, }`,
expectedBazelTargets: []string{`genrule( expectedBazelTargets: []string{
name = "foo", makeBazelTarget("genrule", "foo", attrNameToString{
cmd = "$(locations :foo.tools) -s $(OUTS) $(SRCS)", "cmd": `"$(locations :foo.tools) -s $(OUTS) $(SRCS)"`,
outs = ["foo.out"], "outs": `["foo.out"]`,
srcs = ["foo.in"], "srcs": `["foo.in"]`,
tools = [":foo.tools"], "tools": `[":foo.tools"]`,
)`, }),
`genrule( makeBazelTarget("genrule", "foo.tools", attrNameToString{
name = "foo.tools", "cmd": `"cp $(SRCS) $(OUTS)"`,
cmd = "cp $(SRCS) $(OUTS)", "outs": `[
outs = [
"foo_tool.out", "foo_tool.out",
"foo_tool2.out", "foo_tool2.out",
], ]`,
srcs = ["foo_tool.in"], "srcs": `["foo_tool.in"]`,
)`, }),
}, },
}, },
{ {
description: "genrule using $(locations //absolute:label)", description: "genrule using $(locations //absolute:label)",
moduleTypeUnderTest: "genrule",
moduleTypeUnderTestFactory: genrule.GenRuleFactory,
moduleTypeUnderTestBp2BuildMutator: genrule.GenruleBp2Build,
blueprint: `genrule { blueprint: `genrule {
name: "foo", name: "foo",
out: ["foo.out"], out: ["foo.out"],
@@ -127,21 +126,18 @@ genrule {
cmd: "$(locations :foo.tool) -s $(out) $(in)", cmd: "$(locations :foo.tool) -s $(out) $(in)",
bazel_module: { bp2build_available: true }, bazel_module: { bp2build_available: true },
}`, }`,
expectedBazelTargets: []string{`genrule( expectedBazelTargets: []string{
name = "foo", makeBazelTarget("genrule", "foo", attrNameToString{
cmd = "$(locations //other:foo.tool) -s $(OUTS) $(SRCS)", "cmd": `"$(locations //other:foo.tool) -s $(OUTS) $(SRCS)"`,
outs = ["foo.out"], "outs": `["foo.out"]`,
srcs = ["foo.in"], "srcs": `["foo.in"]`,
tools = ["//other:foo.tool"], "tools": `["//other:foo.tool"]`,
)`, }),
}, },
filesystem: otherGenruleBp, filesystem: otherGenruleBp,
}, },
{ {
description: "genrule srcs using $(locations //absolute:label)", description: "genrule srcs using $(locations //absolute:label)",
moduleTypeUnderTest: "genrule",
moduleTypeUnderTestFactory: genrule.GenRuleFactory,
moduleTypeUnderTestBp2BuildMutator: genrule.GenruleBp2Build,
blueprint: `genrule { blueprint: `genrule {
name: "foo", name: "foo",
out: ["foo.out"], out: ["foo.out"],
@@ -150,21 +146,18 @@ genrule {
cmd: "$(locations :foo.tool) -s $(out) $(location :other.tool)", cmd: "$(locations :foo.tool) -s $(out) $(location :other.tool)",
bazel_module: { bp2build_available: true }, bazel_module: { bp2build_available: true },
}`, }`,
expectedBazelTargets: []string{`genrule( expectedBazelTargets: []string{
name = "foo", makeBazelTarget("genrule", "foo", attrNameToString{
cmd = "$(locations //other:foo.tool) -s $(OUTS) $(location //other:other.tool)", "cmd": `"$(locations //other:foo.tool) -s $(OUTS) $(location //other:other.tool)"`,
outs = ["foo.out"], "outs": `["foo.out"]`,
srcs = ["//other:other.tool"], "srcs": `["//other:other.tool"]`,
tools = ["//other:foo.tool"], "tools": `["//other:foo.tool"]`,
)`, }),
}, },
filesystem: otherGenruleBp, filesystem: otherGenruleBp,
}, },
{ {
description: "genrule using $(location) label should substitute first tool label automatically", description: "genrule using $(location) label should substitute first tool label automatically",
moduleTypeUnderTest: "genrule",
moduleTypeUnderTestFactory: genrule.GenRuleFactory,
moduleTypeUnderTestBp2BuildMutator: genrule.GenruleBp2Build,
blueprint: `genrule { blueprint: `genrule {
name: "foo", name: "foo",
out: ["foo.out"], out: ["foo.out"],
@@ -173,24 +166,21 @@ genrule {
cmd: "$(location) -s $(out) $(in)", cmd: "$(location) -s $(out) $(in)",
bazel_module: { bp2build_available: true }, bazel_module: { bp2build_available: true },
}`, }`,
expectedBazelTargets: []string{`genrule( expectedBazelTargets: []string{
name = "foo", makeBazelTarget("genrule", "foo", attrNameToString{
cmd = "$(location //other:foo.tool) -s $(OUTS) $(SRCS)", "cmd": `"$(location //other:foo.tool) -s $(OUTS) $(SRCS)"`,
outs = ["foo.out"], "outs": `["foo.out"]`,
srcs = ["foo.in"], "srcs": `["foo.in"]`,
tools = [ "tools": `[
"//other:foo.tool", "//other:foo.tool",
"//other:other.tool", "//other:other.tool",
], ]`,
)`, }),
}, },
filesystem: otherGenruleBp, filesystem: otherGenruleBp,
}, },
{ {
description: "genrule using $(locations) label should substitute first tool label automatically", description: "genrule using $(locations) label should substitute first tool label automatically",
moduleTypeUnderTest: "genrule",
moduleTypeUnderTestFactory: genrule.GenRuleFactory,
moduleTypeUnderTestBp2BuildMutator: genrule.GenruleBp2Build,
blueprint: `genrule { blueprint: `genrule {
name: "foo", name: "foo",
out: ["foo.out"], out: ["foo.out"],
@@ -199,24 +189,21 @@ genrule {
cmd: "$(locations) -s $(out) $(in)", cmd: "$(locations) -s $(out) $(in)",
bazel_module: { bp2build_available: true }, bazel_module: { bp2build_available: true },
}`, }`,
expectedBazelTargets: []string{`genrule( expectedBazelTargets: []string{
name = "foo", makeBazelTarget("genrule", "foo", attrNameToString{
cmd = "$(locations //other:foo.tool) -s $(OUTS) $(SRCS)", "cmd": `"$(locations //other:foo.tool) -s $(OUTS) $(SRCS)"`,
outs = ["foo.out"], "outs": `["foo.out"]`,
srcs = ["foo.in"], "srcs": `["foo.in"]`,
tools = [ "tools": `[
"//other:foo.tool", "//other:foo.tool",
"//other:other.tool", "//other:other.tool",
], ]`,
)`, }),
}, },
filesystem: otherGenruleBp, filesystem: otherGenruleBp,
}, },
{ {
description: "genrule without tools or tool_files can convert successfully", description: "genrule without tools or tool_files can convert successfully",
moduleTypeUnderTest: "genrule",
moduleTypeUnderTestFactory: genrule.GenRuleFactory,
moduleTypeUnderTestBp2BuildMutator: genrule.GenruleBp2Build,
blueprint: `genrule { blueprint: `genrule {
name: "foo", name: "foo",
out: ["foo.out"], out: ["foo.out"],
@@ -224,85 +211,28 @@ genrule {
cmd: "cp $(in) $(out)", cmd: "cp $(in) $(out)",
bazel_module: { bp2build_available: true }, bazel_module: { bp2build_available: true },
}`, }`,
expectedBazelTargets: []string{`genrule( expectedBazelTargets: []string{
name = "foo", makeBazelTarget("genrule", "foo", attrNameToString{
cmd = "cp $(SRCS) $(OUTS)", "cmd": `"cp $(SRCS) $(OUTS)"`,
outs = ["foo.out"], "outs": `["foo.out"]`,
srcs = ["foo.in"], "srcs": `["foo.in"]`,
)`, }),
}, },
}, },
} }
dir := "."
for _, testCase := range testCases { for _, testCase := range testCases {
fs := make(map[string][]byte) t.Run(testCase.description, func(t *testing.T) {
toParse := []string{ runGenruleTestCase(t, testCase)
"Android.bp", })
}
for f, content := range testCase.filesystem {
if strings.HasSuffix(f, "Android.bp") {
toParse = append(toParse, f)
}
fs[f] = []byte(content)
}
config := android.TestConfig(buildDir, nil, testCase.blueprint, fs)
ctx := android.NewTestContext(config)
ctx.RegisterModuleType(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestFactory)
ctx.RegisterBp2BuildMutator(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestBp2BuildMutator)
ctx.RegisterForBazelConversion()
_, errs := ctx.ParseFileList(dir, toParse)
if errored(t, testCase, errs) {
continue
}
_, errs = ctx.ResolveDependencies(config)
if errored(t, testCase, errs) {
continue
}
checkDir := dir
if testCase.dir != "" {
checkDir = testCase.dir
}
codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build)
bazelTargets, err := generateBazelTargetsForDir(codegenCtx, checkDir)
android.FailIfErrored(t, err)
if actualCount, expectedCount := len(bazelTargets), len(testCase.expectedBazelTargets); actualCount != expectedCount {
t.Errorf("%s: Expected %d bazel target, got %d", testCase.description, expectedCount, actualCount)
} else {
for i, target := range bazelTargets {
if w, g := testCase.expectedBazelTargets[i], target.content; w != g {
t.Errorf(
"%s: Expected generated Bazel target to be '%s', got '%s'",
testCase.description,
w,
g,
)
}
}
}
} }
} }
func TestBp2BuildInlinesDefaults(t *testing.T) { func TestBp2BuildInlinesDefaults(t *testing.T) {
testCases := []struct { testCases := []bp2buildTestCase{
moduleTypesUnderTest map[string]android.ModuleFactory
bp2buildMutatorsUnderTest map[string]bp2buildMutator
bp string
expectedBazelTarget string
description string
}{
{ {
moduleTypesUnderTest: map[string]android.ModuleFactory{ description: "genrule applies properties from a genrule_defaults dependency if not specified",
"genrule": genrule.GenRuleFactory, blueprint: `genrule_defaults {
"genrule_defaults": func() android.Module { return genrule.DefaultsFactory() },
},
bp2buildMutatorsUnderTest: map[string]bp2buildMutator{
"genrule": genrule.GenruleBp2Build,
},
bp: `genrule_defaults {
name: "gen_defaults", name: "gen_defaults",
cmd: "do-something $(in) $(out)", cmd: "do-something $(in) $(out)",
} }
@@ -314,23 +244,17 @@ genrule {
bazel_module: { bp2build_available: true }, bazel_module: { bp2build_available: true },
} }
`, `,
expectedBazelTarget: `genrule( expectedBazelTargets: []string{
name = "gen", makeBazelTarget("genrule", "gen", attrNameToString{
cmd = "do-something $(SRCS) $(OUTS)", "cmd": `"do-something $(SRCS) $(OUTS)"`,
outs = ["out"], "outs": `["out"]`,
srcs = ["in1"], "srcs": `["in1"]`,
)`, }),
description: "genrule applies properties from a genrule_defaults dependency if not specified", },
}, },
{ {
moduleTypesUnderTest: map[string]android.ModuleFactory{ description: "genrule does merges properties from a genrule_defaults dependency, latest-first",
"genrule": genrule.GenRuleFactory, blueprint: `genrule_defaults {
"genrule_defaults": func() android.Module { return genrule.DefaultsFactory() },
},
bp2buildMutatorsUnderTest: map[string]bp2buildMutator{
"genrule": genrule.GenruleBp2Build,
},
bp: `genrule_defaults {
name: "gen_defaults", name: "gen_defaults",
out: ["out-from-defaults"], out: ["out-from-defaults"],
srcs: ["in-from-defaults"], srcs: ["in-from-defaults"],
@@ -345,29 +269,23 @@ genrule {
bazel_module: { bp2build_available: true }, bazel_module: { bp2build_available: true },
} }
`, `,
expectedBazelTarget: `genrule( expectedBazelTargets: []string{
name = "gen", makeBazelTarget("genrule", "gen", attrNameToString{
cmd = "do-something $(SRCS) $(OUTS)", "cmd": `"do-something $(SRCS) $(OUTS)"`,
outs = [ "outs": `[
"out-from-defaults", "out-from-defaults",
"out", "out",
], ]`,
srcs = [ "srcs": `[
"in-from-defaults", "in-from-defaults",
"in1", "in1",
], ]`,
)`, }),
description: "genrule does merges properties from a genrule_defaults dependency, latest-first", },
}, },
{ {
moduleTypesUnderTest: map[string]android.ModuleFactory{ description: "genrule applies properties from list of genrule_defaults",
"genrule": genrule.GenRuleFactory, blueprint: `genrule_defaults {
"genrule_defaults": func() android.Module { return genrule.DefaultsFactory() },
},
bp2buildMutatorsUnderTest: map[string]bp2buildMutator{
"genrule": genrule.GenruleBp2Build,
},
bp: `genrule_defaults {
name: "gen_defaults1", name: "gen_defaults1",
cmd: "cp $(in) $(out)", cmd: "cp $(in) $(out)",
} }
@@ -384,23 +302,17 @@ genrule {
bazel_module: { bp2build_available: true }, bazel_module: { bp2build_available: true },
} }
`, `,
expectedBazelTarget: `genrule( expectedBazelTargets: []string{
name = "gen", makeBazelTarget("genrule", "gen", attrNameToString{
cmd = "cp $(SRCS) $(OUTS)", "cmd": `"cp $(SRCS) $(OUTS)"`,
outs = ["out"], "outs": `["out"]`,
srcs = ["in1"], "srcs": `["in1"]`,
)`, }),
description: "genrule applies properties from list of genrule_defaults", },
}, },
{ {
moduleTypesUnderTest: map[string]android.ModuleFactory{ description: "genrule applies properties from genrule_defaults transitively",
"genrule": genrule.GenRuleFactory, blueprint: `genrule_defaults {
"genrule_defaults": func() android.Module { return genrule.DefaultsFactory() },
},
bp2buildMutatorsUnderTest: map[string]bp2buildMutator{
"genrule": genrule.GenruleBp2Build,
},
bp: `genrule_defaults {
name: "gen_defaults1", name: "gen_defaults1",
defaults: ["gen_defaults2"], defaults: ["gen_defaults2"],
cmd: "cmd1 $(in) $(out)", // overrides gen_defaults2's cmd property value. cmd: "cmd1 $(in) $(out)", // overrides gen_defaults2's cmd property value.
@@ -427,55 +339,26 @@ genrule {
bazel_module: { bp2build_available: true }, bazel_module: { bp2build_available: true },
} }
`, `,
expectedBazelTarget: `genrule( expectedBazelTargets: []string{
name = "gen", makeBazelTarget("genrule", "gen", attrNameToString{
cmd = "cmd1 $(SRCS) $(OUTS)", "cmd": `"cmd1 $(SRCS) $(OUTS)"`,
outs = [ "outs": `[
"out-from-3", "out-from-3",
"out-from-2", "out-from-2",
"out", "out",
], ]`,
srcs = [ "srcs": `[
"srcs-from-3", "srcs-from-3",
"in1", "in1",
], ]`,
)`, }),
description: "genrule applies properties from genrule_defaults transitively", },
}, },
} }
dir := "."
for _, testCase := range testCases { for _, testCase := range testCases {
config := android.TestConfig(buildDir, nil, testCase.bp, nil) t.Run(testCase.description, func(t *testing.T) {
ctx := android.NewTestContext(config) runGenruleTestCase(t, testCase)
for m, factory := range testCase.moduleTypesUnderTest { })
ctx.RegisterModuleType(m, factory)
}
for mutator, f := range testCase.bp2buildMutatorsUnderTest {
ctx.RegisterBp2BuildMutator(mutator, f)
}
ctx.RegisterForBazelConversion()
_, errs := ctx.ParseFileList(dir, []string{"Android.bp"})
android.FailIfErrored(t, errs)
_, errs = ctx.ResolveDependencies(config)
android.FailIfErrored(t, errs)
codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build)
bazelTargets, err := generateBazelTargetsForDir(codegenCtx, dir)
android.FailIfErrored(t, err)
if actualCount := len(bazelTargets); actualCount != 1 {
t.Fatalf("%s: Expected 1 bazel target, got %d", testCase.description, actualCount)
}
actualBazelTarget := bazelTargets[0]
if actualBazelTarget.content != testCase.expectedBazelTarget {
t.Errorf(
"%s: Expected generated Bazel target to be '%s', got '%s'",
testCase.description,
testCase.expectedBazelTarget,
actualBazelTarget.content,
)
}
} }
} }

View File

@@ -23,6 +23,9 @@ import (
func runPrebuiltEtcTestCase(t *testing.T, tc bp2buildTestCase) { func runPrebuiltEtcTestCase(t *testing.T, tc bp2buildTestCase) {
t.Helper() t.Helper()
(&tc).moduleTypeUnderTest = "prebuilt_etc"
(&tc).moduleTypeUnderTestFactory = etc.PrebuiltEtcFactory
(&tc).moduleTypeUnderTestBp2BuildMutator = etc.PrebuiltEtcBp2Build
runBp2BuildTestCase(t, registerPrebuiltEtcModuleTypes, tc) runBp2BuildTestCase(t, registerPrebuiltEtcModuleTypes, tc)
} }
@@ -31,11 +34,8 @@ func registerPrebuiltEtcModuleTypes(ctx android.RegistrationContext) {
func TestPrebuiltEtcSimple(t *testing.T) { func TestPrebuiltEtcSimple(t *testing.T) {
runPrebuiltEtcTestCase(t, bp2buildTestCase{ runPrebuiltEtcTestCase(t, bp2buildTestCase{
description: "prebuilt_etc - simple example", description: "prebuilt_etc - simple example",
moduleTypeUnderTest: "prebuilt_etc", filesystem: map[string]string{},
moduleTypeUnderTestFactory: etc.PrebuiltEtcFactory,
moduleTypeUnderTestBp2BuildMutator: etc.PrebuiltEtcBp2Build,
filesystem: map[string]string{},
blueprint: ` blueprint: `
prebuilt_etc { prebuilt_etc {
name: "apex_tz_version", name: "apex_tz_version",
@@ -45,22 +45,19 @@ prebuilt_etc {
installable: false, installable: false,
} }
`, `,
expectedBazelTargets: []string{`prebuilt_etc( expectedBazelTargets: []string{
name = "apex_tz_version", makeBazelTarget("prebuilt_etc", "apex_tz_version", attrNameToString{
filename = "tz_version", "filename": `"tz_version"`,
installable = False, "installable": `False`,
src = "version/tz_version", "src": `"version/tz_version"`,
sub_dir = "tz", "sub_dir": `"tz"`,
)`}}) })}})
} }
func TestPrebuiltEtcArchVariant(t *testing.T) { func TestPrebuiltEtcArchVariant(t *testing.T) {
runPrebuiltEtcTestCase(t, bp2buildTestCase{ runPrebuiltEtcTestCase(t, bp2buildTestCase{
description: "prebuilt_etc - simple example", description: "prebuilt_etc - arch variant",
moduleTypeUnderTest: "prebuilt_etc", filesystem: map[string]string{},
moduleTypeUnderTestFactory: etc.PrebuiltEtcFactory,
moduleTypeUnderTestBp2BuildMutator: etc.PrebuiltEtcBp2Build,
filesystem: map[string]string{},
blueprint: ` blueprint: `
prebuilt_etc { prebuilt_etc {
name: "apex_tz_version", name: "apex_tz_version",
@@ -78,15 +75,15 @@ prebuilt_etc {
} }
} }
`, `,
expectedBazelTargets: []string{`prebuilt_etc( expectedBazelTargets: []string{
name = "apex_tz_version", makeBazelTarget("prebuilt_etc", "apex_tz_version", attrNameToString{
filename = "tz_version", "filename": `"tz_version"`,
installable = False, "installable": `False`,
src = select({ "src": `select({
"//build/bazel/platforms/arch:arm": "arm", "//build/bazel/platforms/arch:arm": "arm",
"//build/bazel/platforms/arch:arm64": "arm64", "//build/bazel/platforms/arch:arm64": "arm64",
"//conditions:default": "version/tz_version", "//conditions:default": "version/tz_version",
}), })`,
sub_dir = "tz", "sub_dir": `"tz"`,
)`}}) })}})
} }

View File

@@ -7,7 +7,8 @@ import (
"android/soong/python" "android/soong/python"
) )
func runBp2BuildTestCaseWithLibs(t *testing.T, tc bp2buildTestCase) { func runBp2BuildTestCaseWithPythonLibraries(t *testing.T, tc bp2buildTestCase) {
t.Helper()
runBp2BuildTestCase(t, func(ctx android.RegistrationContext) { runBp2BuildTestCase(t, func(ctx android.RegistrationContext) {
ctx.RegisterModuleType("python_library", python.PythonLibraryFactory) ctx.RegisterModuleType("python_library", python.PythonLibraryFactory)
ctx.RegisterModuleType("python_library_host", python.PythonLibraryHostFactory) ctx.RegisterModuleType("python_library_host", python.PythonLibraryHostFactory)
@@ -15,7 +16,7 @@ func runBp2BuildTestCaseWithLibs(t *testing.T, tc bp2buildTestCase) {
} }
func TestPythonBinaryHostSimple(t *testing.T) { func TestPythonBinaryHostSimple(t *testing.T) {
runBp2BuildTestCaseWithLibs(t, bp2buildTestCase{ runBp2BuildTestCaseWithPythonLibraries(t, bp2buildTestCase{
description: "simple python_binary_host converts to a native py_binary", description: "simple python_binary_host converts to a native py_binary",
moduleTypeUnderTest: "python_binary_host", moduleTypeUnderTest: "python_binary_host",
moduleTypeUnderTestFactory: python.PythonBinaryHostFactory, moduleTypeUnderTestFactory: python.PythonBinaryHostFactory,
@@ -41,17 +42,17 @@ func TestPythonBinaryHostSimple(t *testing.T) {
srcs: ["b/e.py"], srcs: ["b/e.py"],
bazel_module: { bp2build_available: true }, bazel_module: { bp2build_available: true },
}`, }`,
expectedBazelTargets: []string{`py_binary( expectedBazelTargets: []string{
name = "foo", makeBazelTarget("py_binary", "foo", attrNameToString{
data = ["files/data.txt"], "data": `["files/data.txt"]`,
deps = [":bar"], "deps": `[":bar"]`,
main = "a.py", "main": `"a.py"`,
srcs = [ "srcs": `[
"a.py", "a.py",
"b/c.py", "b/c.py",
"b/d.py", "b/d.py",
], ]`,
)`, }),
}, },
}) })
} }
@@ -77,11 +78,11 @@ func TestPythonBinaryHostPy2(t *testing.T) {
bazel_module: { bp2build_available: true }, bazel_module: { bp2build_available: true },
} }
`, `,
expectedBazelTargets: []string{`py_binary( expectedBazelTargets: []string{
name = "foo", makeBazelTarget("py_binary", "foo", attrNameToString{
python_version = "PY2", "python_version": `"PY2"`,
srcs = ["a.py"], "srcs": `["a.py"]`,
)`, }),
}, },
}) })
} }
@@ -109,10 +110,9 @@ func TestPythonBinaryHostPy3(t *testing.T) {
`, `,
expectedBazelTargets: []string{ expectedBazelTargets: []string{
// python_version is PY3 by default. // python_version is PY3 by default.
`py_binary( makeBazelTarget("py_binary", "foo", attrNameToString{
name = "foo", "srcs": `["a.py"]`,
srcs = ["a.py"], }),
)`,
}, },
}) })
} }
@@ -139,14 +139,13 @@ func TestPythonBinaryHostArchVariance(t *testing.T) {
}, },
}`, }`,
expectedBazelTargets: []string{ expectedBazelTargets: []string{
`py_binary( makeBazelTarget("py_binary", "foo-arm", attrNameToString{
name = "foo-arm", "srcs": `select({
srcs = select({
"//build/bazel/platforms/arch:arm": ["arm.py"], "//build/bazel/platforms/arch:arm": ["arm.py"],
"//build/bazel/platforms/arch:x86": ["x86.py"], "//build/bazel/platforms/arch:x86": ["x86.py"],
"//conditions:default": [], "//conditions:default": [],
}), })`,
)`, }),
}, },
}) })
} }

View File

@@ -11,38 +11,49 @@ import (
// TODO(alexmarquez): Should be lifted into a generic Bp2Build file // TODO(alexmarquez): Should be lifted into a generic Bp2Build file
type PythonLibBp2Build func(ctx android.TopDownMutatorContext) type PythonLibBp2Build func(ctx android.TopDownMutatorContext)
func TestPythonLibrary(t *testing.T) { func runPythonLibraryTestCase(t *testing.T, tc bp2buildTestCase) {
testPythonLib(t, "python_library",
python.PythonLibraryFactory, python.PythonLibraryBp2Build,
func(ctx android.RegistrationContext) {})
}
func TestPythonLibraryHost(t *testing.T) {
testPythonLib(t, "python_library_host",
python.PythonLibraryHostFactory, python.PythonLibraryHostBp2Build,
func(ctx android.RegistrationContext) {
ctx.RegisterModuleType("python_library", python.PythonLibraryFactory)
})
}
func testPythonLib(t *testing.T, modType string,
factory android.ModuleFactory, mutator PythonLibBp2Build,
registration func(ctx android.RegistrationContext)) {
t.Helper() t.Helper()
// Simple testCase := tc
runBp2BuildTestCase(t, registration, bp2buildTestCase{ testCase.description = fmt.Sprintf(testCase.description, "python_library")
description: fmt.Sprintf("simple %s converts to a native py_library", modType), testCase.blueprint = fmt.Sprintf(testCase.blueprint, "python_library")
moduleTypeUnderTest: modType, testCase.moduleTypeUnderTest = "python_library"
moduleTypeUnderTestFactory: factory, testCase.moduleTypeUnderTestFactory = python.PythonLibraryFactory
moduleTypeUnderTestBp2BuildMutator: mutator, testCase.moduleTypeUnderTestBp2BuildMutator = python.PythonLibraryBp2Build
filesystem: map[string]string{ runBp2BuildTestCaseSimple(t, testCase)
"a.py": "", }
"b/c.py": "",
"b/d.py": "", func runPythonLibraryHostTestCase(t *testing.T, tc bp2buildTestCase) {
"b/e.py": "", t.Helper()
"files/data.txt": "", testCase := tc
}, testCase.description = fmt.Sprintf(testCase.description, "python_library_host")
blueprint: fmt.Sprintf(`%s { testCase.blueprint = fmt.Sprintf(testCase.blueprint, "python_library_host")
testCase.moduleTypeUnderTest = "python_library_host"
testCase.moduleTypeUnderTestFactory = python.PythonLibraryHostFactory
testCase.moduleTypeUnderTestBp2BuildMutator = python.PythonLibraryHostBp2Build
runBp2BuildTestCase(t, func(ctx android.RegistrationContext) {
ctx.RegisterModuleType("python_library", python.PythonLibraryFactory)
},
testCase)
}
func runPythonLibraryTestCases(t *testing.T, tc bp2buildTestCase) {
t.Helper()
runPythonLibraryTestCase(t, tc)
runPythonLibraryHostTestCase(t, tc)
}
func TestSimplePythonLib(t *testing.T) {
testCases := []bp2buildTestCase{
{
description: "simple %s converts to a native py_library",
filesystem: map[string]string{
"a.py": "",
"b/c.py": "",
"b/d.py": "",
"b/e.py": "",
"files/data.txt": "",
},
blueprint: `%s {
name: "foo", name: "foo",
srcs: ["**/*.py"], srcs: ["**/*.py"],
exclude_srcs: ["b/e.py"], exclude_srcs: ["b/e.py"],
@@ -54,28 +65,23 @@ func testPythonLib(t *testing.T, modType string,
name: "bar", name: "bar",
srcs: ["b/e.py"], srcs: ["b/e.py"],
bazel_module: { bp2build_available: false }, bazel_module: { bp2build_available: false },
}`, modType), }`,
expectedBazelTargets: []string{`py_library( expectedBazelTargets: []string{
name = "foo", makeBazelTarget("py_library", "foo", attrNameToString{
data = ["files/data.txt"], "data": `["files/data.txt"]`,
deps = [":bar"], "deps": `[":bar"]`,
srcs = [ "srcs": `[
"a.py", "a.py",
"b/c.py", "b/c.py",
"b/d.py", "b/d.py",
], ]`,
srcs_version = "PY3", "srcs_version": `"PY3"`,
)`, }),
},
}, },
}) {
description: "py2 %s converts to a native py_library",
// PY2 blueprint: `%s {
runBp2BuildTestCaseSimple(t, bp2buildTestCase{
description: fmt.Sprintf("py2 %s converts to a native py_library", modType),
moduleTypeUnderTest: modType,
moduleTypeUnderTestFactory: factory,
moduleTypeUnderTestBp2BuildMutator: mutator,
blueprint: fmt.Sprintf(`%s {
name: "foo", name: "foo",
srcs: ["a.py"], srcs: ["a.py"],
version: { version: {
@@ -88,22 +94,17 @@ func testPythonLib(t *testing.T, modType string,
}, },
bazel_module: { bp2build_available: true }, bazel_module: { bp2build_available: true },
}`, modType), }`,
expectedBazelTargets: []string{`py_library( expectedBazelTargets: []string{
name = "foo", makeBazelTarget("py_library", "foo", attrNameToString{
srcs = ["a.py"], "srcs": `["a.py"]`,
srcs_version = "PY2", "srcs_version": `"PY2"`,
)`, }),
},
}, },
}) {
description: "py3 %s converts to a native py_library",
// PY3 blueprint: `%s {
runBp2BuildTestCaseSimple(t, bp2buildTestCase{
description: fmt.Sprintf("py3 %s converts to a native py_library", modType),
moduleTypeUnderTest: modType,
moduleTypeUnderTestFactory: factory,
moduleTypeUnderTestBp2BuildMutator: mutator,
blueprint: fmt.Sprintf(`%s {
name: "foo", name: "foo",
srcs: ["a.py"], srcs: ["a.py"],
version: { version: {
@@ -116,22 +117,17 @@ func testPythonLib(t *testing.T, modType string,
}, },
bazel_module: { bp2build_available: true }, bazel_module: { bp2build_available: true },
}`, modType), }`,
expectedBazelTargets: []string{`py_library( expectedBazelTargets: []string{
name = "foo", makeBazelTarget("py_library", "foo", attrNameToString{
srcs = ["a.py"], "srcs": `["a.py"]`,
srcs_version = "PY3", "srcs_version": `"PY3"`,
)`, }),
},
}, },
}) {
description: "py2&3 %s converts to a native py_library",
// Both blueprint: `%s {
runBp2BuildTestCaseSimple(t, bp2buildTestCase{
description: fmt.Sprintf("py2&3 %s converts to a native py_library", modType),
moduleTypeUnderTest: modType,
moduleTypeUnderTestFactory: factory,
moduleTypeUnderTestBp2BuildMutator: mutator,
blueprint: fmt.Sprintf(`%s {
name: "foo", name: "foo",
srcs: ["a.py"], srcs: ["a.py"],
version: { version: {
@@ -144,44 +140,31 @@ func testPythonLib(t *testing.T, modType string,
}, },
bazel_module: { bp2build_available: true }, bazel_module: { bp2build_available: true },
}`, modType), }`,
expectedBazelTargets: []string{ expectedBazelTargets: []string{
// srcs_version is PY2ANDPY3 by default. // srcs_version is PY2ANDPY3 by default.
`py_library( makeBazelTarget("py_library", "foo", attrNameToString{
name = "foo", "srcs": `["a.py"]`,
srcs = ["a.py"], }),
)`, },
}, },
}) }
for _, tc := range testCases {
t.Run(tc.description, func(t *testing.T) {
runPythonLibraryTestCases(t, tc)
})
}
} }
func TestPythonLibraryArchVariance(t *testing.T) { func TestPythonArchVariance(t *testing.T) {
testPythonArchVariance(t, "python_library", "py_library", runPythonLibraryTestCases(t, bp2buildTestCase{
python.PythonLibraryFactory, python.PythonLibraryBp2Build, description: "test %s arch variants",
func(ctx android.RegistrationContext) {})
}
func TestPythonLibraryHostArchVariance(t *testing.T) {
testPythonArchVariance(t, "python_library_host", "py_library",
python.PythonLibraryHostFactory, python.PythonLibraryHostBp2Build,
func(ctx android.RegistrationContext) {})
}
// TODO: refactor python_binary_conversion_test to use this
func testPythonArchVariance(t *testing.T, modType, bazelTarget string,
factory android.ModuleFactory, mutator PythonLibBp2Build,
registration func(ctx android.RegistrationContext)) {
t.Helper()
runBp2BuildTestCase(t, registration, bp2buildTestCase{
description: fmt.Sprintf("test %s arch variants", modType),
moduleTypeUnderTest: modType,
moduleTypeUnderTestFactory: factory,
moduleTypeUnderTestBp2BuildMutator: mutator,
filesystem: map[string]string{ filesystem: map[string]string{
"dir/arm.py": "", "dir/arm.py": "",
"dir/x86.py": "", "dir/x86.py": "",
}, },
blueprint: fmt.Sprintf(`%s { blueprint: `%s {
name: "foo", name: "foo",
arch: { arch: {
arm: { arm: {
@@ -191,17 +174,16 @@ func testPythonArchVariance(t *testing.T, modType, bazelTarget string,
srcs: ["x86.py"], srcs: ["x86.py"],
}, },
}, },
}`, modType), }`,
expectedBazelTargets: []string{ expectedBazelTargets: []string{
fmt.Sprintf(`%s( makeBazelTarget("py_library", "foo", attrNameToString{
name = "foo", "srcs": `select({
srcs = select({
"//build/bazel/platforms/arch:arm": ["arm.py"], "//build/bazel/platforms/arch:arm": ["arm.py"],
"//build/bazel/platforms/arch:x86": ["x86.py"], "//build/bazel/platforms/arch:x86": ["x86.py"],
"//conditions:default": [], "//conditions:default": [],
}), })`,
srcs_version = "PY3", "srcs_version": `"PY3"`,
)`, bazelTarget), }),
}, },
}) })
} }

View File

@@ -64,9 +64,9 @@ func TestShBinarySimple(t *testing.T) {
src: "foo.sh", src: "foo.sh",
bazel_module: { bp2build_available: true }, bazel_module: { bp2build_available: true },
}`, }`,
expectedBazelTargets: []string{`sh_binary( expectedBazelTargets: []string{
name = "foo", makeBazelTarget("sh_binary", "foo", attrNameToString{
srcs = ["foo.sh"], "srcs": `["foo.sh"]`,
)`}, })},
}) })
} }

View File

@@ -364,3 +364,16 @@ func simpleModuleDoNotConvertBp2build(typ, name string) string {
bazel_module: { bp2build_available: false }, bazel_module: { bp2build_available: false },
}`, typ, name) }`, typ, name)
} }
type attrNameToString map[string]string
func makeBazelTarget(typ, name string, attrs attrNameToString) string {
attrStrings := make([]string, 0, len(attrs)+1)
attrStrings = append(attrStrings, fmt.Sprintf(` name = "%s",`, name))
for _, k := range android.SortedStringKeys(attrs) {
attrStrings = append(attrStrings, fmt.Sprintf(" %s = %s,", k, attrs[k]))
}
return fmt.Sprintf(`%s(
%s
)`, typ, strings.Join(attrStrings, "\n"))
}