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 ( |
| 18 | "io/ioutil" |
| 19 | "os" |
Ivan Lozano | c008361 | 2019-09-03 13:49:39 -0700 | [diff] [blame] | 20 | "runtime" |
Ivan Lozano | b9040d6 | 2019-09-24 13:23:50 -0700 | [diff] [blame] | 21 | "strings" |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 22 | "testing" |
| 23 | |
Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 24 | "github.com/google/blueprint/proptools" |
| 25 | |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 26 | "android/soong/android" |
Colin Cross | f28329d | 2020-02-15 11:00:10 -0800 | [diff] [blame] | 27 | "android/soong/cc" |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 28 | ) |
| 29 | |
| 30 | var ( |
| 31 | buildDir string |
| 32 | ) |
| 33 | |
| 34 | func setUp() { |
| 35 | var err error |
| 36 | buildDir, err = ioutil.TempDir("", "soong_rust_test") |
| 37 | if err != nil { |
| 38 | panic(err) |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | func tearDown() { |
| 43 | os.RemoveAll(buildDir) |
| 44 | } |
| 45 | |
| 46 | func TestMain(m *testing.M) { |
| 47 | run := func() int { |
| 48 | setUp() |
| 49 | defer tearDown() |
| 50 | |
| 51 | return m.Run() |
| 52 | } |
| 53 | |
| 54 | os.Exit(run()) |
| 55 | } |
| 56 | |
Thiébaud Weksteen | 0a75e52 | 2020-10-07 14:30:03 +0200 | [diff] [blame] | 57 | // testRust returns a TestContext in which a basic environment has been setup. |
| 58 | // This environment contains a few mocked files. See testRustCtx.useMockedFs |
| 59 | // for the list of these files. |
| 60 | func testRust(t *testing.T, bp string) *android.TestContext { |
| 61 | tctx := newTestRustCtx(t, bp) |
| 62 | tctx.useMockedFs() |
| 63 | tctx.generateConfig() |
| 64 | return tctx.parse(t) |
| 65 | } |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 66 | |
Ivan Lozano | f76cdf7 | 2021-02-12 09:55:06 -0500 | [diff] [blame] | 67 | func testRustVndk(t *testing.T, bp string) *android.TestContext { |
| 68 | tctx := newTestRustCtx(t, bp) |
| 69 | tctx.useMockedFs() |
| 70 | tctx.generateConfig() |
| 71 | tctx.setVndk(t) |
| 72 | return tctx.parse(t) |
| 73 | } |
| 74 | |
Thiébaud Weksteen | 0a75e52 | 2020-10-07 14:30:03 +0200 | [diff] [blame] | 75 | // testRustCov returns a TestContext in which a basic environment has been |
| 76 | // setup. This environment explicitly enables coverage. |
| 77 | func testRustCov(t *testing.T, bp string) *android.TestContext { |
| 78 | tctx := newTestRustCtx(t, bp) |
| 79 | tctx.useMockedFs() |
| 80 | tctx.generateConfig() |
| 81 | tctx.enableCoverage(t) |
| 82 | return tctx.parse(t) |
| 83 | } |
| 84 | |
| 85 | // testRustError ensures that at least one error was raised and its value |
| 86 | // matches the pattern provided. The error can be either in the parsing of the |
| 87 | // Blueprint or when generating the build actions. |
| 88 | func testRustError(t *testing.T, pattern string, bp string) { |
| 89 | tctx := newTestRustCtx(t, bp) |
| 90 | tctx.useMockedFs() |
| 91 | tctx.generateConfig() |
| 92 | tctx.parseError(t, pattern) |
| 93 | } |
| 94 | |
| 95 | // testRustCtx is used to build a particular test environment. Unless your |
| 96 | // tests requires a specific setup, prefer the wrapping functions: testRust, |
| 97 | // testRustCov or testRustError. |
| 98 | type testRustCtx struct { |
| 99 | bp string |
| 100 | fs map[string][]byte |
| 101 | env map[string]string |
| 102 | config *android.Config |
| 103 | } |
| 104 | |
| 105 | // newTestRustCtx returns a new testRustCtx for the Blueprint definition argument. |
| 106 | func newTestRustCtx(t *testing.T, bp string) *testRustCtx { |
| 107 | // TODO (b/140435149) |
| 108 | if runtime.GOOS != "linux" { |
| 109 | t.Skip("Rust Soong tests can only be run on Linux hosts currently") |
| 110 | } |
| 111 | return &testRustCtx{bp: bp} |
| 112 | } |
| 113 | |
| 114 | // useMockedFs setup a default mocked filesystem for the test environment. |
| 115 | func (tctx *testRustCtx) useMockedFs() { |
| 116 | tctx.fs = map[string][]byte{ |
Ivan Lozano | 6eff900 | 2020-12-11 15:25:59 -0500 | [diff] [blame] | 117 | "foo.rs": nil, |
| 118 | "foo.c": nil, |
| 119 | "src/bar.rs": nil, |
| 120 | "src/any.h": nil, |
| 121 | "proto.proto": nil, |
| 122 | "proto/buf.proto": nil, |
| 123 | "buf.proto": nil, |
| 124 | "foo.proto": nil, |
| 125 | "liby.so": nil, |
| 126 | "libz.so": nil, |
Ivan Lozano | 9da4aa8 | 2021-01-29 12:48:05 -0500 | [diff] [blame] | 127 | "data.txt": nil, |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 128 | } |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 129 | } |
| 130 | |
Thiébaud Weksteen | 0a75e52 | 2020-10-07 14:30:03 +0200 | [diff] [blame] | 131 | // generateConfig creates the android.Config based on the bp, fs and env |
| 132 | // attributes of the testRustCtx. |
| 133 | func (tctx *testRustCtx) generateConfig() { |
| 134 | tctx.bp = tctx.bp + GatherRequiredDepsForTest() |
Jiyong Park | 99644e9 | 2020-11-17 22:21:02 +0900 | [diff] [blame] | 135 | tctx.bp = tctx.bp + cc.GatherRequiredDepsForTest(android.NoOsType) |
Thiébaud Weksteen | 0a75e52 | 2020-10-07 14:30:03 +0200 | [diff] [blame] | 136 | cc.GatherRequiredFilesForTest(tctx.fs) |
| 137 | config := android.TestArchConfig(buildDir, tctx.env, tctx.bp, tctx.fs) |
| 138 | tctx.config = &config |
Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 139 | } |
| 140 | |
Thiébaud Weksteen | 0a75e52 | 2020-10-07 14:30:03 +0200 | [diff] [blame] | 141 | // enableCoverage configures the test to enable coverage. |
| 142 | func (tctx *testRustCtx) enableCoverage(t *testing.T) { |
| 143 | if tctx.config == nil { |
| 144 | t.Fatalf("tctx.config not been generated yet. Please call generateConfig first.") |
Ivan Lozano | c008361 | 2019-09-03 13:49:39 -0700 | [diff] [blame] | 145 | } |
Joel Galenson | fa04938 | 2021-01-14 16:03:18 -0800 | [diff] [blame] | 146 | tctx.config.TestProductVariables.ClangCoverage = proptools.BoolPtr(true) |
Thiébaud Weksteen | 0a75e52 | 2020-10-07 14:30:03 +0200 | [diff] [blame] | 147 | tctx.config.TestProductVariables.Native_coverage = proptools.BoolPtr(true) |
| 148 | tctx.config.TestProductVariables.NativeCoveragePaths = []string{"*"} |
| 149 | } |
Ivan Lozano | c008361 | 2019-09-03 13:49:39 -0700 | [diff] [blame] | 150 | |
Ivan Lozano | f76cdf7 | 2021-02-12 09:55:06 -0500 | [diff] [blame] | 151 | func (tctx *testRustCtx) setVndk(t *testing.T) { |
| 152 | if tctx.config == nil { |
| 153 | t.Fatalf("tctx.config not been generated yet. Please call generateConfig first.") |
| 154 | } |
| 155 | tctx.config.TestProductVariables.DeviceVndkVersion = StringPtr("current") |
| 156 | tctx.config.TestProductVariables.ProductVndkVersion = StringPtr("current") |
| 157 | tctx.config.TestProductVariables.Platform_vndk_version = StringPtr("VER") |
| 158 | } |
| 159 | |
Thiébaud Weksteen | 0a75e52 | 2020-10-07 14:30:03 +0200 | [diff] [blame] | 160 | // parse validates the configuration and parses the Blueprint file. It returns |
| 161 | // a TestContext which can be used to retrieve the generated modules via |
| 162 | // ModuleForTests. |
| 163 | func (tctx testRustCtx) parse(t *testing.T) *android.TestContext { |
| 164 | if tctx.config == nil { |
| 165 | t.Fatalf("tctx.config not been generated yet. Please call generateConfig first.") |
Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 166 | } |
Colin Cross | ae8600b | 2020-10-29 17:09:13 -0700 | [diff] [blame] | 167 | ctx := CreateTestContext(*tctx.config) |
| 168 | ctx.Register() |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 169 | _, errs := ctx.ParseFileList(".", []string{"Android.bp"}) |
| 170 | android.FailIfErrored(t, errs) |
Thiébaud Weksteen | 0a75e52 | 2020-10-07 14:30:03 +0200 | [diff] [blame] | 171 | _, errs = ctx.PrepareBuildActions(*tctx.config) |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 172 | android.FailIfErrored(t, errs) |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 173 | return ctx |
| 174 | } |
| 175 | |
Thiébaud Weksteen | 0a75e52 | 2020-10-07 14:30:03 +0200 | [diff] [blame] | 176 | // parseError parses the Blueprint file and ensure that at least one error |
| 177 | // matching the provided pattern is observed. |
| 178 | func (tctx testRustCtx) parseError(t *testing.T, pattern string) { |
| 179 | if tctx.config == nil { |
| 180 | t.Fatalf("tctx.config not been generated yet. Please call generateConfig first.") |
Ivan Lozano | c008361 | 2019-09-03 13:49:39 -0700 | [diff] [blame] | 181 | } |
Colin Cross | ae8600b | 2020-10-29 17:09:13 -0700 | [diff] [blame] | 182 | ctx := CreateTestContext(*tctx.config) |
| 183 | ctx.Register() |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 184 | |
| 185 | _, errs := ctx.ParseFileList(".", []string{"Android.bp"}) |
| 186 | if len(errs) > 0 { |
| 187 | android.FailIfNoMatchingErrors(t, pattern, errs) |
| 188 | return |
| 189 | } |
| 190 | |
Thiébaud Weksteen | 0a75e52 | 2020-10-07 14:30:03 +0200 | [diff] [blame] | 191 | _, errs = ctx.PrepareBuildActions(*tctx.config) |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 192 | if len(errs) > 0 { |
| 193 | android.FailIfNoMatchingErrors(t, pattern, errs) |
| 194 | return |
| 195 | } |
| 196 | |
| 197 | t.Fatalf("missing expected error %q (0 errors are returned)", pattern) |
| 198 | } |
| 199 | |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 200 | // Test that we can extract the link path from a lib path. |
| 201 | func TestLinkPathFromFilePath(t *testing.T) { |
| 202 | barPath := android.PathForTesting("out/soong/.intermediates/external/libbar/libbar/linux_glibc_x86_64_shared/libbar.so") |
| 203 | libName := linkPathFromFilePath(barPath) |
| 204 | expectedResult := "out/soong/.intermediates/external/libbar/libbar/linux_glibc_x86_64_shared/" |
| 205 | |
| 206 | if libName != expectedResult { |
| 207 | t.Errorf("libNameFromFilePath returned the wrong name; expected '%#v', got '%#v'", expectedResult, libName) |
| 208 | } |
| 209 | } |
| 210 | |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 211 | // Test to make sure dependencies are being picked up correctly. |
| 212 | func TestDepsTracking(t *testing.T) { |
| 213 | ctx := testRust(t, ` |
Matthew Maurer | 2ae0513 | 2020-06-23 14:28:53 -0700 | [diff] [blame] | 214 | rust_ffi_host_static { |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 215 | name: "libstatic", |
| 216 | srcs: ["foo.rs"], |
Ivan Lozano | ad8b18b | 2019-10-31 19:38:29 -0700 | [diff] [blame] | 217 | crate_name: "static", |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 218 | } |
Matthew Maurer | 2ae0513 | 2020-06-23 14:28:53 -0700 | [diff] [blame] | 219 | rust_ffi_host_shared { |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 220 | name: "libshared", |
| 221 | srcs: ["foo.rs"], |
Ivan Lozano | ad8b18b | 2019-10-31 19:38:29 -0700 | [diff] [blame] | 222 | crate_name: "shared", |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 223 | } |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 224 | rust_library_host_dylib { |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 225 | name: "libdylib", |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 226 | srcs: ["foo.rs"], |
Ivan Lozano | ad8b18b | 2019-10-31 19:38:29 -0700 | [diff] [blame] | 227 | crate_name: "dylib", |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 228 | } |
| 229 | rust_library_host_rlib { |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 230 | name: "librlib", |
Ivan Lozano | 4384568 | 2020-07-09 21:03:28 -0400 | [diff] [blame] | 231 | srcs: ["foo.rs"], |
Ivan Lozano | ad8b18b | 2019-10-31 19:38:29 -0700 | [diff] [blame] | 232 | crate_name: "rlib", |
Ivan Lozano | fb6f36f | 2021-02-05 12:27:08 -0500 | [diff] [blame] | 233 | static_libs: ["libstatic"], |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 234 | } |
| 235 | rust_proc_macro { |
| 236 | name: "libpm", |
| 237 | srcs: ["foo.rs"], |
Ivan Lozano | ad8b18b | 2019-10-31 19:38:29 -0700 | [diff] [blame] | 238 | crate_name: "pm", |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 239 | } |
| 240 | rust_binary_host { |
Ivan Lozano | 4384568 | 2020-07-09 21:03:28 -0400 | [diff] [blame] | 241 | name: "fizz-buzz", |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 242 | dylibs: ["libdylib"], |
| 243 | rlibs: ["librlib"], |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 244 | proc_macros: ["libpm"], |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 245 | static_libs: ["libstatic"], |
| 246 | shared_libs: ["libshared"], |
Ivan Lozano | 4384568 | 2020-07-09 21:03:28 -0400 | [diff] [blame] | 247 | srcs: ["foo.rs"], |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 248 | } |
| 249 | `) |
Ivan Lozano | 4384568 | 2020-07-09 21:03:28 -0400 | [diff] [blame] | 250 | module := ctx.ModuleForTests("fizz-buzz", "linux_glibc_x86_64").Module().(*Module) |
Ivan Lozano | fb6f36f | 2021-02-05 12:27:08 -0500 | [diff] [blame] | 251 | 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] | 252 | |
| 253 | // Since dependencies are added to AndroidMk* properties, we can check these to see if they've been picked up. |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 254 | if !android.InList("libdylib", module.Properties.AndroidMkDylibs) { |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 255 | t.Errorf("Dylib dependency not detected (dependency missing from AndroidMkDylibs)") |
| 256 | } |
| 257 | |
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 | |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 266 | if !android.InList("libshared", module.Properties.AndroidMkSharedLibs) { |
| 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 | |
| 274 | if !strings.Contains(rustc.Args["rustcFlags"], "-lstatic=static") { |
| 275 | t.Errorf("-lstatic flag not being passed to rustc for static library") |
| 276 | } |
| 277 | |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 278 | } |
Ivan Lozano | b9040d6 | 2019-09-24 13:23:50 -0700 | [diff] [blame] | 279 | |
Ivan Lozano | 4384568 | 2020-07-09 21:03:28 -0400 | [diff] [blame] | 280 | func TestSourceProviderDeps(t *testing.T) { |
| 281 | ctx := testRust(t, ` |
| 282 | rust_binary { |
| 283 | name: "fizz-buzz-dep", |
| 284 | srcs: [ |
| 285 | "foo.rs", |
| 286 | ":my_generator", |
| 287 | ":libbindings", |
| 288 | ], |
Ivan Lozano | 26ecd6c | 2020-07-31 13:40:31 -0400 | [diff] [blame] | 289 | rlibs: ["libbindings"], |
Ivan Lozano | 4384568 | 2020-07-09 21:03:28 -0400 | [diff] [blame] | 290 | } |
| 291 | rust_proc_macro { |
| 292 | name: "libprocmacro", |
| 293 | srcs: [ |
| 294 | "foo.rs", |
| 295 | ":my_generator", |
| 296 | ":libbindings", |
| 297 | ], |
Ivan Lozano | 26ecd6c | 2020-07-31 13:40:31 -0400 | [diff] [blame] | 298 | rlibs: ["libbindings"], |
Ivan Lozano | 4384568 | 2020-07-09 21:03:28 -0400 | [diff] [blame] | 299 | crate_name: "procmacro", |
| 300 | } |
| 301 | rust_library { |
| 302 | name: "libfoo", |
| 303 | srcs: [ |
| 304 | "foo.rs", |
| 305 | ":my_generator", |
Ivan Lozano | 26ecd6c | 2020-07-31 13:40:31 -0400 | [diff] [blame] | 306 | ":libbindings", |
| 307 | ], |
| 308 | rlibs: ["libbindings"], |
Ivan Lozano | 4384568 | 2020-07-09 21:03:28 -0400 | [diff] [blame] | 309 | crate_name: "foo", |
| 310 | } |
| 311 | genrule { |
| 312 | name: "my_generator", |
| 313 | tools: ["any_rust_binary"], |
| 314 | cmd: "$(location) -o $(out) $(in)", |
| 315 | srcs: ["src/any.h"], |
| 316 | out: ["src/any.rs"], |
| 317 | } |
Colin Cross | e9fe294 | 2020-11-10 18:12:15 -0800 | [diff] [blame] | 318 | rust_binary_host { |
| 319 | name: "any_rust_binary", |
| 320 | srcs: [ |
| 321 | "foo.rs", |
| 322 | ], |
| 323 | } |
Ivan Lozano | 4384568 | 2020-07-09 21:03:28 -0400 | [diff] [blame] | 324 | rust_bindgen { |
| 325 | name: "libbindings", |
Ivan Lozano | 26ecd6c | 2020-07-31 13:40:31 -0400 | [diff] [blame] | 326 | crate_name: "bindings", |
| 327 | source_stem: "bindings", |
Ivan Lozano | 4384568 | 2020-07-09 21:03:28 -0400 | [diff] [blame] | 328 | host_supported: true, |
| 329 | wrapper_src: "src/any.h", |
| 330 | } |
| 331 | `) |
| 332 | |
Ivan Lozano | 2b08113 | 2020-09-08 12:46:52 -0400 | [diff] [blame] | 333 | 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] | 334 | if !android.SuffixInList(libfoo.Implicits.Strings(), "/out/bindings.rs") { |
| 335 | t.Errorf("rust_bindgen generated source not included as implicit input for libfoo; Implicits %#v", libfoo.Implicits.Strings()) |
| 336 | } |
| 337 | if !android.SuffixInList(libfoo.Implicits.Strings(), "/out/any.rs") { |
| 338 | t.Errorf("genrule generated source not included as implicit input for libfoo; Implicits %#v", libfoo.Implicits.Strings()) |
| 339 | } |
| 340 | |
| 341 | fizzBuzz := ctx.ModuleForTests("fizz-buzz-dep", "android_arm64_armv8-a").Rule("rustc") |
| 342 | if !android.SuffixInList(fizzBuzz.Implicits.Strings(), "/out/bindings.rs") { |
| 343 | t.Errorf("rust_bindgen generated source not included as implicit input for fizz-buzz-dep; Implicits %#v", libfoo.Implicits.Strings()) |
| 344 | } |
| 345 | if !android.SuffixInList(fizzBuzz.Implicits.Strings(), "/out/any.rs") { |
| 346 | t.Errorf("genrule generated source not included as implicit input for fizz-buzz-dep; Implicits %#v", libfoo.Implicits.Strings()) |
| 347 | } |
| 348 | |
| 349 | libprocmacro := ctx.ModuleForTests("libprocmacro", "linux_glibc_x86_64").Rule("rustc") |
| 350 | if !android.SuffixInList(libprocmacro.Implicits.Strings(), "/out/bindings.rs") { |
| 351 | t.Errorf("rust_bindgen generated source not included as implicit input for libprocmacro; Implicits %#v", libfoo.Implicits.Strings()) |
| 352 | } |
| 353 | if !android.SuffixInList(libprocmacro.Implicits.Strings(), "/out/any.rs") { |
| 354 | t.Errorf("genrule generated source not included as implicit input for libprocmacro; Implicits %#v", libfoo.Implicits.Strings()) |
| 355 | } |
Ivan Lozano | 26ecd6c | 2020-07-31 13:40:31 -0400 | [diff] [blame] | 356 | |
| 357 | // Check that our bindings are picked up as crate dependencies as well |
| 358 | libfooMod := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_dylib").Module().(*Module) |
Ivan Lozano | 2b08113 | 2020-09-08 12:46:52 -0400 | [diff] [blame] | 359 | if !android.InList("libbindings.dylib-std", libfooMod.Properties.AndroidMkRlibs) { |
Ivan Lozano | 26ecd6c | 2020-07-31 13:40:31 -0400 | [diff] [blame] | 360 | t.Errorf("bindgen dependency not detected as a rlib dependency (dependency missing from AndroidMkRlibs)") |
| 361 | } |
| 362 | fizzBuzzMod := ctx.ModuleForTests("fizz-buzz-dep", "android_arm64_armv8-a").Module().(*Module) |
Ivan Lozano | 2b08113 | 2020-09-08 12:46:52 -0400 | [diff] [blame] | 363 | if !android.InList("libbindings.dylib-std", fizzBuzzMod.Properties.AndroidMkRlibs) { |
Ivan Lozano | 26ecd6c | 2020-07-31 13:40:31 -0400 | [diff] [blame] | 364 | t.Errorf("bindgen dependency not detected as a rlib dependency (dependency missing from AndroidMkRlibs)") |
| 365 | } |
| 366 | libprocmacroMod := ctx.ModuleForTests("libprocmacro", "linux_glibc_x86_64").Module().(*Module) |
Ivan Lozano | 2b08113 | 2020-09-08 12:46:52 -0400 | [diff] [blame] | 367 | if !android.InList("libbindings.rlib-std", libprocmacroMod.Properties.AndroidMkRlibs) { |
Ivan Lozano | 26ecd6c | 2020-07-31 13:40:31 -0400 | [diff] [blame] | 368 | t.Errorf("bindgen dependency not detected as a rlib dependency (dependency missing from AndroidMkRlibs)") |
| 369 | } |
| 370 | |
Ivan Lozano | 4384568 | 2020-07-09 21:03:28 -0400 | [diff] [blame] | 371 | } |
| 372 | |
Ivan Lozano | 07cbaf4 | 2020-07-22 16:09:13 -0400 | [diff] [blame] | 373 | func TestSourceProviderTargetMismatch(t *testing.T) { |
| 374 | // This might error while building the dependency tree or when calling depsToPaths() depending on the lunched |
| 375 | // target, which results in two different errors. So don't check the error, just confirm there is one. |
| 376 | testRustError(t, ".*", ` |
| 377 | rust_proc_macro { |
| 378 | name: "libprocmacro", |
| 379 | srcs: [ |
| 380 | "foo.rs", |
| 381 | ":libbindings", |
| 382 | ], |
| 383 | crate_name: "procmacro", |
| 384 | } |
| 385 | rust_bindgen { |
| 386 | name: "libbindings", |
Ivan Lozano | 26ecd6c | 2020-07-31 13:40:31 -0400 | [diff] [blame] | 387 | crate_name: "bindings", |
| 388 | source_stem: "bindings", |
Ivan Lozano | 07cbaf4 | 2020-07-22 16:09:13 -0400 | [diff] [blame] | 389 | wrapper_src: "src/any.h", |
| 390 | } |
| 391 | `) |
| 392 | } |
| 393 | |
Ivan Lozano | b9040d6 | 2019-09-24 13:23:50 -0700 | [diff] [blame] | 394 | // Test to make sure proc_macros use host variants when building device modules. |
| 395 | func TestProcMacroDeviceDeps(t *testing.T) { |
| 396 | ctx := testRust(t, ` |
| 397 | rust_library_host_rlib { |
| 398 | name: "libbar", |
| 399 | srcs: ["foo.rs"], |
Ivan Lozano | ad8b18b | 2019-10-31 19:38:29 -0700 | [diff] [blame] | 400 | crate_name: "bar", |
Ivan Lozano | b9040d6 | 2019-09-24 13:23:50 -0700 | [diff] [blame] | 401 | } |
| 402 | rust_proc_macro { |
| 403 | name: "libpm", |
| 404 | rlibs: ["libbar"], |
| 405 | srcs: ["foo.rs"], |
Ivan Lozano | ad8b18b | 2019-10-31 19:38:29 -0700 | [diff] [blame] | 406 | crate_name: "pm", |
Ivan Lozano | b9040d6 | 2019-09-24 13:23:50 -0700 | [diff] [blame] | 407 | } |
| 408 | rust_binary { |
| 409 | name: "fizz-buzz", |
| 410 | proc_macros: ["libpm"], |
| 411 | srcs: ["foo.rs"], |
| 412 | } |
| 413 | `) |
| 414 | rustc := ctx.ModuleForTests("libpm", "linux_glibc_x86_64").Rule("rustc") |
| 415 | |
| 416 | if !strings.Contains(rustc.Args["libFlags"], "libbar/linux_glibc_x86_64") { |
| 417 | t.Errorf("Proc_macro is not using host variant of dependent modules.") |
| 418 | } |
| 419 | } |
Matthew Maurer | 99020b0 | 2019-10-31 10:44:40 -0700 | [diff] [blame] | 420 | |
| 421 | // Test that no_stdlibs suppresses dependencies on rust standard libraries |
| 422 | func TestNoStdlibs(t *testing.T) { |
| 423 | ctx := testRust(t, ` |
| 424 | rust_binary { |
| 425 | name: "fizz-buzz", |
| 426 | srcs: ["foo.rs"], |
Ivan Lozano | 9d1df10 | 2020-04-28 10:10:23 -0400 | [diff] [blame] | 427 | no_stdlibs: true, |
Matthew Maurer | 99020b0 | 2019-10-31 10:44:40 -0700 | [diff] [blame] | 428 | }`) |
Colin Cross | 7113d20 | 2019-11-20 16:39:12 -0800 | [diff] [blame] | 429 | module := ctx.ModuleForTests("fizz-buzz", "android_arm64_armv8-a").Module().(*Module) |
Matthew Maurer | 99020b0 | 2019-10-31 10:44:40 -0700 | [diff] [blame] | 430 | |
| 431 | if android.InList("libstd", module.Properties.AndroidMkDylibs) { |
| 432 | t.Errorf("no_stdlibs did not suppress dependency on libstd") |
| 433 | } |
| 434 | } |
Ivan Lozano | 9d1df10 | 2020-04-28 10:10:23 -0400 | [diff] [blame] | 435 | |
| 436 | // Test that libraries provide both 32-bit and 64-bit variants. |
| 437 | func TestMultilib(t *testing.T) { |
| 438 | ctx := testRust(t, ` |
| 439 | rust_library_rlib { |
| 440 | name: "libfoo", |
| 441 | srcs: ["foo.rs"], |
| 442 | crate_name: "foo", |
| 443 | }`) |
| 444 | |
Ivan Lozano | 2b08113 | 2020-09-08 12:46:52 -0400 | [diff] [blame] | 445 | _ = ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_rlib_dylib-std") |
| 446 | _ = ctx.ModuleForTests("libfoo", "android_arm_armv7-a-neon_rlib_dylib-std") |
Ivan Lozano | 9d1df10 | 2020-04-28 10:10:23 -0400 | [diff] [blame] | 447 | } |