Source code

Revision control

Copy as Markdown

Other Tools

# 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("//chromium/build/config/rust.gni")
import("//chromium/build/rust/cargo_crate.gni")
import("//chromium/build/rust/rust_executable.gni")
import("//chromium/build/rust/rust_static_library.gni")
import("//chromium/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("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 = [ ":lib" ]
}
}
rust_executable("test_rust_metadata_exe") {
crate_root = "main.rs"
sources = [ "main.rs" ]
deps = [ ":lib" ]
}
# Check that the metadata handling works when linking into a C++ binary too.
executable("test_rust_metadata_cc_exe") {
sources = [ "main.cc" ]
deps = [ ":lib" ]
}
# A source file whose behavior depends on cfg options.
cargo_crate("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.
cargo_crate("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" ]
}