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