Repository: kythe/kythe
Pull request #4998 (2021-07-29 22:15:16)

feat(rust_extractor): add script to automatically set LD_LIBRARY_PATH


Patch:
diff --git a/kythe/extractors/BUILD b/kythe/extractors/BUILD
index fb078ef74..68cb74dd4 100644
--- a/kythe/extractors/BUILD
+++ b/kythe/extractors/BUILD
@@ -197,11 +197,10 @@ extractor_action(
     ],
     data = ["//external:vnames_config"],
     env = {
-        "LD_LIBRARY_PATH": "external/rust_linux_x86_64/lib:external/rust_darwin_x86_64/lib",
         # TODO(wcalandro): Remove this once the extractor has support for VName configuration
         "KYTHE_CORPUS": "kythe.io",
     },
-    extractor = "@io_kythe//kythe/rust/extractor",
+    extractor = "@io_kythe//kythe/rust/extractor:extractor_script",
     mnemonics = [
         "Rustc",
     ],
diff --git a/kythe/rust/extractor/BUILD b/kythe/rust/extractor/BUILD
index 885ab1c9a..8ec9a20aa 100644
--- a/kythe/rust/extractor/BUILD
+++ b/kythe/rust/extractor/BUILD
@@ -32,6 +32,18 @@ rust_binary(
     ],
 )
 
+sh_binary(
+    name = "extractor_script",
+    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"],
+    }),
+)
+
 rust_clippy(
     name = "clippy",
     deps = [
diff --git a/kythe/rust/extractor/extractor_script.sh b/kythe/rust/extractor/extractor_script.sh
new file mode 100755
index 000000000..2f0fe762f
--- /dev/null
+++ b/kythe/rust/extractor/extractor_script.sh
@@ -0,0 +1,36 @@
+#!/bin/bash
+# Copyright 2021 The Kythe Authors. All rights reserved.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# 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 ---
+
+# We have to call find ourselves because rlocation doesn't support wildcards
+PATTERN="$RUNFILES_DIR/rust_*_x86_64/lib/rustlib*lib/librustc_driver-*.so"
+# 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")"
+
+export LD_LIBRARY_PATH="${LD_LIBRARY_PATH:+$LD_LIBRARY_PATH:}$LIB_DIRNAME"
+exec "$(rlocation io_kythe/kythe/rust/extractor/extractor)" "$@"


Test patch:
diff --git a/kythe/rust/extractor/extractor_test.bzl b/kythe/rust/extractor/extractor_test.bzl
index e7a8e5d87..02bfe83f9 100644
--- a/kythe/rust/extractor/extractor_test.bzl
+++ b/kythe/rust/extractor/extractor_test.bzl
@@ -37,7 +37,7 @@ def _rust_extractor_test_impl(ctx):
     )
 
     script = "\n".join(
-        ["export LD_LIBRARY_PATH=%s" % paths.dirname(rustc_lib[0].short_path)] +
+        ["export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:%s" % paths.dirname(rustc_lib[0].short_path)] +
         ["export SYSROOT=%s" % paths.dirname(rust_lib[0].short_path)] +
         ["export TEST_FILE=%s" % source_file.short_path] +
         ["export EXTRACTOR_PATH=%s" % extractor.short_path] +