Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add test/unit/cc_info unittests #608

Merged
merged 2 commits into from
Feb 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion rust/private/rustc.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -654,19 +654,21 @@ def establish_cc_info(ctx, crate_info, toolchain, cc_toolchain, feature_configur
feature_configuration = feature_configuration,
cc_toolchain = cc_toolchain,
static_library = crate_info.output,
# TODO(hlopko): handle PIC/NOPIC correctly
pic_static_library = crate_info.output,
)
elif crate_info.type in ("rlib", "lib"):
# bazel hard-codes a check for endswith((".a", ".pic.a",
# ".lib")) in create_library_to_link, so we work around that
# by creating a symlink to the .rlib with a .a extension.
dot_a = ctx.actions.declare_file(crate_info.name + ".a", sibling = crate_info.output)
ctx.actions.symlink(output = dot_a, target_file = crate_info.output)
# TODO(hlopko): handle PIC/NOPIC correctly
library_to_link = cc_common.create_library_to_link(
actions = ctx.actions,
feature_configuration = feature_configuration,
cc_toolchain = cc_toolchain,
static_library = dot_a,
# TODO(hlopko): handle PIC/NOPIC correctly
pic_static_library = dot_a,
)
elif crate_info.type == "cdylib":
Expand Down
4 changes: 4 additions & 0 deletions test/unit/cc_info/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
load(":cc_info_test.bzl", "cc_info_test_suite")

############################ UNIT TESTS #############################
cc_info_test_suite(name = "cc_info_test_suite")
6 changes: 6 additions & 0 deletions test/unit/cc_info/bin_using_native_dep.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
extern "C" {
fn native_dep() -> isize;
}
fn main() {
println!("{}", unsafe { native_dep() })
}
137 changes: 137 additions & 0 deletions test/unit/cc_info/cc_info_test.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
"""Unittests for rust rules."""

load("@bazel_skylib//lib:unittest.bzl", "analysistest", "asserts")
load("//rust:defs.bzl", "rust_binary", "rust_library", "rust_proc_macro", "rust_shared_library", "rust_static_library")

def _assert_cc_info_has_library_to_link(env, tut, type):
asserts.true(env, CcInfo in tut, "rust_library should provide CcInfo")
cc_info = tut[CcInfo]
linker_inputs = cc_info.linking_context.linker_inputs.to_list()
asserts.equals(env, len(linker_inputs), 1)
library_to_link = linker_inputs[0].libraries[0]
asserts.equals(env, False, library_to_link.alwayslink)

asserts.equals(env, [], library_to_link.lto_bitcode_files)
asserts.equals(env, [], library_to_link.pic_lto_bitcode_files)

asserts.equals(env, [], library_to_link.objects)
asserts.equals(env, [], library_to_link.pic_objects)

if type == "cdylib":
asserts.true(env, library_to_link.dynamic_library != None)
asserts.equals(env, None, library_to_link.interface_library)
asserts.true(env, library_to_link.resolved_symlink_dynamic_library != None)
asserts.equals(env, None, library_to_link.resolved_symlink_interface_library)
asserts.equals(env, None, library_to_link.static_library)
asserts.equals(env, None, library_to_link.pic_static_library)
else:
asserts.equals(env, None, library_to_link.dynamic_library)
asserts.equals(env, None, library_to_link.interface_library)
asserts.equals(env, None, library_to_link.resolved_symlink_dynamic_library)
asserts.equals(env, None, library_to_link.resolved_symlink_interface_library)
asserts.true(env, library_to_link.static_library != None)
asserts.true(env, library_to_link.pic_static_library != None)
asserts.equals(env, library_to_link.static_library, library_to_link.pic_static_library)

def _rlib_provides_cc_info_test_impl(ctx):
env = analysistest.begin(ctx)
tut = analysistest.target_under_test(env)
_assert_cc_info_has_library_to_link(env, tut, "rlib")
return analysistest.end(env)

def _bin_does_not_provide_cc_info_test_impl(ctx):
env = analysistest.begin(ctx)
tut = analysistest.target_under_test(env)
asserts.false(env, CcInfo in tut, "rust_binary should not provide CcInfo")
return analysistest.end(env)

def _proc_macro_does_not_provide_cc_info_test_impl(ctx):
env = analysistest.begin(ctx)
tut = analysistest.target_under_test(env)
asserts.false(env, CcInfo in tut, "rust_proc_macro should not provide CcInfo")
return analysistest.end(env)

def _cdylib_provides_cc_info_test_impl(ctx):
env = analysistest.begin(ctx)
tut = analysistest.target_under_test(env)
_assert_cc_info_has_library_to_link(env, tut, "cdylib")
return analysistest.end(env)

def _staticlib_provides_cc_info_test_impl(ctx):
env = analysistest.begin(ctx)
tut = analysistest.target_under_test(env)
_assert_cc_info_has_library_to_link(env, tut, "staticlib")
return analysistest.end(env)

rlib_provides_cc_info_test = analysistest.make(_rlib_provides_cc_info_test_impl)
bin_does_not_provide_cc_info_test = analysistest.make(_bin_does_not_provide_cc_info_test_impl)
staticlib_provides_cc_info_test = analysistest.make(_staticlib_provides_cc_info_test_impl)
cdylib_provides_cc_info_test = analysistest.make(_cdylib_provides_cc_info_test_impl)
proc_macro_does_not_provide_cc_info_test = analysistest.make(_proc_macro_does_not_provide_cc_info_test_impl)

def _cc_info_test():
rust_library(
name = "rlib",
srcs = ["foo.rs"],
)

rust_binary(
name = "bin",
srcs = ["foo.rs"],
)

rust_static_library(
name = "staticlib",
srcs = ["foo.rs"],
)

rust_shared_library(
name = "cdylib",
srcs = ["foo.rs"],
)

rust_proc_macro(
name = "proc_macro",
srcs = ["proc_macro.rs"],
edition = "2018",
)

rlib_provides_cc_info_test(
name = "rlib_provides_cc_info_test",
target_under_test = ":rlib",
)
bin_does_not_provide_cc_info_test(
name = "bin_does_not_provide_cc_info_test",
target_under_test = ":bin",
)
cdylib_provides_cc_info_test(
name = "cdylib_provides_cc_info_test",
target_under_test = ":cdylib",
)
staticlib_provides_cc_info_test(
name = "staticlib_provides_cc_info_test",
target_under_test = ":staticlib",
)
proc_macro_does_not_provide_cc_info_test(
name = "proc_macro_does_not_provide_cc_info_test",
target_under_test = ":proc_macro",
)

def cc_info_test_suite(name):
"""Entry-point macro called from the BUILD file.

Args:
name: Name of the macro.
"""
_cc_info_test()

native.test_suite(
name = name,
tests = [
":rlib_provides_cc_info_test",
":staticlib_provides_cc_info_test",
":cdylib_provides_cc_info_test",
":proc_macro_does_not_provide_cc_info_test",
":bin_does_not_provide_cc_info_test",
],
)
1 change: 1 addition & 0 deletions test/unit/cc_info/foo.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub fn main() { dbg!(42); }
9 changes: 9 additions & 0 deletions test/unit/cc_info/proc_macro.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
use proc_macro::TokenStream;

extern "C" {
fn native_dep() -> isize;
}
#[proc_macro_derive(UsingNativeDep)]
pub fn use_native_dep(_input: TokenStream) -> TokenStream {
panic!("done")
}