Skip to content

Commit

Permalink
rustc: add support for alwayslink library dependencies
Browse files Browse the repository at this point in the history
This follows up on the previous commit's refactoring and actually adds
alwayslink support.

Fixes #325.
  • Loading branch information
durin42 committed Feb 25, 2021
1 parent ec127db commit 12bcb7c
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 5 deletions.
17 changes: 16 additions & 1 deletion rust/private/rustc.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -821,7 +821,22 @@ def _make_link_flags(toolchain, lib):
args = []
f = get_preferred_artifact(lib)
if lib.alwayslink:
pass # TODO
if toolchain.os.startswith("darwin") or toolchain.os.startswith("macos"):
args.extend([
"-C",
("link-arg=-Wl,-force_load,%s" % f.path),
])
elif toolchain.os.startswith("win"):
args.append("/WHOLEARCHIVE:%s" % f.path)
else:
args.extend([
"-C",
"link-arg=-Wl,--whole-archive",
"-C",
("link-arg=%s" % f.path),
"-C",
"link-arg=-Wl,--no-whole-archive",
])
elif lib.static_library or lib.pic_static_library:
args.append("-lstatic=%s" % get_lib_name(f))
elif _is_dylib(toolchain, lib):
Expand Down
27 changes: 23 additions & 4 deletions test/unit/native_deps/native_deps_test.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -104,24 +104,43 @@ def _bin_has_native_libs_test_impl(ctx):
return analysistest.end(env)

def _extract_linker_args(argv):
return [a for a in argv if a.startswith("link-arg=-Wl") or a.startswith("link-arg=-l") or a.startswith("-l")]
return [a for a in argv if a.startswith("link-arg=-Wl") or a.startswith("link-arg=-l") or a.startswith("-l") or a.endswith(".lo") or a.endswith(".o")]

def _bin_has_native_dep_and_alwayslink_test_impl(ctx):
env = analysistest.begin(ctx)
tut = analysistest.target_under_test(env)
actions = analysistest.target_actions(env)
action = actions[0]

# BUG: this should mention the alwayslink library, but doesn't
asserts.equals(env, _extract_linker_args(action.argv), ["-lstatic=native_dep"])
if ctx.target_platform_has_constraint(ctx.attr._macos_constraint[platform_common.ConstraintValueInfo]):
want = [
"-lstatic=native_dep",
"link-arg=-Wl,-force_load,bazel-out/darwin-fastbuild/bin/test/unit/native_deps/libalwayslink.lo",
]
elif ctx.target_platform_has_constraint(ctx.attr._windows_constraint[platform_common.ConstraintValueInfo]):
want = [
"-lstatic=native_dep",
"/WHOLEARCHIVE:bazel-out/darwin-fastbuild/bin/test/unit/native_deps/libalwayslink.lo",
]
else:
want = [
"-lstatic=native_dep",
"link-arg=-Wl,--whole-archive",
"link-arg=bazel-out/k8-fastbuild/bin/test/unit/native_deps/libalwayslink.lo",
"link-arg=-Wl,--no-whole-archive",
]
asserts.equals(env, _extract_linker_args(action.argv), want)
return analysistest.end(env)

rlib_has_no_native_libs_test = analysistest.make(_rlib_has_no_native_libs_test_impl)
staticlib_has_native_libs_test = analysistest.make(_staticlib_has_native_libs_test_impl)
cdylib_has_native_libs_test = analysistest.make(_cdylib_has_native_libs_test_impl)
proc_macro_has_native_libs_test = analysistest.make(_proc_macro_has_native_libs_test_impl)
bin_has_native_libs_test = analysistest.make(_bin_has_native_libs_test_impl)
bin_has_native_dep_and_alwayslink_test = analysistest.make(_bin_has_native_dep_and_alwayslink_test_impl)
bin_has_native_dep_and_alwayslink_test = analysistest.make(_bin_has_native_dep_and_alwayslink_test_impl, attrs = {
"_macos_constraint": attr.label(default = Label("@platforms//os:macos")),
"_windows_constraint": attr.label(default = Label("@platforms//os:windows")),
})

def _native_dep_test():
rust_library(
Expand Down

0 comments on commit 12bcb7c

Please sign in to comment.