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>
This commit is contained in:
Taus
2026-07-24 15:48:00 +00:00
parent 7721ce2eba
commit e3a0822248
3 changed files with 62 additions and 5 deletions

View File

@@ -1,5 +1,6 @@
load("@rules_pkg//pkg:mappings.bzl", "pkg_filegroup")
load("//misc/bazel:pkg.bzl", "codeql_pack", "codeql_pkg_files")
load("//misc/bazel:utils.bzl", "select_os")
package(default_visibility = ["//visibility:public"])
@@ -46,12 +47,26 @@ codeql_pkg_files(
prefix = "tools/{CODEQL_PLATFORM}",
)
# The Swift front-end parser (wrapper + real binary + bundled Swift runtime),
# shipped next to the extractor. Only on platforms where swift-syntax builds
# (Linux/macOS); elsewhere the group is empty so the pack still builds (Swift
# extraction is simply unavailable there).
pkg_filegroup(
name = "swift-syntax-parse-arch",
srcs = select_os(
posix = ["//unified/swift-syntax-rs:swift-syntax-parse-pkg"],
otherwise = [],
),
prefix = "tools/{CODEQL_PLATFORM}",
)
codeql_pack(
name = "unified",
srcs = [
":codeql-extractor-yml",
":dbscheme-group",
":extractor-arch",
":swift-syntax-parse-arch",
"//unified/tools",
],
)

View File

@@ -1,4 +1,6 @@
load("@rules_rust//rust:defs.bzl", "rust_binary", "rust_library", "rust_test")
load("@rules_shell//shell:sh_binary.bzl", "sh_binary")
load("//misc/bazel:pkg.bzl", "codeql_pkg_runfiles")
load(":swift_runtime.bzl", "swift_runtime_libs")
load(":xcode_transition.bzl", "xcode_transition_swift_library")
@@ -48,13 +50,19 @@ rust_library(
],
)
# The Swift front-end parser. We ship it like `//swift/extractor`: a small shell
# wrapper (`swift-syntax-parse`) sets `LD_LIBRARY_PATH`/`DYLD_LIBRARY_PATH` to its
# own directory and execs the real binary (`swift-syntax-parse.real`); the Swift
# runtime shared libraries are packaged alongside them. `parse.rs` resolves the
# wrapper as a sibling of the extractor executable.
rust_binary(
name = "swift-syntax-parse",
name = "swift-syntax-parse.real",
srcs = ["src/main.rs"],
# `rust_binary` doesn't copy the Swift runtime into runfiles the way
# `swift_binary` does. On Linux, ship the standalone toolchain's runtime;
# on macOS the OS provides it at `/usr/lib/swift` (rpath'd by
# `xcode_swift_toolchain`).
# Target name carries `.real` (invalid in a crate name), so set it explicitly.
crate_name = "swift_syntax_parse",
# On Linux, carry the toolchain's runtime shared libraries as runfiles so
# they get packaged next to the binary. On macOS the OS provides the Swift
# runtime, so nothing extra is bundled.
data = select({
"@platforms//os:macos": [],
"@platforms//os:linux": [":swift_runtime_libs"],
@@ -64,6 +72,28 @@ rust_binary(
deps = [":swift_syntax_rs"],
)
# `swift-syntax-parse` wrapper (see `swift-syntax-parse.sh`). Its runfiles carry
# the real binary and the runtime libraries; packaging flattens them into one
# directory.
sh_binary(
name = "swift-syntax-parse",
srcs = ["swift-syntax-parse.sh"],
data = [":swift-syntax-parse.real"],
target_compatible_with = _SWIFT_SUPPORTED_PLATFORMS,
)
# Packaged form for the extractor pack: the wrapper (as `swift-syntax-parse`),
# the real binary, and the runtime libraries, flattened into one directory.
codeql_pkg_runfiles(
name = "swift-syntax-parse-pkg",
exes = [":swift-syntax-parse"],
# The `.sh` source is shipped as `swift-syntax-parse` (the wrapper); drop the
# original filename.
excludes = ["swift-syntax-parse.sh"],
target_compatible_with = _SWIFT_SUPPORTED_PLATFORMS,
visibility = ["//unified:__pkg__"],
)
rust_test(
name = "swift_syntax_rs_test",
size = "small",

View File

@@ -0,0 +1,12 @@
#!/bin/bash
# Wrapper that lets the shipped `swift-syntax-parse` find its Swift runtime
# libraries, which are packaged in the same directory as this script (and the
# real binary). Mirrors `swift/extractor/extractor.sh`.
if [[ "$(uname)" == Darwin ]]; then
export DYLD_LIBRARY_PATH=$(dirname "$0")
else
export LD_LIBRARY_PATH=$(dirname "$0")
fi
exec -a "$0" "$0.real" "$@"