Skip to content

Commit fb23b33

Browse files
committed
DRAFT symlink handling
1 parent d91735d commit fb23b33

File tree

13 files changed

+146
-275
lines changed

13 files changed

+146
-275
lines changed

Dockerfile

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
FROM ubuntu
2+
RUN apt-get update && apt-get install -y ca-certificates curl git python3 binutils zstd file clang lld
3+
RUN apt-get install -y build-essential
4+
RUN curl -L https://github.com/bazelbuild/bazelisk/releases/download/v1.18.0/bazelisk-linux-amd64 -o /usr/bin/bazel && chmod +x /usr/bin/bazel
5+
RUN adduser fakeuser
6+
USER fakeuser
7+
8+
WORKDIR /rules_rust
9+
RUN echo "8.4.2" > .bazelversion
10+
RUN touch WORKSPACE && bazel help
11+
RUN echo "bazel test //..." > ~/.bash_history

build_docker.sh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
set -eux
2+
3+
docker stop bazel-builder || true
4+
docker rm bazel-builder || true
5+
docker build --platform=linux/amd64 -t bazel-base-image .
6+
docker run --privileged --shm-size=4G \
7+
--mount type=bind,src="$(realpath $(dirname $0))",dst=/rules_rust \
8+
--mount type=bind,src="$HOME/.bazelrc",dst=/home/fakeuser/.bazelrc \
9+
--mount type=volume,src=bazel-cache,dst=/home/fakeuser/.cache/bazel \
10+
--name bazel-builder -it bazel-base-image bash

rust/private/clippy.bzl

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ def _clippy_aspect_impl(target, ctx):
132132
ctx.rule.attr.lint_config[LintsInfo].clippy_lint_files + \
133133
ctx.rule.attr.lint_config[LintsInfo].rustc_lint_files
134134

