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