blob: d84eb10c5612c6ed80ca632c06f3b69c19800a27 [file] [log] [blame]
Ivan Lozano6a884432020-12-02 09:15:16 -05001// 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
15package rust
16
17import (
Ivan Lozanof76cdf72021-02-12 09:55:06 -050018 "strings"
Ivan Lozano6a884432020-12-02 09:15:16 -050019 "testing"
20
21 "android/soong/android"
22 "android/soong/cc"
23)
24
Ivan Lozanofd47b1a2024-05-17 14:13:41 -040025// Test that cc modules can depend on vendor_available rust_ffi_rlib/rust_ffi_static libraries.
Ivan Lozano6a884432020-12-02 09:15:16 -050026func TestVendorLinkage(t *testing.T) {
Kiyoung Kim1db4a742024-04-01 15:50:01 +090027 ctx := testRust(t, `
Ivan Lozano6a884432020-12-02 09:15:16 -050028 cc_binary {
Ivan Lozano0a468a42024-05-13 21:03:34 -040029 name: "fizz_vendor_available",
Ivan Lozanofd47b1a2024-05-17 14:13:41 -040030 static_libs: [
31 "libfoo_vendor",
32 "libfoo_vendor_static"
33 ],
Ivan Lozano0a468a42024-05-13 21:03:34 -040034 vendor_available: true,
35 }
36 cc_binary {
37 name: "fizz_soc_specific",
Ivan Lozanofd47b1a2024-05-17 14:13:41 -040038 static_libs: ["libfoo_vendor"],
Ivan Lozano6a884432020-12-02 09:15:16 -050039 soc_specific: true,
40 }
Ivan Lozano0a468a42024-05-13 21:03:34 -040041 rust_ffi_rlib {
Ivan Lozano6a884432020-12-02 09:15:16 -050042 name: "libfoo_vendor",
43 crate_name: "foo",
44 srcs: ["foo.rs"],
45 vendor_available: true,
46 }
Ivan Lozano0a468a42024-05-13 21:03:34 -040047 rust_ffi_static {
48 name: "libfoo_vendor_static",
49 crate_name: "foo",
50 srcs: ["foo.rs"],
51 vendor_available: true,
52 }
Ivan Lozano6a884432020-12-02 09:15:16 -050053 `)
54
Ivan Lozano0a468a42024-05-13 21:03:34 -040055 vendorBinary := ctx.ModuleForTests("fizz_vendor_available", "android_vendor_arm64_armv8-a").Module().(*cc.Module)
Ivan Lozano6a884432020-12-02 09:15:16 -050056
Ivan Lozanofd47b1a2024-05-17 14:13:41 -040057 if android.InList("libfoo_vendor_static.vendor", vendorBinary.Properties.AndroidMkStaticLibs) {
58 t.Errorf("vendorBinary should not have a staticlib dependency on libfoo_vendor_static.vendor: %#v", vendorBinary.Properties.AndroidMkStaticLibs)
Ivan Lozano6a884432020-12-02 09:15:16 -050059 }
60}
61
Ivan Lozanof76cdf72021-02-12 09:55:06 -050062// Test that variants which use the vndk emit the appropriate cfg flag.
Kiyoung Kim1db4a742024-04-01 15:50:01 +090063func TestImageCfgFlag(t *testing.T) {
64 ctx := testRust(t, `
Ivan Lozano0a468a42024-05-13 21:03:34 -040065 rust_ffi_shared {
Ivan Lozanof76cdf72021-02-12 09:55:06 -050066 name: "libfoo",
67 crate_name: "foo",
68 srcs: ["foo.rs"],
69 vendor_available: true,
Matthew Maurer65a54a82023-02-24 19:19:22 +000070 product_available: true,
Ivan Lozanof76cdf72021-02-12 09:55:06 -050071 }
72 `)
73
Ivan Lozano0a468a42024-05-13 21:03:34 -040074 vendor := ctx.ModuleForTests("libfoo", "android_vendor_arm64_armv8-a_shared").Rule("rustc")
Ivan Lozanof76cdf72021-02-12 09:55:06 -050075
Wen-yi Chu41326c12023-09-22 03:58:59 +000076 if !strings.Contains(vendor.Args["rustcFlags"], "--cfg 'android_vndk'") {
77 t.Errorf("missing \"--cfg 'android_vndk'\" for libfoo vendor variant, rustcFlags: %#v", vendor.Args["rustcFlags"])
Ivan Lozanof76cdf72021-02-12 09:55:06 -050078 }
Wen-yi Chu41326c12023-09-22 03:58:59 +000079 if !strings.Contains(vendor.Args["rustcFlags"], "--cfg 'android_vendor'") {
80 t.Errorf("missing \"--cfg 'android_vendor'\" for libfoo vendor variant, rustcFlags: %#v", vendor.Args["rustcFlags"])
Matthew Maurer65a54a82023-02-24 19:19:22 +000081 }
Wen-yi Chu41326c12023-09-22 03:58:59 +000082 if strings.Contains(vendor.Args["rustcFlags"], "--cfg 'android_product'") {
83 t.Errorf("unexpected \"--cfg 'android_product'\" for libfoo vendor variant, rustcFlags: %#v", vendor.Args["rustcFlags"])
Matthew Maurer65a54a82023-02-24 19:19:22 +000084 }
85
Ivan Lozano0a468a42024-05-13 21:03:34 -040086 product := ctx.ModuleForTests("libfoo", "android_product_arm64_armv8-a_shared").Rule("rustc")
Wen-yi Chu41326c12023-09-22 03:58:59 +000087 if !strings.Contains(product.Args["rustcFlags"], "--cfg 'android_vndk'") {
88 t.Errorf("missing \"--cfg 'android_vndk'\" for libfoo product variant, rustcFlags: %#v", product.Args["rustcFlags"])
Matthew Maurer65a54a82023-02-24 19:19:22 +000089 }
Wen-yi Chu41326c12023-09-22 03:58:59 +000090 if strings.Contains(product.Args["rustcFlags"], "--cfg 'android_vendor'") {
91 t.Errorf("unexpected \"--cfg 'android_vendor'\" for libfoo product variant, rustcFlags: %#v", product.Args["rustcFlags"])
Matthew Maurer65a54a82023-02-24 19:19:22 +000092 }
Wen-yi Chu41326c12023-09-22 03:58:59 +000093 if !strings.Contains(product.Args["rustcFlags"], "--cfg 'android_product'") {
94 t.Errorf("missing \"--cfg 'android_product'\" for libfoo product variant, rustcFlags: %#v", product.Args["rustcFlags"])
Matthew Maurer65a54a82023-02-24 19:19:22 +000095 }
96
Ivan Lozano0a468a42024-05-13 21:03:34 -040097 system := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Rule("rustc")
Wen-yi Chu41326c12023-09-22 03:58:59 +000098 if strings.Contains(system.Args["rustcFlags"], "--cfg 'android_vndk'") {
99 t.Errorf("unexpected \"--cfg 'android_vndk'\" for libfoo system variant, rustcFlags: %#v", system.Args["rustcFlags"])
Matthew Maurer65a54a82023-02-24 19:19:22 +0000100 }
Wen-yi Chu41326c12023-09-22 03:58:59 +0000101 if strings.Contains(system.Args["rustcFlags"], "--cfg 'android_vendor'") {
102 t.Errorf("unexpected \"--cfg 'android_vendor'\" for libfoo system variant, rustcFlags: %#v", system.Args["rustcFlags"])
Matthew Maurer65a54a82023-02-24 19:19:22 +0000103 }
Wen-yi Chu41326c12023-09-22 03:58:59 +0000104 if strings.Contains(system.Args["rustcFlags"], "--cfg 'android_product'") {
105 t.Errorf("unexpected \"--cfg 'android_product'\" for libfoo system variant, rustcFlags: %#v", product.Args["rustcFlags"])
Matthew Maurer65a54a82023-02-24 19:19:22 +0000106 }
107
Ivan Lozanof76cdf72021-02-12 09:55:06 -0500108}
109
Ivan Lozano0a468a42024-05-13 21:03:34 -0400110// Test that cc modules can link against vendor_ramdisk_available rust_ffi_rlib and rust_ffi_static libraries.
Ivan Lozanoe6d30982021-02-05 10:57:43 -0500111func TestVendorRamdiskLinkage(t *testing.T) {
Kiyoung Kim1db4a742024-04-01 15:50:01 +0900112 ctx := testRust(t, `
Ivan Lozano0a468a42024-05-13 21:03:34 -0400113 cc_library_shared {
Ivan Lozanoe6d30982021-02-05 10:57:43 -0500114 name: "libcc_vendor_ramdisk",
Ivan Lozanofd47b1a2024-05-17 14:13:41 -0400115 static_libs: [
116 "libfoo_vendor_ramdisk",
117 "libfoo_static_vendor_ramdisk"
118 ],
Ivan Lozanoe6d30982021-02-05 10:57:43 -0500119 system_shared_libs: [],
120 vendor_ramdisk_available: true,
121 }
Ivan Lozano0a468a42024-05-13 21:03:34 -0400122 rust_ffi_rlib {
Ivan Lozanoe6d30982021-02-05 10:57:43 -0500123 name: "libfoo_vendor_ramdisk",
124 crate_name: "foo",
125 srcs: ["foo.rs"],
126 vendor_ramdisk_available: true,
127 }
Ivan Lozano0a468a42024-05-13 21:03:34 -0400128 rust_ffi_static {
129 name: "libfoo_static_vendor_ramdisk",
130 crate_name: "foo",
131 srcs: ["foo.rs"],
132 vendor_ramdisk_available: true,
133 }
Ivan Lozanoe6d30982021-02-05 10:57:43 -0500134 `)
135
Ivan Lozano0a468a42024-05-13 21:03:34 -0400136 vendorRamdiskLibrary := ctx.ModuleForTests("libcc_vendor_ramdisk", "android_vendor_ramdisk_arm64_armv8-a_shared").Module().(*cc.Module)
Ivan Lozanoe6d30982021-02-05 10:57:43 -0500137
Ivan Lozanofd47b1a2024-05-17 14:13:41 -0400138 if android.InList("libfoo_static_vendor_ramdisk.vendor_ramdisk", vendorRamdiskLibrary.Properties.AndroidMkStaticLibs) {
139 t.Errorf("libcc_vendor_ramdisk should not have a dependency on the libfoo_static_vendor_ramdisk static library")
Ivan Lozanoe6d30982021-02-05 10:57:43 -0500140 }
141}
142
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400143// Test that prebuilt libraries cannot be made vendor available.
Ivan Lozano6a884432020-12-02 09:15:16 -0500144func TestForbiddenVendorLinkage(t *testing.T) {
Kiyoung Kim1db4a742024-04-01 15:50:01 +0900145 testRustError(t, "Rust prebuilt modules not supported for non-system images.", `
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400146 rust_prebuilt_library {
147 name: "librust_prebuilt",
148 crate_name: "rust_prebuilt",
149 rlib: {
150 srcs: ["libtest.rlib"],
151 },
152 dylib: {
153 srcs: ["libtest.so"],
154 },
Ivan Lozano6a884432020-12-02 09:15:16 -0500155 vendor: true,
156 }
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400157 `)
Ivan Lozano6a884432020-12-02 09:15:16 -0500158}
Matthew Maurer993db7a2023-01-24 16:36:02 -0800159
160func checkInstallPartition(t *testing.T, ctx *android.TestContext, name, variant, expected string) {
161 mod := ctx.ModuleForTests(name, variant).Module().(*Module)
162 partitionDefined := false
163 checkPartition := func(specific bool, partition string) {
164 if specific {
165 if expected != partition && !partitionDefined {
166 // The variant is installed to the 'partition'
167 t.Errorf("%s variant of %q must not be installed to %s partition", variant, name, partition)
168 }
169 partitionDefined = true
170 } else {
171 // The variant is not installed to the 'partition'
172 if expected == partition {
173 t.Errorf("%s variant of %q must be installed to %s partition", variant, name, partition)
174 }
175 }
176 }
177 socSpecific := func(m *Module) bool {
178 return m.SocSpecific()
179 }
180 deviceSpecific := func(m *Module) bool {
181 return m.DeviceSpecific()
182 }
183 productSpecific := func(m *Module) bool {
184 return m.ProductSpecific() || m.productSpecificModuleContext()
185 }
186 systemExtSpecific := func(m *Module) bool {
187 return m.SystemExtSpecific()
188 }
189 checkPartition(socSpecific(mod), "vendor")
190 checkPartition(deviceSpecific(mod), "odm")
191 checkPartition(productSpecific(mod), "product")
192 checkPartition(systemExtSpecific(mod), "system_ext")
193 if !partitionDefined && expected != "system" {
194 t.Errorf("%s variant of %q is expected to be installed to %s partition,"+
195 " but installed to system partition", variant, name, expected)
196 }
197}
198
199func TestInstallPartition(t *testing.T) {
200 t.Parallel()
201 t.Helper()
202 ctx := testRust(t, `
203 rust_binary {
204 name: "sample_system",
205 crate_name: "sample",
206 srcs: ["foo.rs"],
207 }
208 rust_binary {
209 name: "sample_system_ext",
210 crate_name: "sample",
211 srcs: ["foo.rs"],
212 system_ext_specific: true,
213 }
214 rust_binary {
215 name: "sample_product",
216 crate_name: "sample",
217 srcs: ["foo.rs"],
218 product_specific: true,
219 }
220 rust_binary {
221 name: "sample_vendor",
222 crate_name: "sample",
223 srcs: ["foo.rs"],
224 vendor: true,
225 }
226 rust_binary {
227 name: "sample_odm",
228 crate_name: "sample",
229 srcs: ["foo.rs"],
230 device_specific: true,
231 }
232 rust_binary {
233 name: "sample_all_available",
234 crate_name: "sample",
235 srcs: ["foo.rs"],
236 vendor_available: true,
237 product_available: true,
238 }
239 `)
240
241 checkInstallPartition(t, ctx, "sample_system", binaryCoreVariant, "system")
242 checkInstallPartition(t, ctx, "sample_system_ext", binaryCoreVariant, "system_ext")
243 checkInstallPartition(t, ctx, "sample_product", binaryProductVariant, "product")
244 checkInstallPartition(t, ctx, "sample_vendor", binaryVendorVariant, "vendor")
245 checkInstallPartition(t, ctx, "sample_odm", binaryVendorVariant, "odm")
246
247 checkInstallPartition(t, ctx, "sample_all_available", binaryCoreVariant, "system")
248}