Yu Liu | 2cc802a | 2023-09-05 17:19:45 -0700 | [diff] [blame] | 1 | // 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 | |
| 15 | package bp2build |
| 16 | |
| 17 | import ( |
Colin Cross | d788b3e | 2023-11-28 13:14:56 -0800 | [diff] [blame^] | 18 | "android/soong/aconfig/codegen" |
Yu Liu | 2cc802a | 2023-09-05 17:19:45 -0700 | [diff] [blame] | 19 | "testing" |
| 20 | |
| 21 | "android/soong/aconfig" |
| 22 | "android/soong/android" |
Yu Liu | 855cfc2 | 2023-09-14 15:10:03 -0700 | [diff] [blame] | 23 | "android/soong/cc" |
Yu Liu | f2b9401 | 2023-09-19 15:09:10 -0700 | [diff] [blame] | 24 | "android/soong/java" |
Yu Liu | 2cc802a | 2023-09-05 17:19:45 -0700 | [diff] [blame] | 25 | ) |
| 26 | |
| 27 | func registerAconfigModuleTypes(ctx android.RegistrationContext) { |
| 28 | aconfig.RegisterBuildComponents(ctx) |
Colin Cross | d788b3e | 2023-11-28 13:14:56 -0800 | [diff] [blame^] | 29 | codegen.RegisterBuildComponents(ctx) |
Yu Liu | 855cfc2 | 2023-09-14 15:10:03 -0700 | [diff] [blame] | 30 | ctx.RegisterModuleType("cc_library", cc.LibraryFactory) |
Yu Liu | f2b9401 | 2023-09-19 15:09:10 -0700 | [diff] [blame] | 31 | ctx.RegisterModuleType("java_library", java.LibraryFactory) |
Yu Liu | 2cc802a | 2023-09-05 17:19:45 -0700 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | func 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 | |
| 62 | func 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 Liu | 855cfc2 | 2023-09-14 15:10:03 -0700 | [diff] [blame] | 99 | |
| 100 | func 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 Liu | 855cfc2 | 2023-09-14 15:10:03 -0700 | [diff] [blame] | 112 | } |
| 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 Chen | 9c2e3ee | 2023-10-11 10:51:28 +0000 | [diff] [blame] | 133 | "target_compatible_with": `["//build/bazel_common_rules/platforms/os:android"]`, |
Yu Liu | 855cfc2 | 2023-09-14 15:10:03 -0700 | [diff] [blame] | 134 | }, |
| 135 | )} |
| 136 | RunBp2BuildTestCase(t, registerAconfigModuleTypes, Bp2buildTestCase{ |
Chris Parsons | 8a532b7 | 2023-09-27 23:11:26 +0000 | [diff] [blame] | 137 | Blueprint: bp, |
| 138 | ExpectedBazelTargets: expectedBazelTargets, |
| 139 | StubbedBuildDefinitions: []string{"server_configurable_flags"}, |
Yu Liu | 855cfc2 | 2023-09-14 15:10:03 -0700 | [diff] [blame] | 140 | }) |
| 141 | } |
Yu Liu | f2b9401 | 2023-09-19 15:09:10 -0700 | [diff] [blame] | 142 | |
| 143 | func 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 Liu | 873ad35 | 2023-10-13 18:19:48 +0000 | [diff] [blame] | 152 | java_library { |
| 153 | name: "foo_java_library", |
| 154 | srcs: ["foo.java"], |
| 155 | sdk_version: "current", |
| 156 | } |
Yu Liu | f2b9401 | 2023-09-19 15:09:10 -0700 | [diff] [blame] | 157 | java_aconfig_library { |
| 158 | name: "foo", |
| 159 | aconfig_declarations: "foo_aconfig_declarations", |
Yu Liu | 873ad35 | 2023-10-13 18:19:48 +0000 | [diff] [blame] | 160 | libs: ["foo_java_library"], |
Colin Cross | 7e2e794 | 2023-11-16 12:56:02 -0800 | [diff] [blame] | 161 | mode: "test", |
Yu Liu | f2b9401 | 2023-09-19 15:09:10 -0700 | [diff] [blame] | 162 | } |
| 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 Liu | 873ad35 | 2023-10-13 18:19:48 +0000 | [diff] [blame] | 174 | "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 Liu | f2b9401 | 2023-09-19 15:09:10 -0700 | [diff] [blame] | 184 | "java_aconfig_library", |
| 185 | "foo", |
| 186 | AttrNameToString{ |
| 187 | "aconfig_declarations": `":foo_aconfig_declarations"`, |
Yu Liu | 873ad35 | 2023-10-13 18:19:48 +0000 | [diff] [blame] | 188 | "libs": `[":foo_java_library-neverlink"]`, |
Yu Liu | f2b9401 | 2023-09-19 15:09:10 -0700 | [diff] [blame] | 189 | "sdk_version": `"system_current"`, |
Jingwen Chen | 9c2e3ee | 2023-10-11 10:51:28 +0000 | [diff] [blame] | 190 | "target_compatible_with": `["//build/bazel_common_rules/platforms/os:android"]`, |
Yu Liu | f2b9401 | 2023-09-19 15:09:10 -0700 | [diff] [blame] | 191 | }, |
| 192 | )} |
| 193 | RunBp2BuildTestCase(t, registerAconfigModuleTypes, Bp2buildTestCase{ |
| 194 | Blueprint: bp, |
| 195 | ExpectedBazelTargets: expectedBazelTargets, |
| 196 | }) |
| 197 | } |
| 198 | |
| 199 | func 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 Cross | 7e2e794 | 2023-11-16 12:56:02 -0800 | [diff] [blame] | 217 | mode: "test", |
Yu Liu | f2b9401 | 2023-09-19 15:09:10 -0700 | [diff] [blame] | 218 | } |
| 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 Liu | f2b9401 | 2023-09-19 15:09:10 -0700 | [diff] [blame] | 234 | "sdk_version": `"system_current"`, |
Jingwen Chen | 9c2e3ee | 2023-10-11 10:51:28 +0000 | [diff] [blame] | 235 | "target_compatible_with": `["//build/bazel_common_rules/platforms/os:android"]`, |
Yu Liu | f2b9401 | 2023-09-19 15:09:10 -0700 | [diff] [blame] | 236 | }, |
| 237 | ), |
| 238 | MakeBazelTargetNoRestrictions( |
| 239 | "java_library", |
| 240 | "foo_library", |
| 241 | AttrNameToString{ |
| 242 | "srcs": `[":foo_aconfig_library.generated_srcjars"]`, |
| 243 | "sdk_version": `"current"`, |
Jingwen Chen | 9c2e3ee | 2023-10-11 10:51:28 +0000 | [diff] [blame] | 244 | "target_compatible_with": `["//build/bazel_common_rules/platforms/os:android"]`, |
Yu Liu | f2b9401 | 2023-09-19 15:09:10 -0700 | [diff] [blame] | 245 | }, |
| 246 | ), |
| 247 | MakeNeverlinkDuplicateTarget("java_library", "foo_library"), |
| 248 | } |
| 249 | |
| 250 | RunBp2BuildTestCase(t, registerAconfigModuleTypes, Bp2buildTestCase{ |
| 251 | Blueprint: bp, |
| 252 | ExpectedBazelTargets: expectedBazelTargets, |
| 253 | }) |
| 254 | } |