blob: ca41680a65104760d4663daef047d5001af18780 [file] [log] [blame]
Yu Liu2cc802a2023-09-05 17:19:45 -07001// Copyright 2023 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 "testing"
19
20 "android/soong/aconfig"
21 "android/soong/android"
Yu Liu855cfc22023-09-14 15:10:03 -070022 "android/soong/cc"
Yu Liuf2b94012023-09-19 15:09:10 -070023 "android/soong/java"
Yu Liu2cc802a2023-09-05 17:19:45 -070024)
25
26func registerAconfigModuleTypes(ctx android.RegistrationContext) {
27 aconfig.RegisterBuildComponents(ctx)
Yu Liu855cfc22023-09-14 15:10:03 -070028 ctx.RegisterModuleType("cc_library", cc.LibraryFactory)
Yu Liuf2b94012023-09-19 15:09:10 -070029 ctx.RegisterModuleType("java_library", java.LibraryFactory)
Yu Liu2cc802a2023-09-05 17:19:45 -070030}
31
32func TestAconfigDeclarations(t *testing.T) {
33 bp := `
34 aconfig_declarations {
35 name: "foo",
36 srcs: [
37 "foo1.aconfig",
38 "test/foo2.aconfig",
39 ],
40 package: "com.android.foo",
41 }
42 `
43 expectedBazelTarget := MakeBazelTargetNoRestrictions(
44 "aconfig_declarations",
45 "foo",
46 AttrNameToString{
47 "srcs": `[
48 "foo1.aconfig",
49 "test/foo2.aconfig",
50 ]`,
51 "package": `"com.android.foo"`,
52 },
53 )
54 RunBp2BuildTestCase(t, registerAconfigModuleTypes, Bp2buildTestCase{
55 Blueprint: bp,
56 ExpectedBazelTargets: []string{expectedBazelTarget},
57 })
58}
59
60func TestAconfigValues(t *testing.T) {
61 bp := `
62 aconfig_values {
63 name: "foo",
64 srcs: [
65 "foo1.textproto",
66 ],
67 package: "com.android.foo",
68 }
69 aconfig_value_set {
70 name: "bar",
71 values: [
72 "foo"
73 ]
74 }
75 `
76 expectedBazelTargets := []string{
77 MakeBazelTargetNoRestrictions(
78 "aconfig_values",
79 "foo",
80 AttrNameToString{
81 "srcs": `["foo1.textproto"]`,
82 "package": `"com.android.foo"`,
83 },
84 ),
85 MakeBazelTargetNoRestrictions(
86 "aconfig_value_set",
87 "bar",
88 AttrNameToString{
89 "values": `[":foo"]`,
90 },
91 )}
92 RunBp2BuildTestCase(t, registerAconfigModuleTypes, Bp2buildTestCase{
93 Blueprint: bp,
94 ExpectedBazelTargets: expectedBazelTargets,
95 })
96}
Yu Liu855cfc22023-09-14 15:10:03 -070097
98func TestCcAconfigLibrary(t *testing.T) {
99 bp := `
100 aconfig_declarations {
101 name: "foo_aconfig_declarations",
102 srcs: [
103 "foo1.aconfig",
104 ],
105 package: "com.android.foo",
106 }
107 cc_library {
108 name: "server_configurable_flags",
109 srcs: ["bar.cc"],
Yu Liu855cfc22023-09-14 15:10:03 -0700110 }
111 cc_aconfig_library {
112 name: "foo",
113 aconfig_declarations: "foo_aconfig_declarations",
114 }
115 `
116 expectedBazelTargets := []string{
117 MakeBazelTargetNoRestrictions(
118 "aconfig_declarations",
119 "foo_aconfig_declarations",
120 AttrNameToString{
121 "srcs": `["foo1.aconfig"]`,
122 "package": `"com.android.foo"`,
123 },
124 ),
125 MakeBazelTargetNoRestrictions(
126 "cc_aconfig_library",
127 "foo",
128 AttrNameToString{
129 "aconfig_declarations": `":foo_aconfig_declarations"`,
130 "dynamic_deps": `[":server_configurable_flags"]`,
Jingwen Chen9c2e3ee2023-10-11 10:51:28 +0000131 "target_compatible_with": `["//build/bazel_common_rules/platforms/os:android"]`,
Yu Liu855cfc22023-09-14 15:10:03 -0700132 },
133 )}
134 RunBp2BuildTestCase(t, registerAconfigModuleTypes, Bp2buildTestCase{
Chris Parsons8a532b72023-09-27 23:11:26 +0000135 Blueprint: bp,
136 ExpectedBazelTargets: expectedBazelTargets,
137 StubbedBuildDefinitions: []string{"server_configurable_flags"},
Yu Liu855cfc22023-09-14 15:10:03 -0700138 })
139}
Yu Liuf2b94012023-09-19 15:09:10 -0700140
141func TestJavaAconfigLibrary(t *testing.T) {
142 bp := `
143 aconfig_declarations {
144 name: "foo_aconfig_declarations",
145 srcs: [
146 "foo1.aconfig",
147 ],
148 package: "com.android.foo",
149 }
Yu Liu873ad352023-10-13 18:19:48 +0000150 java_library {
151 name: "foo_java_library",
152 srcs: ["foo.java"],
153 sdk_version: "current",
154 }
Yu Liuf2b94012023-09-19 15:09:10 -0700155 java_aconfig_library {
156 name: "foo",
157 aconfig_declarations: "foo_aconfig_declarations",
Yu Liu873ad352023-10-13 18:19:48 +0000158 libs: ["foo_java_library"],
Yu Liuf2b94012023-09-19 15:09:10 -0700159 test: true,
160 }
161 `
162 expectedBazelTargets := []string{
163 MakeBazelTargetNoRestrictions(
164 "aconfig_declarations",
165 "foo_aconfig_declarations",
166 AttrNameToString{
167 "srcs": `["foo1.aconfig"]`,
168 "package": `"com.android.foo"`,
169 },
170 ),
171 MakeBazelTargetNoRestrictions(
Yu Liu873ad352023-10-13 18:19:48 +0000172 "java_library",
173 "foo_java_library",
174 AttrNameToString{
175 "srcs": `["foo.java"]`,
176 "sdk_version": `"current"`,
177 "target_compatible_with": `["//build/bazel_common_rules/platforms/os:android"]`,
178 },
179 ),
180 MakeNeverlinkDuplicateTarget("java_library", "foo_java_library"),
181 MakeBazelTargetNoRestrictions(
Yu Liuf2b94012023-09-19 15:09:10 -0700182 "java_aconfig_library",
183 "foo",
184 AttrNameToString{
185 "aconfig_declarations": `":foo_aconfig_declarations"`,
Yu Liu873ad352023-10-13 18:19:48 +0000186 "libs": `[":foo_java_library-neverlink"]`,
Yu Liuf2b94012023-09-19 15:09:10 -0700187 "test": `True`,
188 "sdk_version": `"system_current"`,
Jingwen Chen9c2e3ee2023-10-11 10:51:28 +0000189 "target_compatible_with": `["//build/bazel_common_rules/platforms/os:android"]`,
Yu Liuf2b94012023-09-19 15:09:10 -0700190 },
191 )}
192 RunBp2BuildTestCase(t, registerAconfigModuleTypes, Bp2buildTestCase{
193 Blueprint: bp,
194 ExpectedBazelTargets: expectedBazelTargets,
195 })
196}
197
198func TestJavaAconfigLibraryAsTaggedOutput(t *testing.T) {
199 bp := `
200 aconfig_declarations {
201 name: "foo_aconfig_declarations",
202 srcs: [
203 "foo.aconfig",
204 ],
205 package: "com.android.foo",
206 }
207 java_library {
208 name: "foo_library",
209 srcs: [":foo_aconfig_library{.generated_srcjars}"],
210 sdk_version: "current",
211 bazel_module: { bp2build_available: true },
212 }
213 java_aconfig_library {
214 name: "foo_aconfig_library",
215 aconfig_declarations: "foo_aconfig_declarations",
216 test: true,
217 }
218 `
219 expectedBazelTargets := []string{
220 MakeBazelTargetNoRestrictions(
221 "aconfig_declarations",
222 "foo_aconfig_declarations",
223 AttrNameToString{
224 "srcs": `["foo.aconfig"]`,
225 "package": `"com.android.foo"`,
226 },
227 ),
228 MakeBazelTargetNoRestrictions(
229 "java_aconfig_library",
230 "foo_aconfig_library",
231 AttrNameToString{
232 "aconfig_declarations": `":foo_aconfig_declarations"`,
233 "test": `True`,
234 "sdk_version": `"system_current"`,
Jingwen Chen9c2e3ee2023-10-11 10:51:28 +0000235 "target_compatible_with": `["//build/bazel_common_rules/platforms/os:android"]`,
Yu Liuf2b94012023-09-19 15:09:10 -0700236 },
237 ),
238 MakeBazelTargetNoRestrictions(
239 "java_library",
240 "foo_library",
241 AttrNameToString{
242 "srcs": `[":foo_aconfig_library.generated_srcjars"]`,
243 "sdk_version": `"current"`,
Jingwen Chen9c2e3ee2023-10-11 10:51:28 +0000244 "target_compatible_with": `["//build/bazel_common_rules/platforms/os:android"]`,
Yu Liuf2b94012023-09-19 15:09:10 -0700245 },
246 ),
247 MakeNeverlinkDuplicateTarget("java_library", "foo_library"),
248 }
249
250 RunBp2BuildTestCase(t, registerAconfigModuleTypes, Bp2buildTestCase{
251 Blueprint: bp,
252 ExpectedBazelTargets: expectedBazelTargets,
253 })
254}