Skip to content

Commit

Permalink
Add -L but not -l to rlib compilations
Browse files Browse the repository at this point in the history
Build scripts may add -l flags to dependees, and without the relevant -L
flags those -l flags may fail to resolve, causing build failures.
  • Loading branch information
illicitonion committed Feb 25, 2021
1 parent a37adb7 commit eff3c47
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
5 changes: 3 additions & 2 deletions rust/private/rustc.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -828,11 +828,12 @@ def _add_native_link_flags(args, dep_info, crate_type, cc_toolchain, feature_con
feature_configuration (FeatureConfiguration): feature configuration to use with cc_toolchain
"""
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")

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")
args.add_all(dep_info.transitive_staticlibs, map_each = get_lib_name, format_each = "-lstatic=%s")

Expand Down
22 changes: 21 additions & 1 deletion test/unit/native_deps/native_deps_test.bzl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""Unittests for rust rules."""

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

Expand All @@ -18,12 +18,26 @@ def _assert_argv_contains(env, action, flag):
"Expected {args} to contain {flag}".format(args = action.argv, flag = flag),
)

def _assert_argv_contains_prefix_suffix(env, action, prefix, suffix):
for found_flag in action.argv:
if found_flag.startswith(prefix) and found_flag.endswith(suffix):
return
unittest.fail(
env,
"Expected an arg with prefix '{prefix}' and suffix '{suffix}' in {args}".format(
prefix = prefix,
suffix = suffix,
args = action.argv,
)
)

def _lib_has_no_native_libs_test_impl(ctx):
env = analysistest.begin(ctx)
tut = analysistest.target_under_test(env)
actions = analysistest.target_actions(env)
action = actions[0]
_assert_argv_contains(env, action, "--crate-type=lib")
_assert_argv_contains_prefix_suffix(env, action, "-Lnative=", "/native_deps")
_assert_argv_contains_not(env, action, "-lstatic=native_dep")
_assert_argv_contains_not(env, action, "-ldylib=native_dep")
return analysistest.end(env)
Expand All @@ -34,6 +48,7 @@ def _rlib_has_no_native_libs_test_impl(ctx):
actions = analysistest.target_actions(env)
action = actions[0]
_assert_argv_contains(env, action, "--crate-type=rlib")
_assert_argv_contains_prefix_suffix(env, action, "-Lnative=", "/native_deps")
_assert_argv_contains_not(env, action, "-lstatic=native_dep")
_assert_argv_contains_not(env, action, "-ldylib=native_dep")
return analysistest.end(env)
Expand All @@ -43,6 +58,7 @@ def _dylib_has_native_libs_test_impl(ctx):
tut = analysistest.target_under_test(env)
actions = analysistest.target_actions(env)
action = actions[0]
_assert_argv_contains_prefix_suffix(env, action, "-Lnative=", "/native_deps")
_assert_argv_contains(env, action, "--crate-type=dylib")
_assert_argv_contains(env, action, "-lstatic=native_dep")
return analysistest.end(env)
Expand All @@ -52,6 +68,7 @@ def _cdylib_has_native_libs_test_impl(ctx):
tut = analysistest.target_under_test(env)
actions = analysistest.target_actions(env)
action = actions[0]
_assert_argv_contains_prefix_suffix(env, action, "-Lnative=", "/native_deps")
_assert_argv_contains(env, action, "--crate-type=cdylib")
_assert_argv_contains(env, action, "-lstatic=native_dep")
return analysistest.end(env)
Expand All @@ -61,6 +78,7 @@ def _staticlib_has_native_libs_test_impl(ctx):
tut = analysistest.target_under_test(env)
actions = analysistest.target_actions(env)
action = actions[0]
_assert_argv_contains_prefix_suffix(env, action, "-Lnative=", "/native_deps")
_assert_argv_contains(env, action, "--crate-type=staticlib")
_assert_argv_contains(env, action, "-lstatic=native_dep")
return analysistest.end(env)
Expand All @@ -71,6 +89,7 @@ def _proc_macro_has_native_libs_test_impl(ctx):
actions = analysistest.target_actions(env)
asserts.equals(env, 1, len(actions))
action = actions[0]
_assert_argv_contains_prefix_suffix(env, action, "-Lnative=", "/native_deps")
_assert_argv_contains(env, action, "--crate-type=proc-macro")
_assert_argv_contains(env, action, "-lstatic=native_dep")
return analysistest.end(env)
Expand All @@ -80,6 +99,7 @@ def _bin_has_native_libs_test_impl(ctx):
tut = analysistest.target_under_test(env)
actions = analysistest.target_actions(env)
action = actions[0]
_assert_argv_contains_prefix_suffix(env, action, "-Lnative=", "/native_deps")
_assert_argv_contains(env, action, "-lstatic=native_dep")
return analysistest.end(env)

Expand Down

0 comments on commit eff3c47

Please sign in to comment.