Skip to content

Commit

Permalink
Simplify symlink helper
Browse files Browse the repository at this point in the history
  • Loading branch information
fmeum committed Jul 28, 2022
1 parent 31618be commit 9700359
Showing 1 changed file with 10 additions and 32 deletions.
42 changes: 10 additions & 32 deletions src/test/shell/bazel/bazel_symlink_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -228,17 +228,6 @@ EOF

function test_file_instead_of_symlink() {
mkdir -p a
cat > a/MakeSymlink.java <<'EOF'
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
public class MakeSymlink {
public static void main(String[] args) throws IOException {
Files.createSymbolicLink(Paths.get(args[0]), Paths.get(args[1]));
}
}
EOF

cat > a/a.bzl <<'EOF'
def _bad_symlink_impl(ctx):
symlink = ctx.actions.declare_symlink(ctx.label.name)
Expand Down Expand Up @@ -266,12 +255,6 @@ EOF
cat > a/BUILD <<'EOF'
load(":a.bzl", "bad_symlink", "bad_write")
java_binary(
name = "MakeSymlink",
srcs = ["MakeSymlink.java"],
main_class = "MakeSymlink",
)
bad_symlink(name="bs", link_target="bad/symlink")
genrule(name="bsg", srcs=[":bs"], outs=["bsgo"], cmd="echo BSGO > $@")
Expand All @@ -282,8 +265,9 @@ genrule(
name="bg",
srcs=[],
outs=["bgo"],
exec_tools = [":MakeSymlink"],
cmd = "$(execpath :MakeSymlink) $@ bad/symlink",
# We use python rather than a simple ln since the latter doesn't handle dangling symlinks on
# Windows.
cmd = "python3 -c 'import os, sys; os.symlink(*sys.argv[1:])' bad/symlink $@",
)
EOF

Expand Down Expand Up @@ -344,11 +328,15 @@ EOF
def _a_impl(ctx):
symlink = ctx.actions.declare_symlink(ctx.label.name + ".link")
output = ctx.actions.declare_file(ctx.label.name + ".file")
ctx.actions.run(
ctx.actions.run_shell(
outputs = [symlink],
executable = ctx.executable._make_symlink,
arguments = [symlink.path, ctx.attr.link_target],
inputs = depset([]),
# We use python rather than a simple ln since the latter doesn't handle dangling symlinks
# on Windows.
command = "python3 -c 'import os, sys; os.symlink(*sys.argv[1:])' {target} {name}".format(
target = ctx.attr.link_target,
name = symlink.path,
),
)
ctx.actions.run_shell(
outputs = [output],
Expand All @@ -361,23 +349,13 @@ a = rule(
implementation = _a_impl,
attrs = {
"link_target": attr.string(),
"_make_symlink": attr.label(
default = ":MakeSymlink",
executable = True,
cfg = "exec",
)
}
)
EOF

cat > a/BUILD <<'EOF'
load(":a.bzl", "a")
java_binary(
name = "MakeSymlink",
srcs = ["MakeSymlink.java"],
main_class = "MakeSymlink",
)
a(name="a", link_target="somewhere/over/the/rainbow")
EOF

Expand Down

0 comments on commit 9700359

Please sign in to comment.