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 7d1cc09 commit 9febc37
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 2 deletions.
7 changes: 7 additions & 0 deletions examples/ffi/cc_calling_rust/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 = ["cc/main.c"],
deps = ["//ffi/cc_calling_rust/rust:libsimple_printer"],
)
6 changes: 6 additions & 0 deletions examples/ffi/cc_calling_rust/cc/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#include "simple_printer.h"

int main() {
print_c_hello_rust();
return 0;
}
10 changes: 10 additions & 0 deletions examples/ffi/cc_calling_rust/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 = "libsimple_printer",
srcs = ["libsimple_printer.rs"],
hdrs = ["simple_printer.h"],
crate_name = "simple_printer",
includes = ["."],
visibility = ["//visibility:public"],
)
7 changes: 7 additions & 0 deletions examples/ffi/cc_calling_rust/rust/libsimple_printer.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!");
}
7 changes: 7 additions & 0 deletions examples/ffi/cc_calling_rust/rust/simple_printer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#ifndef SIMPLE_PRINTER_H
#define SIMPLE_PRINTER_H

void print_c_hello_rust();


#endif
6 changes: 5 additions & 1 deletion rust/private/rust.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -931,7 +931,11 @@ 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 9febc37

Please sign in to comment.