1# Copyright 2023 The Chromium Authors 2# Use of this source code is governed by a BSD-style license that can be 3# found in the LICENSE file. 4 5import("//build/config/rust.gni") 6import("//build/rust/cargo_crate.gni") 7import("//build/rust/rust_executable.gni") 8import("//build/rust/rust_static_library.gni") 9import("//build/rust/rust_unit_test.gni") 10 11# This target depends on two variants of the same crate: one directly, and one 12# transitively. With correct metadata handling, this will work. 13rust_static_library("lib") { 14 crate_root = "lib.rs" 15 sources = [ "lib.rs" ] 16 deps = [ 17 ":foo_dependency", 18 ":transitive_dep_2", 19 ] 20 21 # Depending on the other variant directly will fail, as expected. rustc 22 # gives 23 # 24 # error[E0464]: multiple candidates for `rlib` dependency `transitive_dep` 25 # found 26 # 27 # deps += [":transitive_dep_1"] 28 29 # We also test this in a C++ binary, so we want a #[no_mangle] fn. This is 30 # considered unsafe. 31 allow_unsafe = true 32} 33 34if (can_build_rust_unit_tests) { 35 # Tests that the different variants return the expected strings. 36 rust_unit_test("test_rust_metadata_unittests") { 37 crate_root = "tests.rs" 38 sources = [ "tests.rs" ] 39 deps = [ ":lib" ] 40 } 41} 42 43rust_executable("test_rust_metadata_exe") { 44 crate_root = "main.rs" 45 sources = [ "main.rs" ] 46 deps = [ ":lib" ] 47} 48 49# Check that the metadata handling works when linking into a C++ binary too. 50executable("test_rust_metadata_cc_exe") { 51 sources = [ "main.cc" ] 52 deps = [ ":lib" ] 53} 54 55# A source file whose behavior depends on cfg options. 56cargo_crate("transitive_dep_1") { 57 crate_name = "transitive_dep" 58 crate_root = "transitive_dep.rs" 59 sources = [ "transitive_dep.rs" ] 60 61 rustc_metadata = "foo" 62} 63 64# Build the same source again, but with a feature enabled. The metadata should 65# disambiguate the symbols when linking. 66cargo_crate("transitive_dep_2") { 67 crate_name = "transitive_dep" 68 crate_root = "transitive_dep.rs" 69 sources = [ "transitive_dep.rs" ] 70 71 rustc_metadata = "bar" 72 features = [ "bar_feature" ] 73} 74 75# Include one version transitively, since otherwise the names in Rust will 76# conflict. 77rust_static_library("foo_dependency") { 78 crate_root = "foo_dependency.rs" 79 sources = [ "foo_dependency.rs" ] 80 deps = [ ":transitive_dep_1" ] 81} 82