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