testing.go 22 KB

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