testing.go 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810
  1. // Copyright (C) 2019 The Android Open Source Project
  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. package cc
  15. import (
  16. "encoding/json"
  17. "path/filepath"
  18. "testing"
  19. "android/soong/android"
  20. "android/soong/genrule"
  21. "android/soong/multitree"
  22. "android/soong/snapshot"
  23. )
  24. func RegisterRequiredBuildComponentsForTest(ctx android.RegistrationContext) {
  25. RegisterPrebuiltBuildComponents(ctx)
  26. RegisterCCBuildComponents(ctx)
  27. RegisterBinaryBuildComponents(ctx)
  28. RegisterLibraryBuildComponents(ctx)
  29. RegisterLibraryHeadersBuildComponents(ctx)
  30. RegisterLibraryStubBuildComponents(ctx)
  31. multitree.RegisterApiImportsModule(ctx)
  32. ctx.RegisterModuleType("cc_benchmark", BenchmarkFactory)
  33. ctx.RegisterModuleType("cc_object", ObjectFactory)
  34. ctx.RegisterModuleType("cc_genrule", GenRuleFactory)
  35. ctx.RegisterModuleType("ndk_prebuilt_shared_stl", NdkPrebuiltSharedStlFactory)
  36. ctx.RegisterModuleType("ndk_prebuilt_static_stl", NdkPrebuiltStaticStlFactory)
  37. ctx.RegisterModuleType("ndk_prebuilt_object", NdkPrebuiltObjectFactory)
  38. ctx.RegisterModuleType("ndk_library", NdkLibraryFactory)
  39. ctx.RegisterModuleType("ndk_headers", ndkHeadersFactory)
  40. }
  41. func GatherRequiredDepsForTest(oses ...android.OsType) string {
  42. ret := commonDefaultModules()
  43. supportLinuxBionic := false
  44. for _, os := range oses {
  45. if os == android.Windows {
  46. ret += withWindowsModules()
  47. }
  48. if os == android.LinuxBionic {
  49. supportLinuxBionic = true
  50. ret += withLinuxBionic()
  51. }
  52. }
  53. if !supportLinuxBionic {
  54. ret += withoutLinuxBionic()
  55. }
  56. return ret
  57. }
  58. func commonDefaultModules() string {
  59. return `
  60. cc_defaults {
  61. name: "toolchain_libs_defaults",
  62. host_supported: true,
  63. vendor_available: true,
  64. product_available: true,
  65. recovery_available: true,
  66. no_libcrt: true,
  67. sdk_version: "minimum",
  68. nocrt: true,
  69. system_shared_libs: [],
  70. stl: "none",
  71. check_elf_files: false,
  72. sanitize: {
  73. never: true,
  74. },
  75. }
  76. cc_prebuilt_library_static {
  77. name: "libcompiler_rt-extras",
  78. defaults: ["toolchain_libs_defaults"],
  79. vendor_ramdisk_available: true,
  80. srcs: [""],
  81. }
  82. cc_prebuilt_library_static {
  83. name: "libclang_rt.builtins",
  84. defaults: ["toolchain_libs_defaults"],
  85. host_supported: true,
  86. vendor_available: true,
  87. vendor_ramdisk_available: true,
  88. native_bridge_supported: true,
  89. srcs: [""],
  90. }
  91. cc_prebuilt_library_shared {
  92. name: "libclang_rt.hwasan",
  93. defaults: ["toolchain_libs_defaults"],
  94. srcs: [""],
  95. }
  96. cc_prebuilt_library_static {
  97. name: "libunwind",
  98. defaults: [
  99. "linux_bionic_supported",
  100. "toolchain_libs_defaults",
  101. ],
  102. vendor_ramdisk_available: true,
  103. native_bridge_supported: true,
  104. srcs: [""],
  105. }
  106. cc_prebuilt_library_static {
  107. name: "libclang_rt.fuzzer",
  108. defaults: [
  109. "linux_bionic_supported",
  110. "toolchain_libs_defaults",
  111. ],
  112. srcs: [""],
  113. }
  114. // Needed for sanitizer
  115. cc_prebuilt_library_shared {
  116. name: "libclang_rt.ubsan_standalone",
  117. defaults: ["toolchain_libs_defaults"],
  118. srcs: [""],
  119. }
  120. cc_prebuilt_library_static {
  121. name: "libclang_rt.ubsan_standalone.static",
  122. defaults: ["toolchain_libs_defaults"],
  123. srcs: [""],
  124. }
  125. cc_prebuilt_library_static {
  126. name: "libclang_rt.ubsan_minimal",
  127. defaults: ["toolchain_libs_defaults"],
  128. host_supported: true,
  129. target: {
  130. android_arm64: {
  131. srcs: ["libclang_rt.ubsan_minimal.android_arm64.a"],
  132. },
  133. android_arm: {
  134. srcs: ["libclang_rt.ubsan_minimal.android_arm.a"],
  135. },
  136. linux_glibc_x86_64: {
  137. srcs: ["libclang_rt.ubsan_minimal.x86_64.a"],
  138. },
  139. linux_glibc_x86: {
  140. srcs: ["libclang_rt.ubsan_minimal.x86.a"],
  141. },
  142. linux_musl_x86_64: {
  143. srcs: ["libclang_rt.ubsan_minimal.x86_64.a"],
  144. },
  145. linux_musl_x86: {
  146. srcs: ["libclang_rt.ubsan_minimal.x86.a"],
  147. },
  148. },
  149. }
  150. cc_library {
  151. name: "libc",
  152. defaults: ["linux_bionic_supported"],
  153. no_libcrt: true,
  154. nocrt: true,
  155. stl: "none",
  156. system_shared_libs: [],
  157. recovery_available: true,
  158. stubs: {
  159. versions: ["27", "28", "29"],
  160. },
  161. llndk: {
  162. symbol_file: "libc.map.txt",
  163. },
  164. }
  165. cc_library {
  166. name: "libm",
  167. defaults: ["linux_bionic_supported"],
  168. no_libcrt: true,
  169. nocrt: true,
  170. stl: "none",
  171. system_shared_libs: [],
  172. recovery_available: true,
  173. stubs: {
  174. versions: ["27", "28", "29"],
  175. },
  176. apex_available: [
  177. "//apex_available:platform",
  178. "myapex"
  179. ],
  180. llndk: {
  181. symbol_file: "libm.map.txt",
  182. },
  183. }
  184. // Coverage libraries
  185. cc_library {
  186. name: "libprofile-extras",
  187. vendor_available: true,
  188. vendor_ramdisk_available: true,
  189. product_available: true,
  190. recovery_available: true,
  191. native_coverage: false,
  192. system_shared_libs: [],
  193. stl: "none",
  194. }
  195. cc_library {
  196. name: "libprofile-clang-extras",
  197. vendor_available: true,
  198. vendor_ramdisk_available: true,
  199. product_available: true,
  200. recovery_available: true,
  201. native_coverage: false,
  202. system_shared_libs: [],
  203. stl: "none",
  204. }
  205. cc_library {
  206. name: "libprofile-extras_ndk",
  207. vendor_available: true,
  208. product_available: true,
  209. native_coverage: false,
  210. system_shared_libs: [],
  211. stl: "none",
  212. sdk_version: "current",
  213. }
  214. cc_library {
  215. name: "libprofile-clang-extras_ndk",
  216. vendor_available: true,
  217. product_available: true,
  218. native_coverage: false,
  219. system_shared_libs: [],
  220. stl: "none",
  221. sdk_version: "current",
  222. }
  223. cc_library {
  224. name: "libdl",
  225. defaults: ["linux_bionic_supported"],
  226. no_libcrt: true,
  227. nocrt: true,
  228. stl: "none",
  229. system_shared_libs: [],
  230. recovery_available: true,
  231. stubs: {
  232. versions: ["27", "28", "29"],
  233. },
  234. apex_available: [
  235. "//apex_available:platform",
  236. "myapex"
  237. ],
  238. llndk: {
  239. symbol_file: "libdl.map.txt",
  240. },
  241. }
  242. cc_library {
  243. name: "libft2",
  244. no_libcrt: true,
  245. nocrt: true,
  246. system_shared_libs: [],
  247. recovery_available: true,
  248. llndk: {
  249. symbol_file: "libft2.map.txt",
  250. private: true,
  251. }
  252. }
  253. cc_library {
  254. name: "libc++_static",
  255. no_libcrt: true,
  256. nocrt: true,
  257. system_shared_libs: [],
  258. stl: "none",
  259. vendor_available: true,
  260. vendor_ramdisk_available: true,
  261. product_available: true,
  262. recovery_available: true,
  263. host_supported: true,
  264. min_sdk_version: "29",
  265. apex_available: [
  266. "//apex_available:platform",
  267. "//apex_available:anyapex",
  268. ],
  269. }
  270. cc_library {
  271. name: "libc++",
  272. no_libcrt: true,
  273. nocrt: true,
  274. system_shared_libs: [],
  275. stl: "none",
  276. vendor_available: true,
  277. product_available: true,
  278. recovery_available: true,
  279. host_supported: true,
  280. min_sdk_version: "29",
  281. vndk: {
  282. enabled: true,
  283. support_system_process: true,
  284. },
  285. apex_available: [
  286. "//apex_available:platform",
  287. "//apex_available:anyapex",
  288. ],
  289. }
  290. cc_library {
  291. name: "libc++demangle",
  292. no_libcrt: true,
  293. nocrt: true,
  294. system_shared_libs: [],
  295. stl: "none",
  296. host_supported: false,
  297. vendor_available: true,
  298. vendor_ramdisk_available: true,
  299. product_available: true,
  300. recovery_available: true,
  301. min_sdk_version: "29",
  302. apex_available: [
  303. "//apex_available:platform",
  304. "//apex_available:anyapex",
  305. ],
  306. }
  307. cc_defaults {
  308. name: "crt_defaults",
  309. defaults: ["linux_bionic_supported"],
  310. recovery_available: true,
  311. vendor_available: true,
  312. vendor_ramdisk_available: true,
  313. product_available: true,
  314. native_bridge_supported: true,
  315. stl: "none",
  316. min_sdk_version: "16",
  317. crt: true,
  318. system_shared_libs: [],
  319. apex_available: [
  320. "//apex_available:platform",
  321. "//apex_available:anyapex",
  322. ],
  323. }
  324. cc_object {
  325. name: "crtbegin_so",
  326. defaults: ["crt_defaults"],
  327. srcs: ["crtbegin_so.c"],
  328. objs: ["crtbrand"],
  329. }
  330. cc_object {
  331. name: "crtbegin_dynamic",
  332. defaults: ["crt_defaults"],
  333. srcs: ["crtbegin.c"],
  334. objs: ["crtbrand"],
  335. }
  336. cc_object {
  337. name: "crtbegin_static",
  338. defaults: ["crt_defaults"],
  339. srcs: ["crtbegin.c"],
  340. objs: ["crtbrand"],
  341. }
  342. cc_object {
  343. name: "crtend_so",
  344. defaults: ["crt_defaults"],
  345. srcs: ["crtend_so.c"],
  346. objs: ["crtbrand"],
  347. }
  348. cc_object {
  349. name: "crtend_android",
  350. defaults: ["crt_defaults"],
  351. srcs: ["crtend.c"],
  352. objs: ["crtbrand"],
  353. }
  354. cc_object {
  355. name: "crtbrand",
  356. defaults: ["crt_defaults"],
  357. srcs: ["crtbrand.c"],
  358. }
  359. cc_library {
  360. name: "libprotobuf-cpp-lite",
  361. }
  362. cc_library {
  363. name: "ndk_libunwind",
  364. sdk_version: "minimum",
  365. stl: "none",
  366. system_shared_libs: [],
  367. }
  368. ndk_library {
  369. name: "libc",
  370. first_version: "minimum",
  371. symbol_file: "libc.map.txt",
  372. }
  373. ndk_library {
  374. name: "libm",
  375. first_version: "minimum",
  376. symbol_file: "libm.map.txt",
  377. }
  378. ndk_library {
  379. name: "libdl",
  380. first_version: "minimum",
  381. symbol_file: "libdl.map.txt",
  382. }
  383. ndk_prebuilt_shared_stl {
  384. name: "ndk_libc++_shared",
  385. export_include_dirs: ["ndk_libc++_shared"],
  386. }
  387. ndk_prebuilt_static_stl {
  388. name: "ndk_libandroid_support",
  389. export_include_dirs: ["ndk_libandroid_support"],
  390. }
  391. cc_library_static {
  392. name: "libgoogle-benchmark",
  393. sdk_version: "current",
  394. stl: "none",
  395. system_shared_libs: [],
  396. }
  397. cc_library_static {
  398. name: "note_memtag_heap_async",
  399. }
  400. cc_library_static {
  401. name: "note_memtag_heap_sync",
  402. }
  403. cc_library {
  404. name: "libc_musl",
  405. host_supported: true,
  406. no_libcrt: true,
  407. nocrt: true,
  408. system_shared_libs: [],
  409. stl: "none",
  410. }
  411. `
  412. }
  413. func withWindowsModules() string {
  414. return `
  415. cc_prebuilt_library_static {
  416. name: "libwinpthread",
  417. host_supported: true,
  418. enabled: false,
  419. target: {
  420. windows: {
  421. enabled: true,
  422. },
  423. },
  424. stl: "none",
  425. srcs:[""],
  426. }
  427. `
  428. }
  429. func withLinuxBionic() string {
  430. return `
  431. cc_binary {
  432. name: "linker",
  433. defaults: ["linux_bionic_supported"],
  434. recovery_available: true,
  435. stl: "none",
  436. nocrt: true,
  437. static_executable: true,
  438. native_coverage: false,
  439. system_shared_libs: [],
  440. }
  441. cc_genrule {
  442. name: "host_bionic_linker_script",
  443. host_supported: true,
  444. device_supported: false,
  445. target: {
  446. host: {
  447. enabled: false,
  448. },
  449. linux_bionic: {
  450. enabled: true,
  451. },
  452. },
  453. out: ["linker.script"],
  454. }
  455. cc_defaults {
  456. name: "linux_bionic_supported",
  457. host_supported: true,
  458. target: {
  459. host: {
  460. enabled: false,
  461. },
  462. linux_bionic: {
  463. enabled: true,
  464. },
  465. },
  466. }
  467. `
  468. }
  469. func withoutLinuxBionic() string {
  470. return `
  471. cc_defaults {
  472. name: "linux_bionic_supported",
  473. }
  474. `
  475. }
  476. func GatherRequiredFilesForTest(fs map[string][]byte) {
  477. }
  478. // The directory in which cc linux bionic default modules will be defined.
  479. //
  480. // Placing them here ensures that their location does not conflict with default test modules
  481. // defined by other packages.
  482. const linuxBionicDefaultsPath = "defaults/cc/linux-bionic/Android.bp"
  483. // The directory in which the default cc common test modules will be defined.
  484. //
  485. // Placing them here ensures that their location does not conflict with default test modules
  486. // defined by other packages.
  487. const DefaultCcCommonTestModulesDir = "defaults/cc/common/"
  488. // Test fixture preparer that will register most cc build components.
  489. //
  490. // Singletons and mutators should only be added here if they are needed for a majority of cc
  491. // module types, otherwise they should be added under a separate preparer to allow them to be
  492. // selected only when needed to reduce test execution time.
  493. //
  494. // Module types do not have much of an overhead unless they are used so this should include as many
  495. // module types as possible. The exceptions are those module types that require mutators and/or
  496. // singletons in order to function in which case they should be kept together in a separate
  497. // preparer.
  498. var PrepareForTestWithCcBuildComponents = android.GroupFixturePreparers(
  499. android.PrepareForTestWithAndroidBuildComponents,
  500. android.FixtureRegisterWithContext(RegisterRequiredBuildComponentsForTest),
  501. android.FixtureRegisterWithContext(func(ctx android.RegistrationContext) {
  502. ctx.RegisterModuleType("cc_fuzz", LibFuzzFactory)
  503. ctx.RegisterModuleType("cc_test", TestFactory)
  504. ctx.RegisterModuleType("cc_test_library", TestLibraryFactory)
  505. ctx.RegisterModuleType("vndk_prebuilt_shared", VndkPrebuiltSharedFactory)
  506. RegisterVndkLibraryTxtTypes(ctx)
  507. }),
  508. // Additional files needed in tests that disallow non-existent source files.
  509. // This includes files that are needed by all, or at least most, instances of a cc module type.
  510. android.MockFS{
  511. // Needed for ndk_prebuilt_(shared|static)_stl.
  512. "prebuilts/ndk/current/sources/cxx-stl/llvm-libc++/libs": nil,
  513. }.AddToFixture(),
  514. )
  515. // Preparer that will define default cc modules, e.g. standard prebuilt modules.
  516. var PrepareForTestWithCcDefaultModules = android.GroupFixturePreparers(
  517. PrepareForTestWithCcBuildComponents,
  518. // Additional files needed in tests that disallow non-existent source.
  519. android.MockFS{
  520. "defaults/cc/common/libc.map.txt": nil,
  521. "defaults/cc/common/libdl.map.txt": nil,
  522. "defaults/cc/common/libm.map.txt": nil,
  523. "defaults/cc/common/ndk_libandroid_support": nil,
  524. "defaults/cc/common/ndk_libc++_shared": nil,
  525. "defaults/cc/common/crtbegin_so.c": nil,
  526. "defaults/cc/common/crtbegin.c": nil,
  527. "defaults/cc/common/crtend_so.c": nil,
  528. "defaults/cc/common/crtend.c": nil,
  529. "defaults/cc/common/crtbrand.c": nil,
  530. "defaults/cc/common/libclang_rt.ubsan_minimal.android_arm64.a": nil,
  531. "defaults/cc/common/libclang_rt.ubsan_minimal.android_arm.a": nil,
  532. "defaults/cc/common/libclang_rt.ubsan_minimal.x86_64.a": nil,
  533. "defaults/cc/common/libclang_rt.ubsan_minimal.x86.a": nil,
  534. }.AddToFixture(),
  535. // Place the default cc test modules that are common to all platforms in a location that will not
  536. // conflict with default test modules defined by other packages.
  537. android.FixtureAddTextFile(DefaultCcCommonTestModulesDir+"Android.bp", commonDefaultModules()),
  538. // Disable linux bionic by default.
  539. android.FixtureAddTextFile(linuxBionicDefaultsPath, withoutLinuxBionic()),
  540. )
  541. // Prepare a fixture to use all cc module types, mutators and singletons fully.
  542. //
  543. // This should only be used by tests that want to run with as much of the build enabled as possible.
  544. var PrepareForIntegrationTestWithCc = android.GroupFixturePreparers(
  545. android.PrepareForIntegrationTestWithAndroid,
  546. genrule.PrepareForIntegrationTestWithGenrule,
  547. PrepareForTestWithCcDefaultModules,
  548. )
  549. // The preparer to include if running a cc related test for windows.
  550. var PrepareForTestOnWindows = android.GroupFixturePreparers(
  551. // Place the default cc test modules for windows platforms in a location that will not conflict
  552. // with default test modules defined by other packages.
  553. android.FixtureAddTextFile("defaults/cc/windows/Android.bp", withWindowsModules()),
  554. )
  555. // The preparer to include if running a cc related test for linux bionic.
  556. var PrepareForTestOnLinuxBionic = android.GroupFixturePreparers(
  557. // Enable linux bionic
  558. //
  559. // Can be used after PrepareForTestWithCcDefaultModules to override its default behavior of
  560. // disabling linux bionic, hence why this uses FixtureOverrideTextFile.
  561. android.FixtureOverrideTextFile(linuxBionicDefaultsPath, withLinuxBionic()),
  562. )
  563. // This adds some additional modules and singletons which might negatively impact the performance
  564. // of tests so they are not included in the PrepareForIntegrationTestWithCc.
  565. var PrepareForTestWithCcIncludeVndk = android.GroupFixturePreparers(
  566. PrepareForIntegrationTestWithCc,
  567. android.FixtureRegisterWithContext(func(ctx android.RegistrationContext) {
  568. snapshot.VendorSnapshotImageSingleton.Init(ctx)
  569. snapshot.RecoverySnapshotImageSingleton.Init(ctx)
  570. RegisterVendorSnapshotModules(ctx)
  571. RegisterRecoverySnapshotModules(ctx)
  572. ctx.RegisterSingletonType("vndk-snapshot", VndkSnapshotSingleton)
  573. }),
  574. )
  575. // PrepareForTestWithHostMusl sets the host configuration to musl libc instead of glibc. It also disables the test
  576. // on mac, which doesn't support musl libc, and adds musl modules.
  577. var PrepareForTestWithHostMusl = android.GroupFixturePreparers(
  578. android.FixtureModifyConfig(android.ModifyTestConfigForMusl),
  579. android.PrepareForSkipTestOnMac,
  580. android.FixtureAddTextFile("external/musl/Android.bp", `
  581. cc_defaults {
  582. name: "libc_musl_crt_defaults",
  583. host_supported: true,
  584. device_supported: false,
  585. }
  586. cc_object {
  587. name: "libc_musl_crtbegin_so",
  588. defaults: ["libc_musl_crt_defaults"],
  589. }
  590. cc_object {
  591. name: "libc_musl_crtend_so",
  592. defaults: ["libc_musl_crt_defaults"],
  593. }
  594. cc_object {
  595. name: "libc_musl_crtbegin_dynamic",
  596. defaults: ["libc_musl_crt_defaults"],
  597. }
  598. cc_object {
  599. name: "libc_musl_crtbegin_static",
  600. defaults: ["libc_musl_crt_defaults"],
  601. }
  602. cc_object {
  603. name: "libc_musl_crtend",
  604. defaults: ["libc_musl_crt_defaults"],
  605. }
  606. `),
  607. )
  608. // TestConfig is the legacy way of creating a test Config for testing cc modules.
  609. //
  610. // See testCc for an explanation as to how to stop using this deprecated method.
  611. //
  612. // deprecated
  613. func TestConfig(buildDir string, os android.OsType, env map[string]string,
  614. bp string, fs map[string][]byte) android.Config {
  615. // add some modules that are required by the compiler and/or linker
  616. bp = bp + GatherRequiredDepsForTest(os)
  617. mockFS := map[string][]byte{}
  618. GatherRequiredFilesForTest(mockFS)
  619. for k, v := range fs {
  620. mockFS[k] = v
  621. }
  622. return android.TestArchConfig(buildDir, env, bp, mockFS)
  623. }
  624. // CreateTestContext is the legacy way of creating a TestContext for testing cc modules.
  625. //
  626. // See testCc for an explanation as to how to stop using this deprecated method.
  627. //
  628. // deprecated
  629. func CreateTestContext(config android.Config) *android.TestContext {
  630. ctx := android.NewTestArchContext(config)
  631. genrule.RegisterGenruleBuildComponents(ctx)
  632. ctx.RegisterModuleType("cc_fuzz", LibFuzzFactory)
  633. ctx.RegisterModuleType("cc_test", TestFactory)
  634. ctx.RegisterModuleType("cc_test_library", TestLibraryFactory)
  635. ctx.RegisterModuleType("filegroup", android.FileGroupFactory)
  636. ctx.RegisterModuleType("vndk_prebuilt_shared", VndkPrebuiltSharedFactory)
  637. snapshot.VendorSnapshotImageSingleton.Init(ctx)
  638. snapshot.RecoverySnapshotImageSingleton.Init(ctx)
  639. RegisterVendorSnapshotModules(ctx)
  640. RegisterRecoverySnapshotModules(ctx)
  641. ctx.RegisterSingletonType("vndk-snapshot", VndkSnapshotSingleton)
  642. RegisterVndkLibraryTxtTypes(ctx)
  643. ctx.PreArchMutators(android.RegisterDefaultsPreArchMutators)
  644. android.RegisterPrebuiltMutators(ctx)
  645. RegisterRequiredBuildComponentsForTest(ctx)
  646. return ctx
  647. }
  648. func checkSnapshotIncludeExclude(t *testing.T, ctx *android.TestContext, singleton android.TestingSingleton, moduleName, snapshotFilename, subDir, variant string, include bool, fake bool) {
  649. t.Helper()
  650. mod := ctx.ModuleForTests(moduleName, variant)
  651. outputFiles := mod.OutputFiles(t, "")
  652. if len(outputFiles) != 1 {
  653. t.Errorf("%q must have single output\n", moduleName)
  654. return
  655. }
  656. snapshotPath := filepath.Join(subDir, snapshotFilename)
  657. if include {
  658. out := singleton.Output(snapshotPath)
  659. if fake {
  660. if out.Rule == nil {
  661. t.Errorf("Missing rule for module %q output file %q", moduleName, outputFiles[0])
  662. }
  663. } else {
  664. if out.Input.String() != outputFiles[0].String() {
  665. t.Errorf("The input of snapshot %q must be %q, but %q", moduleName, out.Input.String(), outputFiles[0])
  666. }
  667. }
  668. } else {
  669. out := singleton.MaybeOutput(snapshotPath)
  670. if out.Rule != nil {
  671. t.Errorf("There must be no rule for module %q output file %q", moduleName, outputFiles[0])
  672. }
  673. }
  674. }
  675. func CheckSnapshot(t *testing.T, ctx *android.TestContext, singleton android.TestingSingleton, moduleName, snapshotFilename, subDir, variant string) {
  676. t.Helper()
  677. checkSnapshotIncludeExclude(t, ctx, singleton, moduleName, snapshotFilename, subDir, variant, true, false)
  678. }
  679. func CheckSnapshotExclude(t *testing.T, ctx *android.TestContext, singleton android.TestingSingleton, moduleName, snapshotFilename, subDir, variant string) {
  680. t.Helper()
  681. checkSnapshotIncludeExclude(t, ctx, singleton, moduleName, snapshotFilename, subDir, variant, false, false)
  682. }
  683. func CheckSnapshotRule(t *testing.T, ctx *android.TestContext, singleton android.TestingSingleton, moduleName, snapshotFilename, subDir, variant string) {
  684. t.Helper()
  685. checkSnapshotIncludeExclude(t, ctx, singleton, moduleName, snapshotFilename, subDir, variant, true, true)
  686. }
  687. func AssertExcludeFromVendorSnapshotIs(t *testing.T, ctx *android.TestContext, name string, expected bool, variant string) {
  688. t.Helper()
  689. m := ctx.ModuleForTests(name, variant).Module().(LinkableInterface)
  690. if m.ExcludeFromVendorSnapshot() != expected {
  691. t.Errorf("expected %q ExcludeFromVendorSnapshot to be %t", m.String(), expected)
  692. }
  693. }
  694. func GetOutputPaths(ctx *android.TestContext, variant string, moduleNames []string) (paths android.Paths) {
  695. for _, moduleName := range moduleNames {
  696. module := ctx.ModuleForTests(moduleName, variant).Module().(*Module)
  697. output := module.outputFile.Path().RelativeToTop()
  698. paths = append(paths, output)
  699. }
  700. return paths
  701. }
  702. func AssertExcludeFromRecoverySnapshotIs(t *testing.T, ctx *android.TestContext, name string, expected bool, variant string) {
  703. t.Helper()
  704. m := ctx.ModuleForTests(name, variant).Module().(LinkableInterface)
  705. if m.ExcludeFromRecoverySnapshot() != expected {
  706. t.Errorf("expected %q ExcludeFromRecoverySnapshot to be %t", m.String(), expected)
  707. }
  708. }
  709. func checkOverrides(t *testing.T, ctx *android.TestContext, singleton android.TestingSingleton, jsonPath string, expected []string) {
  710. t.Helper()
  711. out := singleton.MaybeOutput(jsonPath)
  712. content := android.ContentFromFileRuleForTests(t, out)
  713. var flags snapshotJsonFlags
  714. if err := json.Unmarshal([]byte(content), &flags); err != nil {
  715. t.Errorf("Error while unmarshalling json %q: %s", jsonPath, err.Error())
  716. return
  717. }
  718. for _, moduleName := range expected {
  719. if !android.InList(moduleName, flags.Overrides) {
  720. t.Errorf("expected %q to be in %q: %q", moduleName, flags.Overrides, content)
  721. return
  722. }
  723. }
  724. }