blob: 818587218cc204ff0469e6ba128ad8c32e7ebff6 [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,
56 }
57 `)
58
Jiyong Parkf58c46e2021-04-01 21:35:20 +090059 vendor := ctx.ModuleForTests("libfoo", "android_vendor.29_arm64_armv8-a_static").Rule("rustc")
Ivan Lozanof76cdf72021-02-12 09:55:06 -050060
61 if !strings.Contains(vendor.Args["rustcFlags"], "--cfg 'android_vndk'") {
62 t.Errorf("missing \"--cfg 'android_vndk'\" for libfoo vendor variant, rustcFlags: %#v", vendor.Args["rustcFlags"])
63 }
64}
65
Ivan Lozanoe6d30982021-02-05 10:57:43 -050066// Test that cc modules can link against vendor_ramdisk_available rust_ffi_static libraries.
67func TestVendorRamdiskLinkage(t *testing.T) {
Ivan Lozanof76cdf72021-02-12 09:55:06 -050068 ctx := testRustVndk(t, `
Ivan Lozanoe6d30982021-02-05 10:57:43 -050069 cc_library_static {
70 name: "libcc_vendor_ramdisk",
71 static_libs: ["libfoo_vendor_ramdisk"],
72 system_shared_libs: [],
73 vendor_ramdisk_available: true,
74 }
75 rust_ffi_static {
76 name: "libfoo_vendor_ramdisk",
77 crate_name: "foo",
78 srcs: ["foo.rs"],
79 vendor_ramdisk_available: true,
80 }
81 `)
82
83 vendorRamdiskLibrary := ctx.ModuleForTests("libcc_vendor_ramdisk", "android_vendor_ramdisk_arm64_armv8-a_static").Module().(*cc.Module)
84
85 if !android.InList("libfoo_vendor_ramdisk.vendor_ramdisk", vendorRamdiskLibrary.Properties.AndroidMkStaticLibs) {
86 t.Errorf("libcc_vendor_ramdisk should have a dependency on libfoo_vendor_ramdisk")
87 }
88}
89
Ivan Lozanoc08897c2021-04-02 12:41:32 -040090// Test that prebuilt libraries cannot be made vendor available.
Ivan Lozano6a884432020-12-02 09:15:16 -050091func TestForbiddenVendorLinkage(t *testing.T) {
Ivan Lozanoc08897c2021-04-02 12:41:32 -040092 testRustVndkError(t, "Rust prebuilt modules not supported for non-system images.", `
93 rust_prebuilt_library {
94 name: "librust_prebuilt",
95 crate_name: "rust_prebuilt",
96 rlib: {
97 srcs: ["libtest.rlib"],
98 },
99 dylib: {
100 srcs: ["libtest.so"],
101 },
Ivan Lozano6a884432020-12-02 09:15:16 -0500102 vendor: true,
103 }
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400104 `)
Ivan Lozano6a884432020-12-02 09:15:16 -0500105}
Matthew Maurer993db7a2023-01-24 16:36:02 -0800106
107func checkInstallPartition(t *testing.T, ctx *android.TestContext, name, variant, expected string) {
108 mod := ctx.ModuleForTests(name, variant).Module().(*Module)
109 partitionDefined := false
110 checkPartition := func(specific bool, partition string) {
111 if specific {
112 if expected != partition && !partitionDefined {
113 // The variant is installed to the 'partition'
114 t.Errorf("%s variant of %q must not be installed to %s partition", variant, name, partition)
115 }
116 partitionDefined = true
117 } else {
118 // The variant is not installed to the 'partition'
119 if expected == partition {
120 t.Errorf("%s variant of %q must be installed to %s partition", variant, name, partition)
121 }
122 }
123 }
124 socSpecific := func(m *Module) bool {
125 return m.SocSpecific()
126 }
127 deviceSpecific := func(m *Module) bool {
128 return m.DeviceSpecific()
129 }
130 productSpecific := func(m *Module) bool {
131 return m.ProductSpecific() || m.productSpecificModuleContext()
132 }
133 systemExtSpecific := func(m *Module) bool {
134 return m.SystemExtSpecific()
135 }
136 checkPartition(socSpecific(mod), "vendor")
137 checkPartition(deviceSpecific(mod), "odm")
138 checkPartition(productSpecific(mod), "product")
139 checkPartition(systemExtSpecific(mod), "system_ext")
140 if !partitionDefined && expected != "system" {
141 t.Errorf("%s variant of %q is expected to be installed to %s partition,"+
142 " but installed to system partition", variant, name, expected)
143 }
144}
145
146func TestInstallPartition(t *testing.T) {
147 t.Parallel()
148 t.Helper()
149 ctx := testRust(t, `
150 rust_binary {
151 name: "sample_system",
152 crate_name: "sample",
153 srcs: ["foo.rs"],
154 }
155 rust_binary {
156 name: "sample_system_ext",
157 crate_name: "sample",
158 srcs: ["foo.rs"],
159 system_ext_specific: true,
160 }
161 rust_binary {
162 name: "sample_product",
163 crate_name: "sample",
164 srcs: ["foo.rs"],
165 product_specific: true,
166 }
167 rust_binary {
168 name: "sample_vendor",
169 crate_name: "sample",
170 srcs: ["foo.rs"],
171 vendor: true,
172 }
173 rust_binary {
174 name: "sample_odm",
175 crate_name: "sample",
176 srcs: ["foo.rs"],
177 device_specific: true,
178 }
179 rust_binary {
180 name: "sample_all_available",
181 crate_name: "sample",
182 srcs: ["foo.rs"],
183 vendor_available: true,
184 product_available: true,
185 }
186 `)
187
188 checkInstallPartition(t, ctx, "sample_system", binaryCoreVariant, "system")
189 checkInstallPartition(t, ctx, "sample_system_ext", binaryCoreVariant, "system_ext")
190 checkInstallPartition(t, ctx, "sample_product", binaryProductVariant, "product")
191 checkInstallPartition(t, ctx, "sample_vendor", binaryVendorVariant, "vendor")
192 checkInstallPartition(t, ctx, "sample_odm", binaryVendorVariant, "odm")
193
194 checkInstallPartition(t, ctx, "sample_all_available", binaryCoreVariant, "system")
195}