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