generated from bazel-contrib/rules-template
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Test cc_binary dynamic_deps runfiles
- Loading branch information
Showing
9 changed files
with
152 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
#pragma once | ||
|
||
void PrintGreeting(); |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
), | ||
}, | ||
) |