blob: fb4d9c1708f0fd46d88aac3611c6c3b8f41259ea [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 Lozanoe6d30982021-02-05 10:57:43 -050025// Test that cc modules can link against vendor_available rust_ffi_static libraries.
Ivan Lozano6a884432020-12-02 09:15:16 -050026func TestVendorLinkage(t *testing.T) {
Ivan Lozanof76cdf72021-02-12 09:55:06 -050027 ctx := testRustVndk(t, `
Ivan Lozano6a884432020-12-02 09:15:16 -050028 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 Parkf58c46e2021-04-01 21:35:20 +090041 vendorBinary := ctx.ModuleForTests("fizz_vendor", "android_vendor.29_arm64_armv8-a").Module().(*cc.Module)
Ivan Lozano6a884432020-12-02 09:15:16 -050042
Ivan Lozanoc08897c2021-04-02 12:41:32 -040043 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 Lozano6a884432020-12-02 09:15:16 -050045 }
46}
47
Ivan Lozanof76cdf72021-02-12 09:55:06 -050048// Test that variants which use the vndk emit the appropriate cfg flag.
49func 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 Maurer65a54a82023-02-24 19:19:22 +000056 product_available: true,
Ivan Lozanof76cdf72021-02-12 09:55:06 -050057 }
58 `)
59
Jiyong Parkf58c46e2021-04-01 21:35:20 +090060 vendor := ctx.ModuleForTests("libfoo", "android_vendor.29_arm64_armv8-a_static").Rule("rustc")
Ivan Lozanof76cdf72021-02-12 09:55:06 -050061
Wen-yi Chu41326c12023-09-22 03:58:59 +000062 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 Lozanof76cdf72021-02-12 09:55:06 -050064 }
Wen-yi Chu41326c12023-09-22 03:58:59 +000065 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 Maurer65a54a82023-02-24 19:19:22 +000067 }
Wen-yi Chu41326c12023-09-22 03:58:59 +000068 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 Maurer65a54a82023-02-24 19:19:22 +000070 }
71
72 product := ctx.ModuleForTests("libfoo", "android_product.29_arm64_armv8-a_static").Rule("rustc")
Wen-yi Chu41326c12023-09-22 03:58:59 +000073 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 Maurer65a54a82023-02-24 19:19:22 +000075 }
Wen-yi Chu41326c12023-09-22 03:58:59 +000076 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 Maurer65a54a82023-02-24 19:19:22 +000078 }
Wen-yi Chu41326c12023-09-22 03:58:59 +000079 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 Maurer65a54a82023-02-24 19:19:22 +000081 }
82
83 system := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_static").Rule("rustc")
Wen-yi Chu41326c12023-09-22 03:58:59 +000084 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 Maurer65a54a82023-02-24 19:19:22 +000086 }
Wen-yi Chu41326c12023-09-22 03:58:59 +000087 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 Maurer65a54a82023-02-24 19:19:22 +000089 }
Wen-yi Chu41326c12023-09-22 03:58:59 +000090 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 Maurer65a54a82023-02-24 19:19:22 +000092 }
93
Ivan Lozanof76cdf72021-02-12 09:55:06 -050094}
95
Ivan Lozanoe6d30982021-02-05 10:57:43 -050096// Test that cc modules can link against vendor_ramdisk_available rust_ffi_static libraries.
97func TestVendorRamdiskLinkage(t *testing.T) {
Ivan Lozanof76cdf72021-02-12 09:55:06 -050098 ctx := testRustVndk(t, `
Ivan Lozanoe6d30982021-02-05 10:57:43 -050099 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 Lozanoc08897c2021-04-02 12:41:32 -0400120// Test that prebuilt libraries cannot be made vendor available.
Ivan Lozano6a884432020-12-02 09:15:16 -0500121func TestForbiddenVendorLinkage(t *testing.T) {
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400122 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 Lozano6a884432020-12-02 09:15:16 -0500132 vendor: true,
133 }
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400134 `)
Ivan Lozano6a884432020-12-02 09:15:16 -0500135}
Matthew Maurer993db7a2023-01-24 16:36:02 -0800136
137func 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
176func 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}