blob: df7cced5d00a15c9bde4dff3701e0ae4209a7ab4 [file] [log] [blame]
Romain Jobredeauxc9b2bba2022-02-15 09:35:07 -05001// Copyright 2022 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package bp2build
16
17import (
18 "android/soong/android"
19 "android/soong/java"
20 "fmt"
21
22 "testing"
23)
24
25func TestConvertAndroidLibrary(t *testing.T) {
26 t.Helper()
27 RunBp2BuildTestCase(t, func(ctx android.RegistrationContext) {}, Bp2buildTestCase{
28 Description: "Android Library - simple example",
29 ModuleTypeUnderTest: "android_library",
30 ModuleTypeUnderTestFactory: java.AndroidLibraryFactory,
31 Filesystem: map[string]string{
32 "lib.java": "",
33 "arm.java": "",
34 "x86.java": "",
35 "res/res.png": "",
36 "manifest/AndroidManifest.xml": "",
37 },
38 Blueprint: simpleModuleDoNotConvertBp2build("android_library", "static_lib_dep") + `
39android_library {
40 name: "TestLib",
41 srcs: ["lib.java"],
42 arch: {
43 arm: {
44 srcs: ["arm.java"],
45 },
46 x86: {
47 srcs: ["x86.java"],
48 }
49 },
50 manifest: "manifest/AndroidManifest.xml",
51 static_libs: ["static_lib_dep"],
52 java_version: "7",
53}
54`,
55 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +000056 MakeBazelTarget(
Romain Jobredeauxc9b2bba2022-02-15 09:35:07 -050057 "android_library",
58 "TestLib",
59 AttrNameToString{
60 "srcs": `["lib.java"] + select({
61 "//build/bazel/platforms/arch:arm": ["arm.java"],
62 "//build/bazel/platforms/arch:x86": ["x86.java"],
63 "//conditions:default": [],
64 })`,
65 "manifest": `"manifest/AndroidManifest.xml"`,
66 "resource_files": `["res/res.png"]`,
67 "deps": `[":static_lib_dep"]`,
68 "exports": `[":static_lib_dep"]`,
69 "javacopts": `["-source 1.7 -target 1.7"]`,
70 }),
Alix82fb94e2022-10-26 20:40:18 +000071 MakeNeverlinkDuplicateTarget("android_library", "TestLib"),
Romain Jobredeauxc9b2bba2022-02-15 09:35:07 -050072 }})
73}
74
75func TestConvertAndroidLibraryWithNoSources(t *testing.T) {
76 t.Helper()
77 RunBp2BuildTestCase(t, func(ctx android.RegistrationContext) {}, Bp2buildTestCase{
78 Description: "Android Library - modules with deps must have sources",
79 ModuleTypeUnderTest: "android_library",
80 ModuleTypeUnderTestFactory: java.AndroidLibraryFactory,
81 Filesystem: map[string]string{
82 "res/res.png": "",
83 "AndroidManifest.xml": "",
84 },
85 Blueprint: simpleModuleDoNotConvertBp2build("android_library", "lib_dep") + `
86android_library {
87 name: "TestLib",
88 srcs: [],
89 manifest: "AndroidManifest.xml",
90 libs: ["lib_dep"],
91}
92`,
93 ExpectedErr: fmt.Errorf("Module has direct dependencies but no sources. Bazel will not allow this."),
94 ExpectedBazelTargets: []string{},
95 })
96}
97
98func TestConvertAndroidLibraryImport(t *testing.T) {
99 t.Helper()
100 RunBp2BuildTestCase(
101 t,
102 func(ctx android.RegistrationContext) {
103 ctx.RegisterModuleType("android_library", java.AndroidLibraryFactory)
104 },
105 Bp2buildTestCase{
106 Description: "Android Library Import",
107 ModuleTypeUnderTest: "android_library_import",
108 ModuleTypeUnderTestFactory: java.AARImportFactory,
109 Filesystem: map[string]string{
110 "import.aar": "",
111 },
112 // Bazel's aar_import can only export *_import targets, so we expect
113 // only "static_import_dep" in exports, but both "static_lib_dep" and
114 // "static_import_dep" in deps
115 Blueprint: simpleModuleDoNotConvertBp2build("android_library", "static_lib_dep") +
116 simpleModuleDoNotConvertBp2build("android_library_import", "static_import_dep") + `
117android_library_import {
118 name: "TestImport",
119 aars: ["import.aar"],
120 static_libs: ["static_lib_dep", "static_import_dep"],
121}
122`,
123 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +0000124 MakeBazelTarget(
Romain Jobredeauxc9b2bba2022-02-15 09:35:07 -0500125 "aar_import",
126 "TestImport",
127 AttrNameToString{
128 "aar": `"import.aar"`,
129 "deps": `[
130 ":static_lib_dep",
131 ":static_import_dep",
132 ]`,
133 "exports": `[":static_import_dep"]`,
134 },
135 ),
136 },
137 },
138 )
139}