Skip to content

Commit

Permalink
fix(rust_extractor): fix running the Rust extractor on macOS (#5013)
Browse files Browse the repository at this point in the history
  • Loading branch information
wcalandro committed Aug 2, 2021
1 parent 920bdec commit 5db173c
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 17 deletions.
3 changes: 1 addition & 2 deletions kythe/rust/extractor/BUILD
Expand Up @@ -34,10 +34,9 @@ rust_binary(

sh_binary(
name = "extractor_script",
srcs = [":extractor_script.sh"],
srcs = ["extractor_script.sh"],
data = [
":extractor",
"@bazel_tools//tools/bash/runfiles",
] + select({
"@platforms//os:linux": ["@rust_linux_x86_64//:rustc_lib"],
"@platforms//os:osx": ["@rust_darwin_x86_64//:rustc_lib"],
Expand Down
18 changes: 6 additions & 12 deletions kythe/rust/extractor/extractor_script.sh
Expand Up @@ -13,24 +13,18 @@
# See the License for the specific language governing permissions and
# limitations under the License.

# --- begin runfiles.bash initialization ---
set -uo pipefail; 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 ---
RUNFILES_DIR="$0.runfiles"

# We have to call find ourselves because rlocation doesn't support wildcards
PATTERN="$RUNFILES_DIR/rust_*_x86_64/lib/rustlib*lib/librustc_driver-*.so"
PATTERN="$RUNFILES_DIR/rust_*_x86_64/lib/rustlib*lib/librustc_driver-*.*"
# Find the Rust directory first so that our next find doesn't search the
# entire runfiles directory
RUST_RUNFILES_LOCATION="$(find "$RUNFILES_DIR" -type d -wholename "$RUNFILES_DIR/rust_*_x86_64")"
LOCATION="$(find "$RUST_RUNFILES_LOCATION" -wholename "$PATTERN")"
LIB_DIRNAME="$(dirname "$LOCATION")"

# Set the dylib search paths for Linux and macOS, respectively
export LD_LIBRARY_PATH="${LD_LIBRARY_PATH:+$LD_LIBRARY_PATH:}$LIB_DIRNAME"
exec "$(rlocation io_kythe/kythe/rust/extractor/extractor)" "$@"
export DYLD_FALLBACK_LIBRARY_PATH="${DYLD_FALLBACK_LIBRARY_PATH:+$DYLD_FALLBACK_LIBRARY_PATH:}$LIB_DIRNAME"

exec "$RUNFILES_DIR/io_kythe/kythe/rust/extractor/extractor" "$@"
3 changes: 2 additions & 1 deletion kythe/rust/extractor/extractor_test.bzl
Expand Up @@ -47,7 +47,8 @@ def _rust_extractor_test_impl(ctx):
rustc_lib_path = paths.dirname(rustc_lib[0].short_path)
rust_lib_path = paths.dirname(rust_lib[0].short_path)
script = "\n".join(
["export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:%s:%s" % (rustc_lib_path, rust_lib_path)] +
["export DYLD_FALLBACK_LIBRARY_PATH=${DYLD_FALLBACK_LIBRARY_PATH:+$DYLD_FALLBACK_LIBRARY_PATH:}%s:%s" % (rustc_lib_path, rust_lib_path)] +
["export LD_LIBRARY_PATH=${LD_LIBRARY_PATH:+$LD_LIBRARY_PATH:}%s:%s" % (rustc_lib_path, rust_lib_path)] +
["export SYSROOT=%s" % rust_lib_path] +
["export TEST_FILE=%s" % source_file.short_path] +
["export EXTRACTOR_PATH=%s" % extractor.short_path] +
Expand Down
3 changes: 1 addition & 2 deletions tools/build_rules/verifier_test/rust_indexer_test.bzl
Expand Up @@ -66,7 +66,6 @@ def _rust_extract_impl(ctx):
outputs = [output],
env = {
"KYTHE_CORPUS": "test_corpus",
"LD_LIBRARY_PATH": paths.dirname(rustc_lib[0].path),
},
)

Expand All @@ -90,7 +89,7 @@ rust_extract = rule(
default = "test_crate",
),
"_extractor": attr.label(
default = Label("//kythe/rust/extractor"),
default = Label("//kythe/rust/extractor:extractor_script"),
executable = True,
cfg = "exec",
),
Expand Down

0 comments on commit 5db173c

Please sign in to comment.