135-
compile_inputs, out_dir, build_env_files, build_flags_files, linkstamp_outs, ambiguous_libs = collect_inputs(
135+
compile_inputs, out_dir, build_env_files, build_flags_files, linkstamp_outs = collect_inputs(
136136
ctx,
137137
ctx.rule.file,
138138
ctx.rule.files,
@@ -171,7 +171,6 @@ def _clippy_aspect_impl(target, ctx):
171171
crate_info = crate_info,
172172
dep_info = dep_info,
173173
linkstamp_outs = linkstamp_outs,
174-
ambiguous_libs = ambiguous_libs,
175174
output_hash = determine_output_hash(crate_info.root, ctx.label),
176175
rust_flags = [],
177176
out_dir = out_dir,

rust/private/rustc.bzl

Lines changed: 27 additions & 183 deletions
Large diffs are not rendered by default.

rust/private/rustdoc.bzl

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ def rustdoc_compile_action(
103103
aliases = crate_info.aliases,
104104
)
105105

106-
compile_inputs, out_dir, build_env_files, build_flags_files, linkstamp_outs, ambiguous_libs = collect_inputs(
106+
compile_inputs, out_dir, build_env_files, build_flags_files, linkstamp_outs = collect_inputs(
107107
ctx = ctx,
108108
file = ctx.file,
109109
files = ctx.files,
@@ -137,7 +137,6 @@ def rustdoc_compile_action(
137137
crate_info = rustdoc_crate_info,
138138
dep_info = dep_info,
139139
linkstamp_outs = linkstamp_outs,
140-
ambiguous_libs = ambiguous_libs,
141140
output_hash = None,
142141
rust_flags = rustdoc_flags,
143142
out_dir = out_dir,

rust/private/unpretty.bzl

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ def _rust_unpretty_aspect_impl(target, ctx):
147147
)
148148
lint_files = []
149149

150-
compile_inputs, out_dir, build_env_files, build_flags_files, linkstamp_outs, ambiguous_libs = collect_inputs(
150+
compile_inputs, out_dir, build_env_files, build_flags_files, linkstamp_outs = collect_inputs(
151151
ctx,
152152
ctx.rule.file,
153153
ctx.rule.files,
@@ -196,7 +196,6 @@ def _rust_unpretty_aspect_impl(target, ctx):
196196
crate_info = crate_info,
197197
dep_info = dep_info,
198198
linkstamp_outs = linkstamp_outs,
199-
ambiguous_libs = ambiguous_libs,
200199
output_hash = determine_output_hash(crate_info.root, ctx.label),
201200
rust_flags = rust_flags,
202201
out_dir = out_dir,

test/unit/ambiguous_libs/ambiguous_libs_test.bzl

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,15 @@ def _ambiguous_deps_test_impl(ctx):
1919
tut = analysistest.target_under_test(env)
2020
rustc_action = [action for action in tut.actions if action.mnemonic == "Rustc"][0]
2121

22+
for_shared_library = _get_crate_info(tut).type in ("dylib", "cdylib", "proc-macro")
23+
pic_suffix = _get_pic_suffix(ctx, for_shared_library)
24+
2225
# We depend on two C++ libraries named "native_dep", which we need to pass to the command line
23-
# in the form of "-lstatic=native-dep-{hash} "-lstatic=native-dep-{hash}.pic.
24-
link_args = [arg for arg in rustc_action.argv if arg.startswith("-lstatic=native_dep-")]
26+
# in the form of "-Clink-arg=${pwd}/bazel-out/darwin_arm64-fastbuild/bin/test/unit/ambiguous_libs/first_dep/libnative_dep.a"
27+
link_args = [arg for arg in rustc_action.argv if arg.endswith("libnative_dep{}.a".format(pic_suffix))]
2528
asserts.equals(env, 2, len(link_args))
2629
asserts.false(env, link_args[0] == link_args[1])
2730

28-
for_shared_library = _get_crate_info(tut).type in ("dylib", "cdylib", "proc-macro")
29-
extension = _get_pic_suffix(ctx, for_shared_library)
30-
31-
asserts.true(env, link_args[0].endswith(extension))
32-
asserts.true(env, link_args[1].endswith(extension))
33-
3431
return analysistest.end(env)
3532

3633
def _get_pic_suffix(ctx, for_shared_library):

test/unit/common.bzl

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,18 @@ def assert_argv_contains_prefix_suffix(env, action, prefix, suffix):
2929
),
3030
)
3131

32+
def assert_argv_contains_prefix_suffix_not(env, action, prefix, suffix):
33+
for found_flag in action.argv:
34+
if found_flag.startswith(prefix) and found_flag.endswith(suffix):
35+
unittest.fail(
36+
env,
37+
"Expected an arg with prefix '{prefix}' and suffix '{suffix}' to not appear in {args}".format(
38+
prefix = prefix,
39+
suffix = suffix,
40+
args = action.argv,
41+
),
42+
)
43+
3244
def assert_argv_contains_prefix(env, action, prefix):
3345
for found_flag in action.argv:
3446
if found_flag.startswith(prefix):

test/unit/linker_inputs_propagation/linker_inputs_propagation_test.bzl

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,19 @@ def _dependency_linkopts_are_propagated_test_impl(ctx):
3030
# Expect a library's own linkopts to come after the flags we create to link them.
3131
# This is required, because linkopts are ordered and the linker will only apply later ones when resolving symbols required for earlier ones.
3232
# This means that if one of our transitive deps has a linkopt like `-lfoo`, the dep will see the symbols of foo at link time.
33-
_assert_contains_in_order(env, link_action.argv, ["-lstatic=foo_with_linkopts", "-Clink-arg=-lfoo_with_linkopts", "--codegen=link-arg=-L/doesnotexist"])
33+
link_flag_index = -1
34+
for i, arg in enumerate(link_action.argv):
35+
if arg.startswith("-Clink-arg=bazel-out") and arg.endswith("test/linker_inputs_propagation/libfoo_with_linkopts.a"):
36+
link_flag_index = i
37+
break
38+
if link_flag_index == -1:
39+
unittest.fail(env, "Link flag not found")
40+
41+
linkopt_index = link_action.argv.index("--codegen=link-arg=-L/doesnotexist")
42+
43+
if linkopt_index < link_flag_index:
44+
unittest.fail(env, "Flags out of order")
45+
3446
return analysistest.end(env)
3547

3648
def _assert_contains_input(env, inputs, name):
@@ -41,12 +53,6 @@ def _assert_contains_input(env, inputs, name):
4153
return
4254
unittest.fail(env, "Expected {} to contain a library starting with {}".format(inputs.to_list(), name))
4355

44-
def _assert_contains_in_order(env, haystack, needle):
45-
for i in range(len(haystack)):
46-
if haystack[i:i + len(needle)] == needle:
47-
return
48-
unittest.fail(env, "Expected {} to contain {}".format(haystack, needle))
49-
5056
def _get_lib_name(ctx, name):
5157
if ctx.target_platform_has_constraint(ctx.attr._windows_constraint[platform_common.ConstraintValueInfo]):
5258
return name

test/unit/native_deps/native_deps_test.bzl

Lines changed: 35 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ load("//rust:defs.bzl", "rust_binary", "rust_library", "rust_proc_macro", "rust_
66
load(
77
"//test/unit:common.bzl",
88
"assert_argv_contains",
9-
"assert_argv_contains_not",
109
"assert_argv_contains_prefix",
1110
"assert_argv_contains_prefix_not",
1211
"assert_argv_contains_prefix_suffix",
12+
"assert_argv_contains_prefix_suffix_not",
1313
"assert_list_contains_adjacent_elements",
1414
)
1515

@@ -28,8 +28,7 @@ def _rlib_has_no_native_libs_test_impl(ctx):
2828
tut = analysistest.target_under_test(env)
2929
action = tut.actions[0]
3030
assert_argv_contains(env, action, "--crate-type=rlib")
31-
assert_argv_contains_not(env, action, "-lstatic=native_dep")
32-
assert_argv_contains_not(env, action, "-ldylib=native_dep")
31+
assert_argv_contains_prefix_suffix_not(env, action, "-Clink-arg=bazel-out/", "test/unit/native_deps/libnative_dep.a")
3332
assert_argv_contains_prefix_not(env, action, "--codegen=linker=")
3433
return analysistest.end(env)
3534

@@ -40,17 +39,15 @@ def _cdylib_has_native_libs_test_impl(ctx):
4039
toolchain = _get_toolchain(ctx)
4140
compilation_mode = ctx.var["COMPILATION_MODE"]
4241
pic_suffix = _get_pic_suffix(ctx, compilation_mode)
43-
assert_argv_contains_prefix_suffix(env, action, "-Lnative=", "/native_deps")
4442
assert_argv_contains(env, action, "--crate-type=cdylib")
45-
assert_argv_contains(env, action, "-lstatic=native_dep{}".format(pic_suffix))
46-
if toolchain.target_os == "windows":
47-
if toolchain.target_triple.abi == "msvc":
48-
native_link_arg = "-Clink-arg=native_dep.lib"
49-
else:
50-
native_link_arg = "-Clink-arg=-lnative_dep.lib"
43+
44+
if toolchain.target_os == "windows" and toolchain.target_triple.abi == "msvc":
45+
assert_argv_contains_prefix_suffix(env, action, "-Lnative=", "/native_deps")
46+
assert_argv_contains(env, action, "-lstatic=native_dep{}".format(pic_suffix))
47+
assert_argv_contains(env, action, "-Clink-arg=native_dep.lib")
5148
else:
52-
native_link_arg = "-Clink-arg=-lnative_dep{}".format(pic_suffix)
53-
assert_argv_contains(env, action, native_link_arg)
49+
assert_argv_contains_prefix_suffix(env, action, "-Clink-arg=bazel-out/", "test/unit/native_deps/libnative_dep{}.a".format(pic_suffix))
50+
5451
assert_argv_contains_prefix(env, action, "--codegen=linker=")
5552
return analysistest.end(env)
5653

@@ -59,17 +56,14 @@ def _staticlib_has_native_libs_test_impl(ctx):
5956
tut = analysistest.target_under_test(env)
6057
action = tut.actions[0]
6158
toolchain = _get_toolchain(ctx)
62-
assert_argv_contains_prefix_suffix(env, action, "-Lnative=", "/native_deps")
63-
assert_argv_contains(env, action, "--crate-type=staticlib")
64-
assert_argv_contains(env, action, "-lstatic=native_dep")
65-
if toolchain.target_os == "windows":
66-
if toolchain.target_triple.abi == "msvc":
67-
native_link_arg = "-Clink-arg=native_dep.lib"
68-
else:
69-
native_link_arg = "-Clink-arg=-lnative_dep.lib"
59+
60+
if toolchain.target_os == "windows" and toolchain.target_triple.abi == "msvc":
61+
assert_argv_contains_prefix_suffix(env, action, "-Lnative=", "/native_deps")
62+
assert_argv_contains(env, action, "-lstatic=native_dep")
63+
assert_argv_contains(env, action, "-Clink-arg=native_dep.lib")
7064
else:
71-
native_link_arg = "-Clink-arg=-lnative_dep"
72-
assert_argv_contains(env, action, native_link_arg)
65+
assert_argv_contains_prefix_suffix(env, action, "-Clink-arg=bazel-out/", "test/unit/native_deps/libnative_dep.a")
66+
7367
assert_argv_contains_prefix(env, action, "--codegen=linker=")
7468
return analysistest.end(env)
7569

@@ -80,17 +74,15 @@ def _proc_macro_has_native_libs_test_impl(ctx):
8074
toolchain = _get_toolchain(ctx)
8175
compilation_mode = ctx.var["COMPILATION_MODE"]
8276
pic_suffix = _get_pic_suffix(ctx, compilation_mode)
83-
assert_argv_contains_prefix_suffix(env, action, "-Lnative=", "/native_deps")
8477
assert_argv_contains(env, action, "--crate-type=proc-macro")
85-
assert_argv_contains(env, action, "-lstatic=native_dep{}".format(pic_suffix))
86-
if toolchain.target_os == "windows":
87-
if toolchain.target_triple.abi == "msvc":
88-
native_link_arg = "-Clink-arg=native_dep.lib"
89-
else:
90-
native_link_arg = "-Clink-arg=-lnative_dep.lib"
78+
79+
if toolchain.target_os == "windows" and toolchain.target_triple.abi == "msvc":
80+
assert_argv_contains_prefix_suffix(env, action, "-Lnative=", "/native_deps")
81+
assert_argv_contains(env, action, "-lstatic=native_dep{}".format(pic_suffix))
82+
assert_argv_contains(env, action, "-Clink-arg=native_dep.lib")
9183
else:
92-
native_link_arg = "-Clink-arg=-lnative_dep{}".format(pic_suffix)
93-
assert_argv_contains(env, action, native_link_arg)
84+
assert_argv_contains_prefix_suffix(env, action, "-Clink-arg=bazel-out/", "test/unit/native_deps/libnative_dep{}.a".format(pic_suffix))
85+
9486
assert_argv_contains_prefix(env, action, "--codegen=linker=")
9587
return analysistest.end(env)
9688

@@ -99,16 +91,11 @@ def _bin_has_native_libs_test_impl(ctx):
9991
tut = analysistest.target_under_test(env)
10092
action = tut.actions[0]
10193
toolchain = _get_toolchain(ctx)
102-
assert_argv_contains_prefix_suffix(env, action, "-Lnative=", "/native_deps")
103-
assert_argv_contains(env, action, "-lstatic=native_dep")
104-
if toolchain.target_os == "windows":
105-
if toolchain.target_triple.abi == "msvc":
106-
native_link_arg = "-Clink-arg=native_dep.lib"
107-
else:
108-
native_link_arg = "-Clink-arg=-lnative_dep.lib"
94+
if toolchain.target_os == "windows" and toolchain.target_triple.abi == "msvc":
95+
assert_argv_contains(env, action, "-lstatic=native_dep")
96+
assert_argv_contains(env, action, "-Clink-arg=native_dep.lib")
10997
else:
110-
native_link_arg = "-Clink-arg=-lnative_dep"
111-
assert_argv_contains(env, action, native_link_arg)
98+
assert_argv_contains_prefix_suffix(env, action, "-Clink-arg=bazel-out/", "test/unit/native_deps/libnative_dep.a")
11299
assert_argv_contains_prefix(env, action, "--codegen=linker=")
113100
return analysistest.end(env)
114101

@@ -144,8 +131,7 @@ def _bin_has_native_dep_and_alwayslink_test_impl(ctx):
144131
if toolchain.target_os in ["macos", "darwin"]:
145132
darwin_component = _get_darwin_component(link_args[-1])
146133
want = [
147-
"-lstatic=native_dep",
148-
"-lnative_dep",
134+
"bazel-out/{}-{}/bin/{}test/unit/native_deps/libnative_dep.a".format(darwin_component, compilation_mode, workspace_prefix),
149135
"-Wl,-force_load,bazel-out/{}-{}/bin/{}test/unit/native_deps/libalwayslink.lo".format(darwin_component, compilation_mode, workspace_prefix),
150136
]
151137
assert_list_contains_adjacent_elements(env, link_args, want)
@@ -158,23 +144,21 @@ def _bin_has_native_dep_and_alwayslink_test_impl(ctx):
158144
]
159145
else:
160146
want = [
161-
"-lstatic=native_dep",
162-
"native_dep.lib",
147+
"bazel-out/x64_windows-{}/bin/{}test/unit/native_deps/libnative_dep.a".format(compilation_mode, workspace_prefix),
163148
"-Wl,--whole-archive",
164149
"bazel-out/x64_windows-{}/bin/{}test/unit/native_deps/alwayslink.lo.lib".format(compilation_mode, workspace_prefix),
165150
"-Wl,--no-whole-archive",
166151
]
167152
elif toolchain.target_arch == "s390x":
168153
want = [
169-
"-lstatic=native_dep",
154+
"bazel-out/s390x-{}/bin/{}test/unit/native_deps/libnative_dep.a".format(compilation_mode, workspace_prefix),
170155
"link-arg=-Wl,--whole-archive",
171156
"link-arg=bazel-out/s390x-{}/bin/{}test/unit/native_deps/libalwayslink.lo".format(compilation_mode, workspace_prefix),
172157
"link-arg=-Wl,--no-whole-archive",
173158
]
174159
else:
175160
want = [
176-
"-lstatic=native_dep",
177-
"-lnative_dep",
161+
"bazel-out/k8-{}/bin/{}test/unit/native_deps/libnative_dep.a".format(compilation_mode, workspace_prefix),
178162
"-Wl,--whole-archive",
179163
"bazel-out/k8-{}/bin/{}test/unit/native_deps/libalwayslink.lo".format(compilation_mode, workspace_prefix),
180164
"-Wl,--no-whole-archive",
@@ -198,8 +182,7 @@ def _cdylib_has_native_dep_and_alwayslink_test_impl(ctx):
198182
if toolchain.target_os in ["macos", "darwin"]:
199183
darwin_component = _get_darwin_component(linker_args[-1])
200184
want = [
201-
"-lstatic=native_dep{}".format(pic_suffix),
202-
"-lnative_dep{}".format(pic_suffix),
185+
"bazel-out/{}-{}/bin/{}test/unit/native_deps/libnative_dep{}.a".format(darwin_component, compilation_mode, workspace_prefix, pic_suffix),
203186
"-Wl,-force_load,bazel-out/{}-{}/bin/{}test/unit/native_deps/libalwayslink{}.lo".format(darwin_component, compilation_mode, workspace_prefix, pic_suffix),
204187
]
205188
elif toolchain.target_os == "windows":
@@ -211,23 +194,21 @@ def _cdylib_has_native_dep_and_alwayslink_test_impl(ctx):
211194
]
212195
else:
213196
want = [
214-
"-lstatic=native_dep",
215-
"native_dep.lib",
197+
"bazel-out/x64_windows-{}/bin/{}test/unit/native_deps/libnative_dep{}.a".format(compilation_mode, workspace_prefix, pic_suffix),
216198
"-Wl,--whole-archive",
217199
"bazel-out/x64_windows-{}/bin/{}test/unit/native_deps/alwayslink.lo.lib".format(compilation_mode, workspace_prefix),
218200
"-Wl,--no-whole-archive",
219201
]
220202
elif toolchain.target_arch == "s390x":
221203
want = [
222-
"-lstatic=native_dep{}".format(pic_suffix),
204+
"bazel-out/s390x-{}/bin/{}test/unit/native_deps/libnative_dep{}.a".format(compilation_mode, workspace_prefix, pic_suffix),
223205
"link-arg=-Wl,--whole-archive",
224206
"link-arg=bazel-out/s390x-{}/bin/{}test/unit/native_deps/libalwayslink{}.lo".format(compilation_mode, workspace_prefix, pic_suffix),
225207
"link-arg=-Wl,--no-whole-archive",
226208
]
227209
else:
228210
want = [
229-
"-lstatic=native_dep{}".format(pic_suffix),
230-
"-lnative_dep{}".format(pic_suffix),
211+
"bazel-out/k8-{}/bin/{}test/unit/native_deps/libnative_dep{}.a".format(compilation_mode, workspace_prefix, pic_suffix),
231212
"-Wl,--whole-archive",
232213
"bazel-out/k8-{}/bin/{}test/unit/native_deps/libalwayslink{}.lo".format(compilation_mode, workspace_prefix, pic_suffix),
233214
"-Wl,--no-whole-archive",

0 commit comments

Comments
 (0)