Skip to content

Commit 72a88a1

Browse files
committed
feat: add runtime_executable adapter for foreign_cc binaries
Introduce a `runtime_executable` rule for turning selected foreign_cc binary outputs into executable Bazel targets. This cannot live directly on the existing foreign_cc rules because foreign_cc outputs are not always expected to contain a binary, so those rules cannot always expose an executable. The adapted executable may require `runtime_library_search_directories = "enabled"` on the producing foreign_cc target when it depends directly or transitively on foreign_cc-produced shared libraries. `runtime_executable` is a more Bazel-native version of `runnable_binary` because it exposes the executable through `DefaultInfo.files_to_run` for downstream consumers. The API is intentionally kept similar to `runnable_binary`. For now, it cannot completely replace `runnable_binary` because `runtime_library_search_directories` defaults to `"disabled"` to minimize blast radius while these features mature.
1 parent 42e8af5 commit 72a88a1

20 files changed

Lines changed: 799 additions & 4 deletions

docs/BUILD.bazel

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,16 @@ stardoc(
8989
],
9090
)
9191

92+
stardoc(
93+
name = "runtime_executable_docs",
94+
out = "src/runtime_executable.md",
95+
input = "@rules_foreign_cc//foreign_cc:runtime_executable.bzl",
96+
deps = [
97+
":rules_cc_deps",
98+
"@rules_foreign_cc//foreign_cc:runtime_executable",
99+
],
100+
)
101+
92102
DOCS_TARGETS = [
93103
":cmake_docs",
94104
":ninja_docs",
@@ -97,6 +107,7 @@ DOCS_TARGETS = [
97107
":meson_docs",
98108
":msbuild_docs",
99109
":providers_docs",
110+
":runtime_executable_docs",
100111
]
101112

102113
build_test(

docs/docs.bzl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,13 @@ load(
1212
_meson = "meson",
1313
_meson_with_requirements = "meson_with_requirements",
1414
_ninja = "ninja",
15+
_runtime_executable = "runtime_executable",
1516
)
1617
load(
1718
"@rules_foreign_cc//foreign_cc:providers.bzl",
1819
_ForeignCcArtifactInfo = "ForeignCcArtifactInfo",
1920
_ForeignCcDepsInfo = "ForeignCcDepsInfo",
21+
_ForeignCcRuntimeExecutableInfo = "ForeignCcRuntimeExecutableInfo",
2022
)
2123
load("@rules_foreign_cc//foreign_cc:repositories.bzl", _rules_foreign_cc_dependencies = "rules_foreign_cc_dependencies")
2224
load("@rules_foreign_cc//foreign_cc/built_tools:cmake_build.bzl", _cmake_tool = "cmake_tool")
@@ -44,7 +46,9 @@ native_tool_toolchain = _native_tool_toolchain
4446
ninja = _ninja
4547
ninja_tool = _ninja_tool
4648
rules_foreign_cc_dependencies = _rules_foreign_cc_dependencies
49+
runtime_executable = _runtime_executable
4750

4851
ForeignCcArtifactInfo = _ForeignCcArtifactInfo
4952
ForeignCcDepsInfo = _ForeignCcDepsInfo
53+
ForeignCcRuntimeExecutableInfo = _ForeignCcRuntimeExecutableInfo
5054
ToolInfo = _ToolInfo

docs/src/SUMMARY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@
1212
- [meson](meson.md)
1313
- [msbuild](msbuild.md)
1414
- [ninja](ninja.md)
15+
- [runtime_executable](runtime_executable.md)

docs/src/rules.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@
55
- [make](./make.md)
66
- [meson](./meson.md)
77
- [ninja](./ninja.md)
8+
- [runtime_executable](./runtime_executable.md)
89

910
For additional rules/macros/providers, see the [full API in one page](./flatten.md).
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
load("@rules_foreign_cc//foreign_cc:defs.bzl", "runtime_executable")
2+
load("@rules_shell//shell:sh_test.bzl", "sh_test")
3+
4+
runtime_executable(
5+
name = "openssl_runtime_executable",
6+
binary = select({
7+
"@rules_cc//cc/compiler:msvc-cl": "openssl.exe",
8+
"//conditions:default": "openssl",
9+
}),
10+
foreign_cc_target = "@openssl//:openssl",
11+
)
12+
13+
sh_test(
14+
name = "openssl_help_test",
15+
size = "small",
16+
srcs = ["openssl_help_test.sh"],
17+
args = ["$(rlocationpath :openssl_runtime_executable)"],
18+
data = [
19+
":openssl_runtime_executable",
20+
"@bazel_tools//tools/bash/runfiles",
21+
],
22+
)
23+
24+
test_suite(
25+
name = "tests",
26+
tests = [":openssl_help_test"],
27+
)
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#!/usr/bin/env bash
2+
3+
set -euo pipefail
4+
5+
set +u
6+
f=bazel_tools/tools/bash/runfiles/runfiles.bash
7+
source "${RUNFILES_DIR:-/dev/null}/$f" 2>/dev/null || \
8+
source "$(grep -sm1 "^$f " "${RUNFILES_MANIFEST_FILE:-/dev/null}" | cut -d ' ' -f 2-)" 2>/dev/null || \
9+
source "$0.runfiles/$f" 2>/dev/null || \
10+
source "$(grep -sm1 "^$f " "$0.runfiles_manifest" | cut -d ' ' -f 2-)" 2>/dev/null || {
11+
echo >&2 "cannot find $f"
12+
exit 1
13+
}
14+
set -u
15+
16+
if [[ "$#" -ne 1 ]]; then
17+
echo "usage: $0 <openssl-runfile-path>" >&2
18+
exit 1
19+
fi
20+
21+
openssl="$(rlocation "$1")"
22+
if [[ -z "$openssl" || ! -x "$openssl" ]]; then
23+
echo "openssl runtime executable is not executable: $1" >&2
24+
exit 1
25+
fi
26+
27+
exec "$openssl" help

examples/third_party/openssl/BUILD.openssl.bazel

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,10 +116,14 @@ configure_make(
116116
"install_name_tool -id @rpath/libcrypto.1.1.dylib $$INSTALLDIR/lib/libcrypto.1.1.dylib",
117117
"install_name_tool -change $$BUILD_TMPDIR/openssl/lib/libcrypto.1.1.dylib @rpath/libcrypto.1.1.dylib $$INSTALLDIR/lib/libssl.1.1.dylib",
118118
"install_name_tool -change $$INSTALLDIR/lib/libcrypto.1.1.dylib @rpath/libcrypto.1.1.dylib $$INSTALLDIR/lib/libssl.1.1.dylib",
119+
"install_name_tool -change $$BUILD_TMPDIR/openssl/lib/libssl.1.1.dylib @rpath/libssl.1.1.dylib $$INSTALLDIR/bin/openssl",
120+
"install_name_tool -change $$BUILD_TMPDIR/openssl/lib/libcrypto.1.1.dylib @rpath/libcrypto.1.1.dylib $$INSTALLDIR/bin/openssl",
119121
]),
120122
"//conditions:default": "",
121123
}),
122124
resource_size = "enormous",
125+
runtime_library_search_directories = "enabled",
126+
shared_ldflags_vars = ["SHLIB_LDFLAGS"],
123127
target_compatible_with = select({
124128
"@platforms//os:windows": ["@platforms//:incompatible"],
125129
"//conditions:default": [],

foreign_cc/BUILD.bazel

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ bzl_library(
9797
":meson",
9898
":msbuild",
9999
":ninja",
100+
":runtime_executable",
100101
":utils",
101102
],
102103
)
@@ -212,3 +213,14 @@ bzl_library(
212213
srcs = ["providers.bzl"],
213214
visibility = ["//visibility:public"],
214215
)
216+
217+
bzl_library(
218+
name = "runtime_executable",
219+
srcs = ["runtime_executable.bzl"],
220+
visibility = ["//visibility:public"],
221+
deps = [
222+
":providers",
223+
"@bazel_skylib//lib:shell",
224+
"@rules_shell//shell:rules_bzl",
225+
],
226+
)

foreign_cc/defs.bzl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ load(":make.bzl", _make = "make", _make_variant = "make_variant")
1111
load(":meson.bzl", _meson = "meson", _meson_with_requirements = "meson_with_requirements")
1212
load(":msbuild.bzl", _msbuild = "msbuild")
1313
load(":ninja.bzl", _ninja = "ninja")
14+
load(":runtime_executable.bzl", _runtime_executable = "runtime_executable")
1415
load(":utils.bzl", _runnable_binary = "runnable_binary")
1516

1617
boost_build = _boost_build
@@ -25,3 +26,4 @@ meson_with_requirements = _meson_with_requirements
2526
msbuild = _msbuild
2627
ninja = _ninja
2728
runnable_binary = _runnable_binary
29+
runtime_executable = _runtime_executable

foreign_cc/private/BUILD.bazel

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
22

33
exports_files([
44
"runnable_binary_wrapper.sh",
5+
"runtime_executable_wrapper.sh.tpl",
56
"settings.sh.in",
67
])
78

@@ -150,7 +151,9 @@ bzl_library(
150151
name = "transitions",
151152
srcs = ["transitions.bzl"],
152153
visibility = ["//foreign_cc:__subpackages__"],
153-
deps = ["//foreign_cc:providers"],
154+
deps = [
155+
"//foreign_cc:providers",
156+
],
154157
)
155158

156159
bzl_library(

0 commit comments

Comments
 (0)