From 04012f0aa1edd1d4108a2ac647a65e59730fc4c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Drago=C8=99=20Tiselice?= <dtiselice@google.com> Date: Tue, 14 May 2019 08:26:08 +0000 Subject: [PATCH] [build] Fix Rust static library dependecies Depending on a static_library from Rust fails because the correct link directory path is not passed to build_rustc_target.py. This fix adds link paths from every one of the dependecies found in non_rust_deps. This is not ideal since it doesn't work with source_set or any shared library that has transitive dependencies. TEST=Build, CQ, and Rust crate that tests scenario. fx run-test static_library_tests Change-Id: I42c2d07e68d5f7a3d76c0edacbcd0d81f50c4993 --- build/rust/rustc_artifact.gni | 9 +++++++ examples/BUILD.gn | 1 + examples/rust_static_linking/BUILD.gn | 34 +++++++++++++++++++++++++ examples/rust_static_linking/src/lib.rs | 18 +++++++++++++ examples/rust_static_linking/static.c | 9 +++++++ 5 files changed, 71 insertions(+) create mode 100644 examples/rust_static_linking/BUILD.gn create mode 100644 examples/rust_static_linking/src/lib.rs create mode 100644 examples/rust_static_linking/static.c diff --git a/build/rust/rustc_artifact.gni b/build/rust/rustc_artifact.gni index ec9cb517376..16c90084b0e 100644 --- a/build/rust/rustc_artifact.gni +++ b/build/rust/rustc_artifact.gni @@ -448,6 +448,15 @@ template("rustc_artifact") { args += [ "--with-unit-tests" ] } + if (defined(invoker.non_rust_deps)) { + foreach(dep, invoker.non_rust_deps) { + args += [ + "--lib-dir", + rebase_path(get_label_info(dep, "target_out_dir")), + ] + } + } + args += dep_info_paths outputs = [ output_file, diff --git a/examples/BUILD.gn b/examples/BUILD.gn index 23141b9b050..2223a6de715 100644 --- a/examples/BUILD.gn +++ b/examples/BUILD.gn @@ -17,5 +17,6 @@ group("tests") { testonly = true deps = [ "hello_world:tests", + "rust_static_linking:static_linking_tests", ] } diff --git a/examples/rust_static_linking/BUILD.gn b/examples/rust_static_linking/BUILD.gn new file mode 100644 index 00000000000..f8f4f2dbb7a --- /dev/null +++ b/examples/rust_static_linking/BUILD.gn @@ -0,0 +1,34 @@ +# Copyright 2019 The Fuchsia Authors. All rights reserved. +# Use of this source code is governed by a BSD-style license that can be +# found in the LICENSE file. + +import("//build/package.gni") +import("//build/rust/rustc_library.gni") + +rustc_library("static_linking") { + name = "static_linking" + with_unit_tests = true + version = "0.1.0" + edition = "2018" + non_rust_deps = [ + ":static", + ] +} + +static_library("static") { + sources = [ + "static.c", + ] +} + +package("static_linking_tests") { + testonly = true + deps = [ + ":static_linking", + ] + tests = [ + { + name = "static_linking_lib_test" + }, + ] +} diff --git a/examples/rust_static_linking/src/lib.rs b/examples/rust_static_linking/src/lib.rs new file mode 100644 index 00000000000..9989dc74fb0 --- /dev/null +++ b/examples/rust_static_linking/src/lib.rs @@ -0,0 +1,18 @@ +// Copyright 2019 The Fuchsia Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#[link(name = "static", kind = "static")] +extern "C" { + pub fn returns_two() -> u32; +} + +#[cfg(test)] +mod tests { + use super::returns_two; + + #[test] + fn linking_works() { + assert_eq!(unsafe { returns_two() }, 2); + } +} diff --git a/examples/rust_static_linking/static.c b/examples/rust_static_linking/static.c new file mode 100644 index 00000000000..60a9ee42cc9 --- /dev/null +++ b/examples/rust_static_linking/static.c @@ -0,0 +1,9 @@ +// Copyright 2019 The Fuchsia Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include <stdint.h> + +uint32_t returns_two() { + return 2; +} -- GitLab