blob: 0581fa7f8d93ace9bdf016c6402504b7c2291e9d [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 Lozano61848422024-12-13 19:45:00 +000025// Test that cc modules can depend on vendor_available 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",
Ivan Lozanofd47b1a2024-05-17 14:13:41 -040032 ],
Ivan Lozano0a468a42024-05-13 21:03:34 -040033 vendor_available: true,
34 }
35 cc_binary {
36 name: "fizz_soc_specific",
Ivan Lozanofd47b1a2024-05-17 14:13:41 -040037 static_libs: ["libfoo_vendor"],
Ivan Lozano6a884432020-12-02 09:15:16 -050038 soc_specific: true,
39 }
Ivan Lozano0a468a42024-05-13 21:03:34 -040040 rust_ffi_static {
Ivan Lozano61848422024-12-13 19:45:00 +000041 name: "libfoo_vendor",
Ivan Lozano0a468a42024-05-13 21:03:34 -040042 crate_name: "foo",
43 srcs: ["foo.rs"],
44 vendor_available: true,
45 }
Ivan Lozano6a884432020-12-02 09:15:16 -050046 `)
47
Ivan Lozano0a468a42024-05-13 21:03:34 -040048 vendorBinary := ctx.ModuleForTests("fizz_vendor_available", "android_vendor_arm64_armv8-a").Module().(*cc.Module)
Ivan Lozano6a884432020-12-02 09:15:16 -050049
Ivan Lozano61848422024-12-13 19:45:00 +000050 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 Lozano6a884432020-12-02 09:15:16 -050052 }
53}
54
Ivan Lozanof76cdf72021-02-12 09:55:06 -050055// Test that variants which use the vndk emit the appropriate cfg flag.
Kiyoung Kim1db4a742024-04-01 15:50:01 +090056func TestImageCfgFlag(t *testing.T) {
57 ctx := testRust(t, `
Ivan Lozano0a468a42024-05-13 21:03:34 -040058 rust_ffi_shared {
Ivan Lozanof76cdf72021-02-12 09:55:06 -050059 name: "libfoo",
60 crate_name: "foo",
61 srcs: ["foo.rs"],
62 vendor_available: true,
Matthew Maurer65a54a82023-02-24 19:19:22 +000063 product_available: true,
Ivan Lozanof76cdf72021-02-12 09:55:06 -050064 }
65 `)
66
Ivan Lozano0a468a42024-05-13 21:03:34 -040067 vendor := ctx.ModuleForTests("libfoo", "android_vendor_arm64_armv8-a_shared").Rule("rustc")
Ivan Lozanof76cdf72021-02-12 09:55:06 -050068
Wen-yi Chu41326c12023-09-22 03:58:59 +000069 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 Lozanof76cdf72021-02-12 09:55:06 -050071 }
Wen-yi Chu41326c12023-09-22 03:58:59 +000072 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 Maurer65a54a82023-02-24 19:19:22 +000074 }
Wen-yi Chu41326c12023-09-22 03:58:59 +000075 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 Maurer65a54a82023-02-24 19:19:22 +000077 }
78
Ivan Lozano0a468a42024-05-13 21:03:34 -040079 product := ctx.ModuleForTests("libfoo", "android_product_arm64_armv8-a_shared").Rule("rustc")
Wen-yi Chu41326c12023-09-22 03:58:59 +000080 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 Maurer65a54a82023-02-24 19:19:22 +000082 }
Wen-yi Chu41326c12023-09-22 03:58:59 +000083 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 Maurer65a54a82023-02-24 19:19:22 +000085 }
Wen-yi Chu41326c12023-09-22 03:58:59 +000086 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 Maurer65a54a82023-02-24 19:19:22 +000088 }
89
Ivan Lozano0a468a42024-05-13 21:03:34 -040090 system := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Rule("rustc")
Wen-yi Chu41326c12023-09-22 03:58:59 +000091 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 Maurer65a54a82023-02-24 19:19:22 +000093 }
Wen-yi Chu41326c12023-09-22 03:58:59 +000094 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 Maurer65a54a82023-02-24 19:19:22 +000096 }
Wen-yi Chu41326c12023-09-22 03:58:59 +000097 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 Maurer65a54a82023-02-24 19:19:22 +000099 }
100
Ivan Lozanof76cdf72021-02-12 09:55:06 -0500101}
102
Ivan Lozano61848422024-12-13 19:45:00 +0000103// Test that cc modules can link against vendor_ramdisk_available rust_ffi_static libraries.
Ivan Lozanoe6d30982021-02-05 10:57:43 -0500104func TestVendorRamdiskLinkage(t *testing.T) {
Kiyoung Kim1db4a742024-04-01 15:50:01 +0900105 ctx := testRust(t, `
Ivan Lozano0a468a42024-05-13 21:03:34 -0400106 cc_library_shared {
Ivan Lozanoe6d30982021-02-05 10:57:43 -0500107 name: "libcc_vendor_ramdisk",
Ivan Lozanofd47b1a2024-05-17 14:13:41 -0400108 static_libs: [
109 "libfoo_vendor_ramdisk",
Ivan Lozanofd47b1a2024-05-17 14:13:41 -0400110 ],
Ivan Lozanoe6d30982021-02-05 10:57:43 -0500111 system_shared_libs: [],
112 vendor_ramdisk_available: true,
113 }
Ivan Lozano0a468a42024-05-13 21:03:34 -0400114 rust_ffi_static {
Ivan Lozano61848422024-12-13 19:45:00 +0000115 name: "libfoo_vendor_ramdisk",
Ivan Lozano0a468a42024-05-13 21:03:34 -0400116 crate_name: "foo",
117 srcs: ["foo.rs"],
118 vendor_ramdisk_available: true,
119 }
Ivan Lozanoe6d30982021-02-05 10:57:43 -0500120 `)
121
Ivan Lozano0a468a42024-05-13 21:03:34 -0400122 vendorRamdiskLibrary := ctx.ModuleForTests("libcc_vendor_ramdisk", "android_vendor_ramdisk_arm64_armv8-a_shared").Module().(*cc.Module)
Ivan Lozanoe6d30982021-02-05 10:57:43 -0500123
Ivan Lozano61848422024-12-13 19:45:00 +0000124 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 Lozanoe6d30982021-02-05 10:57:43 -0500126 }
127}
128
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400129// Test that prebuilt libraries cannot be made vendor available.
Ivan Lozano6a884432020-12-02 09:15:16 -0500130func TestForbiddenVendorLinkage(t *testing.T) {
Kiyoung Kim1db4a742024-04-01 15:50:01 +0900131 testRustError(t, "Rust prebuilt modules not supported for non-system images.", `
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400132 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 Lozano6a884432020-12-02 09:15:16 -0500141 vendor: true,
142 }
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400143 `)
Ivan Lozano6a884432020-12-02 09:15:16 -0500144}
Matthew Maurer993db7a2023-01-24 16:36:02 -0800145
146func checkInstallPartition(t *testing.T, ctx *android.TestContext, name, variant, expected string) {
147 mod := ctx.ModuleForTests(name, variant).Module().(*Module)
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
185func 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}