| # Copyright 2023 The Chromium Authors |
| # Use of this source code is governed by a BSD-style license that can be |
| # found in the LICENSE file. |
| |
| import("//build/config/rust.gni") |
| import("//build/rust/rust_executable.gni") |
| import("//build/rust/rust_static_library.gni") |
| import("//build/rust/rust_unit_test.gni") |
| |
| # This target depends on two variants of the same crate: one directly, and one |
| # transitively. With correct metadata handling, this will work. |
| rust_static_library("test_rust_metadata_lib") { |
| crate_name = "lib" |
| crate_root = "lib.rs" |
| sources = [ "lib.rs" ] |
| deps = [ |
| ":foo_dependency", |
| ":transitive_dep_2", |
| ] |
| |
| # Depending on the other variant directly will fail, as expected. rustc |
| # gives |
| # |
| # error[E0464]: multiple candidates for `rlib` dependency `transitive_dep` |
| # found |
| # |
| # deps += [":transitive_dep_1"] |
| |
| # We also test this in a C++ binary, so we want a #[no_mangle] fn. This is |
| # considered unsafe. |
| allow_unsafe = true |
| } |
| |
| if (can_build_rust_unit_tests) { |
| # Tests that the different variants return the expected strings. |
| rust_unit_test("test_rust_metadata_unittests") { |
| crate_root = "tests.rs" |
| sources = [ "tests.rs" ] |
| deps = [ ":test_rust_metadata_lib" ] |
| } |
| } |
| |
| rust_executable("test_rust_metadata_exe") { |
| crate_root = "main.rs" |
| sources = [ "main.rs" ] |
| deps = [ ":test_rust_metadata_lib" ] |
| } |
| |
| # Check that the metadata handling works when linking into a C++ binary too. |
| executable("test_rust_metadata_cc_exe") { |
| sources = [ "main.cc" ] |
| deps = [ ":test_rust_metadata_lib" ] |
| } |
| |
| # A source file whose behavior depends on cfg options. |
| rust_static_library("transitive_dep_1") { |
| crate_name = "transitive_dep" |
| crate_root = "transitive_dep.rs" |
| sources = [ "transitive_dep.rs" ] |
| |
| rustc_metadata = "foo" |
| } |
| |
| # Build the same source again, but with a feature enabled. The metadata should |
| # disambiguate the symbols when linking. |
| rust_static_library("transitive_dep_2") { |
| crate_name = "transitive_dep" |
| crate_root = "transitive_dep.rs" |
| sources = [ "transitive_dep.rs" ] |
| |
| rustc_metadata = "bar" |
| features = [ "bar_feature" ] |
| } |
| |
| # Include one version transitively, since otherwise the names in Rust will |
| # conflict. |
| rust_static_library("foo_dependency") { |
| crate_root = "foo_dependency.rs" |
| sources = [ "foo_dependency.rs" ] |
| deps = [ ":transitive_dep_1" ] |
| } |