Skip to content

Commit

Permalink
Add hdrs and indlues attributes to rust_static_library
Browse files Browse the repository at this point in the history
  • Loading branch information
Vinh Tran committed Sep 28, 2023
1 parent 4a5d7a1 commit 184a9b4
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 2 deletions.
7 changes: 7 additions & 0 deletions examples/ffi/cpp_calling_rust_with_header/BUILD
Original file line number Diff line number Diff line change
@@ -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"],
)
6 changes: 6 additions & 0 deletions examples/ffi/cpp_calling_rust_with_header/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#include "rusty.h"

int main() {
print_c_hello_rust();
return 0;
}
10 changes: 10 additions & 0 deletions examples/ffi/cpp_calling_rust_with_header/rust/BUILD
Original file line number Diff line number Diff line change
@@ -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"],
)
7 changes: 7 additions & 0 deletions examples/ffi/cpp_calling_rust_with_header/rust/librusty.rs
Original file line number Diff line number Diff line change
@@ -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!");
}
6 changes: 6 additions & 0 deletions examples/ffi/cpp_calling_rust_with_header/rust/rusty.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#ifndef SIMPLE_PRINTER_H
#define SIMPLE_PRINTER_H

extern "C" void print_c_hello_rust();

#endif
7 changes: 6 additions & 1 deletion rust/private/rust.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down
28 changes: 27 additions & 1 deletion rust/private/rustc.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
]

Expand Down

0 comments on commit 184a9b4

Please sign in to comment.