From 184a9b483acf08bc2a3488d8c91370bd933e2921 Mon Sep 17 00:00:00 2001 From: Vinh Tran Date: Thu, 28 Sep 2023 14:22:01 +0000 Subject: [PATCH] Add hdrs and indlues attributes to rust_static_library --- .../ffi/cpp_calling_rust_with_header/BUILD | 7 +++++ .../ffi/cpp_calling_rust_with_header/main.cpp | 6 ++++ .../cpp_calling_rust_with_header/rust/BUILD | 10 +++++++ .../rust/librusty.rs | 7 +++++ .../cpp_calling_rust_with_header/rust/rusty.h | 6 ++++ rust/private/rust.bzl | 7 ++++- rust/private/rustc.bzl | 28 ++++++++++++++++++- 7 files changed, 69 insertions(+), 2 deletions(-) create mode 100644 examples/ffi/cpp_calling_rust_with_header/BUILD create mode 100644 examples/ffi/cpp_calling_rust_with_header/main.cpp create mode 100644 examples/ffi/cpp_calling_rust_with_header/rust/BUILD create mode 100644 examples/ffi/cpp_calling_rust_with_header/rust/librusty.rs create mode 100644 examples/ffi/cpp_calling_rust_with_header/rust/rusty.h diff --git a/examples/ffi/cpp_calling_rust_with_header/BUILD b/examples/ffi/cpp_calling_rust_with_header/BUILD new file mode 100644 index 0000000000..fbabf9197b --- /dev/null +++ b/examples/ffi/cpp_calling_rust_with_header/BUILD @@ -0,0 +1,7 @@ +load("@rules_cc//cc:defs.bzl", "cc_test") + +cc_test( + name = "main", + srcs = ["main.cpp"], + deps = ["//ffi/cpp_calling_rust_with_header/rust:librusty"], +) diff --git a/examples/ffi/cpp_calling_rust_with_header/main.cpp b/examples/ffi/cpp_calling_rust_with_header/main.cpp new file mode 100644 index 0000000000..874cffe1c7 --- /dev/null +++ b/examples/ffi/cpp_calling_rust_with_header/main.cpp @@ -0,0 +1,6 @@ +#include "rusty.h" + +int main() { + print_c_hello_rust(); + return 0; +} diff --git a/examples/ffi/cpp_calling_rust_with_header/rust/BUILD b/examples/ffi/cpp_calling_rust_with_header/rust/BUILD new file mode 100644 index 0000000000..843be6e8d5 --- /dev/null +++ b/examples/ffi/cpp_calling_rust_with_header/rust/BUILD @@ -0,0 +1,10 @@ +load("@rules_rust//rust:defs.bzl", "rust_static_library") + +rust_static_library( + name = "librusty", + srcs = ["librusty.rs"], + hdrs = ["rusty.h"], + crate_name = "rusty", + includes = ["."], + visibility = ["//visibility:public"], +) diff --git a/examples/ffi/cpp_calling_rust_with_header/rust/librusty.rs b/examples/ffi/cpp_calling_rust_with_header/rust/librusty.rs new file mode 100644 index 0000000000..2695864bfb --- /dev/null +++ b/examples/ffi/cpp_calling_rust_with_header/rust/librusty.rs @@ -0,0 +1,7 @@ +//! A simple hello world example that can be called from C + +#[no_mangle] +/// Print "Hello Rust!" +pub extern fn print_c_hello_rust() { + println!("Hello Rust!"); +} diff --git a/examples/ffi/cpp_calling_rust_with_header/rust/rusty.h b/examples/ffi/cpp_calling_rust_with_header/rust/rusty.h new file mode 100644 index 0000000000..0f723b8d20 --- /dev/null +++ b/examples/ffi/cpp_calling_rust_with_header/rust/rusty.h @@ -0,0 +1,6 @@ +#ifndef SIMPLE_PRINTER_H +#define SIMPLE_PRINTER_H + +extern "C" void print_c_hello_rust(); + +#endif \ No newline at end of file diff --git a/rust/private/rust.bzl b/rust/private/rust.bzl index 014d8d0d52..f547e667d4 100644 --- a/rust/private/rust.bzl +++ b/rust/private/rust.bzl @@ -931,7 +931,12 @@ rust_library = rule( rust_static_library = rule( implementation = _rust_static_library_impl, - attrs = dict(_common_attrs.items()), + attrs = dict( + _common_attrs.items() + { + "hdrs": attr.label_list(allow_files = True), + "includes": attr.string_list(), + }.items(), + ), fragments = ["cpp"], host_fragments = ["cpp"], toolchains = [ diff --git a/rust/private/rustc.bzl b/rust/private/rustc.bzl index e76c20bde0..ec8156eec0 100644 --- a/rust/private/rustc.bzl +++ b/rust/private/rustc.bzl @@ -1474,6 +1474,25 @@ def _collect_nonstatic_linker_inputs(cc_info): )) return shared_linker_inputs +# get_includes_paths expects a rule context, a list of directories, and +# whether the directories are package-relative and returns a list of exec +# root-relative paths. This handles the need to search for files both in the +# source tree and generated files. +def _get_includes_paths(ctx, dirs, package_relative = True): + execution_relative_dirs = [] + for rel_dir in dirs: + if rel_dir == ".": + rel_dir = "" + execution_rel_dir = rel_dir + if package_relative: + execution_rel_dir = ctx.label.package + if len(rel_dir) > 0: + execution_rel_dir = execution_rel_dir + "/" + rel_dir + + execution_relative_dirs.append(execution_rel_dir) + + return execution_relative_dirs + def establish_cc_info(ctx, attr, crate_info, toolchain, cc_toolchain, feature_configuration, interface_library): """If the produced crate is suitable yield a CcInfo to allow for interop with cc rules @@ -1548,8 +1567,15 @@ def establish_cc_info(ctx, attr, crate_info, toolchain, cc_toolchain, feature_co linker_inputs = depset([link_input]), ) + compilation_context = None + if hasattr(ctx.attr, "hdrs") and hasattr(ctx.attr, "includes"): + compilation_context = cc_common.create_compilation_context( + headers = depset(ctx.files.hdrs), + includes = depset(_get_includes_paths(ctx, ctx.attr.includes, True)), + ) + cc_infos = [ - CcInfo(linking_context = linking_context), + CcInfo(linking_context = linking_context, compilation_context = compilation_context), toolchain.stdlib_linkflags, ]