Skip to content

Commit f71cd1e

Browse files
tausbnCopilot
andcommitted
unified: Package the swift-syntax parser in the extractor pack
The extractor shells out to a separate `swift-syntax-parse` binary, but nothing placed it in the extractor pack, so a shipped Swift extraction failed at the first spawn. Package it next to the extractor, the same way `//swift/extractor` ships its Swift-linked binary: a small wrapper points the dynamic loader at its own directory and execs the real binary, whose Swift runtime libraries travel alongside it. - `swift-syntax-parse.sh`: wrapper that sets `LD_LIBRARY_PATH` / `DYLD_LIBRARY_PATH` to its directory and execs `swift-syntax-parse.real` (mirrors `swift/extractor/extractor.sh`). - `runtime.bzl`: a `swift_runtime_libs` rule that selects just the Linux Swift runtime shared objects (`usr/lib/swift/linux/*.so`) out of the full toolchain, so only they — not the whole toolchain — travel with the binary. - `swift-syntax-rs/BUILD.bazel`: the `rust_binary` becomes `swift-syntax-parse.real` and carries the runtime libraries as runfiles on Linux; a `sh_binary` (`swift-syntax-parse`) is the wrapper; `codeql_pkg_runfiles` flattens the three (wrapper, real binary, runtime) into one directory. - `BUILD.bazel`: ship that group under `tools/{CODEQL_PLATFORM}` next to the extractor, on the platforms where swift-syntax builds (Linux/macOS). Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent c5d3ab4 commit f71cd1e

4 files changed

Lines changed: 102 additions & 7 deletions

File tree

unified/BUILD.bazel

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
load("@rules_pkg//pkg:mappings.bzl", "pkg_filegroup")
22
load("//misc/bazel:pkg.bzl", "codeql_pack", "codeql_pkg_files")
3+
load("//misc/bazel:utils.bzl", "select_os")
34

45
package(default_visibility = ["//visibility:public"])
56

@@ -46,12 +47,26 @@ codeql_pkg_files(
4647
prefix = "tools/{CODEQL_PLATFORM}",
4748
)
4849

50+
# The Swift front-end parser (wrapper + real binary + bundled Swift runtime),
51+
# shipped next to the extractor. Only on platforms where swift-syntax builds
52+
# (Linux/macOS); elsewhere the group is empty so the pack still builds (Swift
53+
# extraction is simply unavailable there).
54+
pkg_filegroup(
55+
name = "swift-syntax-parse-arch",
56+
srcs = select_os(
57+
posix = ["//unified/swift-syntax-rs:swift-syntax-parse-pkg"],
58+
otherwise = [],
59+
),
60+
prefix = "tools/{CODEQL_PLATFORM}",
61+
)
62+
4963
codeql_pack(
5064
name = "unified",
5165
srcs = [
5266
":codeql-extractor-yml",
5367
":dbscheme-group",
5468
":extractor-arch",
69+
":swift-syntax-parse-arch",
5570
"//unified/tools",
5671
],
5772
)

unified/swift-syntax-rs/BUILD.bazel

Lines changed: 46 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
load("@rules_rust//rust:defs.bzl", "rust_binary", "rust_library", "rust_test")
2+
load("@rules_shell//shell:sh_binary.bzl", "sh_binary")
3+
load("//misc/bazel:pkg.bzl", "codeql_pkg_runfiles")
4+
load(":runtime.bzl", "swift_runtime_libs")
25
load(":xcode_transition.bzl", "xcode_transition_swift_library")
36

47
package(default_visibility = ["//visibility:public"])
@@ -54,22 +57,58 @@ rust_library(
5457
],
5558
)
5659

