diff --git a/bp2build/android_app_certificate_conversion_test.go b/bp2build/android_app_certificate_conversion_test.go index 022c6871e..6a53b00da 100644 --- a/bp2build/android_app_certificate_conversion_test.go +++ b/bp2build/android_app_certificate_conversion_test.go @@ -42,8 +42,9 @@ android_app_certificate { certificate: "chamber_of_secrets_dir", } `, - expectedBazelTargets: []string{`android_app_certificate( - name = "com.android.apogee.cert", - certificate = "chamber_of_secrets_dir", -)`}}) + expectedBazelTargets: []string{ + makeBazelTarget("android_app_certificate", "com.android.apogee.cert", attrNameToString{ + "certificate": `"chamber_of_secrets_dir"`, + }), + }}) } diff --git a/bp2build/apex_conversion_test.go b/bp2build/apex_conversion_test.go index 456f18aa8..1a23db700 100644 --- a/bp2build/apex_conversion_test.go +++ b/bp2build/apex_conversion_test.go @@ -113,29 +113,30 @@ apex { ], } `, - expectedBazelTargets: []string{`apex( - name = "com.android.apogee", - android_manifest = "ApogeeAndroidManifest.xml", - binaries = [ + expectedBazelTargets: []string{ + makeBazelTarget("apex", "com.android.apogee", attrNameToString{ + "android_manifest": `"ApogeeAndroidManifest.xml"`, + "binaries": `[ "binary_1", "binary_2", - ], - certificate = ":com.android.apogee.certificate", - file_contexts = ":com.android.apogee-file_contexts", - installable = False, - key = ":com.android.apogee.key", - manifest = "apogee_manifest.json", - min_sdk_version = "29", - native_shared_libs = [ + ]`, + "certificate": `":com.android.apogee.certificate"`, + "file_contexts": `":com.android.apogee-file_contexts"`, + "installable": "False", + "key": `":com.android.apogee.key"`, + "manifest": `"apogee_manifest.json"`, + "min_sdk_version": `"29"`, + "native_shared_libs": `[ ":native_shared_lib_1", ":native_shared_lib_2", - ], - prebuilts = [ + ]`, + "prebuilts": `[ ":pretend_prebuilt_1", ":pretend_prebuilt_2", - ], - updatable = False, -)`}}) + ]`, + "updatable": "False", + }), + }}) } func TestApexBundleDefaultPropertyValues(t *testing.T) { @@ -151,10 +152,10 @@ apex { manifest: "apogee_manifest.json", } `, - expectedBazelTargets: []string{`apex( - name = "com.android.apogee", - manifest = "apogee_manifest.json", -)`}}) + expectedBazelTargets: []string{makeBazelTarget("apex", "com.android.apogee", attrNameToString{ + "manifest": `"apogee_manifest.json"`, + }), + }}) } func TestApexBundleHasBazelModuleProps(t *testing.T) { @@ -171,8 +172,8 @@ apex { bazel_module: { bp2build_available: true }, } `, - expectedBazelTargets: []string{`apex( - name = "apogee", - manifest = "manifest.json", -)`}}) + expectedBazelTargets: []string{makeBazelTarget("apex", "apogee", attrNameToString{ + "manifest": `"manifest.json"`, + }), + }}) } diff --git a/bp2build/apex_key_conversion_test.go b/bp2build/apex_key_conversion_test.go index 8e1aa0961..17f79a6b0 100644 --- a/bp2build/apex_key_conversion_test.go +++ b/bp2build/apex_key_conversion_test.go @@ -43,9 +43,9 @@ apex_key { private_key: "com.android.apogee.pem", } `, - expectedBazelTargets: []string{`apex_key( - name = "com.android.apogee.key", - private_key = "com.android.apogee.pem", - public_key = "com.android.apogee.avbpubkey", -)`}}) + expectedBazelTargets: []string{makeBazelTarget("apex_key", "com.android.apogee.key", attrNameToString{ + "private_key": `"com.android.apogee.pem"`, + "public_key": `"com.android.apogee.avbpubkey"`, + }), + }}) } diff --git a/bp2build/build_conversion_test.go b/bp2build/build_conversion_test.go index ee1d862cd..983604b93 100644 --- a/bp2build/build_conversion_test.go +++ b/bp2build/build_conversion_test.go @@ -230,32 +230,32 @@ func TestGenerateBazelTargetModules(t *testing.T) { string_prop: "a", bazel_module: { bp2build_available: true }, }`, - expectedBazelTargets: []string{`custom( - name = "foo", - string_list_prop = [ + expectedBazelTargets: []string{ + makeBazelTarget("custom", "foo", attrNameToString{ + "string_list_prop": `[ "a", "b", - ], - string_prop = "a", -)`, + ]`, + "string_prop": `"a"`, + }), }, }, { description: "control characters", blueprint: `custom { - name: "control_characters", + name: "foo", string_list_prop: ["\t", "\n"], string_prop: "a\t\n\r", bazel_module: { bp2build_available: true }, }`, - expectedBazelTargets: []string{`custom( - name = "control_characters", - string_list_prop = [ + expectedBazelTargets: []string{ + makeBazelTarget("custom", "foo", attrNameToString{ + "string_list_prop": `[ "\t", "\n", - ], - string_prop = "a\t\n\r", -)`, + ]`, + "string_prop": `"a\t\n\r"`, + }), }, }, { @@ -271,14 +271,13 @@ custom { arch_paths: ["abc"], bazel_module: { bp2build_available: true }, }`, - expectedBazelTargets: []string{`custom( - name = "dep", - arch_paths = ["abc"], -)`, - `custom( - name = "has_dep", - arch_paths = [":dep"], -)`, + expectedBazelTargets: []string{ + makeBazelTarget("custom", "dep", attrNameToString{ + "arch_paths": `["abc"]`, + }), + makeBazelTarget("custom", "has_dep", attrNameToString{ + "arch_paths": `[":dep"]`, + }), }, }, { @@ -311,9 +310,9 @@ custom { }, bazel_module: { bp2build_available: true }, }`, - expectedBazelTargets: []string{`custom( - name = "arch_paths", - arch_paths = select({ + expectedBazelTargets: []string{ + makeBazelTarget("custom", "arch_paths", attrNameToString{ + "arch_paths": `select({ "//build/bazel/platforms/arch:arm": [ "arm.txt", "lib32.txt", @@ -368,8 +367,8 @@ custom { "windows.txt", ], "//conditions:default": [], - }), -)`, + })`, + }), }, }, { @@ -389,17 +388,16 @@ custom { arch_paths: ["abc"], bazel_module: { bp2build_available: true }, }`, - expectedBazelTargets: []string{`custom( - name = "dep", - arch_paths = ["abc"], -)`, - `custom( - name = "has_dep", - arch_paths = select({ + expectedBazelTargets: []string{ + makeBazelTarget("custom", "dep", attrNameToString{ + "arch_paths": `["abc"]`, + }), + makeBazelTarget("custom", "has_dep", attrNameToString{ + "arch_paths": `select({ "//build/bazel/platforms/arch:x86": [":dep"], "//conditions:default": [], - }), -)`, + })`, + }), }, }, { @@ -409,10 +407,10 @@ custom { embedded_prop: "abc", bazel_module: { bp2build_available: true }, }`, - expectedBazelTargets: []string{`custom( - name = "embedded_props", - embedded_attr = "abc", -)`, + expectedBazelTargets: []string{ + makeBazelTarget("custom", "embedded_props", attrNameToString{ + "embedded_attr": `"abc"`, + }), }, }, { @@ -422,10 +420,10 @@ custom { other_embedded_prop: "abc", bazel_module: { bp2build_available: true }, }`, - expectedBazelTargets: []string{`custom( - name = "ptr_to_embedded_props", - other_embedded_attr = "abc", -)`, + expectedBazelTargets: []string{ + makeBazelTarget("custom", "ptr_to_embedded_props", attrNameToString{ + "other_embedded_attr": `"abc"`, + }), }, }, } @@ -649,9 +647,7 @@ func TestModuleTypeBp2Build(t *testing.T) { bazel_module: { bp2build_available: true }, }`, expectedBazelTargets: []string{ - `filegroup( - name = "fg_foo", -)`, + makeBazelTarget("filegroup", "fg_foo", map[string]string{}), }, }, { @@ -665,9 +661,7 @@ func TestModuleTypeBp2Build(t *testing.T) { bazel_module: { bp2build_available: true }, }`, expectedBazelTargets: []string{ - `filegroup( - name = "fg_foo", -)`, + makeBazelTarget("filegroup", "fg_foo", map[string]string{}), }, }, { @@ -680,13 +674,13 @@ func TestModuleTypeBp2Build(t *testing.T) { srcs: ["a", "b"], bazel_module: { bp2build_available: true }, }`, - expectedBazelTargets: []string{`filegroup( - name = "fg_foo", - srcs = [ + expectedBazelTargets: []string{ + makeBazelTarget("filegroup", "fg_foo", map[string]string{ + "srcs": `[ "a", "b", - ], -)`, + ]`, + }), }, }, { @@ -700,10 +694,10 @@ func TestModuleTypeBp2Build(t *testing.T) { exclude_srcs: ["a"], bazel_module: { bp2build_available: true }, }`, - expectedBazelTargets: []string{`filegroup( - name = "fg_foo", - srcs = ["b"], -)`, + expectedBazelTargets: []string{ + makeBazelTarget("filegroup", "fg_foo", map[string]string{ + "srcs": `["b"]`, + }), }, }, { @@ -712,18 +706,18 @@ func TestModuleTypeBp2Build(t *testing.T) { moduleTypeUnderTestFactory: android.FileGroupFactory, moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build, blueprint: `filegroup { - name: "foo", + name: "fg_foo", srcs: ["**/*.txt"], bazel_module: { bp2build_available: true }, }`, - expectedBazelTargets: []string{`filegroup( - name = "foo", - srcs = [ + expectedBazelTargets: []string{ + makeBazelTarget("filegroup", "fg_foo", map[string]string{ + "srcs": `[ "other/a.txt", "other/b.txt", "other/subdir/a.txt", - ], -)`, + ]`, + }), }, filesystem: map[string]string{ "other/a.txt": "", @@ -737,21 +731,8 @@ func TestModuleTypeBp2Build(t *testing.T) { moduleTypeUnderTest: "filegroup", moduleTypeUnderTestFactory: android.FileGroupFactory, moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build, - blueprint: `filegroup { - name: "foo", - srcs: ["a.txt"], - bazel_module: { bp2build_available: true }, -}`, - dir: "other", - expectedBazelTargets: []string{`filegroup( - name = "fg_foo", - srcs = [ - "a.txt", - "b.txt", - "subdir/a.txt", - ], -)`, - }, + blueprint: ``, + dir: "other", filesystem: map[string]string{ "other/Android.bp": `filegroup { name: "fg_foo", @@ -763,6 +744,15 @@ func TestModuleTypeBp2Build(t *testing.T) { "other/subdir/a.txt": "", "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", @@ -770,21 +760,13 @@ func TestModuleTypeBp2Build(t *testing.T) { moduleTypeUnderTestFactory: android.FileGroupFactory, moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build, blueprint: `filegroup { - name: "foobar", + name: "fg_foo", srcs: [ ":foo", "c", ], bazel_module: { bp2build_available: true }, }`, - expectedBazelTargets: []string{`filegroup( - name = "foobar", - srcs = [ - "//other:foo", - "c", - ], -)`, - }, filesystem: map[string]string{ "other/Android.bp": `filegroup { name: "foo", @@ -792,6 +774,14 @@ func TestModuleTypeBp2Build(t *testing.T) { 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", @@ -799,21 +789,21 @@ func TestModuleTypeBp2Build(t *testing.T) { moduleTypeUnderTestFactory: android.FileGroupFactory, moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build, 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{ "other/Android.bp": `filegroup { name: "foo", 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`, }, expectedBazelTargets: []string{ - `filegroup( - name = "fg_foo", -)`, `// definition for fg_bar`, + makeBazelTarget("filegroup", "fg_foo", map[string]string{}), + `// definition for fg_bar`, }, }, { @@ -1098,6 +1087,9 @@ func TestCombineBuildFilesBp2buildTargets(t *testing.T) { moduleTypeUnderTest: "filegroup", moduleTypeUnderTestFactory: android.FileGroupFactory, moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build, + filesystem: map[string]string{ + "other/BUILD.bazel": `// BUILD file`, + }, blueprint: `filegroup { name: "fg_foo", bazel_module: { @@ -1112,14 +1104,9 @@ func TestCombineBuildFilesBp2buildTargets(t *testing.T) { }, }`, expectedBazelTargets: []string{ - `filegroup( - name = "fg_bar", -)`, + makeBazelTarget("filegroup", "fg_bar", map[string]string{}), `// BUILD file`, }, - filesystem: map[string]string{ - "other/BUILD.bazel": `// BUILD file`, - }, }, } @@ -1195,16 +1182,6 @@ func TestGlobExcludeSrcs(t *testing.T) { exclude_srcs: ["c.txt"], 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{ "a.txt": "", "b.txt": "", @@ -1213,6 +1190,16 @@ func TestGlobExcludeSrcs(t *testing.T) { "dir/e.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", @@ -1235,66 +1222,22 @@ func TestGlobExcludeSrcs(t *testing.T) { "dir/subdir/e.txt": "", "dir/subdir/f.txt": "", }, - expectedBazelTargets: []string{`filegroup( - name = "fg_foo", - srcs = [ + expectedBazelTargets: []string{ + makeBazelTarget("filegroup", "fg_foo", map[string]string{ + "srcs": `[ "a.txt", "//dir/subdir:e.txt", "//dir/subdir:f.txt", - ], -)`, + ]`, + }), }, }, } - dir := "." for _, testCase := range testCases { - fs := make(map[string][]byte) - toParse := []string{ - "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, - ) - } - } - } + t.Run(testCase.description, func(t *testing.T) { + runBp2BuildTestCaseSimple(t, testCase) + }) } } @@ -1305,22 +1248,16 @@ func TestCommonBp2BuildModuleAttrs(t *testing.T) { moduleTypeUnderTest: "filegroup", moduleTypeUnderTestFactory: android.FileGroupFactory, moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build, - blueprint: `filegroup { - name: "reqd", -} - + blueprint: simpleModuleDoNotConvertBp2build("filegroup", "reqd") + ` filegroup { name: "fg_foo", required: ["reqd"], bazel_module: { bp2build_available: true }, }`, - expectedBazelTargets: []string{`filegroup( - name = "fg_foo", - data = [":reqd"], -)`, - `filegroup( - name = "reqd", -)`, + expectedBazelTargets: []string{ + makeBazelTarget("filegroup", "fg_foo", map[string]string{ + "data": `[":reqd"]`, + }), }, }, { @@ -1328,16 +1265,8 @@ filegroup { moduleTypeUnderTest: "python_library", moduleTypeUnderTestFactory: python.PythonLibraryFactory, moduleTypeUnderTestBp2BuildMutator: python.PythonLibraryBp2Build, - blueprint: `python_library { - name: "reqdx86", - bazel_module: { bp2build_available: false, }, -} - -python_library { - name: "reqdarm", - bazel_module: { bp2build_available: false, }, -} - + blueprint: simpleModuleDoNotConvertBp2build("python_library", "reqdx86") + + simpleModuleDoNotConvertBp2build("python_library", "reqdarm") + ` python_library { name: "fg_foo", arch: { @@ -1350,15 +1279,15 @@ python_library { }, bazel_module: { bp2build_available: true }, }`, - expectedBazelTargets: []string{`py_library( - name = "fg_foo", - data = select({ + expectedBazelTargets: []string{ + makeBazelTarget("py_library", "fg_foo", map[string]string{ + "data": `select({ "//build/bazel/platforms/arch:arm": [":reqdarm"], "//build/bazel/platforms/arch:x86": [":reqdx86"], "//conditions:default": [], - }), - srcs_version = "PY3", -)`, + })`, + "srcs_version": `"PY3"`, + }), }, }, { @@ -1366,11 +1295,11 @@ python_library { moduleTypeUnderTest: "python_library", moduleTypeUnderTestFactory: python.PythonLibraryFactory, moduleTypeUnderTestBp2BuildMutator: python.PythonLibraryBp2Build, - blueprint: `python_library { - name: "reqd", - srcs: ["src.py"], -} - + filesystem: map[string]string{ + "data.bin": "", + "src.py": "", + }, + blueprint: simpleModuleDoNotConvertBp2build("python_library", "reqd") + ` python_library { name: "fg_foo", data: ["data.bin"], @@ -1378,23 +1307,13 @@ python_library { bazel_module: { bp2build_available: true }, }`, expectedBazelTargets: []string{ - `py_library( - name = "fg_foo", - data = [ + makeBazelTarget("py_library", "fg_foo", map[string]string{ + "data": `[ "data.bin", ":reqd", - ], - srcs_version = "PY3", -)`, - `py_library( - name = "reqd", - srcs = ["src.py"], - srcs_version = "PY3", -)`, - }, - filesystem: map[string]string{ - "data.bin": "", - "src.py": "", + ]`, + "srcs_version": `"PY3"`, + }), }, }, { @@ -1402,28 +1321,23 @@ python_library { moduleTypeUnderTest: "filegroup", moduleTypeUnderTestFactory: android.FileGroupFactory, moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build, - blueprint: `filegroup { - name: "reqd" -} + blueprint: simpleModuleDoNotConvertBp2build("filegroup", "reqd") + ` filegroup { name: "fg_foo", required: ["reqd"], bazel_module: { bp2build_available: true }, }`, expectedBazelTargets: []string{ - `filegroup( - name = "fg_foo", - data = [":reqd"], -)`, - `filegroup( - name = "reqd", -)`, + makeBazelTarget("filegroup", "fg_foo", map[string]string{ + "data": `[":reqd"]`, + }), }, - filesystem: map[string]string{}, }, } - for _, test := range testCases { - runBp2BuildTestCaseSimple(t, test) + for _, tc := range testCases { + t.Run(tc.description, func(t *testing.T) { + runBp2BuildTestCaseSimple(t, tc) + }) } } diff --git a/bp2build/cc_binary_conversion_test.go b/bp2build/cc_binary_conversion_test.go index 0b71d89c4..f9abcba6f 100644 --- a/bp2build/cc_binary_conversion_test.go +++ b/bp2build/cc_binary_conversion_test.go @@ -28,6 +28,26 @@ const ( 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) { cc.RegisterCCBuildComponents(ctx) ctx.RegisterModuleType("filegroup", android.FileGroupFactory) @@ -36,55 +56,56 @@ func registerCcBinaryModuleTypes(ctx android.RegistrationContext) { ctx.RegisterModuleType("genrule", genrule.GenRuleFactory) } -var binaryReplacer = strings.NewReplacer(ccBinaryTypePlaceHolder, "cc_binary", compatibleWithPlaceHolder, "") -var hostBinaryReplacer = strings.NewReplacer(ccBinaryTypePlaceHolder, "cc_binary_host", compatibleWithPlaceHolder, ` - target_compatible_with = select({ - "//build/bazel/platforms/os:android": ["@platforms//:incompatible"], - "//conditions:default": [], - }),`) +var binaryReplacer = strings.NewReplacer(ccBinaryTypePlaceHolder, "cc_binary") +var hostBinaryReplacer = strings.NewReplacer(ccBinaryTypePlaceHolder, "cc_binary_host") -func runCcBinaryTests(t *testing.T, tc bp2buildTestCase) { +func runCcBinaryTests(t *testing.T, tc ccBinaryBp2buildTestCase) { t.Helper() runCcBinaryTestCase(t, tc) runCcHostBinaryTestCase(t, tc) } -func runCcBinaryTestCase(t *testing.T, tc bp2buildTestCase) { +func runCcBinaryTestCase(t *testing.T, tc ccBinaryBp2buildTestCase) { t.Helper() - testCase := tc - testCase.expectedBazelTargets = append([]string{}, tc.expectedBazelTargets...) - testCase.moduleTypeUnderTest = "cc_binary" - testCase.moduleTypeUnderTestFactory = cc.BinaryFactory - testCase.moduleTypeUnderTestBp2BuildMutator = cc.BinaryBp2build - testCase.description = fmt.Sprintf("%s %s", testCase.moduleTypeUnderTest, testCase.description) - testCase.blueprint = binaryReplacer.Replace(testCase.blueprint) - for i, et := range testCase.expectedBazelTargets { - testCase.expectedBazelTargets[i] = binaryReplacer.Replace(et) + moduleTypeUnderTest := "cc_binary" + testCase := bp2buildTestCase{ + expectedBazelTargets: generateBazelTargetsForTest(tc.targets), + moduleTypeUnderTest: moduleTypeUnderTest, + moduleTypeUnderTestFactory: cc.BinaryFactory, + moduleTypeUnderTestBp2BuildMutator: cc.BinaryBp2build, + description: fmt.Sprintf("%s %s", moduleTypeUnderTest, tc.description), + blueprint: binaryReplacer.Replace(tc.blueprint), } t.Run(testCase.description, func(t *testing.T) { runBp2BuildTestCase(t, registerCcBinaryModuleTypes, testCase) }) } -func runCcHostBinaryTestCase(t *testing.T, tc bp2buildTestCase) { +func runCcHostBinaryTestCase(t *testing.T, tc ccBinaryBp2buildTestCase) { t.Helper() testCase := tc - testCase.expectedBazelTargets = append([]string{}, tc.expectedBazelTargets...) - testCase.moduleTypeUnderTest = "cc_binary_host" - testCase.moduleTypeUnderTestFactory = cc.BinaryHostFactory - testCase.moduleTypeUnderTestBp2BuildMutator = cc.BinaryHostBp2build - testCase.description = fmt.Sprintf("%s %s", testCase.moduleTypeUnderTest, testCase.description) - testCase.blueprint = hostBinaryReplacer.Replace(testCase.blueprint) - for i, et := range testCase.expectedBazelTargets { - testCase.expectedBazelTargets[i] = hostBinaryReplacer.Replace(et) + for i, t := range testCase.targets { + t.attrs["target_compatible_with"] = `select({ + "//build/bazel/platforms/os:android": ["@platforms//:incompatible"], + "//conditions:default": [], + })` + testCase.targets[i] = t } + moduleTypeUnderTest := "cc_binary_host" 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) { - runCcBinaryTests(t, bp2buildTestCase{ + runCcBinaryTests(t, ccBinaryBp2buildTestCase{ description: "basic -- properties -> attrs with little/no transformation", blueprint: ` {rule_name} { @@ -107,33 +128,35 @@ func TestBasicCcBinary(t *testing.T) { }, } `, - expectedBazelTargets: []string{`cc_binary( - name = "foo", - absolute_includes = ["absolute_dir"], - asflags = ["-Dasflag"], - conlyflags = ["-Dconlyflag"], - copts = ["-Dcopt"], - cppflags = ["-Dcppflag"], - linkopts = ["ld-flag"], - local_includes = [ + targets: []testBazelTarget{ + {"cc_binary", "foo", attrNameToString{ + "absolute_includes": `["absolute_dir"]`, + "asflags": `["-Dasflag"]`, + "conlyflags": `["-Dconlyflag"]`, + "copts": `["-Dcopt"]`, + "cppflags": `["-Dcppflag"]`, + "linkopts": `["ld-flag"]`, + "local_includes": `[ "dir", ".", - ], - rtti = True, - srcs = ["a.cc"], - strip = { + ]`, + "rtti": `True`, + "srcs": `["a.cc"]`, + "strip": `{ "all": True, "keep_symbols": True, "keep_symbols_and_debug_frame": True, "keep_symbols_list": ["symbol"], "none": True, - },{target_compatible_with} -)`}, + }`, + }, + }, + }, }) } func TestCcBinaryWithSharedLdflagDisableFeature(t *testing.T) { - runCcBinaryTests(t, bp2buildTestCase{ + runCcBinaryTests(t, ccBinaryBp2buildTestCase{ description: `ldflag "-shared" disables static_flag feature`, blueprint: ` {rule_name} { @@ -142,16 +165,18 @@ func TestCcBinaryWithSharedLdflagDisableFeature(t *testing.T) { include_build_directory: false, } `, - expectedBazelTargets: []string{`cc_binary( - name = "foo", - features = ["-static_flag"], - linkopts = ["-shared"],{target_compatible_with} -)`}, + targets: []testBazelTarget{ + {"cc_binary", "foo", attrNameToString{ + "features": `["-static_flag"]`, + "linkopts": `["-shared"]`, + }, + }, + }, }) } func TestCcBinaryWithLinkStatic(t *testing.T) { - runCcBinaryTests(t, bp2buildTestCase{ + runCcBinaryTests(t, ccBinaryBp2buildTestCase{ description: "link static", blueprint: ` {rule_name} { @@ -160,15 +185,17 @@ func TestCcBinaryWithLinkStatic(t *testing.T) { include_build_directory: false, } `, - expectedBazelTargets: []string{`cc_binary( - name = "foo", - linkshared = False,{target_compatible_with} -)`}, + targets: []testBazelTarget{ + {"cc_binary", "foo", attrNameToString{ + "linkshared": `False`, + }, + }, + }, }) } func TestCcBinaryVersionScript(t *testing.T) { - runCcBinaryTests(t, bp2buildTestCase{ + runCcBinaryTests(t, ccBinaryBp2buildTestCase{ description: `version script`, blueprint: ` {rule_name} { @@ -177,16 +204,18 @@ func TestCcBinaryVersionScript(t *testing.T) { version_script: "vs", } `, - expectedBazelTargets: []string{`cc_binary( - name = "foo", - additional_linker_inputs = ["vs"], - linkopts = ["-Wl,--version-script,$(location vs)"],{target_compatible_with} -)`}, + targets: []testBazelTarget{ + {"cc_binary", "foo", attrNameToString{ + "additional_linker_inputs": `["vs"]`, + "linkopts": `["-Wl,--version-script,$(location vs)"]`, + }, + }, + }, }) } func TestCcBinarySplitSrcsByLang(t *testing.T) { - runCcHostBinaryTestCase(t, bp2buildTestCase{ + runCcHostBinaryTestCase(t, ccBinaryBp2buildTestCase{ description: "split srcs by lang", blueprint: ` {rule_name} { @@ -200,26 +229,28 @@ func TestCcBinarySplitSrcsByLang(t *testing.T) { include_build_directory: false, } ` + simpleModuleDoNotConvertBp2build("filegroup", "fg_foo"), - expectedBazelTargets: []string{`cc_binary( - name = "foo", - srcs = [ + targets: []testBazelTarget{ + {"cc_binary", "foo", attrNameToString{ + "srcs": `[ "cpponly.cpp", ":fg_foo_cpp_srcs", - ], - srcs_as = [ + ]`, + "srcs_as": `[ "asonly.S", ":fg_foo_as_srcs", - ], - srcs_c = [ + ]`, + "srcs_c": `[ "conly.c", ":fg_foo_c_srcs", - ],{target_compatible_with} -)`}, + ]`, + }, + }, + }, }) } func TestCcBinaryDoNotDistinguishBetweenDepsAndImplementationDeps(t *testing.T) { - runCcBinaryTestCase(t, bp2buildTestCase{ + runCcBinaryTestCase(t, ccBinaryBp2buildTestCase{ description: "no implementation deps", blueprint: ` genrule { @@ -251,26 +282,28 @@ genrule { simpleModuleDoNotConvertBp2build("cc_library_static", "not_explicitly_exported_whole_static_dep") + simpleModuleDoNotConvertBp2build("cc_library", "shared_dep") + simpleModuleDoNotConvertBp2build("cc_library", "implementation_shared_dep"), - expectedBazelTargets: []string{`cc_binary( - name = "foo", - deps = [ + targets: []testBazelTarget{ + {"cc_binary", "foo", attrNameToString{ + "deps": `[ ":implementation_static_dep", ":static_dep", - ], - dynamic_deps = [ + ]`, + "dynamic_deps": `[ ":implementation_shared_dep", ":shared_dep", - ], - srcs = [ + ]`, + "srcs": `[ "foo.cpp", ":generated_hdr", ":export_generated_hdr", - ],{target_compatible_with} - whole_archive_deps = [ + ]`, + "whole_archive_deps": `[ ":not_explicitly_exported_whole_static_dep", ":whole_static_dep", - ], -)`}, + ]`, + }, + }, + }, }) } @@ -278,19 +311,21 @@ func TestCcBinaryNocrtTests(t *testing.T) { baseTestCases := []struct { description string soongProperty string - bazelAttr string + bazelAttr attrNameToString }{ { description: "nocrt: true", soongProperty: `nocrt: true,`, - bazelAttr: ` link_crt = False,`, + bazelAttr: attrNameToString{"link_crt": `False`}, }, { description: "nocrt: false", soongProperty: `nocrt: false,`, + bazelAttr: attrNameToString{}, }, { 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 { prop := btc.soongProperty if len(prop) > 0 { prop = "\n" + prop } - attr := btc.bazelAttr - if len(attr) > 0 { - attr = "\n" + attr - } - runCcBinaryTests(t, bp2buildTestCase{ + runCcBinaryTests(t, ccBinaryBp2buildTestCase{ description: btc.description, blueprint: fmt.Sprintf(baseBlueprint, prop), - expectedBazelTargets: []string{ - fmt.Sprintf(baseBazelTarget, attr), + targets: []testBazelTarget{ + {"cc_binary", "foo", btc.bazelAttr}, }, }) } @@ -327,20 +354,21 @@ func TestCcBinaryNo_libcrtTests(t *testing.T) { baseTestCases := []struct { description string soongProperty string - bazelAttr string + bazelAttr attrNameToString }{ { description: "no_libcrt: true", soongProperty: `no_libcrt: true,`, - bazelAttr: ` use_libcrt = False,`, + bazelAttr: attrNameToString{"use_libcrt": `False`}, }, { description: "no_libcrt: false", soongProperty: `no_libcrt: false,`, - bazelAttr: ` use_libcrt = True,`, + bazelAttr: attrNameToString{"use_libcrt": `True`}, }, { 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 { prop := btc.soongProperty if len(prop) > 0 { prop = "\n" + prop } - attr := btc.bazelAttr - if len(attr) > 0 { - attr = "\n" + attr - } - runCcBinaryTests(t, bp2buildTestCase{ + runCcBinaryTests(t, ccBinaryBp2buildTestCase{ description: btc.description, blueprint: fmt.Sprintf(baseBlueprint, prop), - expectedBazelTargets: []string{ - fmt.Sprintf(baseBazelTarget, attr), + targets: []testBazelTarget{ + {"cc_binary", "foo", btc.bazelAttr}, }, }) } @@ -377,31 +397,35 @@ func TestCcBinaryPropertiesToFeatures(t *testing.T) { baseTestCases := []struct { description string soongProperty string - bazelAttr string + bazelAttr attrNameToString }{ { description: "pack_relocation: true", soongProperty: `pack_relocations: true,`, + bazelAttr: attrNameToString{}, }, { description: "pack_relocations: false", soongProperty: `pack_relocations: false,`, - bazelAttr: ` features = ["disable_pack_relocations"],`, + bazelAttr: attrNameToString{"features": `["disable_pack_relocations"]`}, }, { description: "pack_relocations: not set", + bazelAttr: attrNameToString{}, }, { description: "pack_relocation: true", soongProperty: `allow_undefined_symbols: true,`, - bazelAttr: ` features = ["-no_undefined_symbols"],`, + bazelAttr: attrNameToString{"features": `["-no_undefined_symbols"]`}, }, { description: "allow_undefined_symbols: false", soongProperty: `allow_undefined_symbols: false,`, + bazelAttr: attrNameToString{}, }, { description: "allow_undefined_symbols: not set", + bazelAttr: attrNameToString{}, }, } @@ -410,25 +434,16 @@ func TestCcBinaryPropertiesToFeatures(t *testing.T) { include_build_directory: false, } ` - - baseBazelTarget := `cc_binary( - name = "foo",%s{target_compatible_with} -)` - for _, btc := range baseTestCases { prop := btc.soongProperty if len(prop) > 0 { prop = "\n" + prop } - attr := btc.bazelAttr - if len(attr) > 0 { - attr = "\n" + attr - } - runCcBinaryTests(t, bp2buildTestCase{ + runCcBinaryTests(t, ccBinaryBp2buildTestCase{ description: btc.description, blueprint: fmt.Sprintf(baseBlueprint, prop), - expectedBazelTargets: []string{ - fmt.Sprintf(baseBazelTarget, attr), + targets: []testBazelTarget{ + {"cc_binary", "foo", btc.bazelAttr}, }, }) } diff --git a/bp2build/cc_genrule_conversion_test.go b/bp2build/cc_genrule_conversion_test.go index a7e9cb21a..b3624ddb0 100644 --- a/bp2build/cc_genrule_conversion_test.go +++ b/bp2build/cc_genrule_conversion_test.go @@ -39,15 +39,15 @@ cc_genrule { func runCcGenruleTestCase(t *testing.T, tc bp2buildTestCase) { t.Helper() + (&tc).moduleTypeUnderTest = "cc_genrule" + (&tc).moduleTypeUnderTestFactory = cc.GenRuleFactory + (&tc).moduleTypeUnderTestBp2BuildMutator = genrule.CcGenruleBp2Build runBp2BuildTestCase(t, func(ctx android.RegistrationContext) {}, tc) } func TestCliVariableReplacement(t *testing.T) { runCcGenruleTestCase(t, bp2buildTestCase{ - description: "cc_genrule with command line variable replacements", - moduleTypeUnderTest: "cc_genrule", - moduleTypeUnderTestFactory: cc.GenRuleFactory, - moduleTypeUnderTestBp2BuildMutator: genrule.CcGenruleBp2Build, + description: "cc_genrule with command line variable replacements", blueprint: `cc_genrule { name: "foo.tool", out: ["foo_tool.out"], @@ -65,29 +65,24 @@ cc_genrule { bazel_module: { bp2build_available: true }, }`, expectedBazelTargets: []string{ - `genrule( - name = "foo", - cmd = "$(location :foo.tool) --genDir=$(RULEDIR) arg $(SRCS) $(OUTS)", - outs = ["foo.out"], - srcs = ["foo.in"], - tools = [":foo.tool"], -)`, - `genrule( - name = "foo.tool", - cmd = "cp $(SRCS) $(OUTS)", - outs = ["foo_tool.out"], - srcs = ["foo_tool.in"], -)`, + makeBazelTarget("genrule", "foo", attrNameToString{ + "cmd": `"$(location :foo.tool) --genDir=$(RULEDIR) arg $(SRCS) $(OUTS)"`, + "outs": `["foo.out"]`, + "srcs": `["foo.in"]`, + "tools": `[":foo.tool"]`, + }), + makeBazelTarget("genrule", "foo.tool", attrNameToString{ + "cmd": `"cp $(SRCS) $(OUTS)"`, + "outs": `["foo_tool.out"]`, + "srcs": `["foo_tool.in"]`, + }), }, }) } func TestUsingLocationsLabel(t *testing.T) { runCcGenruleTestCase(t, bp2buildTestCase{ - description: "cc_genrule using $(locations :label)", - moduleTypeUnderTest: "cc_genrule", - moduleTypeUnderTestFactory: cc.GenRuleFactory, - moduleTypeUnderTestBp2BuildMutator: genrule.CcGenruleBp2Build, + description: "cc_genrule using $(locations :label)", blueprint: `cc_genrule { name: "foo.tools", out: ["foo_tool.out", "foo_tool2.out"], @@ -104,32 +99,28 @@ cc_genrule { cmd: "$(locations :foo.tools) -s $(out) $(in)", bazel_module: { bp2build_available: true }, }`, - expectedBazelTargets: []string{`genrule( - name = "foo", - cmd = "$(locations :foo.tools) -s $(OUTS) $(SRCS)", - outs = ["foo.out"], - srcs = ["foo.in"], - tools = [":foo.tools"], -)`, - `genrule( - name = "foo.tools", - cmd = "cp $(SRCS) $(OUTS)", - outs = [ + expectedBazelTargets: []string{ + makeBazelTarget("genrule", "foo", attrNameToString{ + "cmd": `"$(locations :foo.tools) -s $(OUTS) $(SRCS)"`, + "outs": `["foo.out"]`, + "srcs": `["foo.in"]`, + "tools": `[":foo.tools"]`, + }), + makeBazelTarget("genrule", "foo.tools", attrNameToString{ + "cmd": `"cp $(SRCS) $(OUTS)"`, + "outs": `[ "foo_tool.out", "foo_tool2.out", - ], - srcs = ["foo_tool.in"], -)`, + ]`, + "srcs": `["foo_tool.in"]`, + }), }, }) } func TestUsingLocationsAbsoluteLabel(t *testing.T) { runCcGenruleTestCase(t, bp2buildTestCase{ - description: "cc_genrule using $(locations //absolute:label)", - moduleTypeUnderTest: "cc_genrule", - moduleTypeUnderTestFactory: cc.GenRuleFactory, - moduleTypeUnderTestBp2BuildMutator: genrule.CcGenruleBp2Build, + description: "cc_genrule using $(locations //absolute:label)", blueprint: `cc_genrule { name: "foo", out: ["foo.out"], @@ -138,24 +129,21 @@ func TestUsingLocationsAbsoluteLabel(t *testing.T) { cmd: "$(locations :foo.tool) -s $(out) $(in)", 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, + 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) { runCcGenruleTestCase(t, bp2buildTestCase{ - description: "cc_genrule srcs using $(locations //absolute:label)", - moduleTypeUnderTest: "cc_genrule", - moduleTypeUnderTestFactory: cc.GenRuleFactory, - moduleTypeUnderTestBp2BuildMutator: genrule.CcGenruleBp2Build, + description: "cc_genrule srcs using $(locations //absolute:label)", blueprint: `cc_genrule { name: "foo", out: ["foo.out"], @@ -164,24 +152,21 @@ func TestSrcsUsingAbsoluteLabel(t *testing.T) { cmd: "$(locations :foo.tool) -s $(out) $(location :other.tool)", 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, + 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) { runCcGenruleTestCase(t, bp2buildTestCase{ - description: "cc_genrule using $(location) label should substitute first tool label automatically", - moduleTypeUnderTest: "cc_genrule", - moduleTypeUnderTestFactory: cc.GenRuleFactory, - moduleTypeUnderTestBp2BuildMutator: genrule.CcGenruleBp2Build, + description: "cc_genrule using $(location) label should substitute first tool label automatically", blueprint: `cc_genrule { name: "foo", out: ["foo.out"], @@ -190,27 +175,24 @@ func TestLocationsLabelUsesFirstToolFile(t *testing.T) { cmd: "$(location) -s $(out) $(in)", bazel_module: { bp2build_available: true }, }`, - expectedBazelTargets: []string{`genrule( - name = "foo", - cmd = "$(location //other:foo.tool) -s $(OUTS) $(SRCS)", - outs = ["foo.out"], - srcs = ["foo.in"], - tools = [ + filesystem: otherCcGenruleBp, + expectedBazelTargets: []string{ + makeBazelTarget("genrule", "foo", attrNameToString{ + "cmd": `"$(location //other:foo.tool) -s $(OUTS) $(SRCS)"`, + "outs": `["foo.out"]`, + "srcs": `["foo.in"]`, + "tools": `[ "//other:foo.tool", "//other:other.tool", - ], -)`, + ]`, + }), }, - filesystem: otherCcGenruleBp, }) } func TestLocationsLabelUsesFirstTool(t *testing.T) { runCcGenruleTestCase(t, bp2buildTestCase{ - description: "cc_genrule using $(locations) label should substitute first tool label automatically", - moduleTypeUnderTest: "cc_genrule", - moduleTypeUnderTestFactory: cc.GenRuleFactory, - moduleTypeUnderTestBp2BuildMutator: genrule.CcGenruleBp2Build, + description: "cc_genrule using $(locations) label should substitute first tool label automatically", blueprint: `cc_genrule { name: "foo", out: ["foo.out"], @@ -219,27 +201,24 @@ func TestLocationsLabelUsesFirstTool(t *testing.T) { cmd: "$(locations) -s $(out) $(in)", 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 = [ + 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", "//other:other.tool", - ], -)`, + ]`, + }), }, - filesystem: otherCcGenruleBp, }) } func TestWithoutToolsOrToolFiles(t *testing.T) { runCcGenruleTestCase(t, bp2buildTestCase{ - description: "cc_genrule without tools or tool_files can convert successfully", - moduleTypeUnderTest: "cc_genrule", - moduleTypeUnderTestFactory: cc.GenRuleFactory, - moduleTypeUnderTestBp2BuildMutator: genrule.CcGenruleBp2Build, + description: "cc_genrule without tools or tool_files can convert successfully", blueprint: `cc_genrule { name: "foo", out: ["foo.out"], @@ -247,12 +226,12 @@ func TestWithoutToolsOrToolFiles(t *testing.T) { cmd: "cp $(in) $(out)", bazel_module: { bp2build_available: true }, }`, - expectedBazelTargets: []string{`genrule( - name = "foo", - cmd = "cp $(SRCS) $(OUTS)", - outs = ["foo.out"], - srcs = ["foo.in"], -)`, + expectedBazelTargets: []string{ + makeBazelTarget("genrule", "foo", attrNameToString{ + "cmd": `"cp $(SRCS) $(OUTS)"`, + "outs": `["foo.out"]`, + "srcs": `["foo.in"]`, + }), }, }) } diff --git a/bp2build/cc_library_conversion_test.go b/bp2build/cc_library_conversion_test.go index cbdc167dd..a3d902a9d 100644 --- a/bp2build/cc_library_conversion_test.go +++ b/bp2build/cc_library_conversion_test.go @@ -81,8 +81,8 @@ func TestCcLibrarySimple(t *testing.T) { "x86_64.cpp": "", "foo-dir/a.h": "", }, - blueprint: soongCcLibraryPreamble + ` -cc_library_headers { name: "some-headers" } + blueprint: soongCcLibraryPreamble + + simpleModuleDoNotConvertBp2build("cc_library_headers", "some-headers") + ` cc_library { name: "foo-lib", srcs: ["impl.cpp"], @@ -117,17 +117,17 @@ cc_library { include_build_directory: false, } `, - expectedBazelTargets: []string{`cc_library( - name = "foo-lib", - copts = ["-Wall"], - export_includes = ["foo-dir"], - implementation_deps = [":some-headers"], - linkopts = ["-Wl,--exclude-libs=bar.a"] + select({ + expectedBazelTargets: []string{ + makeBazelTarget("cc_library", "foo-lib", attrNameToString{ + "copts": `["-Wall"]`, + "export_includes": `["foo-dir"]`, + "implementation_deps": `[":some-headers"]`, + "linkopts": `["-Wl,--exclude-libs=bar.a"] + select({ "//build/bazel/platforms/arch:x86": ["-Wl,--exclude-libs=baz.a"], "//build/bazel/platforms/arch:x86_64": ["-Wl,--exclude-libs=qux.a"], "//conditions:default": [], - }), - srcs = ["impl.cpp"] + select({ + })`, + "srcs": `["impl.cpp"] + select({ "//build/bazel/platforms/arch:x86": ["x86.cpp"], "//build/bazel/platforms/arch:x86_64": ["x86_64.cpp"], "//conditions:default": [], @@ -140,8 +140,10 @@ cc_library { "//build/bazel/platforms/os:linux": ["linux.cpp"], "//build/bazel/platforms/os:linux_bionic": ["bionic.cpp"], "//conditions:default": [], - }), -)`}}) + })`, + }), + }, + }) } func TestCcLibraryTrimmedLdAndroid(t *testing.T) { @@ -188,16 +190,17 @@ cc_library { include_build_directory: false, } `, - expectedBazelTargets: []string{`cc_library( - name = "fake-ld-android", - copts = [ + expectedBazelTargets: []string{ + makeBazelTarget("cc_library", "fake-ld-android", attrNameToString{ + "srcs": `["ld_android.cpp"]`, + "copts": `[ "-Wall", "-Wextra", "-Wunused", "-Werror", - ], - implementation_deps = [":libc_headers"], - linkopts = [ + ]`, + "implementation_deps": `[":libc_headers"]`, + "linkopts": `[ "-Wl,--exclude-libs=libgcc.a", "-Wl,--exclude-libs=libgcc_stripped.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_64": ["-Wl,--exclude-libs=libgcc_eh.a"], "//conditions:default": [], - }), - srcs = ["ld_android.cpp"], -)`}, + })`, + }), + }, }) } @@ -255,15 +258,16 @@ cc_library { `, }, blueprint: soongCcLibraryPreamble, - expectedBazelTargets: []string{`cc_library( - name = "fake-libarm-optimized-routines-math", - copts = select({ + expectedBazelTargets: []string{ + makeBazelTarget("cc_library", "fake-libarm-optimized-routines-math", attrNameToString{ + "copts": `select({ "//build/bazel/platforms/arch:arm64": ["-DHAVE_FAST_FMA=1"], "//conditions:default": [], - }), - local_includes = ["."], - srcs_c = ["math/cosf.c"], -)`}, + })`, + "local_includes": `["."]`, + "srcs_c": `["math/cosf.c"]`, + }), + }, }) } @@ -348,28 +352,29 @@ cc_library { bazel_module: { bp2build_available: false }, } `, - expectedBazelTargets: []string{`cc_library( - name = "a", - copts = ["bothflag"], - implementation_deps = [":static_dep_for_both"], - implementation_dynamic_deps = [":shared_dep_for_both"], - shared = { + expectedBazelTargets: []string{ + makeBazelTarget("cc_library", "a", attrNameToString{ + "copts": `["bothflag"]`, + "implementation_deps": `[":static_dep_for_both"]`, + "implementation_dynamic_deps": `[":shared_dep_for_both"]`, + "shared": `{ "copts": ["sharedflag"], "implementation_deps": [":static_dep_for_shared"], "implementation_dynamic_deps": [":shared_dep_for_shared"], "srcs": ["sharedonly.cpp"], "whole_archive_deps": [":whole_static_lib_for_shared"], - }, - srcs = ["both.cpp"], - static = { + }`, + "srcs": `["both.cpp"]`, + "static": `{ "copts": ["staticflag"], "implementation_deps": [":static_dep_for_static"], "implementation_dynamic_deps": [":shared_dep_for_static"], "srcs": ["staticonly.cpp"], "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", "shared_dep_for_both") + simpleModuleDoNotConvertBp2build("cc_library", "implementation_shared_dep_for_both"), - expectedBazelTargets: []string{`cc_library( - name = "a", - copts = ["bothflag"], - deps = [":static_dep_for_both"], - dynamic_deps = [":shared_dep_for_both"], - implementation_deps = [":implementation_static_dep_for_both"], - implementation_dynamic_deps = [":implementation_shared_dep_for_both"], - shared = { + expectedBazelTargets: []string{ + makeBazelTarget("cc_library", "a", attrNameToString{ + "copts": `["bothflag"]`, + "deps": `[":static_dep_for_both"]`, + "dynamic_deps": `[":shared_dep_for_both"]`, + "implementation_deps": `[":implementation_static_dep_for_both"]`, + "implementation_dynamic_deps": `[":implementation_shared_dep_for_both"]`, + "shared": `{ "copts": ["sharedflag"], "deps": [":static_dep_for_shared"], "dynamic_deps": [":shared_dep_for_shared"], @@ -450,9 +455,9 @@ cc_library { ":not_explicitly_exported_whole_static_dep_for_shared", ":whole_static_dep_for_shared", ], - }, - srcs = ["both.cpp"], - static = { + }`, + "srcs": `["both.cpp"]`, + "static": `{ "copts": ["staticflag"], "deps": [":static_dep_for_static"], "dynamic_deps": [":shared_dep_for_static"], @@ -463,12 +468,13 @@ cc_library { ":not_explicitly_exported_whole_static_dep_for_static", ":whole_static_dep_for_static", ], - }, - whole_archive_deps = [ + }`, + "whole_archive_deps": `[ ":not_explicitly_exported_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, - expectedBazelTargets: []string{`cc_library( - name = "a", - shared = { + expectedBazelTargets: []string{ + makeBazelTarget("cc_library", "a", attrNameToString{ + "shared": `{ "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_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, - expectedBazelTargets: []string{`cc_library( - name = "a", - copts = ["bothflag"], - implementation_deps = [":static_dep_for_both"], - local_includes = ["."], - shared = { + expectedBazelTargets: []string{ + makeBazelTarget("cc_library", "a", attrNameToString{ + "copts": `["bothflag"]`, + "implementation_deps": `[":static_dep_for_both"]`, + "local_includes": `["."]`, + "shared": `{ "copts": ["sharedflag"] + select({ "//build/bazel/platforms/arch:arm": ["-DARM_SHARED"], "//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"], "//conditions:default": [], }), - }, - srcs = ["both.cpp"], - static = { + }`, + "srcs": `["both.cpp"]`, + "static": `{ "copts": ["staticflag"] + select({ "//build/bazel/platforms/arch:x86": ["-DX86_STATIC"], "//conditions:default": [], @@ -644,8 +651,9 @@ cc_library_static { name: "android_dep_for_shared" } "//build/bazel/platforms/arch:x86": ["x86_static.cpp"], "//conditions:default": [], }), - }, -)`}, + }`, + }), + }, }) } @@ -729,10 +737,10 @@ filegroup { `, }, blueprint: soongCcLibraryPreamble, - expectedBazelTargets: []string{`cc_library( - name = "a", - local_includes = ["."], - shared = { + expectedBazelTargets: []string{ + makeBazelTarget("cc_library", "a", attrNameToString{ + "local_includes": `["."]`, + "shared": `{ "srcs": [ "shared_source.cpp", "shared_source.cc", @@ -747,22 +755,22 @@ filegroup { "shared_source.c", ":shared_filegroup_c_srcs", ], - }, - srcs = [ + }`, + "srcs": `[ "both_source.cpp", "both_source.cc", ":both_filegroup_cpp_srcs", - ], - srcs_as = [ + ]`, + "srcs_as": `[ "both_source.s", "both_source.S", ":both_filegroup_as_srcs", - ], - srcs_c = [ + ]`, + "srcs_c": `[ "both_source.c", ":both_filegroup_c_srcs", - ], - static = { + ]`, + "static": `{ "srcs": [ "static_source.cpp", "static_source.cc", @@ -777,8 +785,9 @@ filegroup { "static_source.c", ":static_filegroup_c_srcs", ], - }, -)`}, + }`, + }), + }, }) } @@ -801,12 +810,13 @@ cc_library { `, }, blueprint: soongCcLibraryPreamble, - expectedBazelTargets: []string{`cc_library( - name = "a", - additional_linker_inputs = ["v.map"], - linkopts = ["-Wl,--version-script,$(location v.map)"], - srcs = ["a.cpp"], -)`}, + expectedBazelTargets: []string{ + makeBazelTarget("cc_library", "a", attrNameToString{ + "additional_linker_inputs": `["v.map"]`, + "linkopts": `["-Wl,--version-script,$(location v.map)"]`, + "srcs": `["a.cpp"]`, + }), + }, }) } @@ -837,20 +847,21 @@ cc_library { `, }, blueprint: soongCcLibraryPreamble, - expectedBazelTargets: []string{`cc_library( - name = "a", - additional_linker_inputs = select({ + expectedBazelTargets: []string{ + makeBazelTarget("cc_library", "a", attrNameToString{ + "additional_linker_inputs": `select({ "//build/bazel/platforms/arch:arm": ["arm.map"], "//build/bazel/platforms/arch:arm64": ["arm64.map"], "//conditions:default": [], - }), - linkopts = select({ + })`, + "linkopts": `select({ "//build/bazel/platforms/arch:arm": ["-Wl,--version-script,$(location arm.map)"], "//build/bazel/platforms/arch:arm64": ["-Wl,--version-script,$(location arm64.map)"], "//conditions:default": [], - }), - srcs = ["a.cpp"], -)`}, + })`, + "srcs": `["a.cpp"]`, + }), + }, }) } @@ -872,10 +883,11 @@ cc_library { include_build_directory: false, } `, - expectedBazelTargets: []string{`cc_library( - name = "a", - implementation_dynamic_deps = [":mylib"], -)`}, + expectedBazelTargets: []string{ + makeBazelTarget("cc_library", "a", attrNameToString{ + "implementation_dynamic_deps": `[":mylib"]`, + }), + }, }) } @@ -917,34 +929,33 @@ cc_library { }, include_build_directory: false, }`, - expectedBazelTargets: []string{`cc_library( - name = "a", - features = [ + expectedBazelTargets: []string{ + makeBazelTarget("cc_library", "a", attrNameToString{ + "features": `[ "disable_pack_relocations", "-no_undefined_symbols", - ], - srcs = ["a.cpp"], -)`, `cc_library( - name = "b", - features = select({ + ]`, + "srcs": `["a.cpp"]`, + }), makeBazelTarget("cc_library", "b", attrNameToString{ + "features": `select({ "//build/bazel/platforms/arch:x86_64": [ "disable_pack_relocations", "-no_undefined_symbols", ], "//conditions:default": [], - }), - srcs = ["b.cpp"], -)`, `cc_library( - name = "c", - features = select({ + })`, + "srcs": `["b.cpp"]`, + }), makeBazelTarget("cc_library", "c", attrNameToString{ + "features": `select({ "//build/bazel/platforms/os:darwin": [ "disable_pack_relocations", "-no_undefined_symbols", ], "//conditions:default": [], - }), - srcs = ["c.cpp"], -)`}, + })`, + "srcs": `["c.cpp"]`, + }), + }, }) } @@ -961,13 +972,14 @@ cc_library { include_build_directory: false, } `, - expectedBazelTargets: []string{`cc_library( - name = "a", - copts = [ + expectedBazelTargets: []string{ + makeBazelTarget("cc_library", "a", attrNameToString{ + "copts": `[ "-include", "header.h", - ], -)`}, + ]`, + }), + }, }) } @@ -998,10 +1010,10 @@ func TestCcLibraryCppFlagsGoesIntoCopts(t *testing.T) { include_build_directory: false, } `, - expectedBazelTargets: []string{`cc_library( - name = "a", - copts = ["-Wall"], - cppflags = [ + expectedBazelTargets: []string{ + makeBazelTarget("cc_library", "a", attrNameToString{ + "copts": `["-Wall"]`, + "cppflags": `[ "-fsigned-char", "-pedantic", ] + select({ @@ -1010,9 +1022,10 @@ func TestCcLibraryCppFlagsGoesIntoCopts(t *testing.T) { }) + select({ "//build/bazel/platforms/os:android": ["-DANDROID=1"], "//conditions:default": [], - }), - srcs = ["a.cpp"], -)`}, + })`, + "srcs": `["a.cpp"]`, + }), + }, }) } @@ -1097,31 +1110,30 @@ cc_library { } `, expectedBazelTargets: []string{ - `cc_library( - name = "foo_static", - implementation_deps = select({ + makeBazelTarget("cc_library", "foo_static", attrNameToString{ + "implementation_deps": `select({ "//build/bazel/platforms/arch:arm": [], "//conditions:default": [":arm_static_lib_excludes_bp2build_cc_library_static"], }) + select({ "//build/bazel/product_variables:malloc_not_svelte": [], "//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": [], "//conditions:default": [":arm_shared_lib_excludes"], }) + select({ "//build/bazel/product_variables:malloc_not_svelte": [":malloc_not_svelte_shared_lib"], "//conditions:default": [], - }), - srcs_c = ["common.c"], - whole_archive_deps = select({ + })`, + "srcs_c": `["common.c"]`, + "whole_archive_deps": `select({ "//build/bazel/platforms/arch:arm": [], "//conditions:default": [":arm_whole_static_lib_excludes_bp2build_cc_library_static"], }) + select({ "//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"], - }), -)`, + })`, + }), }, }) } @@ -1143,11 +1155,13 @@ cc_library { include_build_directory: false, } `, - expectedBazelTargets: []string{`cc_library( - name = "foo-lib", - link_crt = False, - srcs = ["impl.cpp"], -)`}}) + expectedBazelTargets: []string{ + makeBazelTarget("cc_library", "foo-lib", attrNameToString{ + "link_crt": `False`, + "srcs": `["impl.cpp"]`, + }), + }, + }) } func TestCCLibraryNoCrtFalse(t *testing.T) { @@ -1167,10 +1181,12 @@ cc_library { include_build_directory: false, } `, - expectedBazelTargets: []string{`cc_library( - name = "foo-lib", - srcs = ["impl.cpp"], -)`}}) + expectedBazelTargets: []string{ + makeBazelTarget("cc_library", "foo-lib", attrNameToString{ + "srcs": `["impl.cpp"]`, + }), + }, + }) } func TestCCLibraryNoCrtArchVariant(t *testing.T) { @@ -1219,11 +1235,12 @@ cc_library { include_build_directory: false, } `, - expectedBazelTargets: []string{`cc_library( - name = "foo-lib", - srcs = ["impl.cpp"], - use_libcrt = False, -)`}}) + expectedBazelTargets: []string{ + makeBazelTarget("cc_library", "foo-lib", attrNameToString{ + "srcs": `["impl.cpp"]`, + "use_libcrt": `False`, + }), + }}) } func TestCCLibraryNoLibCrtFalse(t *testing.T) { @@ -1243,11 +1260,12 @@ cc_library { include_build_directory: false, } `, - expectedBazelTargets: []string{`cc_library( - name = "foo-lib", - srcs = ["impl.cpp"], - use_libcrt = True, -)`}}) + expectedBazelTargets: []string{ + makeBazelTarget("cc_library", "foo-lib", attrNameToString{ + "srcs": `["impl.cpp"]`, + "use_libcrt": `True`, + }), + }}) } func TestCCLibraryNoLibCrtArchVariant(t *testing.T) { @@ -1273,15 +1291,16 @@ cc_library { include_build_directory: false, } `, - expectedBazelTargets: []string{`cc_library( - name = "foo-lib", - srcs = ["impl.cpp"], - use_libcrt = select({ + expectedBazelTargets: []string{ + makeBazelTarget("cc_library", "foo-lib", attrNameToString{ + "srcs": `["impl.cpp"]`, + "use_libcrt": `select({ "//build/bazel/platforms/arch:arm": False, "//build/bazel/platforms/arch:x86": False, "//conditions:default": None, - }), -)`}}) + })`, + }), + }}) } func TestCcLibraryStrip(t *testing.T) { @@ -1331,34 +1350,29 @@ cc_library { include_build_directory: false, } `, - expectedBazelTargets: []string{`cc_library( - name = "all", - strip = { + expectedBazelTargets: []string{ + makeBazelTarget("cc_library", "all", attrNameToString{ + "strip": `{ "all": True, - }, -)`, `cc_library( - name = "keep_symbols", - strip = { + }`, + }), makeBazelTarget("cc_library", "keep_symbols", attrNameToString{ + "strip": `{ "keep_symbols": True, - }, -)`, `cc_library( - name = "keep_symbols_and_debug_frame", - strip = { + }`, + }), makeBazelTarget("cc_library", "keep_symbols_and_debug_frame", attrNameToString{ + "strip": `{ "keep_symbols_and_debug_frame": True, - }, -)`, `cc_library( - name = "keep_symbols_list", - strip = { + }`, + }), makeBazelTarget("cc_library", "keep_symbols_list", attrNameToString{ + "strip": `{ "keep_symbols_list": ["symbol"], - }, -)`, `cc_library( - name = "none", - strip = { + }`, + }), makeBazelTarget("cc_library", "none", attrNameToString{ + "strip": `{ "none": True, - }, -)`, `cc_library( - name = "nothing", -)`}, + }`, + }), makeBazelTarget("cc_library", "nothing", attrNameToString{}), + }, }) } @@ -1393,9 +1407,9 @@ cc_library { include_build_directory: false, } `, - expectedBazelTargets: []string{`cc_library( - name = "multi-arch", - strip = { + expectedBazelTargets: []string{ + makeBazelTarget("cc_library", "multi-arch", attrNameToString{ + "strip": `{ "keep_symbols": select({ "//build/bazel/platforms/arch:arm64": True, "//conditions:default": None, @@ -1411,8 +1425,9 @@ cc_library { ], "//conditions:default": [], }), - }, -)`}, + }`, + }), + }, }) } @@ -1429,10 +1444,11 @@ cc_library { include_build_directory: false, } `, - expectedBazelTargets: []string{`cc_library( - name = "root_empty", - system_dynamic_deps = [], -)`}, + expectedBazelTargets: []string{ + makeBazelTarget("cc_library", "root_empty", attrNameToString{ + "system_dynamic_deps": `[]`, + }), + }, }) } @@ -1451,12 +1467,13 @@ cc_library { include_build_directory: false, } `, - expectedBazelTargets: []string{`cc_library( - name = "static_empty", - static = { + expectedBazelTargets: []string{ + makeBazelTarget("cc_library", "static_empty", attrNameToString{ + "static": `{ "system_dynamic_deps": [], - }, -)`}, + }`, + }), + }, }) } @@ -1475,12 +1492,13 @@ cc_library { include_build_directory: false, } `, - expectedBazelTargets: []string{`cc_library( - name = "shared_empty", - shared = { + expectedBazelTargets: []string{ + makeBazelTarget("cc_library", "shared_empty", attrNameToString{ + "shared": `{ "system_dynamic_deps": [], - }, -)`}, + }`, + }), + }, }) } @@ -1503,12 +1521,13 @@ cc_library { include_build_directory: false, } `, - expectedBazelTargets: []string{`cc_library( - name = "shared_empty", - shared = { + expectedBazelTargets: []string{ + makeBazelTarget("cc_library", "shared_empty", attrNameToString{ + "shared": `{ "system_dynamic_deps": [], - }, -)`}, + }`, + }), + }, }) } @@ -1533,10 +1552,11 @@ cc_library { include_build_directory: false, } `, - expectedBazelTargets: []string{`cc_library( - name = "target_linux_bionic_empty", - system_dynamic_deps = [], -)`}, + expectedBazelTargets: []string{ + makeBazelTarget("cc_library", "target_linux_bionic_empty", attrNameToString{ + "system_dynamic_deps": `[]`, + }), + }, }) } @@ -1557,10 +1577,11 @@ cc_library { include_build_directory: false, } `, - expectedBazelTargets: []string{`cc_library( - name = "target_bionic_empty", - system_dynamic_deps = [], -)`}, + expectedBazelTargets: []string{ + makeBazelTarget("cc_library", "target_bionic_empty", attrNameToString{ + "system_dynamic_deps": `[]`, + }), + }, }) } @@ -1589,13 +1610,14 @@ cc_library { include_build_directory: false, } `, - expectedBazelTargets: []string{`cc_library( - name = "foo", - shared = { + expectedBazelTargets: []string{ + makeBazelTarget("cc_library", "foo", attrNameToString{ + "shared": `{ "system_dynamic_deps": [":libm"], - }, - system_dynamic_deps = [":libc"], -)`}, + }`, + "system_dynamic_deps": `[":libc"]`, + }), + }, }) } @@ -1637,9 +1659,9 @@ cc_library { include_build_directory: false, } `, - expectedBazelTargets: []string{`cc_library( - name = "foo-lib", - srcs = ["base.cpp"] + select({ + expectedBazelTargets: []string{ + makeBazelTarget("cc_library", "foo-lib", attrNameToString{ + "srcs": `["base.cpp"] + select({ "//build/bazel/platforms/os:android": [ "linux.cpp", "bionic.cpp", @@ -1660,9 +1682,10 @@ cc_library { ], "//build/bazel/platforms/os:windows": ["windows.cpp"], "//conditions:default": [], - }), -)`}}) - + })`, + }), + }, + }) } 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"}, } for _, tc := range testCases { - cppStdAttr := "" + cppStdProp := "" 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 != "" { - 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 != "" { - bazelCppStdAttr = fmt.Sprintf("\n cpp_std = \"%s\",", tc.bazel_cpp_std) + attrs["cpp_std"] = fmt.Sprintf(`"%s"`, tc.bazel_cpp_std) } runCcLibraryTestCase(t, bp2buildTestCase{ @@ -1740,10 +1763,10 @@ cc_library { %s // gnu_extensions: *bool include_build_directory: false, } -`, cppStdAttr, gnuExtensionsAttr), - expectedBazelTargets: []string{fmt.Sprintf(`cc_library( - name = "a",%s -)`, bazelCppStdAttr)}, +`, cppStdProp, gnuExtensionsProp), + expectedBazelTargets: []string{ + makeBazelTarget("cc_library", "a", attrs), + }, }) runCcLibraryStaticTestCase(t, bp2buildTestCase{ @@ -1759,10 +1782,10 @@ cc_library_static { %s // gnu_extensions: *bool include_build_directory: false, } -`, cppStdAttr, gnuExtensionsAttr), - expectedBazelTargets: []string{fmt.Sprintf(`cc_library_static( - name = "a",%s -)`, bazelCppStdAttr)}, +`, cppStdProp, gnuExtensionsProp), + expectedBazelTargets: []string{ + makeBazelTarget("cc_library_static", "a", attrs), + }, }) runCcLibrarySharedTestCase(t, bp2buildTestCase{ @@ -1778,10 +1801,10 @@ cc_library_shared { %s // gnu_extensions: *bool include_build_directory: false, } -`, cppStdAttr, gnuExtensionsAttr), - expectedBazelTargets: []string{fmt.Sprintf(`cc_library_shared( - name = "a",%s -)`, bazelCppStdAttr)}, +`, cppStdProp, gnuExtensionsProp), + expectedBazelTargets: []string{ + makeBazelTarget("cc_library_shared", "a", attrs), + }, }) } } diff --git a/bp2build/cc_library_headers_conversion_test.go b/bp2build/cc_library_headers_conversion_test.go index e43672bea..76fdab2f4 100644 --- a/bp2build/cc_library_headers_conversion_test.go +++ b/bp2build/cc_library_headers_conversion_test.go @@ -128,9 +128,9 @@ cc_library_headers { // TODO: Also support export_header_lib_headers }`, - expectedBazelTargets: []string{`cc_library_headers( - name = "foo_headers", - export_includes = [ + expectedBazelTargets: []string{ + makeBazelTarget("cc_library_headers", "foo_headers", attrNameToString{ + "export_includes": `[ "dir-1", "dir-2", ] + select({ @@ -138,12 +138,13 @@ cc_library_headers { "//build/bazel/platforms/arch:x86": ["arch_x86_exported_include_dir"], "//build/bazel/platforms/arch:x86_64": ["arch_x86_64_exported_include_dir"], "//conditions:default": [], - }), - implementation_deps = [ + })`, + "implementation_deps": `[ ":lib-1", ":lib-2", - ], -)`}, + ]`, + }), + }, }) } @@ -191,17 +192,18 @@ cc_library_headers { }, include_build_directory: false, }`, - expectedBazelTargets: []string{`cc_library_headers( - name = "foo_headers", - implementation_deps = [":base-lib"] + select({ + expectedBazelTargets: []string{ + makeBazelTarget("cc_library_headers", "foo_headers", attrNameToString{ + "implementation_deps": `[":base-lib"] + select({ "//build/bazel/platforms/os:android": [":android-lib"], "//build/bazel/platforms/os:darwin": [":darwin-lib"], "//build/bazel/platforms/os:linux": [":linux-lib"], "//build/bazel/platforms/os:linux_bionic": [":linux_bionic-lib"], "//build/bazel/platforms/os:windows": [":windows-lib"], "//conditions:default": [], - }), -)`}, + })`, + }), + }, }) } @@ -231,17 +233,18 @@ cc_library_headers { }, include_build_directory: false, }`, - expectedBazelTargets: []string{`cc_library_headers( - name = "foo_headers", - deps = select({ + expectedBazelTargets: []string{ + makeBazelTarget("cc_library_headers", "foo_headers", attrNameToString{ + "deps": `select({ "//build/bazel/platforms/os:android": [":exported-lib"], "//conditions:default": [], - }), - implementation_deps = select({ + })`, + "implementation_deps": `select({ "//build/bazel/platforms/os:android": [":android-lib"], "//conditions:default": [], - }), -)`}, + })`, + }), + }, }) } @@ -288,9 +291,9 @@ func TestCcLibraryHeadersArchAndTargetExportSystemIncludes(t *testing.T) { }, include_build_directory: false, }`, - expectedBazelTargets: []string{`cc_library_headers( - name = "foo_headers", - export_system_includes = ["shared_include_dir"] + select({ + expectedBazelTargets: []string{ + makeBazelTarget("cc_library_headers", "foo_headers", attrNameToString{ + "export_system_includes": `["shared_include_dir"] + select({ "//build/bazel/platforms/arch:arm": ["arm_include_dir"], "//build/bazel/platforms/arch:x86_64": ["x86_64_include_dir"], "//conditions:default": [], @@ -299,8 +302,9 @@ func TestCcLibraryHeadersArchAndTargetExportSystemIncludes(t *testing.T) { "//build/bazel/platforms/os:darwin": ["darwin_include_dir"], "//build/bazel/platforms/os:linux": ["linux_include_dir"], "//conditions:default": [], - }), -)`}, + })`, + }), + }, }) } @@ -330,9 +334,10 @@ cc_library_headers { no_libcrt: true, include_build_directory: false, }`, - expectedBazelTargets: []string{`cc_library_headers( - name = "lib-1", - export_includes = ["lib-1"], -)`}, + expectedBazelTargets: []string{ + makeBazelTarget("cc_library_headers", "lib-1", attrNameToString{ + "export_includes": `["lib-1"]`, + }), + }, }) } diff --git a/bp2build/cc_library_shared_conversion_test.go b/bp2build/cc_library_shared_conversion_test.go index bb15776eb..4ec95c3a3 100644 --- a/bp2build/cc_library_shared_conversion_test.go +++ b/bp2build/cc_library_shared_conversion_test.go @@ -37,15 +37,15 @@ func registerCcLibrarySharedModuleTypes(ctx android.RegistrationContext) { func runCcLibrarySharedTestCase(t *testing.T, tc bp2buildTestCase) { t.Helper() + (&tc).moduleTypeUnderTest = "cc_library_shared" + (&tc).moduleTypeUnderTestFactory = cc.LibrarySharedFactory + (&tc).moduleTypeUnderTestBp2BuildMutator = cc.CcLibrarySharedBp2Build runBp2BuildTestCase(t, registerCcLibrarySharedModuleTypes, tc) } func TestCcLibrarySharedSimple(t *testing.T) { runCcLibrarySharedTestCase(t, bp2buildTestCase{ - description: "cc_library_shared simple overall test", - moduleTypeUnderTest: "cc_library_shared", - moduleTypeUnderTestFactory: cc.LibrarySharedFactory, - moduleTypeUnderTestBp2BuildMutator: cc.CcLibrarySharedBp2Build, + description: "cc_library_shared simple overall test", filesystem: map[string]string{ // NOTE: include_dir headers *should not* appear in Bazel hdrs later (?) "include_dir_1/include_dir_1_a.h": "", @@ -140,52 +140,50 @@ cc_library_shared { // TODO: Also support export_header_lib_headers }`, - expectedBazelTargets: []string{`cc_library_shared( - name = "foo_shared", - absolute_includes = [ + expectedBazelTargets: []string{ + makeBazelTarget("cc_library_shared", "foo_shared", attrNameToString{ + "absolute_includes": `[ "include_dir_1", "include_dir_2", - ], - copts = [ + ]`, + "copts": `[ "-Dflag1", "-Dflag2", - ], - export_includes = [ + ]`, + "export_includes": `[ "export_include_dir_1", "export_include_dir_2", - ], - implementation_deps = [ + ]`, + "implementation_deps": `[ ":header_lib_1", ":header_lib_2", - ], - implementation_dynamic_deps = [ + ]`, + "implementation_dynamic_deps": `[ ":shared_lib_1", ":shared_lib_2", - ], - local_includes = [ + ]`, + "local_includes": `[ "local_include_dir_1", "local_include_dir_2", ".", - ], - srcs = [ + ]`, + "srcs": `[ "foo_shared1.cc", "foo_shared2.cc", - ], - whole_archive_deps = [ + ]`, + "whole_archive_deps": `[ ":whole_static_lib_1", ":whole_static_lib_2", - ], -)`}, + ]`, + }), + }, }) } func TestCcLibrarySharedArchSpecificSharedLib(t *testing.T) { runCcLibrarySharedTestCase(t, bp2buildTestCase{ - description: "cc_library_shared arch-specific shared_libs with whole_static_libs", - moduleTypeUnderTest: "cc_library_shared", - moduleTypeUnderTestFactory: cc.LibrarySharedFactory, - moduleTypeUnderTestBp2BuildMutator: cc.CcLibrarySharedBp2Build, - filesystem: map[string]string{}, + description: "cc_library_shared arch-specific shared_libs with whole_static_libs", + filesystem: map[string]string{}, blueprint: soongCcLibrarySharedPreamble + ` cc_library_static { name: "static_dep", @@ -200,27 +198,25 @@ cc_library_shared { arch: { arm64: { shared_libs: ["shared_dep"], whole_static_libs: ["static_dep"] } }, include_build_directory: false, }`, - expectedBazelTargets: []string{`cc_library_shared( - name = "foo_shared", - implementation_dynamic_deps = select({ + expectedBazelTargets: []string{ + makeBazelTarget("cc_library_shared", "foo_shared", attrNameToString{ + "implementation_dynamic_deps": `select({ "//build/bazel/platforms/arch:arm64": [":shared_dep"], "//conditions:default": [], - }), - whole_archive_deps = select({ + })`, + "whole_archive_deps": `select({ "//build/bazel/platforms/arch:arm64": [":static_dep"], "//conditions:default": [], - }), -)`}, + })`, + }), + }, }) } func TestCcLibrarySharedOsSpecificSharedLib(t *testing.T) { - runCcLibraryStaticTestCase(t, bp2buildTestCase{ - description: "cc_library_shared os-specific shared_libs", - moduleTypeUnderTest: "cc_library_shared", - moduleTypeUnderTestFactory: cc.LibrarySharedFactory, - moduleTypeUnderTestBp2BuildMutator: cc.CcLibrarySharedBp2Build, - filesystem: map[string]string{}, + runCcLibrarySharedTestCase(t, bp2buildTestCase{ + description: "cc_library_shared os-specific shared_libs", + filesystem: map[string]string{}, blueprint: soongCcLibrarySharedPreamble + ` cc_library_shared { name: "shared_dep", @@ -231,23 +227,21 @@ cc_library_shared { target: { android: { shared_libs: ["shared_dep"], } }, include_build_directory: false, }`, - expectedBazelTargets: []string{`cc_library_shared( - name = "foo_shared", - implementation_dynamic_deps = select({ + expectedBazelTargets: []string{ + makeBazelTarget("cc_library_shared", "foo_shared", attrNameToString{ + "implementation_dynamic_deps": `select({ "//build/bazel/platforms/os:android": [":shared_dep"], "//conditions:default": [], - }), -)`}, + })`, + }), + }, }) } func TestCcLibrarySharedBaseArchOsSpecificSharedLib(t *testing.T) { runCcLibrarySharedTestCase(t, bp2buildTestCase{ - description: "cc_library_shared base, arch, and os-specific shared_libs", - moduleTypeUnderTest: "cc_library_shared", - moduleTypeUnderTestFactory: cc.LibrarySharedFactory, - moduleTypeUnderTestBp2BuildMutator: cc.CcLibrarySharedBp2Build, - filesystem: map[string]string{}, + description: "cc_library_shared base, arch, and os-specific shared_libs", + filesystem: map[string]string{}, blueprint: soongCcLibrarySharedPreamble + ` cc_library_shared { name: "shared_dep", @@ -268,25 +262,23 @@ cc_library_shared { arch: { arm64: { shared_libs: ["shared_dep3"] } }, include_build_directory: false, }`, - expectedBazelTargets: []string{`cc_library_shared( - name = "foo_shared", - implementation_dynamic_deps = [":shared_dep"] + select({ + expectedBazelTargets: []string{ + makeBazelTarget("cc_library_shared", "foo_shared", attrNameToString{ + "implementation_dynamic_deps": `[":shared_dep"] + select({ "//build/bazel/platforms/arch:arm64": [":shared_dep3"], "//conditions:default": [], }) + select({ "//build/bazel/platforms/os:android": [":shared_dep2"], "//conditions:default": [], - }), -)`}, + })`, + }), + }, }) } func TestCcLibrarySharedSimpleExcludeSrcs(t *testing.T) { runCcLibrarySharedTestCase(t, bp2buildTestCase{ - description: "cc_library_shared simple exclude_srcs", - moduleTypeUnderTest: "cc_library_shared", - moduleTypeUnderTestFactory: cc.LibrarySharedFactory, - moduleTypeUnderTestBp2BuildMutator: cc.CcLibrarySharedBp2Build, + description: "cc_library_shared simple exclude_srcs", filesystem: map[string]string{ "common.c": "", "foo-a.c": "", @@ -299,23 +291,21 @@ cc_library_shared { exclude_srcs: ["foo-excluded.c"], include_build_directory: false, }`, - expectedBazelTargets: []string{`cc_library_shared( - name = "foo_shared", - srcs_c = [ + expectedBazelTargets: []string{ + makeBazelTarget("cc_library_shared", "foo_shared", attrNameToString{ + "srcs_c": `[ "common.c", "foo-a.c", - ], -)`}, + ]`, + }), + }, }) } func TestCcLibrarySharedStrip(t *testing.T) { runCcLibrarySharedTestCase(t, bp2buildTestCase{ - description: "cc_library_shared stripping", - moduleTypeUnderTest: "cc_library_shared", - moduleTypeUnderTestFactory: cc.LibrarySharedFactory, - moduleTypeUnderTestBp2BuildMutator: cc.CcLibrarySharedBp2Build, - filesystem: map[string]string{}, + description: "cc_library_shared stripping", + filesystem: map[string]string{}, blueprint: soongCcLibrarySharedPreamble + ` cc_library_shared { name: "foo_shared", @@ -328,9 +318,9 @@ cc_library_shared { }, include_build_directory: false, }`, - expectedBazelTargets: []string{`cc_library_shared( - name = "foo_shared", - strip = { + expectedBazelTargets: []string{ + makeBazelTarget("cc_library_shared", "foo_shared", attrNameToString{ + "strip": `{ "all": True, "keep_symbols": False, "keep_symbols_and_debug_frame": True, @@ -339,17 +329,15 @@ cc_library_shared { "sym2", ], "none": False, - }, -)`}, + }`, + }), + }, }) } func TestCcLibrarySharedVersionScript(t *testing.T) { runCcLibrarySharedTestCase(t, bp2buildTestCase{ - description: "cc_library_shared version script", - moduleTypeUnderTest: "cc_library_shared", - moduleTypeUnderTestFactory: cc.LibrarySharedFactory, - moduleTypeUnderTestBp2BuildMutator: cc.CcLibrarySharedBp2Build, + description: "cc_library_shared version script", filesystem: map[string]string{ "version_script": "", }, @@ -359,20 +347,18 @@ cc_library_shared { version_script: "version_script", include_build_directory: false, }`, - expectedBazelTargets: []string{`cc_library_shared( - name = "foo_shared", - additional_linker_inputs = ["version_script"], - linkopts = ["-Wl,--version-script,$(location version_script)"], -)`}, + expectedBazelTargets: []string{ + makeBazelTarget("cc_library_shared", "foo_shared", attrNameToString{ + "additional_linker_inputs": `["version_script"]`, + "linkopts": `["-Wl,--version-script,$(location version_script)"]`, + }), + }, }) } func TestCcLibrarySharedNoCrtTrue(t *testing.T) { runCcLibrarySharedTestCase(t, bp2buildTestCase{ - description: "cc_library_shared - nocrt: true emits attribute", - moduleTypeUnderTest: "cc_library_shared", - moduleTypeUnderTestFactory: cc.LibrarySharedFactory, - moduleTypeUnderTestBp2BuildMutator: cc.CcLibrarySharedBp2Build, + description: "cc_library_shared - nocrt: true emits attribute", filesystem: map[string]string{ "impl.cpp": "", }, @@ -384,19 +370,18 @@ cc_library_shared { include_build_directory: false, } `, - expectedBazelTargets: []string{`cc_library_shared( - name = "foo_shared", - link_crt = False, - srcs = ["impl.cpp"], -)`}}) + expectedBazelTargets: []string{ + makeBazelTarget("cc_library_shared", "foo_shared", attrNameToString{ + "link_crt": `False`, + "srcs": `["impl.cpp"]`, + }), + }, + }) } func TestCcLibrarySharedNoCrtFalse(t *testing.T) { runCcLibrarySharedTestCase(t, bp2buildTestCase{ - description: "cc_library_shared - nocrt: false doesn't emit attribute", - moduleTypeUnderTest: "cc_library_shared", - moduleTypeUnderTestFactory: cc.LibrarySharedFactory, - moduleTypeUnderTestBp2BuildMutator: cc.CcLibrarySharedBp2Build, + description: "cc_library_shared - nocrt: false doesn't emit attribute", filesystem: map[string]string{ "impl.cpp": "", }, @@ -408,18 +393,17 @@ cc_library_shared { include_build_directory: false, } `, - expectedBazelTargets: []string{`cc_library_shared( - name = "foo_shared", - srcs = ["impl.cpp"], -)`}}) + expectedBazelTargets: []string{ + makeBazelTarget("cc_library_shared", "foo_shared", attrNameToString{ + "srcs": `["impl.cpp"]`, + }), + }, + }) } func TestCcLibrarySharedNoCrtArchVariant(t *testing.T) { runCcLibrarySharedTestCase(t, bp2buildTestCase{ - description: "cc_library_shared - nocrt in select", - moduleTypeUnderTest: "cc_library_shared", - moduleTypeUnderTestFactory: cc.LibrarySharedFactory, - moduleTypeUnderTestBp2BuildMutator: cc.CcLibrarySharedBp2Build, + description: "cc_library_shared - nocrt in select", filesystem: map[string]string{ "impl.cpp": "", }, diff --git a/bp2build/cc_library_static_conversion_test.go b/bp2build/cc_library_static_conversion_test.go index 9887811df..2f760d236 100644 --- a/bp2build/cc_library_static_conversion_test.go +++ b/bp2build/cc_library_static_conversion_test.go @@ -79,15 +79,16 @@ func registerCcLibraryStaticModuleTypes(ctx android.RegistrationContext) { func runCcLibraryStaticTestCase(t *testing.T, tc bp2buildTestCase) { t.Helper() + + (&tc).moduleTypeUnderTest = "cc_library_static" + (&tc).moduleTypeUnderTestFactory = cc.LibraryStaticFactory + (&tc).moduleTypeUnderTestBp2BuildMutator = cc.CcLibraryStaticBp2Build runBp2BuildTestCase(t, registerCcLibraryStaticModuleTypes, tc) } func TestCcLibraryStaticSimple(t *testing.T) { runCcLibraryStaticTestCase(t, bp2buildTestCase{ - description: "cc_library_static test", - moduleTypeUnderTest: "cc_library_static", - moduleTypeUnderTestFactory: cc.LibraryStaticFactory, - moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryStaticBp2Build, + description: "cc_library_static test", filesystem: map[string]string{ // NOTE: include_dir headers *should not* appear in Bazel hdrs later (?) "include_dir_1/include_dir_1_a.h": "", @@ -182,49 +183,47 @@ cc_library_static { // TODO: Also support export_header_lib_headers }`, - expectedBazelTargets: []string{`cc_library_static( - name = "foo_static", - absolute_includes = [ + expectedBazelTargets: []string{ + makeBazelTarget("cc_library_static", "foo_static", attrNameToString{ + "absolute_includes": `[ "include_dir_1", "include_dir_2", - ], - copts = [ + ]`, + "copts": `[ "-Dflag1", "-Dflag2", - ], - export_includes = [ + ]`, + "export_includes": `[ "export_include_dir_1", "export_include_dir_2", - ], - implementation_deps = [ + ]`, + "implementation_deps": `[ ":header_lib_1", ":header_lib_2", ":static_lib_1", ":static_lib_2", - ], - local_includes = [ + ]`, + "local_includes": `[ "local_include_dir_1", "local_include_dir_2", ".", - ], - srcs = [ + ]`, + "srcs": `[ "foo_static1.cc", "foo_static2.cc", - ], - whole_archive_deps = [ + ]`, + "whole_archive_deps": `[ ":whole_static_lib_1", ":whole_static_lib_2", - ], -)`}, + ]`, + }), + }, }) } func TestCcLibraryStaticSubpackage(t *testing.T) { runCcLibraryStaticTestCase(t, bp2buildTestCase{ - description: "cc_library_static subpackage test", - moduleTypeUnderTest: "cc_library_static", - moduleTypeUnderTestFactory: cc.LibraryStaticFactory, - moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryStaticBp2Build, + description: "cc_library_static subpackage test", filesystem: map[string]string{ // subpackage with subdirectory "subpackage/Android.bp": "", @@ -247,20 +246,18 @@ cc_library_static { "subpackage", ], }`, - expectedBazelTargets: []string{`cc_library_static( - name = "foo_static", - absolute_includes = ["subpackage"], - local_includes = ["."], -)`}, + expectedBazelTargets: []string{ + makeBazelTarget("cc_library_static", "foo_static", attrNameToString{ + "absolute_includes": `["subpackage"]`, + "local_includes": `["."]`, + }), + }, }) } func TestCcLibraryStaticExportIncludeDir(t *testing.T) { runCcLibraryStaticTestCase(t, bp2buildTestCase{ - description: "cc_library_static export include dir", - moduleTypeUnderTest: "cc_library_static", - moduleTypeUnderTestFactory: cc.LibraryStaticFactory, - moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryStaticBp2Build, + description: "cc_library_static export include dir", filesystem: map[string]string{ // subpackage with subdirectory "subpackage/Android.bp": "", @@ -273,19 +270,17 @@ cc_library_static { export_include_dirs: ["subpackage"], include_build_directory: false, }`, - expectedBazelTargets: []string{`cc_library_static( - name = "foo_static", - export_includes = ["subpackage"], -)`}, + expectedBazelTargets: []string{ + makeBazelTarget("cc_library_static", "foo_static", attrNameToString{ + "export_includes": `["subpackage"]`, + }), + }, }) } func TestCcLibraryStaticExportSystemIncludeDir(t *testing.T) { runCcLibraryStaticTestCase(t, bp2buildTestCase{ - description: "cc_library_static export system include dir", - moduleTypeUnderTest: "cc_library_static", - moduleTypeUnderTestFactory: cc.LibraryStaticFactory, - moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryStaticBp2Build, + description: "cc_library_static export system include dir", filesystem: map[string]string{ // subpackage with subdirectory "subpackage/Android.bp": "", @@ -298,20 +293,18 @@ cc_library_static { export_system_include_dirs: ["subpackage"], include_build_directory: false, }`, - expectedBazelTargets: []string{`cc_library_static( - name = "foo_static", - export_system_includes = ["subpackage"], -)`}, + expectedBazelTargets: []string{ + makeBazelTarget("cc_library_static", "foo_static", attrNameToString{ + "export_system_includes": `["subpackage"]`, + }), + }, }) } func TestCcLibraryStaticManyIncludeDirs(t *testing.T) { runCcLibraryStaticTestCase(t, bp2buildTestCase{ - description: "cc_library_static include_dirs, local_include_dirs, export_include_dirs (b/183742505)", - moduleTypeUnderTest: "cc_library_static", - moduleTypeUnderTestFactory: cc.LibraryStaticFactory, - moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryStaticBp2Build, - dir: "subpackage", + description: "cc_library_static include_dirs, local_include_dirs, export_include_dirs (b/183742505)", + dir: "subpackage", filesystem: map[string]string{ // subpackage with subdirectory "subpackage/Android.bp": ` @@ -335,28 +328,25 @@ cc_library_static { "subpackage3/subsubpackage/header.h": "", }, blueprint: soongCcLibraryStaticPreamble, - expectedBazelTargets: []string{`cc_library_static( - name = "foo_static", - absolute_includes = [ + expectedBazelTargets: []string{ + makeBazelTarget("cc_library_static", "foo_static", attrNameToString{ + "absolute_includes": `[ "subpackage/subsubpackage", "subpackage2", "subpackage3/subsubpackage", - ], - export_includes = ["./exported_subsubpackage"], - local_includes = [ + ]`, + "export_includes": `["./exported_subsubpackage"]`, + "local_includes": `[ "subsubpackage2", ".", - ], -)`}, + ]`, + })}, }) } func TestCcLibraryStaticIncludeBuildDirectoryDisabled(t *testing.T) { runCcLibraryStaticTestCase(t, bp2buildTestCase{ - description: "cc_library_static include_build_directory disabled", - moduleTypeUnderTest: "cc_library_static", - moduleTypeUnderTestFactory: cc.LibraryStaticFactory, - moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryStaticBp2Build, + description: "cc_library_static include_build_directory disabled", filesystem: map[string]string{ // subpackage with subdirectory "subpackage/Android.bp": "", @@ -370,20 +360,18 @@ cc_library_static { local_include_dirs: ["subpackage2"], include_build_directory: false, }`, - expectedBazelTargets: []string{`cc_library_static( - name = "foo_static", - absolute_includes = ["subpackage"], - local_includes = ["subpackage2"], -)`}, + expectedBazelTargets: []string{ + makeBazelTarget("cc_library_static", "foo_static", attrNameToString{ + "absolute_includes": `["subpackage"]`, + "local_includes": `["subpackage2"]`, + }), + }, }) } func TestCcLibraryStaticIncludeBuildDirectoryEnabled(t *testing.T) { runCcLibraryStaticTestCase(t, bp2buildTestCase{ - description: "cc_library_static include_build_directory enabled", - moduleTypeUnderTest: "cc_library_static", - moduleTypeUnderTestFactory: cc.LibraryStaticFactory, - moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryStaticBp2Build, + description: "cc_library_static include_build_directory enabled", filesystem: map[string]string{ // subpackage with subdirectory "subpackage/Android.bp": "", @@ -399,24 +387,22 @@ cc_library_static { local_include_dirs: ["subpackage2"], include_build_directory: true, }`, - expectedBazelTargets: []string{`cc_library_static( - name = "foo_static", - absolute_includes = ["subpackage"], - local_includes = [ + expectedBazelTargets: []string{ + makeBazelTarget("cc_library_static", "foo_static", attrNameToString{ + "absolute_includes": `["subpackage"]`, + "local_includes": `[ "subpackage2", ".", - ], -)`}, + ]`, + }), + }, }) } func TestCcLibraryStaticArchSpecificStaticLib(t *testing.T) { runCcLibraryStaticTestCase(t, bp2buildTestCase{ - description: "cc_library_static arch-specific static_libs", - moduleTypeUnderTest: "cc_library_static", - moduleTypeUnderTestFactory: cc.LibraryStaticFactory, - moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryStaticBp2Build, - filesystem: map[string]string{}, + description: "cc_library_static arch-specific static_libs", + filesystem: map[string]string{}, blueprint: soongCcLibraryStaticPreamble + ` cc_library_static { name: "static_dep", @@ -431,27 +417,25 @@ cc_library_static { arch: { arm64: { static_libs: ["static_dep"], whole_static_libs: ["static_dep2"] } }, include_build_directory: false, }`, - expectedBazelTargets: []string{`cc_library_static( - name = "foo_static", - implementation_deps = select({ + expectedBazelTargets: []string{ + makeBazelTarget("cc_library_static", "foo_static", attrNameToString{ + "implementation_deps": `select({ "//build/bazel/platforms/arch:arm64": [":static_dep"], "//conditions:default": [], - }), - whole_archive_deps = select({ + })`, + "whole_archive_deps": `select({ "//build/bazel/platforms/arch:arm64": [":static_dep2"], "//conditions:default": [], - }), -)`}, + })`, + }), + }, }) } func TestCcLibraryStaticOsSpecificStaticLib(t *testing.T) { runCcLibraryStaticTestCase(t, bp2buildTestCase{ - description: "cc_library_static os-specific static_libs", - moduleTypeUnderTest: "cc_library_static", - moduleTypeUnderTestFactory: cc.LibraryStaticFactory, - moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryStaticBp2Build, - filesystem: map[string]string{}, + description: "cc_library_static os-specific static_libs", + filesystem: map[string]string{}, blueprint: soongCcLibraryStaticPreamble + ` cc_library_static { name: "static_dep", @@ -466,27 +450,25 @@ cc_library_static { target: { android: { static_libs: ["static_dep"], whole_static_libs: ["static_dep2"] } }, include_build_directory: false, }`, - expectedBazelTargets: []string{`cc_library_static( - name = "foo_static", - implementation_deps = select({ + expectedBazelTargets: []string{ + makeBazelTarget("cc_library_static", "foo_static", attrNameToString{ + "implementation_deps": `select({ "//build/bazel/platforms/os:android": [":static_dep"], "//conditions:default": [], - }), - whole_archive_deps = select({ + })`, + "whole_archive_deps": `select({ "//build/bazel/platforms/os:android": [":static_dep2"], "//conditions:default": [], - }), -)`}, + })`, + }), + }, }) } func TestCcLibraryStaticBaseArchOsSpecificStaticLib(t *testing.T) { runCcLibraryStaticTestCase(t, bp2buildTestCase{ - description: "cc_library_static base, arch and os-specific static_libs", - moduleTypeUnderTest: "cc_library_static", - moduleTypeUnderTestFactory: cc.LibraryStaticFactory, - moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryStaticBp2Build, - filesystem: map[string]string{}, + description: "cc_library_static base, arch and os-specific static_libs", + filesystem: map[string]string{}, blueprint: soongCcLibraryStaticPreamble + ` cc_library_static { name: "static_dep", @@ -512,26 +494,24 @@ cc_library_static { arch: { arm64: { static_libs: ["static_dep4"] } }, include_build_directory: false, }`, - expectedBazelTargets: []string{`cc_library_static( - name = "foo_static", - implementation_deps = [":static_dep"] + select({ + expectedBazelTargets: []string{ + makeBazelTarget("cc_library_static", "foo_static", attrNameToString{ + "implementation_deps": `[":static_dep"] + select({ "//build/bazel/platforms/arch:arm64": [":static_dep4"], "//conditions:default": [], }) + select({ "//build/bazel/platforms/os:android": [":static_dep3"], "//conditions:default": [], - }), - whole_archive_deps = [":static_dep2"], -)`}, + })`, + "whole_archive_deps": `[":static_dep2"]`, + }), + }, }) } func TestCcLibraryStaticSimpleExcludeSrcs(t *testing.T) { runCcLibraryStaticTestCase(t, bp2buildTestCase{ - description: "cc_library_static simple exclude_srcs", - moduleTypeUnderTest: "cc_library_static", - moduleTypeUnderTestFactory: cc.LibraryStaticFactory, - moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryStaticBp2Build, + description: "cc_library_static simple exclude_srcs", filesystem: map[string]string{ "common.c": "", "foo-a.c": "", @@ -544,22 +524,20 @@ cc_library_static { exclude_srcs: ["foo-excluded.c"], include_build_directory: false, }`, - expectedBazelTargets: []string{`cc_library_static( - name = "foo_static", - srcs_c = [ + expectedBazelTargets: []string{ + makeBazelTarget("cc_library_static", "foo_static", attrNameToString{ + "srcs_c": `[ "common.c", "foo-a.c", - ], -)`}, + ]`, + }), + }, }) } func TestCcLibraryStaticOneArchSrcs(t *testing.T) { runCcLibraryStaticTestCase(t, bp2buildTestCase{ - description: "cc_library_static one arch specific srcs", - moduleTypeUnderTest: "cc_library_static", - moduleTypeUnderTestFactory: cc.LibraryStaticFactory, - moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryStaticBp2Build, + description: "cc_library_static one arch specific srcs", filesystem: map[string]string{ "common.c": "", "foo-arm.c": "", @@ -571,22 +549,20 @@ cc_library_static { arch: { arm: { srcs: ["foo-arm.c"] } }, include_build_directory: false, }`, - expectedBazelTargets: []string{`cc_library_static( - name = "foo_static", - srcs_c = ["common.c"] + select({ + expectedBazelTargets: []string{ + makeBazelTarget("cc_library_static", "foo_static", attrNameToString{ + "srcs_c": `["common.c"] + select({ "//build/bazel/platforms/arch:arm": ["foo-arm.c"], "//conditions:default": [], - }), -)`}, + })`, + }), + }, }) } func TestCcLibraryStaticOneArchSrcsExcludeSrcs(t *testing.T) { runCcLibraryStaticTestCase(t, bp2buildTestCase{ - description: "cc_library_static one arch specific srcs and exclude_srcs", - moduleTypeUnderTest: "cc_library_static", - moduleTypeUnderTestFactory: cc.LibraryStaticFactory, - moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryStaticBp2Build, + description: "cc_library_static one arch specific srcs and exclude_srcs", filesystem: map[string]string{ "common.c": "", "for-arm.c": "", @@ -603,22 +579,20 @@ cc_library_static { }, include_build_directory: false, }`, - expectedBazelTargets: []string{`cc_library_static( - name = "foo_static", - srcs_c = ["common.c"] + select({ + expectedBazelTargets: []string{ + makeBazelTarget("cc_library_static", "foo_static", attrNameToString{ + "srcs_c": `["common.c"] + select({ "//build/bazel/platforms/arch:arm": ["for-arm.c"], "//conditions:default": ["not-for-arm.c"], - }), -)`}, + })`, + }), + }, }) } func TestCcLibraryStaticTwoArchExcludeSrcs(t *testing.T) { runCcLibraryStaticTestCase(t, bp2buildTestCase{ - description: "cc_library_static arch specific exclude_srcs for 2 architectures", - moduleTypeUnderTest: "cc_library_static", - moduleTypeUnderTestFactory: cc.LibraryStaticFactory, - moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryStaticBp2Build, + description: "cc_library_static arch specific exclude_srcs for 2 architectures", filesystem: map[string]string{ "common.c": "", "for-arm.c": "", @@ -637,9 +611,9 @@ cc_library_static { }, include_build_directory: false, } `, - expectedBazelTargets: []string{`cc_library_static( - name = "foo_static", - srcs_c = ["common.c"] + select({ + expectedBazelTargets: []string{ + makeBazelTarget("cc_library_static", "foo_static", attrNameToString{ + "srcs_c": `["common.c"] + select({ "//build/bazel/platforms/arch:arm": [ "not-for-x86.c", "for-arm.c", @@ -652,16 +626,15 @@ cc_library_static { "not-for-arm.c", "not-for-x86.c", ], - }), -)`}, + })`, + }), + }, }) } + func TestCcLibraryStaticFourArchExcludeSrcs(t *testing.T) { runCcLibraryStaticTestCase(t, bp2buildTestCase{ - description: "cc_library_static arch specific exclude_srcs for 4 architectures", - moduleTypeUnderTest: "cc_library_static", - moduleTypeUnderTestFactory: cc.LibraryStaticFactory, - moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryStaticBp2Build, + description: "cc_library_static arch specific exclude_srcs for 4 architectures", filesystem: map[string]string{ "common.c": "", "for-arm.c": "", @@ -687,9 +660,9 @@ cc_library_static { }, include_build_directory: false, } `, - expectedBazelTargets: []string{`cc_library_static( - name = "foo_static", - srcs_c = ["common.c"] + select({ + expectedBazelTargets: []string{ + makeBazelTarget("cc_library_static", "foo_static", attrNameToString{ + "srcs_c": `["common.c"] + select({ "//build/bazel/platforms/arch:arm": [ "not-for-arm64.c", "not-for-x86.c", @@ -720,17 +693,15 @@ cc_library_static { "not-for-x86.c", "not-for-x86_64.c", ], - }), -)`}, + })`, + }), + }, }) } func TestCcLibraryStaticOneArchEmpty(t *testing.T) { runCcLibraryStaticTestCase(t, bp2buildTestCase{ - description: "cc_library_static one arch empty", - moduleTypeUnderTest: "cc_library_static", - moduleTypeUnderTestFactory: cc.LibraryStaticFactory, - moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryStaticBp2Build, + description: "cc_library_static one arch empty", filesystem: map[string]string{ "common.cc": "", "foo-no-arm.cc": "", @@ -746,22 +717,20 @@ cc_library_static { }, include_build_directory: false, }`, - expectedBazelTargets: []string{`cc_library_static( - name = "foo_static", - srcs = ["common.cc"] + select({ + expectedBazelTargets: []string{ + makeBazelTarget("cc_library_static", "foo_static", attrNameToString{ + "srcs": `["common.cc"] + select({ "//build/bazel/platforms/arch:arm": [], "//conditions:default": ["foo-no-arm.cc"], - }), -)`}, + })`, + }), + }, }) } func TestCcLibraryStaticOneArchEmptyOtherSet(t *testing.T) { runCcLibraryStaticTestCase(t, bp2buildTestCase{ - description: "cc_library_static one arch empty other set", - moduleTypeUnderTest: "cc_library_static", - moduleTypeUnderTestFactory: cc.LibraryStaticFactory, - moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryStaticBp2Build, + description: "cc_library_static one arch empty other set", filesystem: map[string]string{ "common.cc": "", "foo-no-arm.cc": "", @@ -779,27 +748,25 @@ cc_library_static { }, include_build_directory: false, }`, - expectedBazelTargets: []string{`cc_library_static( - name = "foo_static", - srcs = ["common.cc"] + select({ + expectedBazelTargets: []string{ + makeBazelTarget("cc_library_static", "foo_static", attrNameToString{ + "srcs": `["common.cc"] + select({ "//build/bazel/platforms/arch:arm": [], "//build/bazel/platforms/arch:x86": [ "foo-no-arm.cc", "x86-only.cc", ], "//conditions:default": ["foo-no-arm.cc"], - }), -)`}, + })`, + }), + }, }) } func TestCcLibraryStaticMultipleDepSameName(t *testing.T) { runCcLibraryStaticTestCase(t, bp2buildTestCase{ - description: "cc_library_static multiple dep same name panic", - moduleTypeUnderTest: "cc_library_static", - moduleTypeUnderTestFactory: cc.LibraryStaticFactory, - moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryStaticBp2Build, - filesystem: map[string]string{}, + description: "cc_library_static multiple dep same name panic", + filesystem: map[string]string{}, blueprint: soongCcLibraryStaticPreamble + ` cc_library_static { name: "static_dep", @@ -810,19 +777,17 @@ cc_library_static { static_libs: ["static_dep", "static_dep"], include_build_directory: false, }`, - expectedBazelTargets: []string{`cc_library_static( - name = "foo_static", - implementation_deps = [":static_dep"], -)`}, + expectedBazelTargets: []string{ + makeBazelTarget("cc_library_static", "foo_static", attrNameToString{ + "implementation_deps": `[":static_dep"]`, + }), + }, }) } func TestCcLibraryStaticOneMultilibSrcsExcludeSrcs(t *testing.T) { runCcLibraryStaticTestCase(t, bp2buildTestCase{ - description: "cc_library_static 1 multilib srcs and exclude_srcs", - moduleTypeUnderTest: "cc_library_static", - moduleTypeUnderTestFactory: cc.LibraryStaticFactory, - moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryStaticBp2Build, + description: "cc_library_static 1 multilib srcs and exclude_srcs", filesystem: map[string]string{ "common.c": "", "for-lib32.c": "", @@ -837,23 +802,21 @@ cc_library_static { }, include_build_directory: false, } `, - expectedBazelTargets: []string{`cc_library_static( - name = "foo_static", - srcs_c = ["common.c"] + select({ + expectedBazelTargets: []string{ + makeBazelTarget("cc_library_static", "foo_static", attrNameToString{ + "srcs_c": `["common.c"] + select({ "//build/bazel/platforms/arch:arm": ["for-lib32.c"], "//build/bazel/platforms/arch:x86": ["for-lib32.c"], "//conditions:default": ["not-for-lib32.c"], - }), -)`}, + })`, + }), + }, }) } func TestCcLibraryStaticTwoMultilibSrcsExcludeSrcs(t *testing.T) { runCcLibraryStaticTestCase(t, bp2buildTestCase{ - description: "cc_library_static 2 multilib srcs and exclude_srcs", - moduleTypeUnderTest: "cc_library_static", - moduleTypeUnderTestFactory: cc.LibraryStaticFactory, - moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryStaticBp2Build, + description: "cc_library_static 2 multilib srcs and exclude_srcs", filesystem: map[string]string{ "common.c": "", "for-lib32.c": "", @@ -863,7 +826,7 @@ func TestCcLibraryStaticTwoMultilibSrcsExcludeSrcs(t *testing.T) { }, blueprint: soongCcLibraryStaticPreamble + ` cc_library_static { - name: "foo_static2", + name: "foo_static", srcs: ["common.c", "not-for-*.c"], multilib: { lib32: { srcs: ["for-lib32.c"], exclude_srcs: ["not-for-lib32.c"] }, @@ -871,9 +834,9 @@ cc_library_static { }, include_build_directory: false, } `, - expectedBazelTargets: []string{`cc_library_static( - name = "foo_static2", - srcs_c = ["common.c"] + select({ + expectedBazelTargets: []string{ + makeBazelTarget("cc_library_static", "foo_static", attrNameToString{ + "srcs_c": `["common.c"] + select({ "//build/bazel/platforms/arch:arm": [ "not-for-lib64.c", "for-lib32.c", @@ -894,17 +857,15 @@ cc_library_static { "not-for-lib32.c", "not-for-lib64.c", ], - }), -)`}, + })`, + }), + }, }) } func TestCcLibrarySTaticArchMultilibSrcsExcludeSrcs(t *testing.T) { runCcLibraryStaticTestCase(t, bp2buildTestCase{ - description: "cc_library_static arch and multilib srcs and exclude_srcs", - moduleTypeUnderTest: "cc_library_static", - moduleTypeUnderTestFactory: cc.LibraryStaticFactory, - moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryStaticBp2Build, + description: "cc_library_static arch and multilib srcs and exclude_srcs", filesystem: map[string]string{ "common.c": "", "for-arm.c": "", @@ -923,7 +884,7 @@ func TestCcLibrarySTaticArchMultilibSrcsExcludeSrcs(t *testing.T) { }, blueprint: soongCcLibraryStaticPreamble + ` cc_library_static { - name: "foo_static3", + name: "foo_static", srcs: ["common.c", "not-for-*.c"], exclude_srcs: ["not-for-everything.c"], arch: { @@ -938,9 +899,9 @@ cc_library_static { }, include_build_directory: false, }`, - expectedBazelTargets: []string{`cc_library_static( - name = "foo_static3", - srcs_c = ["common.c"] + select({ + expectedBazelTargets: []string{ + makeBazelTarget("cc_library_static", "foo_static", attrNameToString{ + "srcs_c": `["common.c"] + select({ "//build/bazel/platforms/arch:arm": [ "not-for-arm64.c", "not-for-lib64.c", @@ -981,16 +942,14 @@ cc_library_static { "not-for-x86.c", "not-for-x86_64.c", ], - }), -)`}, + })`, + }), + }, }) } func TestCcLibraryStaticGeneratedHeadersAllPartitions(t *testing.T) { runCcLibraryStaticTestCase(t, bp2buildTestCase{ - moduleTypeUnderTest: "cc_library_static", - moduleTypeUnderTestFactory: cc.LibraryStaticFactory, - moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryStaticBp2Build, blueprint: soongCcLibraryStaticPreamble + ` genrule { name: "generated_hdr", @@ -1009,31 +968,29 @@ cc_library_static { export_generated_headers: ["export_generated_hdr"], include_build_directory: false, }`, - expectedBazelTargets: []string{`cc_library_static( - name = "foo_static", - hdrs = [":export_generated_hdr"], - srcs = [ + expectedBazelTargets: []string{ + makeBazelTarget("cc_library_static", "foo_static", attrNameToString{ + "hdrs": `[":export_generated_hdr"]`, + "srcs": `[ "cpp_src.cpp", ":generated_hdr", - ], - srcs_as = [ + ]`, + "srcs_as": `[ "as_src.S", ":generated_hdr", - ], - srcs_c = [ + ]`, + "srcs_c": `[ "c_src.c", ":generated_hdr", - ], -)`}, + ]`, + }), + }, }) } func TestCcLibraryStaticArchSrcsExcludeSrcsGeneratedFiles(t *testing.T) { runCcLibraryStaticTestCase(t, bp2buildTestCase{ - description: "cc_library_static arch srcs/exclude_srcs with generated files", - moduleTypeUnderTest: "cc_library_static", - moduleTypeUnderTestFactory: cc.LibraryStaticFactory, - moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryStaticBp2Build, + description: "cc_library_static arch srcs/exclude_srcs with generated files", filesystem: map[string]string{ "common.cpp": "", "for-x86.cpp": "", @@ -1082,7 +1039,7 @@ genrule { } cc_library_static { - name: "foo_static3", + name: "foo_static", srcs: ["common.cpp", "not-for-*.cpp"], exclude_srcs: ["not-for-everything.cpp"], generated_sources: ["generated_src", "generated_src_other_pkg", "generated_src_not_x86"], @@ -1105,9 +1062,9 @@ cc_library_static { include_build_directory: false, } `, - expectedBazelTargets: []string{`cc_library_static( - name = "foo_static3", - srcs = [ + expectedBazelTargets: []string{ + makeBazelTarget("cc_library_static", "foo_static", attrNameToString{ + "srcs": `[ "common.cpp", ":generated_src", "//dep:generated_src_other_pkg", @@ -1128,18 +1085,16 @@ cc_library_static { "//dep:generated_hdr_other_pkg_android", ], "//conditions:default": [], - }), -)`}, + })`, + }), + }, }) } func TestCcLibraryStaticGetTargetProperties(t *testing.T) { runCcLibraryStaticTestCase(t, bp2buildTestCase{ - description: "cc_library_static complex GetTargetProperties", - moduleTypeUnderTest: "cc_library_static", - moduleTypeUnderTestFactory: cc.LibraryStaticFactory, - moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryStaticBp2Build, + description: "cc_library_static complex GetTargetProperties", blueprint: soongCcLibraryStaticPreamble + ` cc_library_static { name: "foo_static", @@ -1168,9 +1123,9 @@ cc_library_static { }, include_build_directory: false, }`, - expectedBazelTargets: []string{`cc_library_static( - name = "foo_static", - srcs_c = select({ + expectedBazelTargets: []string{ + makeBazelTarget("cc_library_static", "foo_static", attrNameToString{ + "srcs_c": `select({ "//build/bazel/platforms/os:android": ["android_src.c"], "//conditions:default": [], }) + select({ @@ -1181,17 +1136,15 @@ cc_library_static { "//build/bazel/platforms/os_arch:linux_bionic_arm64": ["linux_bionic_arm64_src.c"], "//build/bazel/platforms/os_arch:linux_bionic_x86_64": ["linux_bionic_x86_64_src.c"], "//conditions:default": [], - }), -)`}, + })`, + }), + }, }) } func TestCcLibraryStaticProductVariableSelects(t *testing.T) { runCcLibraryStaticTestCase(t, bp2buildTestCase{ - description: "cc_library_static product variable selects", - moduleTypeUnderTest: "cc_library_static", - moduleTypeUnderTestFactory: cc.LibraryStaticFactory, - moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryStaticBp2Build, + description: "cc_library_static product variable selects", blueprint: soongCcLibraryStaticPreamble + ` cc_library_static { name: "foo_static", @@ -1209,9 +1162,9 @@ cc_library_static { }, include_build_directory: false, } `, - expectedBazelTargets: []string{`cc_library_static( - name = "foo_static", - copts = select({ + expectedBazelTargets: []string{ + makeBazelTarget("cc_library_static", "foo_static", attrNameToString{ + "copts": `select({ "//build/bazel/product_variables:binder32bit": ["-Wbinder32bit"], "//conditions:default": [], }) + select({ @@ -1220,19 +1173,17 @@ cc_library_static { }) + select({ "//build/bazel/product_variables:malloc_zero_contents": ["-Wmalloc_zero_contents"], "//conditions:default": [], - }), - srcs_c = ["common.c"], -)`}, + })`, + "srcs_c": `["common.c"]`, + }), + }, }) } func TestCcLibraryStaticProductVariableArchSpecificSelects(t *testing.T) { runCcLibraryStaticTestCase(t, bp2buildTestCase{ - description: "cc_library_static arch-specific product variable selects", - moduleTypeUnderTest: "cc_library_static", - moduleTypeUnderTestFactory: cc.LibraryStaticFactory, - moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryStaticBp2Build, - filesystem: map[string]string{}, + description: "cc_library_static arch-specific product variable selects", + filesystem: map[string]string{}, blueprint: soongCcLibraryStaticPreamble + ` cc_library_static { name: "foo_static", @@ -1271,9 +1222,9 @@ cc_library_static { }, include_build_directory: false, } `, - expectedBazelTargets: []string{`cc_library_static( - name = "foo_static", - copts = select({ + expectedBazelTargets: []string{ + makeBazelTarget("cc_library_static", "foo_static", attrNameToString{ + "copts": `select({ "//build/bazel/product_variables:malloc_not_svelte": ["-Wmalloc_not_svelte"], "//conditions:default": [], }) + select({ @@ -1288,19 +1239,17 @@ cc_library_static { }) + select({ "//build/bazel/product_variables:malloc_not_svelte-x86": ["-Wlib32_malloc_not_svelte"], "//conditions:default": [], - }), - srcs_c = ["common.c"], -)`}, + })`, + "srcs_c": `["common.c"]`, + }), + }, }) } func TestCcLibraryStaticProductVariableStringReplacement(t *testing.T) { runCcLibraryStaticTestCase(t, bp2buildTestCase{ - description: "cc_library_static product variable string replacement", - moduleTypeUnderTest: "cc_library_static", - moduleTypeUnderTestFactory: cc.LibraryStaticFactory, - moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryStaticBp2Build, - filesystem: map[string]string{}, + description: "cc_library_static product variable string replacement", + filesystem: map[string]string{}, blueprint: soongCcLibraryStaticPreamble + ` cc_library_static { name: "foo_static", @@ -1312,23 +1261,21 @@ cc_library_static { }, include_build_directory: false, } `, - expectedBazelTargets: []string{`cc_library_static( - name = "foo_static", - asflags = select({ + expectedBazelTargets: []string{ + makeBazelTarget("cc_library_static", "foo_static", attrNameToString{ + "asflags": `select({ "//build/bazel/product_variables:platform_sdk_version": ["-DPLATFORM_SDK_VERSION=$(Platform_sdk_version)"], "//conditions:default": [], - }), - srcs_as = ["common.S"], -)`}, + })`, + "srcs_as": `["common.S"]`, + }), + }, }) } func TestStaticLibrary_SystemSharedLibsRootEmpty(t *testing.T) { runCcLibraryStaticTestCase(t, bp2buildTestCase{ - description: "cc_library_static system_shared_lib empty root", - moduleTypeUnderTest: "cc_library_static", - moduleTypeUnderTestFactory: cc.LibraryStaticFactory, - moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryStaticBp2Build, + description: "cc_library_static system_shared_lib empty root", blueprint: soongCcLibraryStaticPreamble + ` cc_library_static { name: "root_empty", @@ -1336,19 +1283,17 @@ cc_library_static { include_build_directory: false, } `, - expectedBazelTargets: []string{`cc_library_static( - name = "root_empty", - system_dynamic_deps = [], -)`}, + expectedBazelTargets: []string{ + makeBazelTarget("cc_library_static", "root_empty", attrNameToString{ + "system_dynamic_deps": `[]`, + }), + }, }) } func TestStaticLibrary_SystemSharedLibsStaticEmpty(t *testing.T) { runCcLibraryStaticTestCase(t, bp2buildTestCase{ - description: "cc_library_static system_shared_lib empty static default", - moduleTypeUnderTest: "cc_library_static", - moduleTypeUnderTestFactory: cc.LibraryStaticFactory, - moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryStaticBp2Build, + description: "cc_library_static system_shared_lib empty static default", blueprint: soongCcLibraryStaticPreamble + ` cc_defaults { name: "static_empty_defaults", @@ -1362,19 +1307,17 @@ cc_library_static { defaults: ["static_empty_defaults"], } `, - expectedBazelTargets: []string{`cc_library_static( - name = "static_empty", - system_dynamic_deps = [], -)`}, + expectedBazelTargets: []string{ + makeBazelTarget("cc_library_static", "static_empty", attrNameToString{ + "system_dynamic_deps": `[]`, + }), + }, }) } func TestStaticLibrary_SystemSharedLibsBionicEmpty(t *testing.T) { runCcLibraryStaticTestCase(t, bp2buildTestCase{ - description: "cc_library_static system_shared_lib empty for bionic variant", - moduleTypeUnderTest: "cc_library_static", - moduleTypeUnderTestFactory: cc.LibraryStaticFactory, - moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryStaticBp2Build, + description: "cc_library_static system_shared_lib empty for bionic variant", blueprint: soongCcLibraryStaticPreamble + ` cc_library_static { name: "target_bionic_empty", @@ -1386,10 +1329,11 @@ cc_library_static { include_build_directory: false, } `, - expectedBazelTargets: []string{`cc_library_static( - name = "target_bionic_empty", - system_dynamic_deps = [], -)`}, + expectedBazelTargets: []string{ + makeBazelTarget("cc_library_static", "target_bionic_empty", attrNameToString{ + "system_dynamic_deps": `[]`, + }), + }, }) } @@ -1399,10 +1343,7 @@ func TestStaticLibrary_SystemSharedLibsLinuxBionicEmpty(t *testing.T) { // only for linux_bionic, but `android` had `["libc", "libdl", "libm"]. // b/195791252 tracks the fix. runCcLibraryStaticTestCase(t, bp2buildTestCase{ - description: "cc_library_static system_shared_lib empty for linux_bionic variant", - moduleTypeUnderTest: "cc_library_static", - moduleTypeUnderTestFactory: cc.LibraryStaticFactory, - moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryStaticBp2Build, + description: "cc_library_static system_shared_lib empty for linux_bionic variant", blueprint: soongCcLibraryStaticPreamble + ` cc_library_static { name: "target_linux_bionic_empty", @@ -1414,19 +1355,17 @@ cc_library_static { include_build_directory: false, } `, - expectedBazelTargets: []string{`cc_library_static( - name = "target_linux_bionic_empty", - system_dynamic_deps = [], -)`}, + expectedBazelTargets: []string{ + makeBazelTarget("cc_library_static", "target_linux_bionic_empty", attrNameToString{ + "system_dynamic_deps": `[]`, + }), + }, }) } func TestStaticLibrary_SystemSharedLibsBionic(t *testing.T) { runCcLibraryStaticTestCase(t, bp2buildTestCase{ - description: "cc_library_static system_shared_libs set for bionic variant", - moduleTypeUnderTest: "cc_library_static", - moduleTypeUnderTestFactory: cc.LibraryStaticFactory, - moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryStaticBp2Build, + description: "cc_library_static system_shared_libs set for bionic variant", blueprint: soongCcLibraryStaticPreamble + ` cc_library{name: "libc"} @@ -1440,23 +1379,21 @@ cc_library_static { include_build_directory: false, } `, - expectedBazelTargets: []string{`cc_library_static( - name = "target_bionic", - system_dynamic_deps = select({ + expectedBazelTargets: []string{ + makeBazelTarget("cc_library_static", "target_bionic", attrNameToString{ + "system_dynamic_deps": `select({ "//build/bazel/platforms/os:android": [":libc"], "//build/bazel/platforms/os:linux_bionic": [":libc"], "//conditions:default": [], - }), -)`}, + })`, + }), + }, }) } func TestStaticLibrary_SystemSharedLibsLinuxRootAndLinuxBionic(t *testing.T) { runCcLibraryStaticTestCase(t, bp2buildTestCase{ - description: "cc_library_static system_shared_libs set for root and linux_bionic variant", - moduleTypeUnderTest: "cc_library_static", - moduleTypeUnderTestFactory: cc.LibraryStaticFactory, - moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryStaticBp2Build, + description: "cc_library_static system_shared_libs set for root and linux_bionic variant", blueprint: soongCcLibraryStaticPreamble + ` cc_library{name: "libc"} cc_library{name: "libm"} @@ -1472,12 +1409,13 @@ cc_library_static { include_build_directory: false, } `, - expectedBazelTargets: []string{`cc_library_static( - name = "target_linux_bionic", - system_dynamic_deps = [":libc"] + select({ + expectedBazelTargets: []string{ + makeBazelTarget("cc_library_static", "target_linux_bionic", attrNameToString{ + "system_dynamic_deps": `[":libc"] + select({ "//build/bazel/platforms/os:linux_bionic": [":libm"], "//conditions:default": [], - }), -)`}, + })`, + }), + }, }) } diff --git a/bp2build/cc_object_conversion_test.go b/bp2build/cc_object_conversion_test.go index c4b276a64..5ab9129e7 100644 --- a/bp2build/cc_object_conversion_test.go +++ b/bp2build/cc_object_conversion_test.go @@ -28,15 +28,15 @@ func registerCcObjectModuleTypes(ctx android.RegistrationContext) { func runCcObjectTestCase(t *testing.T, tc bp2buildTestCase) { t.Helper() + (&tc).moduleTypeUnderTest = "cc_object" + (&tc).moduleTypeUnderTestFactory = cc.ObjectFactory + (&tc).moduleTypeUnderTestBp2BuildMutator = cc.ObjectBp2Build runBp2BuildTestCase(t, registerCcObjectModuleTypes, tc) } func TestCcObjectSimple(t *testing.T) { runCcObjectTestCase(t, bp2buildTestCase{ - description: "simple cc_object generates cc_object with include header dep", - moduleTypeUnderTest: "cc_object", - moduleTypeUnderTestFactory: cc.ObjectFactory, - moduleTypeUnderTestBp2BuildMutator: cc.ObjectBp2Build, + description: "simple cc_object generates cc_object with include header dep", filesystem: map[string]string{ "a/b/foo.h": "", "a/b/bar.h": "", @@ -58,30 +58,27 @@ func TestCcObjectSimple(t *testing.T) { exclude_srcs: ["a/b/exclude.c"], } `, - expectedBazelTargets: []string{`cc_object( - name = "foo", - copts = [ + expectedBazelTargets: []string{ + makeBazelTarget("cc_object", "foo", attrNameToString{ + "copts": `[ "-fno-addrsig", "-Wno-gcc-compat", "-Wall", "-Werror", - ], - local_includes = [ + ]`, + "local_includes": `[ "include", ".", - ], - srcs = ["a/b/c.c"], - system_dynamic_deps = [], -)`, + ]`, + "srcs": `["a/b/c.c"]`, + "system_dynamic_deps": `[]`, + }), }, }) } func TestCcObjectDefaults(t *testing.T) { runCcObjectTestCase(t, bp2buildTestCase{ - moduleTypeUnderTest: "cc_object", - moduleTypeUnderTestFactory: cc.ObjectFactory, - moduleTypeUnderTestBp2BuildMutator: cc.ObjectBp2Build, blueprint: `cc_object { name: "foo", system_shared_libs: [], @@ -101,33 +98,26 @@ cc_defaults { cc_defaults { name: "foo_bar_defaults", cflags: [ - "-Wno-gcc-compat", - "-Wall", "-Werror", ], } `, - expectedBazelTargets: []string{`cc_object( - name = "foo", - copts = [ - "-Wno-gcc-compat", - "-Wall", + expectedBazelTargets: []string{ + makeBazelTarget("cc_object", "foo", attrNameToString{ + "copts": `[ "-Werror", "-fno-addrsig", - ], - local_includes = ["."], - srcs = ["a/b/c.c"], - system_dynamic_deps = [], -)`, + ]`, + "local_includes": `["."]`, + "srcs": `["a/b/c.c"]`, + "system_dynamic_deps": `[]`, + }), }}) } func TestCcObjectCcObjetDepsInObjs(t *testing.T) { runCcObjectTestCase(t, bp2buildTestCase{ - description: "cc_object with cc_object deps in objs props", - moduleTypeUnderTest: "cc_object", - moduleTypeUnderTestFactory: cc.ObjectFactory, - moduleTypeUnderTestBp2BuildMutator: cc.ObjectBp2Build, + description: "cc_object with cc_object deps in objs props", filesystem: map[string]string{ "a/b/c.c": "", "x/y/z.c": "", @@ -147,28 +137,24 @@ cc_object { include_build_directory: false, } `, - expectedBazelTargets: []string{`cc_object( - name = "bar", - copts = ["-fno-addrsig"], - srcs = ["x/y/z.c"], - system_dynamic_deps = [], -)`, `cc_object( - name = "foo", - copts = ["-fno-addrsig"], - deps = [":bar"], - srcs = ["a/b/c.c"], - system_dynamic_deps = [], -)`, + expectedBazelTargets: []string{ + makeBazelTarget("cc_object", "bar", attrNameToString{ + "copts": `["-fno-addrsig"]`, + "srcs": `["x/y/z.c"]`, + "system_dynamic_deps": `[]`, + }), makeBazelTarget("cc_object", "foo", attrNameToString{ + "copts": `["-fno-addrsig"]`, + "deps": `[":bar"]`, + "srcs": `["a/b/c.c"]`, + "system_dynamic_deps": `[]`, + }), }, }) } func TestCcObjectIncludeBuildDirFalse(t *testing.T) { runCcObjectTestCase(t, bp2buildTestCase{ - description: "cc_object with include_build_dir: false", - moduleTypeUnderTest: "cc_object", - moduleTypeUnderTestFactory: cc.ObjectFactory, - moduleTypeUnderTestBp2BuildMutator: cc.ObjectBp2Build, + description: "cc_object with include_build_dir: false", filesystem: map[string]string{ "a/b/c.c": "", "x/y/z.c": "", @@ -180,22 +166,19 @@ func TestCcObjectIncludeBuildDirFalse(t *testing.T) { include_build_directory: false, } `, - expectedBazelTargets: []string{`cc_object( - name = "foo", - copts = ["-fno-addrsig"], - srcs = ["a/b/c.c"], - system_dynamic_deps = [], -)`, + expectedBazelTargets: []string{ + makeBazelTarget("cc_object", "foo", attrNameToString{ + "copts": `["-fno-addrsig"]`, + "srcs": `["a/b/c.c"]`, + "system_dynamic_deps": `[]`, + }), }, }) } func TestCcObjectProductVariable(t *testing.T) { runCcObjectTestCase(t, bp2buildTestCase{ - description: "cc_object with product variable", - moduleTypeUnderTest: "cc_object", - moduleTypeUnderTestFactory: cc.ObjectFactory, - moduleTypeUnderTestBp2BuildMutator: cc.ObjectBp2Build, + description: "cc_object with product variable", blueprint: `cc_object { name: "foo", system_shared_libs: [], @@ -208,26 +191,23 @@ func TestCcObjectProductVariable(t *testing.T) { srcs: ["src.S"], } `, - expectedBazelTargets: []string{`cc_object( - name = "foo", - asflags = select({ + expectedBazelTargets: []string{ + makeBazelTarget("cc_object", "foo", attrNameToString{ + "asflags": `select({ "//build/bazel/product_variables:platform_sdk_version": ["-DPLATFORM_SDK_VERSION=$(Platform_sdk_version)"], "//conditions:default": [], - }), - copts = ["-fno-addrsig"], - srcs_as = ["src.S"], - system_dynamic_deps = [], -)`, + })`, + "copts": `["-fno-addrsig"]`, + "srcs_as": `["src.S"]`, + "system_dynamic_deps": `[]`, + }), }, }) } func TestCcObjectCflagsOneArch(t *testing.T) { runCcObjectTestCase(t, bp2buildTestCase{ - description: "cc_object setting cflags for one arch", - moduleTypeUnderTest: "cc_object", - moduleTypeUnderTestFactory: cc.ObjectFactory, - moduleTypeUnderTestBp2BuildMutator: cc.ObjectBp2Build, + description: "cc_object setting cflags for one arch", blueprint: `cc_object { name: "foo", system_shared_libs: [], @@ -244,28 +224,24 @@ func TestCcObjectCflagsOneArch(t *testing.T) { } `, expectedBazelTargets: []string{ - `cc_object( - name = "foo", - copts = ["-fno-addrsig"] + select({ + makeBazelTarget("cc_object", "foo", attrNameToString{ + "copts": `["-fno-addrsig"] + select({ "//build/bazel/platforms/arch:x86": ["-fPIC"], "//conditions:default": [], - }), - srcs = ["a.cpp"] + select({ + })`, + "srcs": `["a.cpp"] + select({ "//build/bazel/platforms/arch:arm": ["arch/arm/file.cpp"], "//conditions:default": [], - }), - system_dynamic_deps = [], -)`, + })`, + "system_dynamic_deps": `[]`, + }), }, }) } func TestCcObjectCflagsFourArch(t *testing.T) { runCcObjectTestCase(t, bp2buildTestCase{ - description: "cc_object setting cflags for 4 architectures", - moduleTypeUnderTest: "cc_object", - moduleTypeUnderTestFactory: cc.ObjectFactory, - moduleTypeUnderTestBp2BuildMutator: cc.ObjectBp2Build, + description: "cc_object setting cflags for 4 architectures", blueprint: `cc_object { name: "foo", system_shared_libs: [], @@ -292,34 +268,30 @@ func TestCcObjectCflagsFourArch(t *testing.T) { } `, expectedBazelTargets: []string{ - `cc_object( - name = "foo", - copts = ["-fno-addrsig"] + select({ + makeBazelTarget("cc_object", "foo", attrNameToString{ + "copts": `["-fno-addrsig"] + select({ "//build/bazel/platforms/arch:arm": ["-Wall"], "//build/bazel/platforms/arch:arm64": ["-Wall"], "//build/bazel/platforms/arch:x86": ["-fPIC"], "//build/bazel/platforms/arch:x86_64": ["-fPIC"], "//conditions:default": [], - }), - srcs = ["base.cpp"] + select({ + })`, + "srcs": `["base.cpp"] + select({ "//build/bazel/platforms/arch:arm": ["arm.cpp"], "//build/bazel/platforms/arch:arm64": ["arm64.cpp"], "//build/bazel/platforms/arch:x86": ["x86.cpp"], "//build/bazel/platforms/arch:x86_64": ["x86_64.cpp"], "//conditions:default": [], - }), - system_dynamic_deps = [], -)`, + })`, + "system_dynamic_deps": `[]`, + }), }, }) } func TestCcObjectLinkerScript(t *testing.T) { runCcObjectTestCase(t, bp2buildTestCase{ - description: "cc_object setting linker_script", - moduleTypeUnderTest: "cc_object", - moduleTypeUnderTestFactory: cc.ObjectFactory, - moduleTypeUnderTestBp2BuildMutator: cc.ObjectBp2Build, + description: "cc_object setting linker_script", blueprint: `cc_object { name: "foo", srcs: ["base.cpp"], @@ -328,22 +300,18 @@ func TestCcObjectLinkerScript(t *testing.T) { } `, expectedBazelTargets: []string{ - `cc_object( - name = "foo", - copts = ["-fno-addrsig"], - linker_script = "bunny.lds", - srcs = ["base.cpp"], -)`, + makeBazelTarget("cc_object", "foo", attrNameToString{ + "copts": `["-fno-addrsig"]`, + "linker_script": `"bunny.lds"`, + "srcs": `["base.cpp"]`, + }), }, }) } func TestCcObjectDepsAndLinkerScriptSelects(t *testing.T) { runCcObjectTestCase(t, bp2buildTestCase{ - description: "cc_object setting deps and linker_script across archs", - moduleTypeUnderTest: "cc_object", - moduleTypeUnderTestFactory: cc.ObjectFactory, - moduleTypeUnderTestBp2BuildMutator: cc.ObjectBp2Build, + description: "cc_object setting deps and linker_script across archs", blueprint: `cc_object { name: "foo", srcs: ["base.cpp"], @@ -389,33 +357,29 @@ cc_object { } `, expectedBazelTargets: []string{ - `cc_object( - name = "foo", - copts = ["-fno-addrsig"], - deps = select({ + makeBazelTarget("cc_object", "foo", attrNameToString{ + "copts": `["-fno-addrsig"]`, + "deps": `select({ "//build/bazel/platforms/arch:arm": [":arm_obj"], "//build/bazel/platforms/arch:x86": [":x86_obj"], "//build/bazel/platforms/arch:x86_64": [":x86_64_obj"], "//conditions:default": [], - }), - linker_script = select({ + })`, + "linker_script": `select({ "//build/bazel/platforms/arch:arm": "arm.lds", "//build/bazel/platforms/arch:x86": "x86.lds", "//build/bazel/platforms/arch:x86_64": "x86_64.lds", "//conditions:default": None, - }), - srcs = ["base.cpp"], -)`, + })`, + "srcs": `["base.cpp"]`, + }), }, }) } func TestCcObjectSelectOnLinuxAndBionicArchs(t *testing.T) { runCcObjectTestCase(t, bp2buildTestCase{ - description: "cc_object setting srcs based on linux and bionic archs", - moduleTypeUnderTest: "cc_object", - moduleTypeUnderTestFactory: cc.ObjectFactory, - moduleTypeUnderTestBp2BuildMutator: cc.ObjectBp2Build, + description: "cc_object setting srcs based on linux and bionic archs", blueprint: `cc_object { name: "foo", srcs: ["base.cpp"], @@ -434,10 +398,9 @@ func TestCcObjectSelectOnLinuxAndBionicArchs(t *testing.T) { } `, expectedBazelTargets: []string{ - `cc_object( - name = "foo", - copts = ["-fno-addrsig"], - srcs = ["base.cpp"] + select({ + makeBazelTarget("cc_object", "foo", attrNameToString{ + "copts": `["-fno-addrsig"]`, + "srcs": `["base.cpp"] + select({ "//build/bazel/platforms/os_arch:android_arm64": [ "linux_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_musl_x86": ["linux_x86.cpp"], "//conditions:default": [], - }), -)`, + })`, + }), }, }) } diff --git a/bp2build/cc_prebuilt_library_shared_test.go b/bp2build/cc_prebuilt_library_shared_test.go index 97545c886..bac3908da 100644 --- a/bp2build/cc_prebuilt_library_shared_test.go +++ b/bp2build/cc_prebuilt_library_shared_test.go @@ -23,10 +23,9 @@ cc_prebuilt_library_shared { bazel_module: { bp2build_available: true }, }`, expectedBazelTargets: []string{ - `prebuilt_library_shared( - name = "libtest", - shared_library = "libf.so", -)`, + makeBazelTarget("prebuilt_library_shared", "libtest", attrNameToString{ + "shared_library": `"libf.so"`, + }), }, }) } @@ -52,14 +51,13 @@ cc_prebuilt_library_shared { bazel_module: { bp2build_available: true }, }`, expectedBazelTargets: []string{ - `prebuilt_library_shared( - name = "libtest", - shared_library = select({ + makeBazelTarget("prebuilt_library_shared", "libtest", attrNameToString{ + "shared_library": `select({ "//build/bazel/platforms/arch:arm": "libg.so", "//build/bazel/platforms/arch:arm64": "libf.so", "//conditions:default": None, - }), -)`, + })`, + }), }, }) } diff --git a/bp2build/filegroup_conversion_test.go b/bp2build/filegroup_conversion_test.go index ad9923610..9f4add2ff 100644 --- a/bp2build/filegroup_conversion_test.go +++ b/bp2build/filegroup_conversion_test.go @@ -23,6 +23,9 @@ import ( func runFilegroupTestCase(t *testing.T, tc bp2buildTestCase) { t.Helper() + (&tc).moduleTypeUnderTest = "filegroup" + (&tc).moduleTypeUnderTestFactory = android.FileGroupFactory + (&tc).moduleTypeUnderTestBp2BuildMutator = android.FilegroupBp2Build runBp2BuildTestCase(t, registerFilegroupModuleTypes, tc) } @@ -30,11 +33,8 @@ func registerFilegroupModuleTypes(ctx android.RegistrationContext) {} func TestFilegroupSameNameAsFile_OneFile(t *testing.T) { runFilegroupTestCase(t, bp2buildTestCase{ - description: "filegroup - same name as file, with one file", - moduleTypeUnderTest: "filegroup", - moduleTypeUnderTestFactory: android.FileGroupFactory, - moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build, - filesystem: map[string]string{}, + description: "filegroup - same name as file, with one file", + filesystem: map[string]string{}, blueprint: ` filegroup { name: "foo", @@ -46,11 +46,8 @@ filegroup { func TestFilegroupSameNameAsFile_MultipleFiles(t *testing.T) { runFilegroupTestCase(t, bp2buildTestCase{ - description: "filegroup - same name as file, with multiple files", - moduleTypeUnderTest: "filegroup", - moduleTypeUnderTestFactory: android.FileGroupFactory, - moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build, - filesystem: map[string]string{}, + description: "filegroup - same name as file, with multiple files", + filesystem: map[string]string{}, blueprint: ` filegroup { name: "foo", diff --git a/bp2build/genrule_conversion_test.go b/bp2build/genrule_conversion_test.go index f3bc1baf4..597666694 100644 --- a/bp2build/genrule_conversion_test.go +++ b/bp2build/genrule_conversion_test.go @@ -17,10 +17,21 @@ package bp2build import ( "android/soong/android" "android/soong/genrule" - "strings" "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) { otherGenruleBp := map[string]string{ "other/Android.bp": `genrule { @@ -39,10 +50,7 @@ genrule { testCases := []bp2buildTestCase{ { - description: "genrule with command line variable replacements", - moduleTypeUnderTest: "genrule", - moduleTypeUnderTestFactory: genrule.GenRuleFactory, - moduleTypeUnderTestBp2BuildMutator: genrule.GenruleBp2Build, + description: "genrule with command line variable replacements", blueprint: `genrule { name: "foo.tool", out: ["foo_tool.out"], @@ -60,26 +68,21 @@ genrule { bazel_module: { bp2build_available: true }, }`, expectedBazelTargets: []string{ - `genrule( - name = "foo", - cmd = "$(location :foo.tool) --genDir=$(GENDIR) arg $(SRCS) $(OUTS)", - outs = ["foo.out"], - srcs = ["foo.in"], - tools = [":foo.tool"], -)`, - `genrule( - name = "foo.tool", - cmd = "cp $(SRCS) $(OUTS)", - outs = ["foo_tool.out"], - srcs = ["foo_tool.in"], -)`, + makeBazelTarget("genrule", "foo", attrNameToString{ + "cmd": `"$(location :foo.tool) --genDir=$(GENDIR) arg $(SRCS) $(OUTS)"`, + "outs": `["foo.out"]`, + "srcs": `["foo.in"]`, + "tools": `[":foo.tool"]`, + }), + makeBazelTarget("genrule", "foo.tool", attrNameToString{ + "cmd": `"cp $(SRCS) $(OUTS)"`, + "outs": `["foo_tool.out"]`, + "srcs": `["foo_tool.in"]`, + }), }, }, { - description: "genrule using $(locations :label)", - moduleTypeUnderTest: "genrule", - moduleTypeUnderTestFactory: genrule.GenRuleFactory, - moduleTypeUnderTestBp2BuildMutator: genrule.GenruleBp2Build, + description: "genrule using $(locations :label)", blueprint: `genrule { name: "foo.tools", out: ["foo_tool.out", "foo_tool2.out"], @@ -96,29 +99,25 @@ genrule { cmd: "$(locations :foo.tools) -s $(out) $(in)", bazel_module: { bp2build_available: true }, }`, - expectedBazelTargets: []string{`genrule( - name = "foo", - cmd = "$(locations :foo.tools) -s $(OUTS) $(SRCS)", - outs = ["foo.out"], - srcs = ["foo.in"], - tools = [":foo.tools"], -)`, - `genrule( - name = "foo.tools", - cmd = "cp $(SRCS) $(OUTS)", - outs = [ + expectedBazelTargets: []string{ + makeBazelTarget("genrule", "foo", attrNameToString{ + "cmd": `"$(locations :foo.tools) -s $(OUTS) $(SRCS)"`, + "outs": `["foo.out"]`, + "srcs": `["foo.in"]`, + "tools": `[":foo.tools"]`, + }), + makeBazelTarget("genrule", "foo.tools", attrNameToString{ + "cmd": `"cp $(SRCS) $(OUTS)"`, + "outs": `[ "foo_tool.out", "foo_tool2.out", - ], - srcs = ["foo_tool.in"], -)`, + ]`, + "srcs": `["foo_tool.in"]`, + }), }, }, { - description: "genrule using $(locations //absolute:label)", - moduleTypeUnderTest: "genrule", - moduleTypeUnderTestFactory: genrule.GenRuleFactory, - moduleTypeUnderTestBp2BuildMutator: genrule.GenruleBp2Build, + description: "genrule using $(locations //absolute:label)", blueprint: `genrule { name: "foo", out: ["foo.out"], @@ -127,21 +126,18 @@ genrule { cmd: "$(locations :foo.tool) -s $(out) $(in)", 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"], -)`, + expectedBazelTargets: []string{ + makeBazelTarget("genrule", "foo", attrNameToString{ + "cmd": `"$(locations //other:foo.tool) -s $(OUTS) $(SRCS)"`, + "outs": `["foo.out"]`, + "srcs": `["foo.in"]`, + "tools": `["//other:foo.tool"]`, + }), }, filesystem: otherGenruleBp, }, { - description: "genrule srcs using $(locations //absolute:label)", - moduleTypeUnderTest: "genrule", - moduleTypeUnderTestFactory: genrule.GenRuleFactory, - moduleTypeUnderTestBp2BuildMutator: genrule.GenruleBp2Build, + description: "genrule srcs using $(locations //absolute:label)", blueprint: `genrule { name: "foo", out: ["foo.out"], @@ -150,21 +146,18 @@ genrule { cmd: "$(locations :foo.tool) -s $(out) $(location :other.tool)", 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"], -)`, + 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"]`, + }), }, filesystem: otherGenruleBp, }, { - description: "genrule using $(location) label should substitute first tool label automatically", - moduleTypeUnderTest: "genrule", - moduleTypeUnderTestFactory: genrule.GenRuleFactory, - moduleTypeUnderTestBp2BuildMutator: genrule.GenruleBp2Build, + description: "genrule using $(location) label should substitute first tool label automatically", blueprint: `genrule { name: "foo", out: ["foo.out"], @@ -173,24 +166,21 @@ genrule { cmd: "$(location) -s $(out) $(in)", bazel_module: { bp2build_available: true }, }`, - expectedBazelTargets: []string{`genrule( - name = "foo", - cmd = "$(location //other:foo.tool) -s $(OUTS) $(SRCS)", - outs = ["foo.out"], - srcs = ["foo.in"], - tools = [ + expectedBazelTargets: []string{ + makeBazelTarget("genrule", "foo", attrNameToString{ + "cmd": `"$(location //other:foo.tool) -s $(OUTS) $(SRCS)"`, + "outs": `["foo.out"]`, + "srcs": `["foo.in"]`, + "tools": `[ "//other:foo.tool", "//other:other.tool", - ], -)`, + ]`, + }), }, filesystem: otherGenruleBp, }, { - description: "genrule using $(locations) label should substitute first tool label automatically", - moduleTypeUnderTest: "genrule", - moduleTypeUnderTestFactory: genrule.GenRuleFactory, - moduleTypeUnderTestBp2BuildMutator: genrule.GenruleBp2Build, + description: "genrule using $(locations) label should substitute first tool label automatically", blueprint: `genrule { name: "foo", out: ["foo.out"], @@ -199,24 +189,21 @@ genrule { cmd: "$(locations) -s $(out) $(in)", 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 = [ + expectedBazelTargets: []string{ + makeBazelTarget("genrule", "foo", attrNameToString{ + "cmd": `"$(locations //other:foo.tool) -s $(OUTS) $(SRCS)"`, + "outs": `["foo.out"]`, + "srcs": `["foo.in"]`, + "tools": `[ "//other:foo.tool", "//other:other.tool", - ], -)`, + ]`, + }), }, filesystem: otherGenruleBp, }, { - description: "genrule without tools or tool_files can convert successfully", - moduleTypeUnderTest: "genrule", - moduleTypeUnderTestFactory: genrule.GenRuleFactory, - moduleTypeUnderTestBp2BuildMutator: genrule.GenruleBp2Build, + description: "genrule without tools or tool_files can convert successfully", blueprint: `genrule { name: "foo", out: ["foo.out"], @@ -224,85 +211,28 @@ genrule { cmd: "cp $(in) $(out)", bazel_module: { bp2build_available: true }, }`, - expectedBazelTargets: []string{`genrule( - name = "foo", - cmd = "cp $(SRCS) $(OUTS)", - outs = ["foo.out"], - srcs = ["foo.in"], -)`, + expectedBazelTargets: []string{ + makeBazelTarget("genrule", "foo", attrNameToString{ + "cmd": `"cp $(SRCS) $(OUTS)"`, + "outs": `["foo.out"]`, + "srcs": `["foo.in"]`, + }), }, }, } - dir := "." for _, testCase := range testCases { - fs := make(map[string][]byte) - toParse := []string{ - "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, - ) - } - } - } + t.Run(testCase.description, func(t *testing.T) { + runGenruleTestCase(t, testCase) + }) } } func TestBp2BuildInlinesDefaults(t *testing.T) { - testCases := []struct { - moduleTypesUnderTest map[string]android.ModuleFactory - bp2buildMutatorsUnderTest map[string]bp2buildMutator - bp string - expectedBazelTarget string - description string - }{ + testCases := []bp2buildTestCase{ { - moduleTypesUnderTest: map[string]android.ModuleFactory{ - "genrule": genrule.GenRuleFactory, - "genrule_defaults": func() android.Module { return genrule.DefaultsFactory() }, - }, - bp2buildMutatorsUnderTest: map[string]bp2buildMutator{ - "genrule": genrule.GenruleBp2Build, - }, - bp: `genrule_defaults { + description: "genrule applies properties from a genrule_defaults dependency if not specified", + blueprint: `genrule_defaults { name: "gen_defaults", cmd: "do-something $(in) $(out)", } @@ -314,23 +244,17 @@ genrule { bazel_module: { bp2build_available: true }, } `, - expectedBazelTarget: `genrule( - name = "gen", - cmd = "do-something $(SRCS) $(OUTS)", - outs = ["out"], - srcs = ["in1"], -)`, - description: "genrule applies properties from a genrule_defaults dependency if not specified", + expectedBazelTargets: []string{ + makeBazelTarget("genrule", "gen", attrNameToString{ + "cmd": `"do-something $(SRCS) $(OUTS)"`, + "outs": `["out"]`, + "srcs": `["in1"]`, + }), + }, }, { - moduleTypesUnderTest: map[string]android.ModuleFactory{ - "genrule": genrule.GenRuleFactory, - "genrule_defaults": func() android.Module { return genrule.DefaultsFactory() }, - }, - bp2buildMutatorsUnderTest: map[string]bp2buildMutator{ - "genrule": genrule.GenruleBp2Build, - }, - bp: `genrule_defaults { + description: "genrule does merges properties from a genrule_defaults dependency, latest-first", + blueprint: `genrule_defaults { name: "gen_defaults", out: ["out-from-defaults"], srcs: ["in-from-defaults"], @@ -345,29 +269,23 @@ genrule { bazel_module: { bp2build_available: true }, } `, - expectedBazelTarget: `genrule( - name = "gen", - cmd = "do-something $(SRCS) $(OUTS)", - outs = [ + expectedBazelTargets: []string{ + makeBazelTarget("genrule", "gen", attrNameToString{ + "cmd": `"do-something $(SRCS) $(OUTS)"`, + "outs": `[ "out-from-defaults", "out", - ], - srcs = [ + ]`, + "srcs": `[ "in-from-defaults", "in1", - ], -)`, - description: "genrule does merges properties from a genrule_defaults dependency, latest-first", + ]`, + }), + }, }, { - moduleTypesUnderTest: map[string]android.ModuleFactory{ - "genrule": genrule.GenRuleFactory, - "genrule_defaults": func() android.Module { return genrule.DefaultsFactory() }, - }, - bp2buildMutatorsUnderTest: map[string]bp2buildMutator{ - "genrule": genrule.GenruleBp2Build, - }, - bp: `genrule_defaults { + description: "genrule applies properties from list of genrule_defaults", + blueprint: `genrule_defaults { name: "gen_defaults1", cmd: "cp $(in) $(out)", } @@ -384,23 +302,17 @@ genrule { bazel_module: { bp2build_available: true }, } `, - expectedBazelTarget: `genrule( - name = "gen", - cmd = "cp $(SRCS) $(OUTS)", - outs = ["out"], - srcs = ["in1"], -)`, - description: "genrule applies properties from list of genrule_defaults", + expectedBazelTargets: []string{ + makeBazelTarget("genrule", "gen", attrNameToString{ + "cmd": `"cp $(SRCS) $(OUTS)"`, + "outs": `["out"]`, + "srcs": `["in1"]`, + }), + }, }, { - moduleTypesUnderTest: map[string]android.ModuleFactory{ - "genrule": genrule.GenRuleFactory, - "genrule_defaults": func() android.Module { return genrule.DefaultsFactory() }, - }, - bp2buildMutatorsUnderTest: map[string]bp2buildMutator{ - "genrule": genrule.GenruleBp2Build, - }, - bp: `genrule_defaults { + description: "genrule applies properties from genrule_defaults transitively", + blueprint: `genrule_defaults { name: "gen_defaults1", defaults: ["gen_defaults2"], cmd: "cmd1 $(in) $(out)", // overrides gen_defaults2's cmd property value. @@ -427,55 +339,26 @@ genrule { bazel_module: { bp2build_available: true }, } `, - expectedBazelTarget: `genrule( - name = "gen", - cmd = "cmd1 $(SRCS) $(OUTS)", - outs = [ + expectedBazelTargets: []string{ + makeBazelTarget("genrule", "gen", attrNameToString{ + "cmd": `"cmd1 $(SRCS) $(OUTS)"`, + "outs": `[ "out-from-3", "out-from-2", "out", - ], - srcs = [ + ]`, + "srcs": `[ "srcs-from-3", "in1", - ], -)`, - description: "genrule applies properties from genrule_defaults transitively", + ]`, + }), + }, }, } - dir := "." for _, testCase := range testCases { - config := android.TestConfig(buildDir, nil, testCase.bp, nil) - ctx := android.NewTestContext(config) - 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, - ) - } + t.Run(testCase.description, func(t *testing.T) { + runGenruleTestCase(t, testCase) + }) } } diff --git a/bp2build/prebuilt_etc_conversion_test.go b/bp2build/prebuilt_etc_conversion_test.go index 62e407bf1..118930994 100644 --- a/bp2build/prebuilt_etc_conversion_test.go +++ b/bp2build/prebuilt_etc_conversion_test.go @@ -23,6 +23,9 @@ import ( func runPrebuiltEtcTestCase(t *testing.T, tc bp2buildTestCase) { t.Helper() + (&tc).moduleTypeUnderTest = "prebuilt_etc" + (&tc).moduleTypeUnderTestFactory = etc.PrebuiltEtcFactory + (&tc).moduleTypeUnderTestBp2BuildMutator = etc.PrebuiltEtcBp2Build runBp2BuildTestCase(t, registerPrebuiltEtcModuleTypes, tc) } @@ -31,11 +34,8 @@ func registerPrebuiltEtcModuleTypes(ctx android.RegistrationContext) { func TestPrebuiltEtcSimple(t *testing.T) { runPrebuiltEtcTestCase(t, bp2buildTestCase{ - description: "prebuilt_etc - simple example", - moduleTypeUnderTest: "prebuilt_etc", - moduleTypeUnderTestFactory: etc.PrebuiltEtcFactory, - moduleTypeUnderTestBp2BuildMutator: etc.PrebuiltEtcBp2Build, - filesystem: map[string]string{}, + description: "prebuilt_etc - simple example", + filesystem: map[string]string{}, blueprint: ` prebuilt_etc { name: "apex_tz_version", @@ -45,22 +45,19 @@ prebuilt_etc { installable: false, } `, - expectedBazelTargets: []string{`prebuilt_etc( - name = "apex_tz_version", - filename = "tz_version", - installable = False, - src = "version/tz_version", - sub_dir = "tz", -)`}}) + expectedBazelTargets: []string{ + makeBazelTarget("prebuilt_etc", "apex_tz_version", attrNameToString{ + "filename": `"tz_version"`, + "installable": `False`, + "src": `"version/tz_version"`, + "sub_dir": `"tz"`, + })}}) } func TestPrebuiltEtcArchVariant(t *testing.T) { runPrebuiltEtcTestCase(t, bp2buildTestCase{ - description: "prebuilt_etc - simple example", - moduleTypeUnderTest: "prebuilt_etc", - moduleTypeUnderTestFactory: etc.PrebuiltEtcFactory, - moduleTypeUnderTestBp2BuildMutator: etc.PrebuiltEtcBp2Build, - filesystem: map[string]string{}, + description: "prebuilt_etc - arch variant", + filesystem: map[string]string{}, blueprint: ` prebuilt_etc { name: "apex_tz_version", @@ -78,15 +75,15 @@ prebuilt_etc { } } `, - expectedBazelTargets: []string{`prebuilt_etc( - name = "apex_tz_version", - filename = "tz_version", - installable = False, - src = select({ + expectedBazelTargets: []string{ + makeBazelTarget("prebuilt_etc", "apex_tz_version", attrNameToString{ + "filename": `"tz_version"`, + "installable": `False`, + "src": `select({ "//build/bazel/platforms/arch:arm": "arm", "//build/bazel/platforms/arch:arm64": "arm64", "//conditions:default": "version/tz_version", - }), - sub_dir = "tz", -)`}}) + })`, + "sub_dir": `"tz"`, + })}}) } diff --git a/bp2build/python_binary_conversion_test.go b/bp2build/python_binary_conversion_test.go index 5b4829eb0..01b6aa268 100644 --- a/bp2build/python_binary_conversion_test.go +++ b/bp2build/python_binary_conversion_test.go @@ -7,7 +7,8 @@ import ( "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) { ctx.RegisterModuleType("python_library", python.PythonLibraryFactory) ctx.RegisterModuleType("python_library_host", python.PythonLibraryHostFactory) @@ -15,7 +16,7 @@ func runBp2BuildTestCaseWithLibs(t *testing.T, tc bp2buildTestCase) { } func TestPythonBinaryHostSimple(t *testing.T) { - runBp2BuildTestCaseWithLibs(t, bp2buildTestCase{ + runBp2BuildTestCaseWithPythonLibraries(t, bp2buildTestCase{ description: "simple python_binary_host converts to a native py_binary", moduleTypeUnderTest: "python_binary_host", moduleTypeUnderTestFactory: python.PythonBinaryHostFactory, @@ -41,17 +42,17 @@ func TestPythonBinaryHostSimple(t *testing.T) { srcs: ["b/e.py"], bazel_module: { bp2build_available: true }, }`, - expectedBazelTargets: []string{`py_binary( - name = "foo", - data = ["files/data.txt"], - deps = [":bar"], - main = "a.py", - srcs = [ + expectedBazelTargets: []string{ + makeBazelTarget("py_binary", "foo", attrNameToString{ + "data": `["files/data.txt"]`, + "deps": `[":bar"]`, + "main": `"a.py"`, + "srcs": `[ "a.py", "b/c.py", "b/d.py", - ], -)`, + ]`, + }), }, }) } @@ -77,11 +78,11 @@ func TestPythonBinaryHostPy2(t *testing.T) { bazel_module: { bp2build_available: true }, } `, - expectedBazelTargets: []string{`py_binary( - name = "foo", - python_version = "PY2", - srcs = ["a.py"], -)`, + expectedBazelTargets: []string{ + makeBazelTarget("py_binary", "foo", attrNameToString{ + "python_version": `"PY2"`, + "srcs": `["a.py"]`, + }), }, }) } @@ -109,10 +110,9 @@ func TestPythonBinaryHostPy3(t *testing.T) { `, expectedBazelTargets: []string{ // python_version is PY3 by default. - `py_binary( - name = "foo", - srcs = ["a.py"], -)`, + makeBazelTarget("py_binary", "foo", attrNameToString{ + "srcs": `["a.py"]`, + }), }, }) } @@ -139,14 +139,13 @@ func TestPythonBinaryHostArchVariance(t *testing.T) { }, }`, expectedBazelTargets: []string{ - `py_binary( - name = "foo-arm", - srcs = select({ + makeBazelTarget("py_binary", "foo-arm", attrNameToString{ + "srcs": `select({ "//build/bazel/platforms/arch:arm": ["arm.py"], "//build/bazel/platforms/arch:x86": ["x86.py"], "//conditions:default": [], - }), -)`, + })`, + }), }, }) } diff --git a/bp2build/python_library_conversion_test.go b/bp2build/python_library_conversion_test.go index 7f983ad50..e3345921e 100644 --- a/bp2build/python_library_conversion_test.go +++ b/bp2build/python_library_conversion_test.go @@ -11,38 +11,49 @@ import ( // TODO(alexmarquez): Should be lifted into a generic Bp2Build file type PythonLibBp2Build func(ctx android.TopDownMutatorContext) -func TestPythonLibrary(t *testing.T) { - 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)) { +func runPythonLibraryTestCase(t *testing.T, tc bp2buildTestCase) { t.Helper() - // Simple - runBp2BuildTestCase(t, registration, bp2buildTestCase{ - description: fmt.Sprintf("simple %s converts to a native py_library", modType), - moduleTypeUnderTest: modType, - moduleTypeUnderTestFactory: factory, - moduleTypeUnderTestBp2BuildMutator: mutator, - filesystem: map[string]string{ - "a.py": "", - "b/c.py": "", - "b/d.py": "", - "b/e.py": "", - "files/data.txt": "", - }, - blueprint: fmt.Sprintf(`%s { + testCase := tc + testCase.description = fmt.Sprintf(testCase.description, "python_library") + testCase.blueprint = fmt.Sprintf(testCase.blueprint, "python_library") + testCase.moduleTypeUnderTest = "python_library" + testCase.moduleTypeUnderTestFactory = python.PythonLibraryFactory + testCase.moduleTypeUnderTestBp2BuildMutator = python.PythonLibraryBp2Build + runBp2BuildTestCaseSimple(t, testCase) +} + +func runPythonLibraryHostTestCase(t *testing.T, tc bp2buildTestCase) { + t.Helper() + testCase := tc + testCase.description = fmt.Sprintf(testCase.description, "python_library_host") + 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", srcs: ["**/*.py"], exclude_srcs: ["b/e.py"], @@ -54,28 +65,23 @@ func testPythonLib(t *testing.T, modType string, name: "bar", srcs: ["b/e.py"], bazel_module: { bp2build_available: false }, - }`, modType), - expectedBazelTargets: []string{`py_library( - name = "foo", - data = ["files/data.txt"], - deps = [":bar"], - srcs = [ + }`, + expectedBazelTargets: []string{ + makeBazelTarget("py_library", "foo", attrNameToString{ + "data": `["files/data.txt"]`, + "deps": `[":bar"]`, + "srcs": `[ "a.py", "b/c.py", "b/d.py", - ], - srcs_version = "PY3", -)`, + ]`, + "srcs_version": `"PY3"`, + }), + }, }, - }) - - // PY2 - 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 { + { + description: "py2 %s converts to a native py_library", + blueprint: `%s { name: "foo", srcs: ["a.py"], version: { @@ -88,22 +94,17 @@ func testPythonLib(t *testing.T, modType string, }, bazel_module: { bp2build_available: true }, -}`, modType), - expectedBazelTargets: []string{`py_library( - name = "foo", - srcs = ["a.py"], - srcs_version = "PY2", -)`, +}`, + expectedBazelTargets: []string{ + makeBazelTarget("py_library", "foo", attrNameToString{ + "srcs": `["a.py"]`, + "srcs_version": `"PY2"`, + }), + }, }, - }) - - // PY3 - 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 { + { + description: "py3 %s converts to a native py_library", + blueprint: `%s { name: "foo", srcs: ["a.py"], version: { @@ -116,22 +117,17 @@ func testPythonLib(t *testing.T, modType string, }, bazel_module: { bp2build_available: true }, -}`, modType), - expectedBazelTargets: []string{`py_library( - name = "foo", - srcs = ["a.py"], - srcs_version = "PY3", -)`, +}`, + expectedBazelTargets: []string{ + makeBazelTarget("py_library", "foo", attrNameToString{ + "srcs": `["a.py"]`, + "srcs_version": `"PY3"`, + }), + }, }, - }) - - // Both - 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 { + { + description: "py2&3 %s converts to a native py_library", + blueprint: `%s { name: "foo", srcs: ["a.py"], version: { @@ -144,44 +140,31 @@ func testPythonLib(t *testing.T, modType string, }, bazel_module: { bp2build_available: true }, -}`, modType), - expectedBazelTargets: []string{ - // srcs_version is PY2ANDPY3 by default. - `py_library( - name = "foo", - srcs = ["a.py"], -)`, +}`, + expectedBazelTargets: []string{ + // srcs_version is PY2ANDPY3 by default. + makeBazelTarget("py_library", "foo", attrNameToString{ + "srcs": `["a.py"]`, + }), + }, }, - }) + } + + for _, tc := range testCases { + t.Run(tc.description, func(t *testing.T) { + runPythonLibraryTestCases(t, tc) + }) + } } -func TestPythonLibraryArchVariance(t *testing.T) { - testPythonArchVariance(t, "python_library", "py_library", - python.PythonLibraryFactory, python.PythonLibraryBp2Build, - 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, +func TestPythonArchVariance(t *testing.T) { + runPythonLibraryTestCases(t, bp2buildTestCase{ + description: "test %s arch variants", filesystem: map[string]string{ "dir/arm.py": "", "dir/x86.py": "", }, - blueprint: fmt.Sprintf(`%s { + blueprint: `%s { name: "foo", arch: { arm: { @@ -191,17 +174,16 @@ func testPythonArchVariance(t *testing.T, modType, bazelTarget string, srcs: ["x86.py"], }, }, - }`, modType), + }`, expectedBazelTargets: []string{ - fmt.Sprintf(`%s( - name = "foo", - srcs = select({ + makeBazelTarget("py_library", "foo", attrNameToString{ + "srcs": `select({ "//build/bazel/platforms/arch:arm": ["arm.py"], "//build/bazel/platforms/arch:x86": ["x86.py"], "//conditions:default": [], - }), - srcs_version = "PY3", -)`, bazelTarget), + })`, + "srcs_version": `"PY3"`, + }), }, }) } diff --git a/bp2build/sh_conversion_test.go b/bp2build/sh_conversion_test.go index 82e0a14ca..1ca4a0e85 100644 --- a/bp2build/sh_conversion_test.go +++ b/bp2build/sh_conversion_test.go @@ -64,9 +64,9 @@ func TestShBinarySimple(t *testing.T) { src: "foo.sh", bazel_module: { bp2build_available: true }, }`, - expectedBazelTargets: []string{`sh_binary( - name = "foo", - srcs = ["foo.sh"], -)`}, + expectedBazelTargets: []string{ + makeBazelTarget("sh_binary", "foo", attrNameToString{ + "srcs": `["foo.sh"]`, + })}, }) } diff --git a/bp2build/testing.go b/bp2build/testing.go index 7c2f43a06..daa9c22db 100644 --- a/bp2build/testing.go +++ b/bp2build/testing.go @@ -364,3 +364,16 @@ func simpleModuleDoNotConvertBp2build(typ, name string) string { bazel_module: { bp2build_available: false }, }`, 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")) +}