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, |
Wei Li | f034cb4 | 2022-01-19 15:54:31 -0800 | [diff] [blame] | 107 | compressible: false, |
Rupert Shuttleworth | 6e4950a | 2021-07-27 01:34:59 -0400 | [diff] [blame] | 108 | native_shared_libs: [ |
| 109 | "native_shared_lib_1", |
| 110 | "native_shared_lib_2", |
| 111 | ], |
| 112 | binaries: [ |
Jingwen Chen | b07c901 | 2021-12-08 10:05:45 +0000 | [diff] [blame] | 113 | "cc_binary_1", |
| 114 | "sh_binary_2", |
Rupert Shuttleworth | 6e4950a | 2021-07-27 01:34:59 -0400 | [diff] [blame] | 115 | ], |
Rupert Shuttleworth | 9447e1e | 2021-07-28 05:53:42 -0400 | [diff] [blame] | 116 | prebuilts: [ |
| 117 | "pretend_prebuilt_1", |
| 118 | "pretend_prebuilt_2", |
| 119 | ], |
Rupert Shuttleworth | a9d76dd | 2021-07-02 07:17:16 -0400 | [diff] [blame] | 120 | } |
| 121 | `, |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 122 | expectedBazelTargets: []string{ |
| 123 | makeBazelTarget("apex", "com.android.apogee", attrNameToString{ |
| 124 | "android_manifest": `"ApogeeAndroidManifest.xml"`, |
| 125 | "binaries": `[ |
Jingwen Chen | b07c901 | 2021-12-08 10:05:45 +0000 | [diff] [blame] | 126 | ":cc_binary_1", |
| 127 | ":sh_binary_2", |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 128 | ]`, |
| 129 | "certificate": `":com.android.apogee.certificate"`, |
| 130 | "file_contexts": `":com.android.apogee-file_contexts"`, |
| 131 | "installable": "False", |
| 132 | "key": `":com.android.apogee.key"`, |
| 133 | "manifest": `"apogee_manifest.json"`, |
| 134 | "min_sdk_version": `"29"`, |
Yu Liu | 4ae55d1 | 2022-01-05 17:17:23 -0800 | [diff] [blame] | 135 | "native_shared_libs_32": `[ |
Rupert Shuttleworth | 6e4950a | 2021-07-27 01:34:59 -0400 | [diff] [blame] | 136 | ":native_shared_lib_1", |
| 137 | ":native_shared_lib_2", |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 138 | ]`, |
Yu Liu | 4ae55d1 | 2022-01-05 17:17:23 -0800 | [diff] [blame] | 139 | "native_shared_libs_64": `select({ |
| 140 | "//build/bazel/platforms/arch:arm64": [ |
| 141 | ":native_shared_lib_1", |
| 142 | ":native_shared_lib_2", |
| 143 | ], |
| 144 | "//build/bazel/platforms/arch:x86_64": [ |
| 145 | ":native_shared_lib_1", |
| 146 | ":native_shared_lib_2", |
| 147 | ], |
| 148 | "//conditions:default": [], |
| 149 | })`, |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 150 | "prebuilts": `[ |
Rupert Shuttleworth | 9447e1e | 2021-07-28 05:53:42 -0400 | [diff] [blame] | 151 | ":pretend_prebuilt_1", |
| 152 | ":pretend_prebuilt_2", |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 153 | ]`, |
Wei Li | f034cb4 | 2022-01-19 15:54:31 -0800 | [diff] [blame] | 154 | "updatable": "False", |
| 155 | "compressible": "False", |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 156 | }), |
| 157 | }}) |
Rupert Shuttleworth | 6e4950a | 2021-07-27 01:34:59 -0400 | [diff] [blame] | 158 | } |
| 159 | |
Yu Liu | 4ae55d1 | 2022-01-05 17:17:23 -0800 | [diff] [blame] | 160 | func TestApexBundleCompileMultilibBoth(t *testing.T) { |
| 161 | runApexTestCase(t, bp2buildTestCase{ |
| 162 | description: "apex - example with compile_multilib=both", |
| 163 | moduleTypeUnderTest: "apex", |
| 164 | moduleTypeUnderTestFactory: apex.BundleFactory, |
| 165 | filesystem: map[string]string{}, |
| 166 | blueprint: createMultilibBlueprint("both"), |
| 167 | expectedBazelTargets: []string{ |
| 168 | makeBazelTarget("apex", "com.android.apogee", attrNameToString{ |
| 169 | "native_shared_libs_32": `[ |
| 170 | ":native_shared_lib_1", |
| 171 | ":native_shared_lib_3", |
| 172 | ] + select({ |
| 173 | "//build/bazel/platforms/arch:arm": [":native_shared_lib_2"], |
| 174 | "//build/bazel/platforms/arch:x86": [":native_shared_lib_2"], |
| 175 | "//conditions:default": [], |
| 176 | })`, |
| 177 | "native_shared_libs_64": `select({ |
| 178 | "//build/bazel/platforms/arch:arm64": [ |
| 179 | ":native_shared_lib_1", |
| 180 | ":native_shared_lib_4", |
| 181 | ":native_shared_lib_2", |
| 182 | ], |
| 183 | "//build/bazel/platforms/arch:x86_64": [ |
| 184 | ":native_shared_lib_1", |
| 185 | ":native_shared_lib_4", |
| 186 | ":native_shared_lib_2", |
| 187 | ], |
| 188 | "//conditions:default": [], |
| 189 | })`, |
| 190 | }), |
| 191 | }}) |
| 192 | } |
| 193 | |
| 194 | func TestApexBundleCompileMultilibFirst(t *testing.T) { |
| 195 | runApexTestCase(t, bp2buildTestCase{ |
| 196 | description: "apex - example with compile_multilib=first", |
| 197 | moduleTypeUnderTest: "apex", |
| 198 | moduleTypeUnderTestFactory: apex.BundleFactory, |
| 199 | filesystem: map[string]string{}, |
| 200 | blueprint: createMultilibBlueprint("first"), |
| 201 | expectedBazelTargets: []string{ |
| 202 | makeBazelTarget("apex", "com.android.apogee", attrNameToString{ |
| 203 | "native_shared_libs_32": `select({ |
| 204 | "//build/bazel/platforms/arch:arm": [ |
| 205 | ":native_shared_lib_1", |
| 206 | ":native_shared_lib_3", |
| 207 | ":native_shared_lib_2", |
| 208 | ], |
| 209 | "//build/bazel/platforms/arch:x86": [ |
| 210 | ":native_shared_lib_1", |
| 211 | ":native_shared_lib_3", |
| 212 | ":native_shared_lib_2", |
| 213 | ], |
| 214 | "//conditions:default": [], |
| 215 | })`, |
| 216 | "native_shared_libs_64": `select({ |
| 217 | "//build/bazel/platforms/arch:arm64": [ |
| 218 | ":native_shared_lib_1", |
| 219 | ":native_shared_lib_4", |
| 220 | ":native_shared_lib_2", |
| 221 | ], |
| 222 | "//build/bazel/platforms/arch:x86_64": [ |
| 223 | ":native_shared_lib_1", |
| 224 | ":native_shared_lib_4", |
| 225 | ":native_shared_lib_2", |
| 226 | ], |
| 227 | "//conditions:default": [], |
| 228 | })`, |
| 229 | }), |
| 230 | }}) |
| 231 | } |
| 232 | |
| 233 | func TestApexBundleCompileMultilib32(t *testing.T) { |
| 234 | runApexTestCase(t, bp2buildTestCase{ |
| 235 | description: "apex - example with compile_multilib=32", |
| 236 | moduleTypeUnderTest: "apex", |
| 237 | moduleTypeUnderTestFactory: apex.BundleFactory, |
| 238 | filesystem: map[string]string{}, |
| 239 | blueprint: createMultilibBlueprint("32"), |
| 240 | expectedBazelTargets: []string{ |
| 241 | makeBazelTarget("apex", "com.android.apogee", attrNameToString{ |
| 242 | "native_shared_libs_32": `[ |
| 243 | ":native_shared_lib_1", |
| 244 | ":native_shared_lib_3", |
| 245 | ] + select({ |
| 246 | "//build/bazel/platforms/arch:arm": [":native_shared_lib_2"], |
| 247 | "//build/bazel/platforms/arch:x86": [":native_shared_lib_2"], |
| 248 | "//conditions:default": [], |
| 249 | })`, |
| 250 | }), |
| 251 | }}) |
| 252 | } |
| 253 | |
| 254 | func TestApexBundleCompileMultilib64(t *testing.T) { |
| 255 | runApexTestCase(t, bp2buildTestCase{ |
| 256 | description: "apex - example with compile_multilib=64", |
| 257 | moduleTypeUnderTest: "apex", |
| 258 | moduleTypeUnderTestFactory: apex.BundleFactory, |
| 259 | filesystem: map[string]string{}, |
| 260 | blueprint: createMultilibBlueprint("64"), |
| 261 | expectedBazelTargets: []string{ |
| 262 | makeBazelTarget("apex", "com.android.apogee", attrNameToString{ |
| 263 | "native_shared_libs_64": `select({ |
| 264 | "//build/bazel/platforms/arch:arm64": [ |
| 265 | ":native_shared_lib_1", |
| 266 | ":native_shared_lib_4", |
| 267 | ":native_shared_lib_2", |
| 268 | ], |
| 269 | "//build/bazel/platforms/arch:x86_64": [ |
| 270 | ":native_shared_lib_1", |
| 271 | ":native_shared_lib_4", |
| 272 | ":native_shared_lib_2", |
| 273 | ], |
| 274 | "//conditions:default": [], |
| 275 | })`, |
| 276 | }), |
| 277 | }}) |
| 278 | } |
| 279 | |
Rupert Shuttleworth | 6e4950a | 2021-07-27 01:34:59 -0400 | [diff] [blame] | 280 | func TestApexBundleDefaultPropertyValues(t *testing.T) { |
| 281 | runApexTestCase(t, bp2buildTestCase{ |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 282 | description: "apex - default property values", |
| 283 | moduleTypeUnderTest: "apex", |
| 284 | moduleTypeUnderTestFactory: apex.BundleFactory, |
| 285 | filesystem: map[string]string{}, |
Rupert Shuttleworth | 6e4950a | 2021-07-27 01:34:59 -0400 | [diff] [blame] | 286 | blueprint: ` |
| 287 | apex { |
| 288 | name: "com.android.apogee", |
| 289 | manifest: "apogee_manifest.json", |
| 290 | } |
| 291 | `, |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 292 | expectedBazelTargets: []string{makeBazelTarget("apex", "com.android.apogee", attrNameToString{ |
| 293 | "manifest": `"apogee_manifest.json"`, |
| 294 | }), |
| 295 | }}) |
Rupert Shuttleworth | a9d76dd | 2021-07-02 07:17:16 -0400 | [diff] [blame] | 296 | } |
Jingwen Chen | f59a8e1 | 2021-07-16 09:28:53 +0000 | [diff] [blame] | 297 | |
| 298 | func TestApexBundleHasBazelModuleProps(t *testing.T) { |
| 299 | runApexTestCase(t, bp2buildTestCase{ |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 300 | description: "apex - has bazel module props", |
| 301 | moduleTypeUnderTest: "apex", |
| 302 | moduleTypeUnderTestFactory: apex.BundleFactory, |
| 303 | filesystem: map[string]string{}, |
Jingwen Chen | f59a8e1 | 2021-07-16 09:28:53 +0000 | [diff] [blame] | 304 | blueprint: ` |
| 305 | apex { |
| 306 | name: "apogee", |
| 307 | manifest: "manifest.json", |
| 308 | bazel_module: { bp2build_available: true }, |
| 309 | } |
| 310 | `, |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 311 | expectedBazelTargets: []string{makeBazelTarget("apex", "apogee", attrNameToString{ |
| 312 | "manifest": `"manifest.json"`, |
| 313 | }), |
| 314 | }}) |
Jingwen Chen | f59a8e1 | 2021-07-16 09:28:53 +0000 | [diff] [blame] | 315 | } |
Yu Liu | 4ae55d1 | 2022-01-05 17:17:23 -0800 | [diff] [blame] | 316 | |
| 317 | func createMultilibBlueprint(compile_multilib string) string { |
| 318 | return ` |
| 319 | cc_library { |
| 320 | name: "native_shared_lib_1", |
| 321 | bazel_module: { bp2build_available: false }, |
| 322 | } |
| 323 | |
| 324 | cc_library { |
| 325 | name: "native_shared_lib_2", |
| 326 | bazel_module: { bp2build_available: false }, |
| 327 | } |
| 328 | |
| 329 | cc_library { |
| 330 | name: "native_shared_lib_3", |
| 331 | bazel_module: { bp2build_available: false }, |
| 332 | } |
| 333 | |
| 334 | cc_library { |
| 335 | name: "native_shared_lib_4", |
| 336 | bazel_module: { bp2build_available: false }, |
| 337 | } |
| 338 | |
| 339 | apex { |
| 340 | name: "com.android.apogee", |
| 341 | compile_multilib: "` + compile_multilib + `", |
| 342 | multilib: { |
| 343 | both: { |
| 344 | native_shared_libs: [ |
| 345 | "native_shared_lib_1", |
| 346 | ], |
| 347 | }, |
| 348 | first: { |
| 349 | native_shared_libs: [ |
| 350 | "native_shared_lib_2", |
| 351 | ], |
| 352 | }, |
| 353 | lib32: { |
| 354 | native_shared_libs: [ |
| 355 | "native_shared_lib_3", |
| 356 | ], |
| 357 | }, |
| 358 | lib64: { |
| 359 | native_shared_libs: [ |
| 360 | "native_shared_lib_4", |
| 361 | ], |
| 362 | }, |
| 363 | }, |
| 364 | }` |
| 365 | } |