blob: ff8269420d32a9b9b4e07590713ad60a1fa51d02 [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 }),
71 }})
72}
73
74func TestConvertAndroidLibraryWithNoSources(t *testing.T) {
75 t.Helper()
76 RunBp2BuildTestCase(t, func(ctx android.RegistrationContext) {}, Bp2buildTestCase{
77 Description: "Android Library - modules with deps must have sources",
78 ModuleTypeUnderTest: "android_library",
79 ModuleTypeUnderTestFactory: java.AndroidLibraryFactory,
80 Filesystem: map[string]string{
81 "res/res.png": "",
82 "AndroidManifest.xml": "",
83 },
84 Blueprint: simpleModuleDoNotConvertBp2build("android_library", "lib_dep") + `
85android_library {
86 name: "TestLib",
87 srcs: [],
88 manifest: "AndroidManifest.xml",
89 libs: ["lib_dep"],
90}
91`,
92 ExpectedErr: fmt.Errorf("Module has direct dependencies but no sources. Bazel will not allow this."),
93 ExpectedBazelTargets: []string{},
94 })
95}
96
97func TestConvertAndroidLibraryImport(t *testing.T) {
98 t.Helper()
99 RunBp2BuildTestCase(
100 t,
101 func(ctx android.RegistrationContext) {
102 ctx.RegisterModuleType("android_library", java.AndroidLibraryFactory)
103 },
104 Bp2buildTestCase{
105 Description: "Android Library Import",
106 ModuleTypeUnderTest: "android_library_import",
107 ModuleTypeUnderTestFactory: java.AARImportFactory,
108 Filesystem: map[string]string{
109 "import.aar": "",
110 },
111 // Bazel's aar_import can only export *_import targets, so we expect
112 // only "static_import_dep" in exports, but both "static_lib_dep" and
113 // "static_import_dep" in deps
114 Blueprint: simpleModuleDoNotConvertBp2build("android_library", "static_lib_dep") +
115 simpleModuleDoNotConvertBp2build("android_library_import", "static_import_dep") + `
116android_library_import {
117 name: "TestImport",
118 aars: ["import.aar"],
119 static_libs: ["static_lib_dep", "static_import_dep"],
120}
121`,
122 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +0000123 MakeBazelTarget(
Romain Jobredeauxc9b2bba2022-02-15 09:35:07 -0500124 "aar_import",
125 "TestImport",
126 AttrNameToString{
127 "aar": `"import.aar"`,
128 "deps": `[
129 ":static_lib_dep",
130 ":static_import_dep",
131 ]`,
132 "exports": `[":static_import_dep"]`,
133 },
134 ),
135 },
136 },
137 )
138}