2022-02-15 15:35:07 +01:00
|
|
|
// Copyright 2022 Google Inc. All rights reserved.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package bp2build
|
|
|
|
|
|
|
|
import (
|
2023-09-19 03:12:48 +02:00
|
|
|
"testing"
|
|
|
|
|
2022-02-15 15:35:07 +01:00
|
|
|
"android/soong/android"
|
|
|
|
"android/soong/java"
|
|
|
|
)
|
|
|
|
|
2023-09-21 17:07:30 +02:00
|
|
|
func runAndroidLibraryImportTestWithRegistrationCtxFunc(t *testing.T, registrationCtxFunc func(ctx android.RegistrationContext), tc Bp2buildTestCase) {
|
|
|
|
t.Helper()
|
|
|
|
(&tc).ModuleTypeUnderTest = "android_library_import"
|
|
|
|
(&tc).ModuleTypeUnderTestFactory = java.AARImportFactory
|
|
|
|
RunBp2BuildTestCase(t, registrationCtxFunc, tc)
|
|
|
|
}
|
|
|
|
|
|
|
|
func runAndroidLibraryImportTest(t *testing.T, tc Bp2buildTestCase) {
|
|
|
|
runAndroidLibraryImportTestWithRegistrationCtxFunc(t, func(ctx android.RegistrationContext) {}, tc)
|
|
|
|
}
|
|
|
|
|
2022-02-15 15:35:07 +01:00
|
|
|
func TestConvertAndroidLibrary(t *testing.T) {
|
|
|
|
t.Helper()
|
|
|
|
RunBp2BuildTestCase(t, func(ctx android.RegistrationContext) {}, Bp2buildTestCase{
|
|
|
|
Description: "Android Library - simple example",
|
|
|
|
ModuleTypeUnderTest: "android_library",
|
|
|
|
ModuleTypeUnderTestFactory: java.AndroidLibraryFactory,
|
|
|
|
Filesystem: map[string]string{
|
|
|
|
"lib.java": "",
|
|
|
|
"arm.java": "",
|
|
|
|
"x86.java": "",
|
|
|
|
"res/res.png": "",
|
|
|
|
"manifest/AndroidManifest.xml": "",
|
|
|
|
},
|
2023-09-19 03:12:48 +02:00
|
|
|
StubbedBuildDefinitions: []string{"static_lib_dep"},
|
|
|
|
Blueprint: simpleModule("android_library", "static_lib_dep") + `
|
2022-02-15 15:35:07 +01:00
|
|
|
android_library {
|
2023-08-07 19:38:18 +02:00
|
|
|
name: "TestLib",
|
|
|
|
srcs: ["lib.java"],
|
|
|
|
arch: {
|
|
|
|
arm: {
|
|
|
|
srcs: ["arm.java"],
|
2022-02-15 15:35:07 +01:00
|
|
|
},
|
2023-08-07 19:38:18 +02:00
|
|
|
x86: {
|
|
|
|
srcs: ["x86.java"],
|
|
|
|
}
|
|
|
|
},
|
|
|
|
manifest: "manifest/AndroidManifest.xml",
|
|
|
|
static_libs: ["static_lib_dep"],
|
|
|
|
sdk_version: "current",
|
2022-02-15 15:35:07 +01:00
|
|
|
}
|
|
|
|
`,
|
|
|
|
ExpectedBazelTargets: []string{
|
2022-08-31 20:28:19 +02:00
|
|
|
MakeBazelTarget(
|
2022-02-15 15:35:07 +01:00
|
|
|
"android_library",
|
|
|
|
"TestLib",
|
|
|
|
AttrNameToString{
|
|
|
|
"srcs": `["lib.java"] + select({
|
2023-10-11 12:51:28 +02:00
|
|
|
"//build/bazel_common_rules/platforms/arch:arm": ["arm.java"],
|
|
|
|
"//build/bazel_common_rules/platforms/arch:x86": ["x86.java"],
|
2022-02-15 15:35:07 +01:00
|
|
|
"//conditions:default": [],
|
|
|
|
})`,
|
|
|
|
"manifest": `"manifest/AndroidManifest.xml"`,
|
|
|
|
"resource_files": `["res/res.png"]`,
|
|
|
|
"deps": `[":static_lib_dep"]`,
|
|
|
|
"exports": `[":static_lib_dep"]`,
|
2023-08-07 19:38:18 +02:00
|
|
|
"sdk_version": `"current"`, // use as default
|
2022-02-15 15:35:07 +01:00
|
|
|
}),
|
2023-08-07 19:38:18 +02:00
|
|
|
MakeNeverlinkDuplicateTarget("android_library", "TestLib"),
|
2022-02-15 15:35:07 +01:00
|
|
|
}})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestConvertAndroidLibraryWithNoSources(t *testing.T) {
|
|
|
|
t.Helper()
|
|
|
|
RunBp2BuildTestCase(t, func(ctx android.RegistrationContext) {}, Bp2buildTestCase{
|
2023-09-27 21:29:34 +02:00
|
|
|
Description: "Android Library - modules will deps when there are no sources",
|
2022-02-15 15:35:07 +01:00
|
|
|
ModuleTypeUnderTest: "android_library",
|
|
|
|
ModuleTypeUnderTestFactory: java.AndroidLibraryFactory,
|
|
|
|
Filesystem: map[string]string{
|
|
|
|
"res/res.png": "",
|
|
|
|
"AndroidManifest.xml": "",
|
|
|
|
},
|
2023-09-19 03:12:48 +02:00
|
|
|
Blueprint: simpleModule("android_library", "lib_dep") + `
|
2022-02-15 15:35:07 +01:00
|
|
|
android_library {
|
2023-08-07 19:38:18 +02:00
|
|
|
name: "TestLib",
|
|
|
|
srcs: [],
|
|
|
|
manifest: "AndroidManifest.xml",
|
|
|
|
libs: ["lib_dep"],
|
|
|
|
sdk_version: "current",
|
2022-02-15 15:35:07 +01:00
|
|
|
}
|
|
|
|
`,
|
2023-10-05 17:47:07 +02:00
|
|
|
StubbedBuildDefinitions: []string{"lib_dep"},
|
2023-09-27 21:29:34 +02:00
|
|
|
ExpectedBazelTargets: []string{
|
|
|
|
MakeBazelTarget(
|
|
|
|
"android_library",
|
|
|
|
"TestLib",
|
|
|
|
AttrNameToString{
|
|
|
|
"manifest": `"AndroidManifest.xml"`,
|
|
|
|
"resource_files": `["res/res.png"]`,
|
|
|
|
"sdk_version": `"current"`, // use as default
|
|
|
|
},
|
|
|
|
),
|
|
|
|
MakeNeverlinkDuplicateTarget("android_library", "TestLib"),
|
|
|
|
},
|
2022-02-15 15:35:07 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestConvertAndroidLibraryImport(t *testing.T) {
|
2023-09-21 17:07:30 +02:00
|
|
|
runAndroidLibraryImportTestWithRegistrationCtxFunc(t,
|
2022-02-15 15:35:07 +01:00
|
|
|
func(ctx android.RegistrationContext) {
|
|
|
|
ctx.RegisterModuleType("android_library", java.AndroidLibraryFactory)
|
|
|
|
},
|
|
|
|
Bp2buildTestCase{
|
2023-09-21 17:07:30 +02:00
|
|
|
Description: "Android Library Import",
|
2023-09-21 22:36:35 +02:00
|
|
|
StubbedBuildDefinitions: []string{"static_lib_dep", "static_import_dep", "static_import_dep-neverlink"},
|
2022-02-15 15:35:07 +01:00
|
|
|
// Bazel's aar_import can only export *_import targets, so we expect
|
|
|
|
// only "static_import_dep" in exports, but both "static_lib_dep" and
|
|
|
|
// "static_import_dep" in deps
|
2023-09-19 03:12:48 +02:00
|
|
|
Blueprint: simpleModule("android_library", "static_lib_dep") + `
|
2022-02-15 15:35:07 +01:00
|
|
|
android_library_import {
|
|
|
|
name: "TestImport",
|
|
|
|
aars: ["import.aar"],
|
|
|
|
static_libs: ["static_lib_dep", "static_import_dep"],
|
2023-08-07 19:38:18 +02:00
|
|
|
sdk_version: "current",
|
2022-02-15 15:35:07 +01:00
|
|
|
}
|
2023-09-19 03:12:48 +02:00
|
|
|
|
|
|
|
android_library_import {
|
|
|
|
name: "static_import_dep",
|
2023-09-21 22:36:35 +02:00
|
|
|
aars: ["import.aar"],
|
2023-09-19 03:12:48 +02:00
|
|
|
}
|
2022-02-15 15:35:07 +01:00
|
|
|
`,
|
|
|
|
ExpectedBazelTargets: []string{
|
2022-08-31 20:28:19 +02:00
|
|
|
MakeBazelTarget(
|
2022-02-15 15:35:07 +01:00
|
|
|
"aar_import",
|
|
|
|
"TestImport",
|
|
|
|
AttrNameToString{
|
|
|
|
"aar": `"import.aar"`,
|
|
|
|
"deps": `[
|
|
|
|
":static_lib_dep",
|
|
|
|
":static_import_dep",
|
|
|
|
]`,
|
2023-08-07 19:38:18 +02:00
|
|
|
"exports": `[":static_import_dep"]`,
|
|
|
|
"sdk_version": `"current"`, // use as default
|
2022-02-15 15:35:07 +01:00
|
|
|
},
|
|
|
|
),
|
2023-01-06 04:42:07 +01:00
|
|
|
MakeNeverlinkDuplicateTarget("android_library", "TestImport"),
|
2022-02-15 15:35:07 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
2023-01-20 17:10:52 +01:00
|
|
|
|
|
|
|
func TestConvertAndroidLibraryKotlin(t *testing.T) {
|
|
|
|
t.Helper()
|
|
|
|
RunBp2BuildTestCase(t, func(ctx android.RegistrationContext) {}, Bp2buildTestCase{
|
|
|
|
Description: "Android Library with .kt srcs and common_srcs attribute",
|
|
|
|
ModuleTypeUnderTest: "android_library",
|
|
|
|
ModuleTypeUnderTestFactory: java.AndroidLibraryFactory,
|
|
|
|
Filesystem: map[string]string{
|
|
|
|
"AndroidManifest.xml": "",
|
|
|
|
},
|
|
|
|
Blueprint: `
|
|
|
|
android_library {
|
2023-08-07 19:38:18 +02:00
|
|
|
name: "TestLib",
|
|
|
|
srcs: ["a.java", "b.kt"],
|
|
|
|
common_srcs: ["c.kt"],
|
|
|
|
sdk_version: "current",
|
2023-01-20 17:10:52 +01:00
|
|
|
}
|
|
|
|
`,
|
|
|
|
ExpectedBazelTargets: []string{
|
|
|
|
MakeBazelTarget(
|
|
|
|
"android_library",
|
|
|
|
"TestLib",
|
|
|
|
AttrNameToString{
|
|
|
|
"srcs": `[
|
|
|
|
"a.java",
|
|
|
|
"b.kt",
|
|
|
|
]`,
|
|
|
|
"common_srcs": `["c.kt"]`,
|
|
|
|
"manifest": `"AndroidManifest.xml"`,
|
|
|
|
"resource_files": `[]`,
|
2023-08-07 19:38:18 +02:00
|
|
|
"sdk_version": `"current"`, // use as default
|
2023-01-20 17:10:52 +01:00
|
|
|
}),
|
|
|
|
MakeNeverlinkDuplicateTarget("android_library", "TestLib"),
|
|
|
|
}})
|
|
|
|
}
|
2023-03-06 20:43:55 +01:00
|
|
|
|
|
|
|
func TestConvertAndroidLibraryKotlinCflags(t *testing.T) {
|
|
|
|
t.Helper()
|
|
|
|
RunBp2BuildTestCase(t, func(ctx android.RegistrationContext) {}, Bp2buildTestCase{
|
|
|
|
Description: "Android Library with .kt srcs and kotlincflags ",
|
|
|
|
ModuleTypeUnderTest: "android_library",
|
|
|
|
ModuleTypeUnderTestFactory: java.AndroidLibraryFactory,
|
|
|
|
Filesystem: map[string]string{
|
|
|
|
"AndroidManifest.xml": "",
|
|
|
|
},
|
|
|
|
Blueprint: `
|
|
|
|
android_library {
|
2023-08-07 19:38:18 +02:00
|
|
|
name: "TestLib",
|
|
|
|
srcs: ["a.java", "b.kt"],
|
|
|
|
kotlincflags: ["-flag1", "-flag2"],
|
|
|
|
sdk_version: "current",
|
2023-03-06 20:43:55 +01:00
|
|
|
}
|
|
|
|
`,
|
|
|
|
ExpectedBazelTargets: []string{
|
|
|
|
MakeBazelTarget(
|
|
|
|
"android_library",
|
|
|
|
"TestLib",
|
|
|
|
AttrNameToString{
|
|
|
|
"srcs": `[
|
|
|
|
"a.java",
|
|
|
|
"b.kt",
|
|
|
|
]`,
|
|
|
|
"kotlincflags": `[
|
|
|
|
"-flag1",
|
|
|
|
"-flag2",
|
|
|
|
]`,
|
|
|
|
"manifest": `"AndroidManifest.xml"`,
|
|
|
|
"resource_files": `[]`,
|
2023-08-07 19:38:18 +02:00
|
|
|
"sdk_version": `"current"`, // use as default
|
2023-03-06 20:43:55 +01:00
|
|
|
}),
|
|
|
|
MakeNeverlinkDuplicateTarget("android_library", "TestLib"),
|
|
|
|
}})
|
|
|
|
}
|
2023-09-21 17:07:30 +02:00
|
|
|
|
|
|
|
func TestAarImportFailsToConvertNoAars(t *testing.T) {
|
|
|
|
runAndroidLibraryImportTest(t,
|
|
|
|
Bp2buildTestCase{
|
|
|
|
Description: "Android Library Import with no aars does not convert.",
|
|
|
|
Blueprint: `
|
|
|
|
android_library_import {
|
|
|
|
name: "no_aar_import",
|
|
|
|
}
|
|
|
|
`,
|
|
|
|
ExpectedBazelTargets: []string{},
|
|
|
|
})
|
|
|
|
}
|