Skip to content

Commit

Permalink
Merge branch 'main' into windows-pdb
Browse files Browse the repository at this point in the history
  • Loading branch information
ddeville committed Dec 16, 2021
2 parents 0bc4fdf + 004e629 commit 2c92366
Show file tree
Hide file tree
Showing 16 changed files with 168 additions and 5 deletions.
4 changes: 3 additions & 1 deletion cargo/cargo_build_script.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,10 @@ def _build_script_impl(ctx):
# Pull in env vars which may be required for the cc_toolchain to work (e.g. on OSX, the SDK version).
# We hope that the linker env is sufficient for the whole cc_toolchain.
cc_toolchain, feature_configuration = find_cc_toolchain(ctx)
_, _, linker_env = get_linker_and_args(ctx, ctx.attr, cc_toolchain, feature_configuration, None)
linker, link_args, linker_env = get_linker_and_args(ctx, ctx.attr, cc_toolchain, feature_configuration, None)
env.update(**linker_env)
env["LD"] = linker
env["LDFLAGS"] = " ".join(link_args)

# MSVC requires INCLUDE to be set
cc_env = get_cc_compile_env(cc_toolchain, feature_configuration)
Expand Down
4 changes: 4 additions & 0 deletions cargo/cargo_build_script_runner/bin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,10 @@ fn run_buildrs() -> Result<(), String> {
}
}

if let Some(ld_path) = env::var_os("LD") {
command.env("LD", exec_root.join(ld_path));
}