60+
# The Swift front-end parser. We ship it like `//swift/extractor`: a small shell
61+
# wrapper (`swift-syntax-parse`) sets `LD_LIBRARY_PATH`/`DYLD_LIBRARY_PATH` to its
62+
# own directory and execs the real binary (`swift-syntax-parse.real`); the Swift
63+
# runtime shared libraries are packaged alongside them. `parse.rs` resolves the
64+
# wrapper as a sibling of the extractor executable.
5765
rust_binary(
58-
name = "swift-syntax-parse",
66+
name = "swift-syntax-parse.real",
5967
srcs = ["src/main.rs"],
60-
# `rust_binary` doesn't copy the Swift runtime into runfiles the way
61-
# `swift_binary` does. On Linux, ship the standalone toolchain's runtime;
62-
# on macOS the OS provides it at `/usr/lib/swift` (rpath'd by
63-
# `xcode_swift_toolchain`).
68+
# Target name carries `.real` (invalid in a crate name), so set it explicitly.
69+
crate_name = "swift_syntax_parse",
70+
# On Linux, carry the toolchain's runtime shared libraries as runfiles so
71+
# they get packaged next to the binary. On macOS the OS provides the Swift
72+
# runtime, so nothing extra is bundled.
6473
data = select({
65-
"@platforms//os:macos": [],
66-
"@platforms//os:linux": ["@swift_toolchain_ubuntu24.04//:files"],
74+
"@platforms//os:linux": [":swift_runtime_libs"],
75+
"//conditions:default": [],
6776
}),
6877
edition = "2024",
6978
target_compatible_with = _SWIFT_SUPPORTED_PLATFORMS,
7079
deps = [":swift_syntax_rs"],
7180
)
7281

82+
# The Linux runtime shared libraries (`libswiftCore.so`, …) filtered out of the
83+
# full toolchain, so only they (not the whole toolchain) travel with the binary.
84+
swift_runtime_libs(
85+
name = "swift_runtime_libs",
86+
toolchain = "@swift_toolchain_ubuntu24.04//:files",
87+
target_compatible_with = ["@platforms//os:linux"],
88+
)
89+
90+
# `swift-syntax-parse` wrapper (see `swift-syntax-parse.sh`). Its runfiles carry
91+
# the real binary and the runtime libraries; packaging flattens them into one
92+
# directory.
93+
sh_binary(
94+
name = "swift-syntax-parse",
95+
srcs = ["swift-syntax-parse.sh"],
96+
data = [":swift-syntax-parse.real"],
97+
target_compatible_with = _SWIFT_SUPPORTED_PLATFORMS,
98+
)
99+
100+
# Packaged form for the extractor pack: the wrapper (as `swift-syntax-parse`),
101+
# the real binary, and the runtime libraries, flattened into one directory.
102+
codeql_pkg_runfiles(
103+
name = "swift-syntax-parse-pkg",
104+
exes = [":swift-syntax-parse"],
105+
# The `.sh` source is shipped as `swift-syntax-parse` (the wrapper); drop the
106+
# original filename.
107+
excludes = ["swift-syntax-parse.sh"],
108+
target_compatible_with = _SWIFT_SUPPORTED_PLATFORMS,
109+
visibility = ["//unified:__pkg__"],
110+
)
111+
73112
rust_test(
74113
name = "swift_syntax_rs_test",
75114
size = "small",
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
"""Select just the Swift runtime shared libraries from a toolchain filegroup.
2+
3+
The standalone Swift toolchain's `:files` target is the *entire* toolchain
4+
(compiler, headers, static archives, …). For shipping `swift-syntax-parse` we
5+
only want the Linux runtime shared objects under `usr/lib/swift/linux/`, so they
6+
can travel next to the binary in the extractor pack (found at run time via the
7+
`swift-syntax-parse.sh` wrapper's `LD_LIBRARY_PATH`). Flattening the whole
8+
toolchain would both bloat the pack and collide on duplicate header basenames.
9+
"""
10+
11+
def _swift_runtime_libs_impl(ctx):
12+
libs = [
13+
f
14+
for f in ctx.files.toolchain
15+
if "/usr/lib/swift/linux/" in f.path and f.basename.endswith(".so")
16+
]
17+
return [DefaultInfo(files = depset(libs))]
18+
19+
swift_runtime_libs = rule(
20+
implementation = _swift_runtime_libs_impl,
21+
doc = "Filegroup of the Swift toolchain's Linux runtime shared libraries.",
22+
attrs = {
23+
"toolchain": attr.label(
24+
mandatory = True,
25+
allow_files = True,
26+
doc = "A filegroup exposing the whole Swift toolchain's files.",
27+
),
28+
},
29+
)
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/bin/bash
2+
3+
# Wrapper that lets the shipped `swift-syntax-parse` find its Swift runtime
4+
# libraries, which are packaged in the same directory as this script (and the
5+
# real binary). Mirrors `swift/extractor/extractor.sh`.
6+
if [[ "$(uname)" == Darwin ]]; then
7+
export DYLD_LIBRARY_PATH=$(dirname "$0")
8+
else
9+
export LD_LIBRARY_PATH=$(dirname "$0")
10+
fi
11+
12+
exec -a "$0" "$0.real" "$@"

0 commit comments

Comments
 (0)