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.
BREAKING: Modify path layout to fix relative RPATH lookups (#104)
The executable of an executable transitioned rule is now created in a subdirectory of the target's package, which allows it to match the path depth under the execroot of the original executable and thus gets relative RPATH entries to resolve. Also copy any DLLs next to the new executable on Windows. The new path layout can be disabled via `--no@with_cfg.bzl//:incompatible_same_depth_path_layout`, e.g. if users hardcoded rlocationpaths and don't want to update them now. It is recommended to avoid this and use `$(rlocationpath ...)` instead. Fixes #85
- Loading branch information
Showing
14 changed files
with
221 additions
and
9 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 |
---|---|---|
@@ -1,8 +1,15 @@ | ||
load("@bazel_skylib//:bzl_library.bzl", "bzl_library") | ||
load("@bazel_skylib//rules:common_settings.bzl", "bool_flag") | ||
|
||
bzl_library( | ||
name = "with_cfg", | ||
srcs = ["with_cfg.bzl"], | ||
visibility = ["//visibility:public"], | ||
deps = ["//with_cfg:defs"], | ||
) | ||
|
||
bool_flag( | ||
name = "incompatible_same_depth_path_layout", | ||
build_setting_default = True, | ||
visibility = ["//visibility:public"], | ||
) |
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
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
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,58 @@ | ||
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_from_symlinked_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"], | ||
target_compatible_with = select({ | ||
# TODO: Get this test to pass on Windows. | ||
"@platforms//os:windows": ["@platforms//:incompatible"], | ||
# TODO: Reenable on macOS after: | ||
# 1. Bazel is released with the fix in https://github.com/bazelbuild/bazel/pull/23089. | ||
# 2. Enabling --incompatible_macos_set_install_name. | ||
# 3. Adding a dep on apple_support, as the default Unix toolchain only supports this flag | ||
# after https://github.com/bazelbuild/bazel/pull/23090. | ||
"@platforms//os:macos": ["@platforms//:incompatible"], | ||
"//conditions:default": [], | ||
}), | ||
) | ||
|
||
sh_test( | ||
name = "bin_test_from_regular_runfiles", | ||
srcs = ["bin_test.sh"], | ||
args = select({ | ||
"@platforms//os:windows": ["$(rlocationpath :bin_runfiles)/bin.exe.runfiles/$(rlocationpath :bin)"], | ||
"//conditions:default": ["$(rlocationpath :bin_runfiles)/bin.runfiles/$(rlocationpath :bin)"], | ||
}), | ||
data = [ | ||
":bin", | ||
":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,21 @@ | ||
#!/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=$("$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,8 @@ | ||
#define COMPILING_DLL | ||
#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,13 @@ | ||
#pragma once | ||
|
||
#ifdef _WIN32 | ||
#ifdef COMPILING_DLL | ||
#define DLLEXPORT __declspec(dllexport) | ||
#else | ||
#define DLLEXPORT __declspec(dllimport) | ||
#endif | ||
#else | ||
#define DLLEXPORT | ||
#endif | ||
|
||
DLLEXPORT 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,53 @@ | ||
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) | ||
|
||
ctx.actions.run_shell( | ||
inputs = default_info.default_runfiles.files, | ||
outputs = [out_dir], | ||
command = """ | ||
mkdir -p {out_dir}/{name}.runfiles | ||
cp {executable} {out_dir}/{name} | ||
""".format( | ||
name = name, | ||
out_dir = out_dir.path, | ||
executable = executable.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, | ||
), | ||
}, | ||
) |
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
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