blob: ac830c69f8252ac9a79d1c366e0e9737921dcdd4 [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 (
Colin Crossd788b3e2023-11-28 13:14:56 -080018 "android/soong/aconfig/codegen"
Yu Liu2cc802a2023-09-05 17:19:45 -070019 "testing"
20
21 "android/soong/aconfig"
22 "android/soong/android"
Yu Liu855cfc22023-09-14 15:10:03 -070023 "android/soong/cc"
Yu Liuf2b94012023-09-19 15:09:10 -070024 "android/soong/java"
Yu Liu2cc802a2023-09-05 17:19:45 -070025)
26
27func registerAconfigModuleTypes(ctx android.RegistrationContext) {
28 aconfig.RegisterBuildComponents(ctx)
Colin Crossd788b3e2023-11-28 13:14:56 -080029 codegen.RegisterBuildComponents(ctx)
Yu Liu855cfc22023-09-14 15:10:03 -070030 ctx.RegisterModuleType("cc_library", cc.LibraryFactory)
Yu Liuf2b94012023-09-19 15:09:10 -070031 ctx.RegisterModuleType("java_library", java.LibraryFactory)
Yu Liu2cc802a2023-09-05 17:19:45 -070032}
33
34func TestAconfigDeclarations(t *testing.T) {
35 bp := `
36 aconfig_declarations {
37 name: "foo",
38 srcs: [
39 "foo1.aconfig",
40 "test/foo2.aconfig",
41 ],
42 package: "com.android.foo",
43 }
44 `
45 expectedBazelTarget := MakeBazelTargetNoRestrictions(
46 "aconfig_declarations",
47 "foo",
48 AttrNameToString{
49 "srcs": `[
50 "foo1.aconfig",
51 "test/foo2.aconfig",
52 ]`,
53 "package": `"com.android.foo"`,
54 },
55 )
56 RunBp2BuildTestCase(t, registerAconfigModuleTypes, Bp2buildTestCase{
57 Blueprint: bp,
58 ExpectedBazelTargets: []string{expectedBazelTarget},
59 })
60}
61
62func TestAconfigValues(t *testing.T) {
63 bp := `
64 aconfig_values {
65 name: "foo",
66 srcs: [
67 "foo1.textproto",
68 ],
69 package: "com.android.foo",
70 }
71 aconfig_value_set {
72 name: "bar",
73 values: [
74 "foo"
75 ]
76 }
77 `
78 expectedBazelTargets := []string{
79 MakeBazelTargetNoRestrictions(
80 "aconfig_values",
81 "foo",
82 AttrNameToString{
83 "srcs": `["foo1.textproto"]`,
84 "package": `"com.android.foo"`,
85 },
86 ),
87 MakeBazelTargetNoRestrictions(
88 "aconfig_value_set",
89 "bar",
90 AttrNameToString{
91 "values": `[":foo"]`,
92 },
93 )}
94 RunBp2BuildTestCase(t, registerAconfigModuleTypes, Bp2buildTestCase{
95 Blueprint: bp,
96 ExpectedBazelTargets: expectedBazelTargets,
97 })
98}
Yu Liu855cfc22023-09-14 15:10:03 -070099
100func TestCcAconfigLibrary(t *testing.T) {
101 bp := `
102 aconfig_declarations {
103 name: "foo_aconfig_declarations",
104 srcs: [
105 "foo1.aconfig",
106 ],
107 package: "com.android.foo",
108 }
109 cc_library {
110 name: "server_configurable_flags",
111 srcs: ["bar.cc"],
Yu Liu855cfc22023-09-14 15:10:03 -0700112 }
113 cc_aconfig_library {
114 name: "foo",
115 aconfig_declarations: "foo_aconfig_declarations",
116 }
117 `
118 expectedBazelTargets := []string{
119 MakeBazelTargetNoRestrictions(
120 "aconfig_declarations",
121 "foo_aconfig_declarations",
122 AttrNameToString{
123 "srcs": `["foo1.aconfig"]`,
124 "package": `"com.android.foo"`,
125 },
126 ),
127 MakeBazelTargetNoRestrictions(
128 "cc_aconfig_library",
129 "foo",
130 AttrNameToString{
131 "aconfig_declarations": `":foo_aconfig_declarations"`,
132 "dynamic_deps": `[":server_configurable_flags"]`,
Jingwen Chen9c2e3ee2023-10-11 10:51:28 +0000133 "target_compatible_with": `["//build/bazel_common_rules/platforms/os:android"]`,
Yu Liu855cfc22023-09-14 15:10:03 -0700134 },
135 )}
136 RunBp2BuildTestCase(t, registerAconfigModuleTypes, Bp2buildTestCase{
Chris Parsons8a532b72023-09-27 23:11:26 +0000137 Blueprint: bp,
138 ExpectedBazelTargets: expectedBazelTargets,
139 StubbedBuildDefinitions: []string{"server_configurable_flags"},
Yu Liu855cfc22023-09-14 15:10:03 -0700140 })
141}
Yu Liuf2b94012023-09-19 15:09:10 -0700142
143func TestJavaAconfigLibrary(t *testing.T) {
144 bp := `
145 aconfig_declarations {
146 name: "foo_aconfig_declarations",
147 srcs: [
148 "foo1.aconfig",
149 ],
150 package: "com.android.foo",
151 }
Yu Liu873ad352023-10-13 18:19:48 +0000152 java_library {
153 name: "foo_java_library",
154 srcs: ["foo.java"],
155 sdk_version: "current",
156 }
Yu Liuf2b94012023-09-19 15:09:10 -0700157 java_aconfig_library {
158 name: "foo",
159 aconfig_declarations: "foo_aconfig_declarations",
Yu Liu873ad352023-10-13 18:19:48 +0000160 libs: ["foo_java_library"],
Colin Cross7e2e7942023-11-16 12:56:02 -0800161 mode: "test",
Yu Liuf2b94012023-09-19 15:09:10 -0700162 }
163 `
164 expectedBazelTargets := []string{
165 MakeBazelTargetNoRestrictions(
166 "aconfig_declarations",
167 "foo_aconfig_declarations",
168 AttrNameToString{
169 "srcs": `["foo1.aconfig"]`,
170 "package": `"com.android.foo"`,
171 },
172 ),
173 MakeBazelTargetNoRestrictions(
Yu Liu873ad352023-10-13 18:19:48 +0000174 "java_library",
175 "foo_java_library",
176 AttrNameToString{
177 "srcs": `["foo.java"]`,
178 "sdk_version": `"current"`,
179 "target_compatible_with": `["//build/bazel_common_rules/platforms/os:android"]`,
180 },
181 ),
182 MakeNeverlinkDuplicateTarget("java_library", "foo_java_library"),
183 MakeBazelTargetNoRestrictions(
Yu Liuf2b94012023-09-19 15:09:10 -0700184 "java_aconfig_library",
185 "foo",
186 AttrNameToString{
187 "aconfig_declarations": `":foo_aconfig_declarations"`,
Yu Liu873ad352023-10-13 18:19:48 +0000188 "libs": `[":foo_java_library-neverlink"]`,
Yu Liuf2b94012023-09-19 15:09:10 -0700189 "sdk_version": `"system_current"`,
Jingwen Chen9c2e3ee2023-10-11 10:51:28 +0000190 "target_compatible_with": `["//build/bazel_common_rules/platforms/os:android"]`,
Yu Liuf2b94012023-09-19 15:09:10 -0700191 },
192 )}
193 RunBp2BuildTestCase(t, registerAconfigModuleTypes, Bp2buildTestCase{
194 Blueprint: bp,
195 ExpectedBazelTargets: expectedBazelTargets,
196 })
197}
198
199func TestJavaAconfigLibraryAsTaggedOutput(t *testing.T) {
200 bp := `
201 aconfig_declarations {
202 name: "foo_aconfig_declarations",
203 srcs: [
204 "foo.aconfig",
205 ],
206 package: "com.android.foo",
207 }
208 java_library {
209 name: "foo_library",
210 srcs: [":foo_aconfig_library{.generated_srcjars}"],
211 sdk_version: "current",
212 bazel_module: { bp2build_available: true },
213 }
214 java_aconfig_library {
215 name: "foo_aconfig_library",
216 aconfig_declarations: "foo_aconfig_declarations",
Colin Cross7e2e7942023-11-16 12:56:02 -0800217 mode: "test",
Yu Liuf2b94012023-09-19 15:09:10 -0700218 }
219 `
220 expectedBazelTargets := []string{
221 MakeBazelTargetNoRestrictions(
222 "aconfig_declarations",
223 "foo_aconfig_declarations",
224 AttrNameToString{
225 "srcs": `["foo.aconfig"]`,
226 "package": `"com.android.foo"`,
227 },
228 ),
229 MakeBazelTargetNoRestrictions(
230 "java_aconfig_library",
231 "foo_aconfig_library",
232 AttrNameToString{
233 "aconfig_declarations": `":foo_aconfig_declarations"`,
Yu Liuf2b94012023-09-19 15:09:10 -0700234 "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}