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

Do not pass native libraries to rlib compilations #576

Merged
merged 2 commits into from
Feb 5, 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
1 change: 1 addition & 0 deletions proto/proto.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ def _rust_proto_compile(protos, descriptor_sets, imports, crate_name, ctx, is_gr
return rustc_compile_action(
ctx = ctx,
toolchain = find_toolchain(ctx),
crate_type = "rlib",
crate_info = rust_common.crate_info(
name = crate_name,
type = "rlib",
Expand Down
2 changes: 2 additions & 0 deletions rust/private/clippy.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ def _clippy_aspect_impl(target, ctx):

toolchain = find_toolchain(ctx)
crate_info = target[rust_common.crate_info]
crate_type = crate_info.type

if crate_info.is_test:
root = crate_info.root
Expand Down Expand Up @@ -89,6 +90,7 @@ def _clippy_aspect_impl(target, ctx):
toolchain.clippy_driver.path,
cc_toolchain,
feature_configuration,
crate_type,
crate_info,
dep_info,
output_hash = determine_output_hash(root),
Expand Down
14 changes: 9 additions & 5 deletions rust/private/rust.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -155,9 +155,10 @@ def _rust_library_impl(ctx):
output_hash = determine_output_hash(crate_root)

crate_name = name_to_crate_name(ctx.label.name)
crate_type = getattr(ctx.attr, "crate_type")
rust_lib_name = _determine_lib_name(
crate_name,
ctx.attr.crate_type,
crate_type,
toolchain,
output_hash,
)
Expand All @@ -166,9 +167,10 @@ def _rust_library_impl(ctx):
return rustc_compile_action(
ctx = ctx,
toolchain = toolchain,
crate_type = crate_type,
crate_info = rust_common.crate_info(
name = crate_name,
type = ctx.attr.crate_type,
type = crate_type,
root = crate_root,
srcs = ctx.files.srcs,
deps = ctx.attr.deps,
Expand Down Expand Up @@ -201,6 +203,7 @@ def _rust_binary_impl(ctx):
return rustc_compile_action(
ctx = ctx,
toolchain = toolchain,
crate_type = crate_type,
crate_info = rust_common.crate_info(
name = crate_name,
type = crate_type,
Expand Down Expand Up @@ -230,14 +233,14 @@ def _rust_test_common(ctx, toolchain, output):
_assert_no_deprecated_attributes(ctx)

crate_name = name_to_crate_name(ctx.label.name)

crate_type = "bin"
if ctx.attr.crate:
# Target is building the crate in `test` config
# Build the test binary using the dependency's srcs.
crate = ctx.attr.crate[rust_common.crate_info]
target = rust_common.crate_info(
name = crate_name,
type = crate.type,
type = crate_type,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this assert that crate.type == "bin"?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think so? Crate under test can be a rust_library with crate type rlib or pretty much any other, it's only the test that will be "bin" in any case.

root = crate.root,
srcs = crate.srcs + ctx.files.srcs,
deps = crate.deps + ctx.attr.deps,
Expand All @@ -252,7 +255,7 @@ def _rust_test_common(ctx, toolchain, output):
# Target is a standalone crate. Build the test binary as its own crate.
target = rust_common.crate_info(
name = crate_name,
type = "lib",
type = crate_type,
root = crate_root_src(ctx.attr, ctx.files.srcs, "lib"),
srcs = ctx.files.srcs,
deps = ctx.attr.deps,
Expand All @@ -267,6 +270,7 @@ def _rust_test_common(ctx, toolchain, output):
return rustc_compile_action(
ctx = ctx,
toolchain = toolchain,
crate_type = crate_type,
crate_info = target,
rust_flags = ["--test"],
environ = ctx.attr.env,
Expand Down
16 changes: 13 additions & 3 deletions rust/private/rustc.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,7 @@ def construct_arguments(
tool_path,
cc_toolchain,
feature_configuration,
crate_type,
crate_info,
dep_info,
output_hash,
Expand All @@ -383,6 +384,7 @@ def construct_arguments(
tool_path (str): Path to rustc
cc_toolchain (CcToolchain): The CcToolchain for the current target.
feature_configuration (FeatureConfiguration): Class used to construct command lines from CROSSTOOL features.
crate_type (str): Crate type of the current target.
crate_info (CrateInfo): The CrateInfo provider of the target crate
dep_info (DepInfo): The DepInfo provider of the target crate
output_hash (str): The hashed path of the crate root
Expand Down Expand Up @@ -508,7 +510,7 @@ def construct_arguments(
args.add("--codegen=linker=" + ld)
args.add_joined("--codegen", link_args, join_with = " ", format_joined = "link-args=%s")

add_native_link_flags(args, dep_info)
add_native_link_flags(args, dep_info, crate_type)

# These always need to be added, even if not linking this crate.
add_crate_link_flags(args, dep_info)
Expand Down Expand Up @@ -541,6 +543,7 @@ def construct_arguments(
def rustc_compile_action(
ctx,
toolchain,
crate_type,
crate_info,
output_hash = None,
rust_flags = [],
Expand All @@ -550,6 +553,7 @@ def rustc_compile_action(
Args:
ctx (ctx): The rule's context object
toolchain (rust_toolchain): The current `rust_toolchain`
crate_type (str): Crate type of the current target
crate_info (CrateInfo): The CrateInfo provider for the current target.
output_hash (str, optional): The hashed path of the crate root. Defaults to None.
rust_flags (list, optional): Additional flags to pass to rustc. Defaults to [].
Expand Down Expand Up @@ -588,6 +592,7 @@ def rustc_compile_action(
toolchain.rustc.path,
cc_toolchain,
feature_configuration,
crate_type,
crate_info,
dep_info,
output_hash,
Expand Down Expand Up @@ -816,13 +821,18 @@ def _get_crate_dirname(crate):
"""
return crate.output.dirname

def add_native_link_flags(args, dep_info):
def add_native_link_flags(args, dep_info, crate_type):
"""Adds linker flags for all dependencies of the current target.

Args:
args (Args): The Args struct for a ctx.action
dep_info (DepInfo): Dependeincy Info provider
dep_info (DepInfo): Dependency Info provider
crate_type: Crate type of the current target

"""
if crate_type in ["lib", "rlib"]:
return

native_libs = depset(transitive = [dep_info.transitive_dylibs, dep_info.transitive_staticlibs])
args.add_all(native_libs, map_each = _get_dirname, uniquify = True, format_each = "-Lnative=%s")
args.add_all(dep_info.transitive_dylibs, map_each = get_lib_name, format_each = "-ldylib=%s")
Expand Down
3 changes: 2 additions & 1 deletion rust/rust.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
# See the License for the specific language governing permissions and
# limitations under the License.

# buildifier: disable=module-docstring
"""Public entry point to all Rust rules and supported APIs."""

load(
"//rust/private:clippy.bzl",
_rust_clippy = "rust_clippy",
Expand Down
32 changes: 32 additions & 0 deletions rust/rust_common.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Copyright 2015 The Bazel Authors. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Module with Rust definitions required to write custom Rust rules."""

CrateInfo = provider(
doc = "A provider containing general Crate information.",
fields = {
"aliases": "Dict[Label, String]: Renamed and aliased crates",
"deps": "List[Provider]: This crate's (rust or cc) dependencies' providers.",
"edition": "str: The edition of this crate.",
"is_test": "bool: If the crate is being compiled in a test context",
"name": "str: The name of this crate.",
"output": "File: The output File that will be produced, depends on crate type.",
"proc_macro_deps": "List[CrateInfo]: This crate's rust proc_macro dependencies' providers.",
"root": "File: The source File entrypoint to this crate, eg. lib.rs",
"rustc_env": "Dict[String, String]: Additional `\"key\": \"value\"` environment variables to set for rustc.",
"srcs": "List[File]: All source Files that are part of the crate.",
"type": "str: The type of this crate. eg. lib or bin",
},
)
1 change: 1 addition & 0 deletions test/unit/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# BUILD file intentionally left empty
4 changes: 4 additions & 0 deletions test/unit/native_deps/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
load(":native_deps_test.bzl", "native_deps_test_suite")

############################ UNIT TESTS #############################
native_deps_test_suite(name = "native_deps_test_suite")
6 changes: 6 additions & 0 deletions test/unit/native_deps/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() })
}
6 changes: 6 additions & 0 deletions test/unit/native_deps/lib_using_native_dep.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
extern "C" {
fn native_dep() -> isize;
}
pub fn use_native_dep() {
println!("{}", unsafe { native_dep() })
}
1 change: 1 addition & 0 deletions test/unit/native_deps/native_dep.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
extern "C" int native_dep() { return 42; }
Loading