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