// replace env vars with a ${pwd} prefix with the exec_root
for (key, value) in env::vars() {
let exec_root_str = exec_root.to_str().expect("exec_root not in utf8");
Expand Down
2 changes: 2 additions & 0 deletions crate_universe/src/renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -614,6 +614,7 @@ mod tests {
name = "rule_prefix__lazy_static__1_4_0",
strip_prefix = "",
build_file = Label("//:BUILD.lazy_static-1.4.0.bazel"),
init_submodules = True,
remote = "https://github.com/rust-lang-nursery/lazy-static.rs.git",
commit = "421669662b35fcb455f2902daed2e20bbbba79b6",
)
Expand Down Expand Up @@ -655,6 +656,7 @@ mod tests {
name = "rule_prefix__lazy_static__1_4_0",
strip_prefix = "",
build_file = Label("//:BUILD.lazy_static-1.4.0.bazel"),
init_submodules = True,
remote = "https://github.com/rust-lang-nursery/lazy-static.rs.git",
commit = "421669662b35fcb455f2902daed2e20bbbba79b6",
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
name = "{{ repository_name }}",
strip_prefix = "{{ repo.path_to_crate_root }}",
build_file = Label("//:BUILD.{{crate.pkg_name}}-{{crate.pkg_version}}.bazel"),
init_submodules = True,
remote = "{{ repo.remote }}",
commit = "{{ repo.commit }}",
)
5 changes: 4 additions & 1 deletion rust/private/rust.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,9 @@ def _rust_binary_impl(ctx):
def _create_test_launcher(ctx, toolchain, output, env, providers):
"""Create a process wrapper to ensure runtime environment variables are defined for the test binary
WARNING: This function is subject to deletion with the removal of
incompatible_disable_custom_test_launcher
Args:
ctx (ctx): The rule's context object
toolchain (rust_toolchain): The current rust toolchain
Expand Down Expand Up @@ -488,7 +491,7 @@ def _rust_test_common(ctx, toolchain, output):
)
providers.append(testing.TestEnvironment(env))

if any(["{pwd}" in v for v in env.values()]):
if not toolchain._incompatible_disable_custom_test_launcher and any(["{pwd}" in v for v in env.values()]):
# Some of the environment variables require expanding {pwd} placeholder at runtime,
# we need a launcher for that.
return _create_test_launcher(ctx, toolchain, output, env, providers)
Expand Down
7 changes: 6 additions & 1 deletion rust/private/utils.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,12 @@ def determine_output_hash(crate_root):
Returns:
str: A string representation of the hash.
"""
return repr(hash(crate_root.path))

# Take the absolute value of hash() since it could be negative.
h = hash(crate_root.path)
if h < 0:
h = -h
return repr(h)

def get_preferred_artifact(library_to_link):
"""Get the first available library to link from a LibraryToLink object.
Expand Down
6 changes: 6 additions & 0 deletions rust/settings/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ incompatible_flag(
issue = "https://github.com/bazelbuild/rules_rust/issues/1051",
)

incompatible_flag(
name = "incompatible_disable_custom_test_launcher",
build_setting_default = False,
issue = "https://github.com/bazelbuild/rules_rust/issues/1069",
)

bzl_library(
name = "rules",
srcs = glob(["**/*.bzl"]),
Expand Down
5 changes: 5 additions & 0 deletions rust/toolchain.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ def _rust_toolchain_impl(ctx):

make_rust_providers_target_independent = ctx.attr._incompatible_make_rust_providers_target_independent[IncompatibleFlagInfo]
remove_transitive_libs_from_dep_info = ctx.attr._incompatible_remove_transitive_libs_from_dep_info[IncompatibleFlagInfo]
disable_custom_test_launcher = ctx.attr._incompatible_disable_custom_test_launcher[IncompatibleFlagInfo]

expanded_stdlib_linkflags = []
for flag in ctx.attr.stdlib_linkflags:
Expand Down Expand Up @@ -286,6 +287,7 @@ def _rust_toolchain_impl(ctx):
libstd_and_allocator_ccinfo = _make_libstd_and_allocator_ccinfo(ctx, ctx.attr.rust_lib, ctx.attr.allocator_library),
_incompatible_make_rust_providers_target_independent = make_rust_providers_target_independent.enabled,
_incompatible_remove_transitive_libs_from_dep_info = remove_transitive_libs_from_dep_info.enabled,
_incompatible_disable_custom_test_launcher = disable_custom_test_launcher.enabled,
)
return [toolchain]

Expand Down Expand Up @@ -398,6 +400,9 @@ rust_toolchain = rule(
"_crosstool": attr.label(
default = Label("@bazel_tools//tools/cpp:current_cc_toolchain"),
),
"_incompatible_disable_custom_test_launcher": attr.label(
default = Label("@rules_rust//rust/settings:incompatible_disable_custom_test_launcher"),
),
"_incompatible_make_rust_providers_target_independent": attr.label(
default = "@rules_rust//rust/settings:incompatible_make_rust_providers_target_independent",
),
Expand Down
2 changes: 1 addition & 1 deletion test/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ load(

rule_test(
name = "hello_lib_rule_test",
generates = ["libhello_lib--145651613.rlib"],
generates = ["libhello_lib-145651613.rlib"],
rule = "//test/rust:hello_lib",
)

Expand Down
28 changes: 28 additions & 0 deletions test/unit/crate_name/crate_name_test.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,21 @@ def _invalid_custom_crate_name_test_impl(ctx):
asserts.expect_failure(env, "contains invalid character(s): -")
return analysistest.end(env)

def _slib_library_name_test_impl(ctx):
"""Regression test for extra-filename.
Checks that the extra hash value appended to the library filename only
contains one dash. Previously, the hash for `slib` was negative,
resulting in an extra dash in the filename (--codegen_extra_filename=--517943325).
Args:
ctx: rule context.
"""
env = analysistest.begin(ctx)
tut = analysistest.target_under_test(env)
assert_argv_contains(env, tut.actions[0], "--codegen=extra-filename=-517943325")
return analysistest.end(env)

default_crate_name_library_test = analysistest.make(
_default_crate_name_library_test_impl,
)
Expand All @@ -82,6 +97,9 @@ invalid_custom_crate_name_test = analysistest.make(
_invalid_custom_crate_name_test_impl,
expect_failure = True,
)
slib_library_name_test = analysistest.make(
_slib_library_name_test_impl,
)

def _crate_name_test():
rust_library(
Expand Down Expand Up @@ -130,6 +148,16 @@ def _crate_name_test():
tags = ["manual", "norustfmt"],
)

rust_library(
name = "slib",
srcs = ["slib.rs"],
)

slib_library_name_test(
name = "slib_library_name_test",
target_under_test = ":slib",
)

default_crate_name_library_test(
name = "default_crate_name_library_test",
target_under_test = ":default-crate-name-library",
Expand Down
1 change: 1 addition & 0 deletions test/unit/crate_name/slib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

5 changes: 5 additions & 0 deletions test/unit/disable_custom_test_launcher/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
load(":disable_custom_test_launcher_test.bzl", "disable_custom_test_launcher_test_suite")

disable_custom_test_launcher_test_suite(
name = "disable_custom_test_launcher_test_suite",
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
"""Unittests for rust rules."""

load("@bazel_skylib//lib:unittest.bzl", "analysistest", "asserts")
load("@bazel_skylib//rules:write_file.bzl", "write_file")
load("//rust:defs.bzl", "rust_test")

def _incompatible_enable_custom_test_launcher_test_impl(ctx):
env = analysistest.begin(ctx)
tut = analysistest.target_under_test(env)

executable = tut.files_to_run.executable
asserts.true(env, executable.basename.endswith(".launcher") or executable.basename.endswith(".launcher.exe"))

return analysistest.end(env)

def _incompatible_disable_custom_test_launcher_test_impl(ctx):
env = analysistest.begin(ctx)
tut = analysistest.target_under_test(env)

executable = tut.files_to_run.executable
asserts.false(env, executable.basename.endswith(".launcher") or executable.basename.endswith(".launcher.exe"))

return analysistest.end(env)

incompatible_enable_custom_test_launcher_test = analysistest.make(
_incompatible_enable_custom_test_launcher_test_impl,
config_settings = {
"@//rust/settings:incompatible_disable_custom_test_launcher": False,
},
)

incompatible_disable_custom_test_launcher_test = analysistest.make(
_incompatible_disable_custom_test_launcher_test_impl,
config_settings = {
"@//rust/settings:incompatible_disable_custom_test_launcher": True,
},
)

def _disable_custom_test_launcher_test():
write_file(
name = "src",
out = "lib.rs",
content = [],
)

write_file(
name = "data",
out = "data.txt",
content = [],
)

rust_test(
name = "disable_custom_test_launcher_test",
srcs = [":lib.rs"],
env = {"CUSTOM_TEST_ENV": "$(execpath :data)"},
data = [":data"],
)

incompatible_enable_custom_test_launcher_test(
name = "incompatible_enable_custom_test_launcher_test",
target_under_test = ":disable_custom_test_launcher_test",
)

incompatible_disable_custom_test_launcher_test(
name = "incompatible_disable_custom_test_launcher_test",
target_under_test = ":disable_custom_test_launcher_test",
)

def disable_custom_test_launcher_test_suite(name):
"""Entry-point macro called from the BUILD file.
Args:
name: Name of the macro.
"""
_disable_custom_test_launcher_test()

native.test_suite(
name = name,
tests = [
":incompatible_disable_custom_test_launcher_test",
":incompatible_enable_custom_test_launcher_test",
],
)
3 changes: 3 additions & 0 deletions test/unit/win_interface_library/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub fn main() {
println!("Hello");
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

load("@bazel_skylib//lib:unittest.bzl", "analysistest", "asserts")
load("@rules_cc//cc:defs.bzl", "cc_binary")
load("//rust:defs.bzl", "rust_shared_library")
load("//rust:defs.bzl", "rust_binary", "rust_shared_library")

def _win_interface_library_test_impl(ctx):
env = analysistest.begin(ctx)
Expand Down Expand Up @@ -49,6 +49,18 @@ def win_interface_library_analysis_test_suite(name):
target_compatible_with = ["@platforms//os:windows"],
)

rust_binary(
name = "myrustbin",
srcs = ["main.rs"],
target_compatible_with = ["@platforms//os:windows"],
)

native.filegroup(
name = "myrustbin.pdb",
srcs = [":myrustbin"],
output_group = "pdb_file",
)

win_interface_library_test(
name = "win_interface_library_test",
target_under_test = ":mylib",
Expand Down
3 changes: 3 additions & 0 deletions util/launcher/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# WARNING: This package is subject to deletion with the removal of
# incompatible_disable_custom_test_launcher

load("//rust:defs.bzl", "rust_binary")

package(default_visibility = ["//visibility:public"])
Expand Down

0 comments on commit 2c92366

Please sign in to comment.