Rupert Shuttleworth | a9d76dd | 2021-07-02 07:17:16 -0400 | [diff] [blame] | 1 | // Copyright 2021 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 ( |
| 18 | "android/soong/android" |
| 19 | "android/soong/apex" |
Rupert Shuttleworth | 6e4950a | 2021-07-27 01:34:59 -0400 | [diff] [blame] | 20 | "android/soong/cc" |
| 21 | "android/soong/java" |
Jingwen Chen | b07c901 | 2021-12-08 10:05:45 +0000 | [diff] [blame] | 22 | "android/soong/sh" |
Rupert Shuttleworth | 6e4950a | 2021-07-27 01:34:59 -0400 | [diff] [blame] | 23 | |
Rupert Shuttleworth | a9d76dd | 2021-07-02 07:17:16 -0400 | [diff] [blame] | 24 | "testing" |
| 25 | ) |
| 26 | |
| 27 | func runApexTestCase(t *testing.T, tc bp2buildTestCase) { |
| 28 | t.Helper() |
| 29 | runBp2BuildTestCase(t, registerApexModuleTypes, tc) |
| 30 | } |
| 31 | |
| 32 | func registerApexModuleTypes(ctx android.RegistrationContext) { |
Rupert Shuttleworth | 6e4950a | 2021-07-27 01:34:59 -0400 | [diff] [blame] | 33 | // CC module types needed as they can be APEX dependencies |
| 34 | cc.RegisterCCBuildComponents(ctx) |
| 35 | |
Jingwen Chen | b07c901 | 2021-12-08 10:05:45 +0000 | [diff] [blame] | 36 | ctx.RegisterModuleType("sh_binary", sh.ShBinaryFactory) |
| 37 | ctx.RegisterModuleType("cc_binary", cc.BinaryFactory) |
Rupert Shuttleworth | 6e4950a | 2021-07-27 01:34:59 -0400 | [diff] [blame] | 38 | ctx.RegisterModuleType("cc_library", cc.LibraryFactory) |
| 39 | ctx.RegisterModuleType("apex_key", apex.ApexKeyFactory) |
| 40 | ctx.RegisterModuleType("android_app_certificate", java.AndroidAppCertificateFactory) |
| 41 | ctx.RegisterModuleType("filegroup", android.FileGroupFactory) |
Rupert Shuttleworth | a9d76dd | 2021-07-02 07:17:16 -0400 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | func TestApexBundleSimple(t *testing.T) { |
| 45 | runApexTestCase(t, bp2buildTestCase{ |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 46 | description: "apex - example with all props", |
| 47 | moduleTypeUnderTest: "apex", |
| 48 | moduleTypeUnderTestFactory: apex.BundleFactory, |
| 49 | filesystem: map[string]string{}, |
Rupert Shuttleworth | a9d76dd | 2021-07-02 07:17:16 -0400 | [diff] [blame] | 50 | blueprint: ` |
Rupert Shuttleworth | 6e4950a | 2021-07-27 01:34:59 -0400 | [diff] [blame] | 51 | apex_key { |
Jingwen Chen | b07c901 | 2021-12-08 10:05:45 +0000 | [diff] [blame] | 52 | name: "com.android.apogee.key", |
| 53 | public_key: "com.android.apogee.avbpubkey", |
| 54 | private_key: "com.android.apogee.pem", |
Rupert Shuttleworth | 6e4950a | 2021-07-27 01:34:59 -0400 | [diff] [blame] | 55 | bazel_module: { bp2build_available: false }, |
| 56 | } |
| 57 | |
| 58 | android_app_certificate { |
Jingwen Chen | b07c901 | 2021-12-08 10:05:45 +0000 | [diff] [blame] | 59 | name: "com.android.apogee.certificate", |
| 60 | certificate: "com.android.apogee", |
Rupert Shuttleworth | 6e4950a | 2021-07-27 01:34:59 -0400 | [diff] [blame] | 61 | bazel_module: { bp2build_available: false }, |
| 62 | } |
| 63 | |
| 64 | cc_library { |
Jingwen Chen | b07c901 | 2021-12-08 10:05:45 +0000 | [diff] [blame] | 65 | name: "native_shared_lib_1", |
| 66 | bazel_module: { bp2build_available: false }, |
| 67 | } |
| 68 | |
| 69 | cc_library { |
| 70 | name: "native_shared_lib_2", |
Rupert Shuttleworth | 6e4950a | 2021-07-27 01:34:59 -0400 | [diff] [blame] | 71 | bazel_module: { bp2build_available: false }, |
| 72 | } |
| 73 | |
Rupert Shuttleworth | 9447e1e | 2021-07-28 05:53:42 -0400 | [diff] [blame] | 74 | // TODO(b/194878861): Add bp2build support for prebuilt_etc |
| 75 | cc_library { |
Jingwen Chen | b07c901 | 2021-12-08 10:05:45 +0000 | [diff] [blame] | 76 | name: "pretend_prebuilt_1", |
| 77 | bazel_module: { bp2build_available: false }, |
Rupert Shuttleworth | 9447e1e | 2021-07-28 05:53:42 -0400 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | // TODO(b/194878861): Add bp2build support for prebuilt_etc |
| 81 | cc_library { |
Jingwen Chen | b07c901 | 2021-12-08 10:05:45 +0000 | [diff] [blame] | 82 | name: "pretend_prebuilt_2", |
| 83 | bazel_module: { bp2build_available: false }, |
Rupert Shuttleworth | 9447e1e | 2021-07-28 05:53:42 -0400 | [diff] [blame] | 84 | } |
| 85 | |
Rupert Shuttleworth | 6e4950a | 2021-07-27 01:34:59 -0400 | [diff] [blame] | 86 | filegroup { |
| 87 | name: "com.android.apogee-file_contexts", |
Jingwen Chen | b07c901 | 2021-12-08 10:05:45 +0000 | [diff] [blame] | 88 | srcs: [ |
| 89 | "com.android.apogee-file_contexts", |
| 90 | ], |
| 91 | bazel_module: { bp2build_available: false }, |
Rupert Shuttleworth | 6e4950a | 2021-07-27 01:34:59 -0400 | [diff] [blame] | 92 | } |
| 93 | |
Jingwen Chen | b07c901 | 2021-12-08 10:05:45 +0000 | [diff] [blame] | 94 | cc_binary { name: "cc_binary_1", bazel_module: { bp2build_available: false } } |
| 95 | sh_binary { name: "sh_binary_2", bazel_module: { bp2build_available: false } } |
| 96 | |
Rupert Shuttleworth | a9d76dd | 2021-07-02 07:17:16 -0400 | [diff] [blame] | 97 | apex { |
Rupert Shuttleworth | 6e4950a | 2021-07-27 01:34:59 -0400 | [diff] [blame] | 98 | name: "com.android.apogee", |
| 99 | manifest: "apogee_manifest.json", |
| 100 | androidManifest: "ApogeeAndroidManifest.xml", |
Jingwen Chen | b07c901 | 2021-12-08 10:05:45 +0000 | [diff] [blame] | 101 | file_contexts: "com.android.apogee-file_contexts", |
Rupert Shuttleworth | 6e4950a | 2021-07-27 01:34:59 -0400 | [diff] [blame] | 102 | min_sdk_version: "29", |
| 103 | key: "com.android.apogee.key", |
| 104 | certificate: "com.android.apogee.certificate", |
| 105 | updatable: false, |
| 106 | installable: false, |
| 107 | native_shared_libs: [ |
| 108 | "native_shared_lib_1", |
| 109 | "native_shared_lib_2", |
| 110 | ], |
| 111 | binaries: [ |
Jingwen Chen | b07c901 | 2021-12-08 10:05:45 +0000 | [diff] [blame] | 112 | "cc_binary_1", |
| 113 | "sh_binary_2", |
Rupert Shuttleworth | 6e4950a | 2021-07-27 01:34:59 -0400 | [diff] [blame] | 114 | ], |
Rupert Shuttleworth | 9447e1e | 2021-07-28 05:53:42 -0400 | [diff] [blame] | 115 | prebuilts: [ |
| 116 | "pretend_prebuilt_1", |
| 117 | "pretend_prebuilt_2", |
| 118 | ], |
Rupert Shuttleworth | a9d76dd | 2021-07-02 07:17:16 -0400 | [diff] [blame] | 119 | } |
| 120 | `, |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 121 | expectedBazelTargets: []string{ |
| 122 | makeBazelTarget("apex", "com.android.apogee", attrNameToString{ |
| 123 | "android_manifest": `"ApogeeAndroidManifest.xml"`, |
| 124 | "binaries": `[ |
Jingwen Chen | b07c901 | 2021-12-08 10:05:45 +0000 | [diff] [blame] | 125 | ":cc_binary_1", |
| 126 | ":sh_binary_2", |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 127 | ]`, |
| 128 | "certificate": `":com.android.apogee.certificate"`, |
| 129 | "file_contexts": `":com.android.apogee-file_contexts"`, |
| 130 | "installable": "False", |
| 131 | "key": `":com.android.apogee.key"`, |
| 132 | "manifest": `"apogee_manifest.json"`, |
| 133 | "min_sdk_version": `"29"`, |
Yu Liu | 4ae55d1 | 2022-01-05 17:17:23 -0800 | [diff] [blame^] | 134 | "native_shared_libs_32": `[ |
Rupert Shuttleworth | 6e4950a | 2021-07-27 01:34:59 -0400 | [diff] [blame] | 135 | ":native_shared_lib_1", |
| 136 | ":native_shared_lib_2", |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 137 | ]`, |
Yu Liu | 4ae55d1 | 2022-01-05 17:17:23 -0800 | [diff] [blame^] | 138 | "native_shared_libs_64": `select({ |
| 139 | "//build/bazel/platforms/arch:arm64": [ |
| 140 | ":native_shared_lib_1", |
| 141 | ":native_shared_lib_2", |
| 142 | ], |
| 143 | "//build/bazel/platforms/arch:x86_64": [ |
| 144 | ":native_shared_lib_1", |
| 145 | ":native_shared_lib_2", |
| 146 | ], |
| 147 | "//conditions:default": [], |
| 148 | })`, |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 149 | "prebuilts": `[ |
Rupert Shuttleworth | 9447e1e | 2021-07-28 05:53:42 -0400 | [diff] [blame] | 150 | ":pretend_prebuilt_1", |
| 151 | ":pretend_prebuilt_2", |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 152 | ]`, |
| 153 | "updatable": "False", |
| 154 | }), |
| 155 | }}) |
Rupert Shuttleworth | 6e4950a | 2021-07-27 01:34:59 -0400 | [diff] [blame] | 156 | } |
| 157 | |
Yu Liu | 4ae55d1 | 2022-01-05 17:17:23 -0800 | [diff] [blame^] | 158 | func TestApexBundleCompileMultilibBoth(t *testing.T) { |
| 159 | runApexTestCase(t, bp2buildTestCase{ |
| 160 | description: "apex - example with compile_multilib=both", |
| 161 | moduleTypeUnderTest: "apex", |
| 162 | moduleTypeUnderTestFactory: apex.BundleFactory, |
| 163 | filesystem: map[string]string{}, |
| 164 | blueprint: createMultilibBlueprint("both"), |
| 165 | expectedBazelTargets: []string{ |
| 166 | makeBazelTarget("apex", "com.android.apogee", attrNameToString{ |
| 167 | "native_shared_libs_32": `[ |
| 168 | ":native_shared_lib_1", |
| 169 | ":native_shared_lib_3", |
| 170 | ] + select({ |
| 171 | "//build/bazel/platforms/arch:arm": [":native_shared_lib_2"], |
| 172 | "//build/bazel/platforms/arch:x86": [":native_shared_lib_2"], |
| 173 | "//conditions:default": [], |
| 174 | })`, |
| 175 | "native_shared_libs_64": `select({ |
| 176 | "//build/bazel/platforms/arch:arm64": [ |
| 177 | ":native_shared_lib_1", |
| 178 | ":native_shared_lib_4", |
| 179 | ":native_shared_lib_2", |
| 180 | ], |
| 181 | "//build/bazel/platforms/arch:x86_64": [ |
| 182 | ":native_shared_lib_1", |
| 183 | ":native_shared_lib_4", |
| 184 | ":native_shared_lib_2", |
| 185 | ], |
| 186 | "//conditions:default": [], |
| 187 | })`, |
| 188 | }), |
| 189 | }}) |
| 190 | } |
| 191 | |
| 192 | func TestApexBundleCompileMultilibFirst(t *testing.T) { |
| 193 | runApexTestCase(t, bp2buildTestCase{ |
| 194 | description: "apex - example with compile_multilib=first", |
| 195 | moduleTypeUnderTest: "apex", |
| 196 | moduleTypeUnderTestFactory: apex.BundleFactory, |
| 197 | filesystem: map[string]string{}, |
| 198 | blueprint: createMultilibBlueprint("first"), |
| 199 | expectedBazelTargets: []string{ |
| 200 | makeBazelTarget("apex", "com.android.apogee", attrNameToString{ |
| 201 | "native_shared_libs_32": `select({ |
| 202 | "//build/bazel/platforms/arch:arm": [ |
| 203 | ":native_shared_lib_1", |
| 204 | ":native_shared_lib_3", |
| 205 | ":native_shared_lib_2", |
| 206 | ], |
| 207 | "//build/bazel/platforms/arch:x86": [ |
| 208 | ":native_shared_lib_1", |
| 209 | ":native_shared_lib_3", |
| 210 | ":native_shared_lib_2", |
| 211 | ], |
| 212 | "//conditions:default": [], |
| 213 | })`, |
| 214 | "native_shared_libs_64": `select({ |
| 215 | "//build/bazel/platforms/arch:arm64": [ |
| 216 | ":native_shared_lib_1", |
| 217 | ":native_shared_lib_4", |
| 218 | ":native_shared_lib_2", |
| 219 | ], |
| 220 | "//build/bazel/platforms/arch:x86_64": [ |
| 221 | ":native_shared_lib_1", |
| 222 | ":native_shared_lib_4", |
| 223 | ":native_shared_lib_2", |
| 224 | ], |
| 225 | "//conditions:default": [], |
| 226 | })`, |
| 227 | }), |
| 228 | }}) |
| 229 | } |
| 230 | |
| 231 | func TestApexBundleCompileMultilib32(t *testing.T) { |
| 232 | runApexTestCase(t, bp2buildTestCase{ |
| 233 | description: "apex - example with compile_multilib=32", |
| 234 | moduleTypeUnderTest: "apex", |
| 235 | moduleTypeUnderTestFactory: apex.BundleFactory, |
| 236 | filesystem: map[string]string{}, |
| 237 | blueprint: createMultilibBlueprint("32"), |
| 238 | expectedBazelTargets: []string{ |
| 239 | makeBazelTarget("apex", "com.android.apogee", attrNameToString{ |
| 240 | "native_shared_libs_32": `[ |
| 241 | ":native_shared_lib_1", |
| 242 | ":native_shared_lib_3", |
| 243 | ] + select({ |
| 244 | "//build/bazel/platforms/arch:arm": [":native_shared_lib_2"], |
| 245 | "//build/bazel/platforms/arch:x86": [":native_shared_lib_2"], |
| 246 | "//conditions:default": [], |
| 247 | })`, |
| 248 | }), |
| 249 | }}) |
| 250 | } |
| 251 | |
| 252 | func TestApexBundleCompileMultilib64(t *testing.T) { |
| 253 | runApexTestCase(t, bp2buildTestCase{ |
| 254 | description: "apex - example with compile_multilib=64", |
| 255 | moduleTypeUnderTest: "apex", |
| 256 | moduleTypeUnderTestFactory: apex.BundleFactory, |
| 257 | filesystem: map[string]string{}, |
| 258 | blueprint: createMultilibBlueprint("64"), |
| 259 | expectedBazelTargets: []string{ |
| 260 | makeBazelTarget("apex", "com.android.apogee", attrNameToString{ |
| 261 | "native_shared_libs_64": `select({ |
| 262 | "//build/bazel/platforms/arch:arm64": [ |
| 263 | ":native_shared_lib_1", |
| 264 | ":native_shared_lib_4", |
| 265 | ":native_shared_lib_2", |
| 266 | ], |
| 267 | "//build/bazel/platforms/arch:x86_64": [ |
| 268 | ":native_shared_lib_1", |
| 269 | ":native_shared_lib_4", |
| 270 | ":native_shared_lib_2", |
| 271 | ], |
| 272 | "//conditions:default": [], |
| 273 | })`, |
| 274 | }), |
| 275 | }}) |
| 276 | } |
| 277 | |
Rupert Shuttleworth | 6e4950a | 2021-07-27 01:34:59 -0400 | [diff] [blame] | 278 | func TestApexBundleDefaultPropertyValues(t *testing.T) { |
| 279 | runApexTestCase(t, bp2buildTestCase{ |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 280 | description: "apex - default property values", |
| 281 | moduleTypeUnderTest: "apex", |
| 282 | moduleTypeUnderTestFactory: apex.BundleFactory, |
| 283 | filesystem: map[string]string{}, |
Rupert Shuttleworth | 6e4950a | 2021-07-27 01:34:59 -0400 | [diff] [blame] | 284 | blueprint: ` |
| 285 | apex { |
| 286 | name: "com.android.apogee", |
| 287 | manifest: "apogee_manifest.json", |
| 288 | } |
| 289 | `, |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 290 | expectedBazelTargets: []string{makeBazelTarget("apex", "com.android.apogee", attrNameToString{ |
| 291 | "manifest": `"apogee_manifest.json"`, |
| 292 | }), |
| 293 | }}) |
Rupert Shuttleworth | a9d76dd | 2021-07-02 07:17:16 -0400 | [diff] [blame] | 294 | } |
Jingwen Chen | f59a8e1 | 2021-07-16 09:28:53 +0000 | [diff] [blame] | 295 | |
| 296 | func TestApexBundleHasBazelModuleProps(t *testing.T) { |
| 297 | runApexTestCase(t, bp2buildTestCase{ |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 298 | description: "apex - has bazel module props", |
| 299 | moduleTypeUnderTest: "apex", |
| 300 | moduleTypeUnderTestFactory: apex.BundleFactory, |
| 301 | filesystem: map[string]string{}, |
Jingwen Chen | f59a8e1 | 2021-07-16 09:28:53 +0000 | [diff] [blame] | 302 | blueprint: ` |
| 303 | apex { |
| 304 | name: "apogee", |
| 305 | manifest: "manifest.json", |
| 306 | bazel_module: { bp2build_available: true }, |
| 307 | } |
| 308 | `, |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 309 | expectedBazelTargets: []string{makeBazelTarget("apex", "apogee", attrNameToString{ |
| 310 | "manifest": `"manifest.json"`, |
| 311 | }), |
| 312 | }}) |
Jingwen Chen | f59a8e1 | 2021-07-16 09:28:53 +0000 | [diff] [blame] | 313 | } |
Yu Liu | 4ae55d1 | 2022-01-05 17:17:23 -0800 | [diff] [blame^] | 314 | |
| 315 | func createMultilibBlueprint(compile_multilib string) string { |
| 316 | return ` |
| 317 | cc_library { |
| 318 | name: "native_shared_lib_1", |
| 319 | bazel_module: { bp2build_available: false }, |
| 320 | } |
| 321 | |
| 322 | cc_library { |
| 323 | name: "native_shared_lib_2", |
| 324 | bazel_module: { bp2build_available: false }, |
| 325 | } |
| 326 | |
| 327 | cc_library { |
| 328 | name: "native_shared_lib_3", |
| 329 | bazel_module: { bp2build_available: false }, |
| 330 | } |
| 331 | |
| 332 | cc_library { |
| 333 | name: "native_shared_lib_4", |
| 334 | bazel_module: { bp2build_available: false }, |
| 335 | } |
| 336 | |
| 337 | apex { |
| 338 | name: "com.android.apogee", |
| 339 | compile_multilib: "` + compile_multilib + `", |
| 340 | multilib: { |
| 341 | both: { |
| 342 | native_shared_libs: [ |
| 343 | "native_shared_lib_1", |
| 344 | ], |
| 345 | }, |
| 346 | first: { |
| 347 | native_shared_libs: [ |
| 348 | "native_shared_lib_2", |
| 349 | ], |
| 350 | }, |
| 351 | lib32: { |
| 352 | native_shared_libs: [ |
| 353 | "native_shared_lib_3", |
| 354 | ], |
| 355 | }, |
| 356 | lib64: { |
| 357 | native_shared_libs: [ |
| 358 | "native_shared_lib_4", |
| 359 | ], |
| 360 | }, |
| 361 | }, |
| 362 | }` |
| 363 | } |