image_test.go 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. // Copyright 2020 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 rust
  15. import (
  16. "strings"
  17. "testing"
  18. "android/soong/android"
  19. "android/soong/cc"
  20. )
  21. // Test that cc modules can link against vendor_available rust_ffi_static libraries.
  22. func TestVendorLinkage(t *testing.T) {
  23. ctx := testRustVndk(t, `
  24. cc_binary {
  25. name: "fizz_vendor",
  26. static_libs: ["libfoo_vendor"],
  27. soc_specific: true,
  28. }
  29. rust_ffi_static {
  30. name: "libfoo_vendor",
  31. crate_name: "foo",
  32. srcs: ["foo.rs"],
  33. vendor_available: true,
  34. }
  35. `)
  36. vendorBinary := ctx.ModuleForTests("fizz_vendor", "android_vendor.29_arm64_armv8-a").Module().(*cc.Module)
  37. if !android.InList("libfoo_vendor.vendor", vendorBinary.Properties.AndroidMkStaticLibs) {
  38. t.Errorf("vendorBinary should have a dependency on libfoo_vendor: %#v", vendorBinary.Properties.AndroidMkStaticLibs)
  39. }
  40. }
  41. // Test that variants which use the vndk emit the appropriate cfg flag.
  42. func TestImageVndkCfgFlag(t *testing.T) {
  43. ctx := testRustVndk(t, `
  44. rust_ffi_static {
  45. name: "libfoo",
  46. crate_name: "foo",
  47. srcs: ["foo.rs"],
  48. vendor_available: true,
  49. product_available: true,
  50. }
  51. `)
  52. vendor := ctx.ModuleForTests("libfoo", "android_vendor.29_arm64_armv8-a_static").Rule("rustc")
  53. if !strings.Contains(vendor.Args["rustcFlags"], "--cfg 'android_vndk'") {
  54. t.Errorf("missing \"--cfg 'android_vndk'\" for libfoo vendor variant, rustcFlags: %#v", vendor.Args["rustcFlags"])
  55. }
  56. if !strings.Contains(vendor.Args["rustcFlags"], "--cfg 'android_vendor'") {
  57. t.Errorf("missing \"--cfg 'android_vendor'\" for libfoo vendor variant, rustcFlags: %#v", vendor.Args["rustcFlags"])
  58. }
  59. if strings.Contains(vendor.Args["rustcFlags"], "--cfg 'android_product'") {
  60. t.Errorf("unexpected \"--cfg 'android_product'\" for libfoo vendor variant, rustcFlags: %#v", vendor.Args["rustcFlags"])
  61. }
  62. product := ctx.ModuleForTests("libfoo", "android_product.29_arm64_armv8-a_static").Rule("rustc")
  63. if !strings.Contains(product.Args["rustcFlags"], "--cfg 'android_vndk'") {
  64. t.Errorf("missing \"--cfg 'android_vndk'\" for libfoo product variant, rustcFlags: %#v", product.Args["rustcFlags"])
  65. }
  66. if strings.Contains(product.Args["rustcFlags"], "--cfg 'android_vendor'") {
  67. t.Errorf("unexpected \"--cfg 'android_vendor'\" for libfoo product variant, rustcFlags: %#v", product.Args["rustcFlags"])
  68. }
  69. if !strings.Contains(product.Args["rustcFlags"], "--cfg 'android_product'") {
  70. t.Errorf("missing \"--cfg 'android_product'\" for libfoo product variant, rustcFlags: %#v", product.Args["rustcFlags"])
  71. }
  72. system := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_static").Rule("rustc")
  73. if strings.Contains(system.Args["rustcFlags"], "--cfg 'android_vndk'") {
  74. t.Errorf("unexpected \"--cfg 'android_vndk'\" for libfoo system variant, rustcFlags: %#v", system.Args["rustcFlags"])
  75. }
  76. if strings.Contains(system.Args["rustcFlags"], "--cfg 'android_vendor'") {
  77. t.Errorf("unexpected \"--cfg 'android_vendor'\" for libfoo system variant, rustcFlags: %#v", system.Args["rustcFlags"])
  78. }
  79. if strings.Contains(system.Args["rustcFlags"], "--cfg 'android_product'") {
  80. t.Errorf("unexpected \"--cfg 'android_product'\" for libfoo system variant, rustcFlags: %#v", product.Args["rustcFlags"])
  81. }
  82. }
  83. // Test that cc modules can link against vendor_ramdisk_available rust_ffi_static libraries.
  84. func TestVendorRamdiskLinkage(t *testing.T) {
  85. ctx := testRustVndk(t, `
  86. cc_library_static {
  87. name: "libcc_vendor_ramdisk",
  88. static_libs: ["libfoo_vendor_ramdisk"],
  89. system_shared_libs: [],
  90. vendor_ramdisk_available: true,
  91. }
  92. rust_ffi_static {
  93. name: "libfoo_vendor_ramdisk",
  94. crate_name: "foo",
  95. srcs: ["foo.rs"],
  96. vendor_ramdisk_available: true,
  97. }
  98. `)
  99. vendorRamdiskLibrary := ctx.ModuleForTests("libcc_vendor_ramdisk", "android_vendor_ramdisk_arm64_armv8-a_static").Module().(*cc.Module)
  100. if !android.InList("libfoo_vendor_ramdisk.vendor_ramdisk", vendorRamdiskLibrary.Properties.AndroidMkStaticLibs) {
  101. t.Errorf("libcc_vendor_ramdisk should have a dependency on libfoo_vendor_ramdisk")
  102. }
  103. }
  104. // Test that prebuilt libraries cannot be made vendor available.
  105. func TestForbiddenVendorLinkage(t *testing.T) {
  106. testRustVndkError(t, "Rust prebuilt modules not supported for non-system images.", `
  107. rust_prebuilt_library {
  108. name: "librust_prebuilt",
  109. crate_name: "rust_prebuilt",
  110. rlib: {
  111. srcs: ["libtest.rlib"],
  112. },
  113. dylib: {
  114. srcs: ["libtest.so"],
  115. },
  116. vendor: true,
  117. }
  118. `)
  119. }
  120. func checkInstallPartition(t *testing.T, ctx *android.TestContext, name, variant, expected string) {
  121. mod := ctx.ModuleForTests(name, variant).Module().(*Module)
  122. partitionDefined := false
  123. checkPartition := func(specific bool, partition string) {
  124. if specific {
  125. if expected != partition && !partitionDefined {
  126. // The variant is installed to the 'partition'
  127. t.Errorf("%s variant of %q must not be installed to %s partition", variant, name, partition)
  128. }
  129. partitionDefined = true
  130. } else {
  131. // The variant is not installed to the 'partition'
  132. if expected == partition {
  133. t.Errorf("%s variant of %q must be installed to %s partition", variant, name, partition)
  134. }
  135. }
  136. }
  137. socSpecific := func(m *Module) bool {
  138. return m.SocSpecific()
  139. }
  140. deviceSpecific := func(m *Module) bool {
  141. return m.DeviceSpecific()
  142. }
  143. productSpecific := func(m *Module) bool {
  144. return m.ProductSpecific() || m.productSpecificModuleContext()
  145. }
  146. systemExtSpecific := func(m *Module) bool {
  147. return m.SystemExtSpecific()
  148. }
  149. checkPartition(socSpecific(mod), "vendor")
  150. checkPartition(deviceSpecific(mod), "odm")
  151. checkPartition(productSpecific(mod), "product")
  152. checkPartition(systemExtSpecific(mod), "system_ext")
  153. if !partitionDefined && expected != "system" {
  154. t.Errorf("%s variant of %q is expected to be installed to %s partition,"+
  155. " but installed to system partition", variant, name, expected)
  156. }
  157. }
  158. func TestInstallPartition(t *testing.T) {
  159. t.Parallel()
  160. t.Helper()
  161. ctx := testRust(t, `
  162. rust_binary {
  163. name: "sample_system",
  164. crate_name: "sample",
  165. srcs: ["foo.rs"],
  166. }
  167. rust_binary {
  168. name: "sample_system_ext",
  169. crate_name: "sample",
  170. srcs: ["foo.rs"],
  171. system_ext_specific: true,
  172. }
  173. rust_binary {
  174. name: "sample_product",
  175. crate_name: "sample",
  176. srcs: ["foo.rs"],
  177. product_specific: true,
  178. }
  179. rust_binary {
  180. name: "sample_vendor",
  181. crate_name: "sample",
  182. srcs: ["foo.rs"],
  183. vendor: true,
  184. }
  185. rust_binary {
  186. name: "sample_odm",
  187. crate_name: "sample",
  188. srcs: ["foo.rs"],
  189. device_specific: true,
  190. }
  191. rust_binary {
  192. name: "sample_all_available",
  193. crate_name: "sample",
  194. srcs: ["foo.rs"],
  195. vendor_available: true,
  196. product_available: true,
  197. }
  198. `)
  199. checkInstallPartition(t, ctx, "sample_system", binaryCoreVariant, "system")
  200. checkInstallPartition(t, ctx, "sample_system_ext", binaryCoreVariant, "system_ext")
  201. checkInstallPartition(t, ctx, "sample_product", binaryProductVariant, "product")
  202. checkInstallPartition(t, ctx, "sample_vendor", binaryVendorVariant, "vendor")
  203. checkInstallPartition(t, ctx, "sample_odm", binaryVendorVariant, "odm")
  204. checkInstallPartition(t, ctx, "sample_all_available", binaryCoreVariant, "system")
  205. }