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