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