Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 1 | // Copyright 2019 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 | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 18 | "os" |
Ivan Lozano | c008361 | 2019-09-03 13:49:39 -0700 | [diff] [blame] | 19 | "runtime" |
Ivan Lozano | b9040d6 | 2019-09-24 13:23:50 -0700 | [diff] [blame] | 20 | "strings" |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 21 | "testing" |
| 22 | |
Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 23 | "github.com/google/blueprint/proptools" |
| 24 | |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 25 | "android/soong/android" |
Paul Duffin | 2c4ca8d | 2021-03-07 19:18:38 +0000 | [diff] [blame] | 26 | "android/soong/genrule" |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 27 | ) |
| 28 | |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 29 | func TestMain(m *testing.M) { |
Paul Duffin | 2c4ca8d | 2021-03-07 19:18:38 +0000 | [diff] [blame] | 30 | os.Exit(m.Run()) |
| 31 | } |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 32 | |
Paul Duffin | 2c4ca8d | 2021-03-07 19:18:38 +0000 | [diff] [blame] | 33 | var prepareForRustTest = android.GroupFixturePreparers( |
| 34 | android.PrepareForTestWithArchMutator, |
| 35 | android.PrepareForTestWithDefaults, |
| 36 | android.PrepareForTestWithPrebuilts, |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 37 | |
Paul Duffin | 2c4ca8d | 2021-03-07 19:18:38 +0000 | [diff] [blame] | 38 | genrule.PrepareForTestWithGenRuleBuildComponents, |
| 39 | |
Ivan Lozano | 1921e80 | 2021-05-20 13:39:16 -0400 | [diff] [blame] | 40 | PrepareForTestWithRustIncludeVndk, |
| 41 | android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) { |
| 42 | variables.DeviceVndkVersion = StringPtr("current") |
| 43 | variables.ProductVndkVersion = StringPtr("current") |
| 44 | variables.Platform_vndk_version = StringPtr("29") |
| 45 | }), |
Paul Duffin | 2c4ca8d | 2021-03-07 19:18:38 +0000 | [diff] [blame] | 46 | ) |
| 47 | |
| 48 | var rustMockedFiles = android.MockFS{ |
Ivan Lozano | 1921e80 | 2021-05-20 13:39:16 -0400 | [diff] [blame] | 49 | "foo.rs": nil, |
| 50 | "foo.c": nil, |
| 51 | "src/bar.rs": nil, |
| 52 | "src/any.h": nil, |
| 53 | "c_includes/c_header.h": nil, |
| 54 | "rust_includes/rust_headers.h": nil, |
| 55 | "proto.proto": nil, |
| 56 | "proto/buf.proto": nil, |
| 57 | "buf.proto": nil, |
| 58 | "foo.proto": nil, |
| 59 | "liby.so": nil, |
| 60 | "libz.so": nil, |
| 61 | "data.txt": nil, |
Ivan Lozano | 3149e6e | 2021-06-01 15:09:53 -0400 | [diff] [blame] | 62 | "liblog.map.txt": nil, |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 63 | } |
| 64 | |
Thiébaud Weksteen | 0a75e52 | 2020-10-07 14:30:03 +0200 | [diff] [blame] | 65 | // testRust returns a TestContext in which a basic environment has been setup. |
Paul Duffin | 2c4ca8d | 2021-03-07 19:18:38 +0000 | [diff] [blame] | 66 | // This environment contains a few mocked files. See rustMockedFiles for the list of these files. |
Thiébaud Weksteen | 0a75e52 | 2020-10-07 14:30:03 +0200 | [diff] [blame] | 67 | func testRust(t *testing.T, bp string) *android.TestContext { |
Paul Duffin | 2c4ca8d | 2021-03-07 19:18:38 +0000 | [diff] [blame] | 68 | skipTestIfOsNotSupported(t) |
| 69 | result := android.GroupFixturePreparers( |
| 70 | prepareForRustTest, |
| 71 | rustMockedFiles.AddToFixture(), |
| 72 | ). |
| 73 | RunTestWithBp(t, bp) |
| 74 | return result.TestContext |
Thiébaud Weksteen | 0a75e52 | 2020-10-07 14:30:03 +0200 | [diff] [blame] | 75 | } |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 76 | |
Ivan Lozano | f76cdf7 | 2021-02-12 09:55:06 -0500 | [diff] [blame] | 77 | func testRustVndk(t *testing.T, bp string) *android.TestContext { |
Ivan Lozano | 1921e80 | 2021-05-20 13:39:16 -0400 | [diff] [blame] | 78 | return testRustVndkFs(t, bp, rustMockedFiles) |
| 79 | } |
| 80 | |
Ivan Lozano | 3149e6e | 2021-06-01 15:09:53 -0400 | [diff] [blame] | 81 | const ( |
Ivan Lozano | c2ca1ee | 2021-11-09 16:23:40 -0500 | [diff] [blame] | 82 | sharedVendorVariant = "android_vendor.29_arm64_armv8-a_shared" |
| 83 | rlibVendorVariant = "android_vendor.29_arm64_armv8-a_rlib_rlib-std" |
| 84 | sharedRecoveryVariant = "android_recovery_arm64_armv8-a_shared" |
| 85 | rlibRecoveryVariant = "android_recovery_arm64_armv8-a_rlib_rlib-std" |
Matthew Maurer | 993db7a | 2023-01-24 16:36:02 -0800 | [diff] [blame] | 86 | binaryCoreVariant = "android_arm64_armv8-a" |
| 87 | binaryVendorVariant = "android_vendor.29_arm64_armv8-a" |
| 88 | binaryProductVariant = "android_product.29_arm64_armv8-a" |
| 89 | binaryRecoveryVariant = "android_recovery_arm64_armv8-a" |
Ivan Lozano | 3149e6e | 2021-06-01 15:09:53 -0400 | [diff] [blame] | 90 | ) |
Ivan Lozano | 1921e80 | 2021-05-20 13:39:16 -0400 | [diff] [blame] | 91 | |
| 92 | func testRustVndkFs(t *testing.T, bp string, fs android.MockFS) *android.TestContext { |
Ivan Lozano | 3149e6e | 2021-06-01 15:09:53 -0400 | [diff] [blame] | 93 | return testRustVndkFsVersions(t, bp, fs, "current", "current", "29") |
| 94 | } |
| 95 | |
| 96 | func testRustVndkFsVersions(t *testing.T, bp string, fs android.MockFS, device_version, product_version, vndk_version string) *android.TestContext { |
Paul Duffin | 2c4ca8d | 2021-03-07 19:18:38 +0000 | [diff] [blame] | 97 | skipTestIfOsNotSupported(t) |
| 98 | result := android.GroupFixturePreparers( |
| 99 | prepareForRustTest, |
Ivan Lozano | 1921e80 | 2021-05-20 13:39:16 -0400 | [diff] [blame] | 100 | fs.AddToFixture(), |
Paul Duffin | 2c4ca8d | 2021-03-07 19:18:38 +0000 | [diff] [blame] | 101 | android.FixtureModifyProductVariables( |
| 102 | func(variables android.FixtureProductVariables) { |
Ivan Lozano | 3149e6e | 2021-06-01 15:09:53 -0400 | [diff] [blame] | 103 | variables.DeviceVndkVersion = StringPtr(device_version) |
| 104 | variables.ProductVndkVersion = StringPtr(product_version) |
| 105 | variables.Platform_vndk_version = StringPtr(vndk_version) |
Paul Duffin | 2c4ca8d | 2021-03-07 19:18:38 +0000 | [diff] [blame] | 106 | }, |
| 107 | ), |
| 108 | ).RunTestWithBp(t, bp) |
| 109 | return result.TestContext |
Ivan Lozano | c2ca1ee | 2021-11-09 16:23:40 -0500 | [diff] [blame] | 110 | } |
Ivan Lozano | 1921e80 | 2021-05-20 13:39:16 -0400 | [diff] [blame] | 111 | |
Ivan Lozano | c2ca1ee | 2021-11-09 16:23:40 -0500 | [diff] [blame] | 112 | func testRustRecoveryFsVersions(t *testing.T, bp string, fs android.MockFS, device_version, vndk_version, recovery_version string) *android.TestContext { |
| 113 | skipTestIfOsNotSupported(t) |
| 114 | result := android.GroupFixturePreparers( |
| 115 | prepareForRustTest, |
| 116 | fs.AddToFixture(), |
| 117 | android.FixtureModifyProductVariables( |
| 118 | func(variables android.FixtureProductVariables) { |
| 119 | variables.DeviceVndkVersion = StringPtr(device_version) |
| 120 | variables.RecoverySnapshotVersion = StringPtr(recovery_version) |
| 121 | variables.Platform_vndk_version = StringPtr(vndk_version) |
| 122 | }, |
| 123 | ), |
| 124 | ).RunTestWithBp(t, bp) |
| 125 | return result.TestContext |
Ivan Lozano | f76cdf7 | 2021-02-12 09:55:06 -0500 | [diff] [blame] | 126 | } |
| 127 | |
Thiébaud Weksteen | 0a75e52 | 2020-10-07 14:30:03 +0200 | [diff] [blame] | 128 | // testRustCov returns a TestContext in which a basic environment has been |
| 129 | // setup. This environment explicitly enables coverage. |
| 130 | func testRustCov(t *testing.T, bp string) *android.TestContext { |
Paul Duffin | 2c4ca8d | 2021-03-07 19:18:38 +0000 | [diff] [blame] | 131 | skipTestIfOsNotSupported(t) |
| 132 | result := android.GroupFixturePreparers( |
| 133 | prepareForRustTest, |
| 134 | rustMockedFiles.AddToFixture(), |
| 135 | android.FixtureModifyProductVariables( |
| 136 | func(variables android.FixtureProductVariables) { |
| 137 | variables.ClangCoverage = proptools.BoolPtr(true) |
| 138 | variables.Native_coverage = proptools.BoolPtr(true) |
| 139 | variables.NativeCoveragePaths = []string{"*"} |
| 140 | }, |
| 141 | ), |
| 142 | ).RunTestWithBp(t, bp) |
| 143 | return result.TestContext |
Thiébaud Weksteen | 0a75e52 | 2020-10-07 14:30:03 +0200 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | // testRustError ensures that at least one error was raised and its value |
| 147 | // matches the pattern provided. The error can be either in the parsing of the |
| 148 | // Blueprint or when generating the build actions. |
| 149 | func testRustError(t *testing.T, pattern string, bp string) { |
Paul Duffin | 2c4ca8d | 2021-03-07 19:18:38 +0000 | [diff] [blame] | 150 | skipTestIfOsNotSupported(t) |
| 151 | android.GroupFixturePreparers( |
| 152 | prepareForRustTest, |
| 153 | rustMockedFiles.AddToFixture(), |
| 154 | ). |
| 155 | ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(pattern)). |
| 156 | RunTestWithBp(t, bp) |
Thiébaud Weksteen | 0a75e52 | 2020-10-07 14:30:03 +0200 | [diff] [blame] | 157 | } |
| 158 | |
Ivan Lozano | c08897c | 2021-04-02 12:41:32 -0400 | [diff] [blame] | 159 | // testRustVndkError is similar to testRustError, but can be used to test VNDK-related errors. |
| 160 | func testRustVndkError(t *testing.T, pattern string, bp string) { |
Ivan Lozano | 1921e80 | 2021-05-20 13:39:16 -0400 | [diff] [blame] | 161 | testRustVndkFsError(t, pattern, bp, rustMockedFiles) |
| 162 | } |
| 163 | |
| 164 | func testRustVndkFsError(t *testing.T, pattern string, bp string, fs android.MockFS) { |
Ivan Lozano | c08897c | 2021-04-02 12:41:32 -0400 | [diff] [blame] | 165 | skipTestIfOsNotSupported(t) |
| 166 | android.GroupFixturePreparers( |
| 167 | prepareForRustTest, |
Ivan Lozano | 1921e80 | 2021-05-20 13:39:16 -0400 | [diff] [blame] | 168 | fs.AddToFixture(), |
Ivan Lozano | c08897c | 2021-04-02 12:41:32 -0400 | [diff] [blame] | 169 | android.FixtureModifyProductVariables( |
| 170 | func(variables android.FixtureProductVariables) { |
| 171 | variables.DeviceVndkVersion = StringPtr("current") |
| 172 | variables.ProductVndkVersion = StringPtr("current") |
| 173 | variables.Platform_vndk_version = StringPtr("VER") |
| 174 | }, |
| 175 | ), |
| 176 | ). |
| 177 | ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(pattern)). |
| 178 | RunTestWithBp(t, bp) |
| 179 | } |
| 180 | |
Thiébaud Weksteen | 0a75e52 | 2020-10-07 14:30:03 +0200 | [diff] [blame] | 181 | // testRustCtx is used to build a particular test environment. Unless your |
| 182 | // tests requires a specific setup, prefer the wrapping functions: testRust, |
| 183 | // testRustCov or testRustError. |
| 184 | type testRustCtx struct { |
| 185 | bp string |
| 186 | fs map[string][]byte |
| 187 | env map[string]string |
| 188 | config *android.Config |
| 189 | } |
| 190 | |
Paul Duffin | 2c4ca8d | 2021-03-07 19:18:38 +0000 | [diff] [blame] | 191 | func skipTestIfOsNotSupported(t *testing.T) { |
Thiébaud Weksteen | 0a75e52 | 2020-10-07 14:30:03 +0200 | [diff] [blame] | 192 | // TODO (b/140435149) |
| 193 | if runtime.GOOS != "linux" { |
| 194 | t.Skip("Rust Soong tests can only be run on Linux hosts currently") |
| 195 | } |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 196 | } |
| 197 | |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 198 | // Test that we can extract the link path from a lib path. |
| 199 | func TestLinkPathFromFilePath(t *testing.T) { |
| 200 | barPath := android.PathForTesting("out/soong/.intermediates/external/libbar/libbar/linux_glibc_x86_64_shared/libbar.so") |
| 201 | libName := linkPathFromFilePath(barPath) |
| 202 | expectedResult := "out/soong/.intermediates/external/libbar/libbar/linux_glibc_x86_64_shared/" |
| 203 | |
| 204 | if libName != expectedResult { |
| 205 | t.Errorf("libNameFromFilePath returned the wrong name; expected '%#v', got '%#v'", expectedResult, libName) |
| 206 | } |
| 207 | } |
| 208 | |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 209 | // Test to make sure dependencies are being picked up correctly. |
| 210 | func TestDepsTracking(t *testing.T) { |
| 211 | ctx := testRust(t, ` |
Sam Delmerico | 51d6d1c | 2023-03-28 16:54:00 -0400 | [diff] [blame^] | 212 | cc_library { |
| 213 | host_supported: true, |
| 214 | name: "cc_stubs_dep", |
| 215 | } |
Matthew Maurer | 2ae0513 | 2020-06-23 14:28:53 -0700 | [diff] [blame] | 216 | rust_ffi_host_static { |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 217 | name: "libstatic", |
| 218 | srcs: ["foo.rs"], |
Ivan Lozano | ad8b18b | 2019-10-31 19:38:29 -0700 | [diff] [blame] | 219 | crate_name: "static", |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 220 | } |
Ivan Lozano | 63bb768 | 2021-03-23 15:53:44 -0400 | [diff] [blame] | 221 | rust_ffi_host_static { |
| 222 | name: "libwholestatic", |
| 223 | srcs: ["foo.rs"], |
| 224 | crate_name: "wholestatic", |
| 225 | } |
Matthew Maurer | 2ae0513 | 2020-06-23 14:28:53 -0700 | [diff] [blame] | 226 | rust_ffi_host_shared { |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 227 | name: "libshared", |
| 228 | srcs: ["foo.rs"], |
Ivan Lozano | ad8b18b | 2019-10-31 19:38:29 -0700 | [diff] [blame] | 229 | crate_name: "shared", |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 230 | } |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 231 | rust_library_host_dylib { |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 232 | name: "libdylib", |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 233 | srcs: ["foo.rs"], |
Ivan Lozano | ad8b18b | 2019-10-31 19:38:29 -0700 | [diff] [blame] | 234 | crate_name: "dylib", |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 235 | } |
| 236 | rust_library_host_rlib { |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 237 | name: "librlib", |
Ivan Lozano | 4384568 | 2020-07-09 21:03:28 -0400 | [diff] [blame] | 238 | srcs: ["foo.rs"], |
Ivan Lozano | ad8b18b | 2019-10-31 19:38:29 -0700 | [diff] [blame] | 239 | crate_name: "rlib", |
Ivan Lozano | fb6f36f | 2021-02-05 12:27:08 -0500 | [diff] [blame] | 240 | static_libs: ["libstatic"], |
Ivan Lozano | 63bb768 | 2021-03-23 15:53:44 -0400 | [diff] [blame] | 241 | whole_static_libs: ["libwholestatic"], |
Sam Delmerico | 51d6d1c | 2023-03-28 16:54:00 -0400 | [diff] [blame^] | 242 | shared_libs: ["cc_stubs_dep"], |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 243 | } |
| 244 | rust_proc_macro { |
| 245 | name: "libpm", |
| 246 | srcs: ["foo.rs"], |
Ivan Lozano | ad8b18b | 2019-10-31 19:38:29 -0700 | [diff] [blame] | 247 | crate_name: "pm", |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 248 | } |
| 249 | rust_binary_host { |
Ivan Lozano | 4384568 | 2020-07-09 21:03:28 -0400 | [diff] [blame] | 250 | name: "fizz-buzz", |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 251 | dylibs: ["libdylib"], |
| 252 | rlibs: ["librlib"], |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 253 | proc_macros: ["libpm"], |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 254 | static_libs: ["libstatic"], |
| 255 | shared_libs: ["libshared"], |
Ivan Lozano | 4384568 | 2020-07-09 21:03:28 -0400 | [diff] [blame] | 256 | srcs: ["foo.rs"], |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 257 | } |
| 258 | `) |
Ivan Lozano | 4384568 | 2020-07-09 21:03:28 -0400 | [diff] [blame] | 259 | module := ctx.ModuleForTests("fizz-buzz", "linux_glibc_x86_64").Module().(*Module) |
Ivan Lozano | fb6f36f | 2021-02-05 12:27:08 -0500 | [diff] [blame] | 260 | rustc := ctx.ModuleForTests("librlib", "linux_glibc_x86_64_rlib_rlib-std").Rule("rustc") |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 261 | |
| 262 | // Since dependencies are added to AndroidMk* properties, we can check these to see if they've been picked up. |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 263 | if !android.InList("libdylib", module.Properties.AndroidMkDylibs) { |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 264 | t.Errorf("Dylib dependency not detected (dependency missing from AndroidMkDylibs)") |
| 265 | } |
| 266 | |
Ivan Lozano | 2b08113 | 2020-09-08 12:46:52 -0400 | [diff] [blame] | 267 | if !android.InList("librlib.rlib-std", module.Properties.AndroidMkRlibs) { |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 268 | t.Errorf("Rlib dependency not detected (dependency missing from AndroidMkRlibs)") |
| 269 | } |
| 270 | |
| 271 | if !android.InList("libpm", module.Properties.AndroidMkProcMacroLibs) { |
| 272 | t.Errorf("Proc_macro dependency not detected (dependency missing from AndroidMkProcMacroLibs)") |
| 273 | } |
| 274 | |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 275 | if !android.InList("libshared", module.Properties.AndroidMkSharedLibs) { |
| 276 | t.Errorf("Shared library dependency not detected (dependency missing from AndroidMkSharedLibs)") |
| 277 | } |
| 278 | |
| 279 | if !android.InList("libstatic", module.Properties.AndroidMkStaticLibs) { |
| 280 | t.Errorf("Static library dependency not detected (dependency missing from AndroidMkStaticLibs)") |
| 281 | } |
Ivan Lozano | 3dfa12d | 2021-02-04 11:29:41 -0500 | [diff] [blame] | 282 | |
Ivan Lozano | 63bb768 | 2021-03-23 15:53:44 -0400 | [diff] [blame] | 283 | if !strings.Contains(rustc.Args["rustcFlags"], "-lstatic=wholestatic") { |
| 284 | t.Errorf("-lstatic flag not being passed to rustc for static library %#v", rustc.Args["rustcFlags"]) |
Ivan Lozano | 3dfa12d | 2021-02-04 11:29:41 -0500 | [diff] [blame] | 285 | } |
| 286 | |
Sam Delmerico | 51d6d1c | 2023-03-28 16:54:00 -0400 | [diff] [blame^] | 287 | if !strings.Contains(rustc.Args["linkFlags"], "cc_stubs_dep.so") { |
| 288 | t.Errorf("shared cc_library not being passed to rustc linkFlags %#v", rustc.Args["linkFlags"]) |
| 289 | } |
| 290 | |
| 291 | if !android.SuffixInList(rustc.OrderOnly.Strings(), "cc_stubs_dep.so") { |
| 292 | t.Errorf("shared cc dep not being passed as order-only to rustc %#v", rustc.OrderOnly.Strings()) |
| 293 | } |
| 294 | |
| 295 | if !android.SuffixInList(rustc.Implicits.Strings(), "cc_stubs_dep.so.toc") { |
| 296 | t.Errorf("shared cc dep TOC not being passed as implicit to rustc %#v", rustc.Implicits.Strings()) |
| 297 | } |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 298 | } |
Ivan Lozano | b9040d6 | 2019-09-24 13:23:50 -0700 | [diff] [blame] | 299 | |
Ivan Lozano | 4384568 | 2020-07-09 21:03:28 -0400 | [diff] [blame] | 300 | func TestSourceProviderDeps(t *testing.T) { |
| 301 | ctx := testRust(t, ` |
| 302 | rust_binary { |
| 303 | name: "fizz-buzz-dep", |
| 304 | srcs: [ |
| 305 | "foo.rs", |
| 306 | ":my_generator", |
| 307 | ":libbindings", |
| 308 | ], |
Ivan Lozano | 26ecd6c | 2020-07-31 13:40:31 -0400 | [diff] [blame] | 309 | rlibs: ["libbindings"], |
Ivan Lozano | 4384568 | 2020-07-09 21:03:28 -0400 | [diff] [blame] | 310 | } |
| 311 | rust_proc_macro { |
| 312 | name: "libprocmacro", |
| 313 | srcs: [ |
| 314 | "foo.rs", |
| 315 | ":my_generator", |
| 316 | ":libbindings", |
| 317 | ], |
Ivan Lozano | 26ecd6c | 2020-07-31 13:40:31 -0400 | [diff] [blame] | 318 | rlibs: ["libbindings"], |
Ivan Lozano | 4384568 | 2020-07-09 21:03:28 -0400 | [diff] [blame] | 319 | crate_name: "procmacro", |
| 320 | } |
| 321 | rust_library { |
| 322 | name: "libfoo", |
| 323 | srcs: [ |
| 324 | "foo.rs", |
| 325 | ":my_generator", |
Ivan Lozano | 26ecd6c | 2020-07-31 13:40:31 -0400 | [diff] [blame] | 326 | ":libbindings", |
| 327 | ], |
| 328 | rlibs: ["libbindings"], |
Ivan Lozano | 4384568 | 2020-07-09 21:03:28 -0400 | [diff] [blame] | 329 | crate_name: "foo", |
| 330 | } |
| 331 | genrule { |
| 332 | name: "my_generator", |
| 333 | tools: ["any_rust_binary"], |
| 334 | cmd: "$(location) -o $(out) $(in)", |
| 335 | srcs: ["src/any.h"], |
| 336 | out: ["src/any.rs"], |
| 337 | } |
Colin Cross | e9fe294 | 2020-11-10 18:12:15 -0800 | [diff] [blame] | 338 | rust_binary_host { |
| 339 | name: "any_rust_binary", |
| 340 | srcs: [ |
| 341 | "foo.rs", |
| 342 | ], |
| 343 | } |
Ivan Lozano | 4384568 | 2020-07-09 21:03:28 -0400 | [diff] [blame] | 344 | rust_bindgen { |
| 345 | name: "libbindings", |
Ivan Lozano | 26ecd6c | 2020-07-31 13:40:31 -0400 | [diff] [blame] | 346 | crate_name: "bindings", |
| 347 | source_stem: "bindings", |
Ivan Lozano | 4384568 | 2020-07-09 21:03:28 -0400 | [diff] [blame] | 348 | host_supported: true, |
| 349 | wrapper_src: "src/any.h", |
Sam Delmerico | 51d6d1c | 2023-03-28 16:54:00 -0400 | [diff] [blame^] | 350 | } |
Ivan Lozano | 4384568 | 2020-07-09 21:03:28 -0400 | [diff] [blame] | 351 | `) |
| 352 | |
Ivan Lozano | 2b08113 | 2020-09-08 12:46:52 -0400 | [diff] [blame] | 353 | libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_rlib_dylib-std").Rule("rustc") |
Ivan Lozano | 4384568 | 2020-07-09 21:03:28 -0400 | [diff] [blame] | 354 | if !android.SuffixInList(libfoo.Implicits.Strings(), "/out/bindings.rs") { |
| 355 | t.Errorf("rust_bindgen generated source not included as implicit input for libfoo; Implicits %#v", libfoo.Implicits.Strings()) |
| 356 | } |
| 357 | if !android.SuffixInList(libfoo.Implicits.Strings(), "/out/any.rs") { |
| 358 | t.Errorf("genrule generated source not included as implicit input for libfoo; Implicits %#v", libfoo.Implicits.Strings()) |
| 359 | } |
| 360 | |
| 361 | fizzBuzz := ctx.ModuleForTests("fizz-buzz-dep", "android_arm64_armv8-a").Rule("rustc") |
| 362 | if !android.SuffixInList(fizzBuzz.Implicits.Strings(), "/out/bindings.rs") { |
| 363 | t.Errorf("rust_bindgen generated source not included as implicit input for fizz-buzz-dep; Implicits %#v", libfoo.Implicits.Strings()) |
| 364 | } |
| 365 | if !android.SuffixInList(fizzBuzz.Implicits.Strings(), "/out/any.rs") { |
| 366 | t.Errorf("genrule generated source not included as implicit input for fizz-buzz-dep; Implicits %#v", libfoo.Implicits.Strings()) |
| 367 | } |
| 368 | |
| 369 | libprocmacro := ctx.ModuleForTests("libprocmacro", "linux_glibc_x86_64").Rule("rustc") |
| 370 | if !android.SuffixInList(libprocmacro.Implicits.Strings(), "/out/bindings.rs") { |
| 371 | t.Errorf("rust_bindgen generated source not included as implicit input for libprocmacro; Implicits %#v", libfoo.Implicits.Strings()) |
| 372 | } |
| 373 | if !android.SuffixInList(libprocmacro.Implicits.Strings(), "/out/any.rs") { |
| 374 | t.Errorf("genrule generated source not included as implicit input for libprocmacro; Implicits %#v", libfoo.Implicits.Strings()) |
| 375 | } |
Ivan Lozano | 26ecd6c | 2020-07-31 13:40:31 -0400 | [diff] [blame] | 376 | |
| 377 | // Check that our bindings are picked up as crate dependencies as well |
| 378 | libfooMod := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_dylib").Module().(*Module) |
Ivan Lozano | 2b08113 | 2020-09-08 12:46:52 -0400 | [diff] [blame] | 379 | if !android.InList("libbindings.dylib-std", libfooMod.Properties.AndroidMkRlibs) { |
Ivan Lozano | 26ecd6c | 2020-07-31 13:40:31 -0400 | [diff] [blame] | 380 | t.Errorf("bindgen dependency not detected as a rlib dependency (dependency missing from AndroidMkRlibs)") |
| 381 | } |
| 382 | fizzBuzzMod := ctx.ModuleForTests("fizz-buzz-dep", "android_arm64_armv8-a").Module().(*Module) |
Ivan Lozano | 2b08113 | 2020-09-08 12:46:52 -0400 | [diff] [blame] | 383 | if !android.InList("libbindings.dylib-std", fizzBuzzMod.Properties.AndroidMkRlibs) { |
Ivan Lozano | 26ecd6c | 2020-07-31 13:40:31 -0400 | [diff] [blame] | 384 | t.Errorf("bindgen dependency not detected as a rlib dependency (dependency missing from AndroidMkRlibs)") |
| 385 | } |
| 386 | libprocmacroMod := ctx.ModuleForTests("libprocmacro", "linux_glibc_x86_64").Module().(*Module) |
Ivan Lozano | 2b08113 | 2020-09-08 12:46:52 -0400 | [diff] [blame] | 387 | if !android.InList("libbindings.rlib-std", libprocmacroMod.Properties.AndroidMkRlibs) { |
Ivan Lozano | 26ecd6c | 2020-07-31 13:40:31 -0400 | [diff] [blame] | 388 | t.Errorf("bindgen dependency not detected as a rlib dependency (dependency missing from AndroidMkRlibs)") |
| 389 | } |
Ivan Lozano | 4384568 | 2020-07-09 21:03:28 -0400 | [diff] [blame] | 390 | } |
| 391 | |
Ivan Lozano | 07cbaf4 | 2020-07-22 16:09:13 -0400 | [diff] [blame] | 392 | func TestSourceProviderTargetMismatch(t *testing.T) { |
| 393 | // This might error while building the dependency tree or when calling depsToPaths() depending on the lunched |
| 394 | // target, which results in two different errors. So don't check the error, just confirm there is one. |
| 395 | testRustError(t, ".*", ` |
| 396 | rust_proc_macro { |
| 397 | name: "libprocmacro", |
| 398 | srcs: [ |
| 399 | "foo.rs", |
| 400 | ":libbindings", |
| 401 | ], |
| 402 | crate_name: "procmacro", |
| 403 | } |
| 404 | rust_bindgen { |
| 405 | name: "libbindings", |
Ivan Lozano | 26ecd6c | 2020-07-31 13:40:31 -0400 | [diff] [blame] | 406 | crate_name: "bindings", |
| 407 | source_stem: "bindings", |
Ivan Lozano | 07cbaf4 | 2020-07-22 16:09:13 -0400 | [diff] [blame] | 408 | wrapper_src: "src/any.h", |
| 409 | } |
| 410 | `) |
| 411 | } |
| 412 | |
Ivan Lozano | b9040d6 | 2019-09-24 13:23:50 -0700 | [diff] [blame] | 413 | // Test to make sure proc_macros use host variants when building device modules. |
| 414 | func TestProcMacroDeviceDeps(t *testing.T) { |
| 415 | ctx := testRust(t, ` |
| 416 | rust_library_host_rlib { |
| 417 | name: "libbar", |
| 418 | srcs: ["foo.rs"], |
Ivan Lozano | ad8b18b | 2019-10-31 19:38:29 -0700 | [diff] [blame] | 419 | crate_name: "bar", |
Ivan Lozano | b9040d6 | 2019-09-24 13:23:50 -0700 | [diff] [blame] | 420 | } |
| 421 | rust_proc_macro { |
| 422 | name: "libpm", |
| 423 | rlibs: ["libbar"], |
| 424 | srcs: ["foo.rs"], |
Ivan Lozano | ad8b18b | 2019-10-31 19:38:29 -0700 | [diff] [blame] | 425 | crate_name: "pm", |
Ivan Lozano | b9040d6 | 2019-09-24 13:23:50 -0700 | [diff] [blame] | 426 | } |
| 427 | rust_binary { |
| 428 | name: "fizz-buzz", |
| 429 | proc_macros: ["libpm"], |
| 430 | srcs: ["foo.rs"], |
| 431 | } |
| 432 | `) |
| 433 | rustc := ctx.ModuleForTests("libpm", "linux_glibc_x86_64").Rule("rustc") |
| 434 | |
| 435 | if !strings.Contains(rustc.Args["libFlags"], "libbar/linux_glibc_x86_64") { |
| 436 | t.Errorf("Proc_macro is not using host variant of dependent modules.") |
| 437 | } |
| 438 | } |
Matthew Maurer | 99020b0 | 2019-10-31 10:44:40 -0700 | [diff] [blame] | 439 | |
| 440 | // Test that no_stdlibs suppresses dependencies on rust standard libraries |
| 441 | func TestNoStdlibs(t *testing.T) { |
| 442 | ctx := testRust(t, ` |
| 443 | rust_binary { |
| 444 | name: "fizz-buzz", |
| 445 | srcs: ["foo.rs"], |
Ivan Lozano | 9d1df10 | 2020-04-28 10:10:23 -0400 | [diff] [blame] | 446 | no_stdlibs: true, |
Matthew Maurer | 99020b0 | 2019-10-31 10:44:40 -0700 | [diff] [blame] | 447 | }`) |
Colin Cross | 7113d20 | 2019-11-20 16:39:12 -0800 | [diff] [blame] | 448 | module := ctx.ModuleForTests("fizz-buzz", "android_arm64_armv8-a").Module().(*Module) |
Matthew Maurer | 99020b0 | 2019-10-31 10:44:40 -0700 | [diff] [blame] | 449 | |
| 450 | if android.InList("libstd", module.Properties.AndroidMkDylibs) { |
| 451 | t.Errorf("no_stdlibs did not suppress dependency on libstd") |
| 452 | } |
| 453 | } |
Ivan Lozano | 9d1df10 | 2020-04-28 10:10:23 -0400 | [diff] [blame] | 454 | |
| 455 | // Test that libraries provide both 32-bit and 64-bit variants. |
| 456 | func TestMultilib(t *testing.T) { |
| 457 | ctx := testRust(t, ` |
| 458 | rust_library_rlib { |
| 459 | name: "libfoo", |
| 460 | srcs: ["foo.rs"], |
| 461 | crate_name: "foo", |
| 462 | }`) |
| 463 | |
Ivan Lozano | 2b08113 | 2020-09-08 12:46:52 -0400 | [diff] [blame] | 464 | _ = ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_rlib_dylib-std") |
| 465 | _ = ctx.ModuleForTests("libfoo", "android_arm_armv7-a-neon_rlib_dylib-std") |
Ivan Lozano | 9d1df10 | 2020-04-28 10:10:23 -0400 | [diff] [blame] | 466 | } |
Thiébaud Weksteen | e4dd14b | 2021-04-14 11:18:47 +0200 | [diff] [blame] | 467 | |
| 468 | // Test that library size measurements are generated. |
| 469 | func TestLibrarySizes(t *testing.T) { |
| 470 | ctx := testRust(t, ` |
| 471 | rust_library_dylib { |
| 472 | name: "libwaldo", |
| 473 | srcs: ["foo.rs"], |
| 474 | crate_name: "waldo", |
| 475 | }`) |
| 476 | |
| 477 | m := ctx.SingletonForTests("file_metrics") |
Ivan Lozano | 8d10fc3 | 2021-11-05 16:36:47 -0400 | [diff] [blame] | 478 | m.Output("unstripped/libwaldo.dylib.so.bloaty.csv") |
Thiébaud Weksteen | e4dd14b | 2021-04-14 11:18:47 +0200 | [diff] [blame] | 479 | m.Output("libwaldo.dylib.so.bloaty.csv") |
Thiébaud Weksteen | e4dd14b | 2021-04-14 11:18:47 +0200 | [diff] [blame] | 480 | } |
Ivan Lozano | 62cd038 | 2021-11-01 10:27:54 -0400 | [diff] [blame] | 481 | |
| 482 | func assertString(t *testing.T, got, expected string) { |
| 483 | t.Helper() |
| 484 | if got != expected { |
| 485 | t.Errorf("expected %q got %q", expected, got) |
| 486 | } |
| 487 | } |