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