Skip to content

Commit

Permalink
Test cc_binary dynamic_deps runfiles
Browse files Browse the repository at this point in the history
  • Loading branch information
fmeum committed Jul 23, 2024
1 parent b37e695 commit eea765c
Show file tree
Hide file tree
Showing 10 changed files with 155 additions and 0 deletions.
3 changes: 3 additions & 0 deletions examples/.bazelrc
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ startup --nowindows_enable_symlinks
# top-level configuration.
common --experimental_output_directory_naming_scheme=diff_against_dynamic_baseline

# Required with Bazel 6
common --experimental_cc_shared_library

# Prevent Java compiler warnings such as:
# [0.002s][warning][perf,memops] Cannot use file /tmp/hsperfdata_runner/2 because it is locked by another process (errno = 11)
common --incompatible_sandbox_hermetic_tmp
Expand Down
33 changes: 33 additions & 0 deletions examples/dynamic_deps/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
load("//dynamic_deps/rules:runfiles_dir.bzl", "runfiles_dir")
load(":cc_define_binary.bzl", "cc_define_binary")

cc_define_binary(
name = "bin",
srcs = ["bin.cpp"],
dynamic_deps = ["//dynamic_deps/lib:shared"],
deps = ["//dynamic_deps/lib"],
)

runfiles_dir(
name = "bin_runfiles",
executable = ":bin",
)

sh_test(
name = "bin_test_runfiles",
srcs = ["bin_test.sh"],
args = ["$(rlocationpath :bin)"],
data = [":bin"],
deps = ["@bazel_tools//tools/bash/runfiles"],
)

sh_test(
name = "bin_test_direct",
srcs = ["bin_test.sh"],
args = select({
"@platforms//os:windows": ["$(rlocationpath :bin_runfiles)/bin.exe"],
"//conditions:default": ["$(rlocationpath :bin_runfiles)/bin"],
}),
data = [":bin_runfiles"],
deps = ["@bazel_tools//tools/bash/runfiles"],
)
6 changes: 6 additions & 0 deletions examples/dynamic_deps/bin.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#include "dynamic_deps/lib/lib.h"

int main() {
PrintGreeting();
return 0;
}
25 changes: 25 additions & 0 deletions examples/dynamic_deps/bin_test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/usr/bin/env bash

# --- begin runfiles.bash initialization v3 ---
# Copy-pasted from the Bazel Bash runfiles library v3.
set -uo pipefail; set +e; f=bazel_tools/tools/bash/runfiles/runfiles.bash
# shellcheck disable=SC1090
source "${RUNFILES_DIR:-/dev/null}/$f" 2>/dev/null || \
source "$(grep -sm1 "^$f " "${RUNFILES_MANIFEST_FILE:-/dev/null}" | cut -f2- -d' ')" 2>/dev/null || \
source "$0.runfiles/$f" 2>/dev/null || \
source "$(grep -sm1 "^$f " "$0.runfiles_manifest" | cut -f2- -d' ')" 2>/dev/null || \
source "$(grep -sm1 "^$f " "$0.exe.runfiles_manifest" | cut -f2- -d' ')" 2>/dev/null || \
{ echo>&2 "ERROR: cannot find $f"; exit 1; }; f=; set -e
# --- end runfiles.bash initialization v3 ---

bin_path=$(rlocation "$1" || { >&2 echo "$1 not found"; exit 1; })
output=$(env \
-u RUNFILES_DIR \
-u RUNFILES_MANIFEST \
-u RUNFILES_MANIFEST_ONLY \
-u JAVA_RUNFILES \
"$bin_path" || { >&2 echo "Failed to run $bin_path"; exit 1; })
if [[ "$output" != "Hello,_world!" ]]; then
echo "Unexpected output: $output"
exit 1
fi
5 changes: 5 additions & 0 deletions examples/dynamic_deps/cc_define_binary.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
load("@with_cfg.bzl", "with_cfg")

_builder = with_cfg(native.cc_binary)
_builder.extend("copt", ["-DGREETING=\"Hello,_world!\""])
cc_define_binary, _cc_define_binary = _builder.build()
14 changes: 14 additions & 0 deletions examples/dynamic_deps/lib/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
cc_library(
name = "lib",
srcs = ["lib.cpp"],
hdrs = ["lib.h"],
visibility = ["//dynamic_deps:__pkg__"],
tags = ["manual"],
)

cc_shared_library(
name = "shared",
deps = [":lib"],
visibility = ["//dynamic_deps:__pkg__"],
tags = ["manual"],
)
7 changes: 7 additions & 0 deletions examples/dynamic_deps/lib/lib.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#include "lib.h"

#include <iostream>

void PrintGreeting() {
std::cout << GREETING << std::endl;
}
3 changes: 3 additions & 0 deletions examples/dynamic_deps/lib/lib.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#pragma once

void PrintGreeting();
Empty file.
59 changes: 59 additions & 0 deletions examples/dynamic_deps/rules/runfiles_dir.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
def _runfiles_dir_impl(ctx):
# type: (ctx) -> list
out_dir = ctx.actions.declare_directory(ctx.label.name)
default_info = ctx.attr.executable[DefaultInfo]
executable = default_info.files_to_run.executable
name = executable.basename

args = ctx.actions.args()
args.add(out_dir.path)

direct_inputs = [default_info.files_to_run.repo_mapping_manifest]
transitive_inputs = [default_info.default_runfiles.files]
inputs = depset(direct_inputs, transitive = transitive_inputs)

ctx.actions.run_shell(
inputs = inputs,
outputs = [out_dir],
command = """
mkdir -p {out_dir}/{name}.runfiles
cp {executable} {out_dir}/{name}
cp {repo_mapping} {out_dir}/{name}.repo_mapping
""".format(
name = name,
out_dir = out_dir.path,
executable = executable.path,
repo_mapping = default_info.files_to_run.repo_mapping_manifest.path,
) + "\n".join([
"""
mkdir -p $(dirname {out_dir}/{name}.runfiles/{rlocationpath})
cp {path} {out_dir}/{name}.runfiles/{rlocationpath}
""".format(
name = name,
out_dir = out_dir.path,
path = f.path,
rlocationpath = _rlocationpath(ctx, f),
)
for f in default_info.default_runfiles.files.to_list()
]),
)

return [
DefaultInfo(files = depset([out_dir])),
]

def _rlocationpath(ctx, f):
if f.short_path.startswith("../"):
return f.short_path[3:]
else:
return ctx.workspace_name + "/" + f.short_path

runfiles_dir = rule(
implementation = _runfiles_dir_impl,
attrs = {
"executable": attr.label(
cfg = "target",
executable = True,
),
},
)

0 comments on commit eea765c

Please sign in to comment.