From 830d4c539ff0cb7712d5a75f2a02a26fbf0e6adc Mon Sep 17 00:00:00 2001 From: Arthur Baars Date: Fri, 17 Jan 2025 10:38:58 +0100 Subject: [PATCH] Rust: extract sources of crates --- rust/tools/BUILD.bazel | 2 +- rust/tools/autobuild.py | 27 +++++++++++++++++++++++++++ rust/tools/autobuild.sh | 4 +++- 3 files changed, 31 insertions(+), 2 deletions(-) create mode 100644 rust/tools/autobuild.py diff --git a/rust/tools/BUILD.bazel b/rust/tools/BUILD.bazel index 92a97109edee..6b1c42d4a13b 100644 --- a/rust/tools/BUILD.bazel +++ b/rust/tools/BUILD.bazel @@ -2,7 +2,7 @@ load("//misc/bazel:pkg.bzl", "codeql_pkg_files") codeql_pkg_files( name = "tools", - srcs = glob(["*.cmd"]), + srcs = glob(["*.cmd", "autobuild.py"]), exes = glob(["*.sh"]), visibility = ["//rust:__pkg__"], ) diff --git a/rust/tools/autobuild.py b/rust/tools/autobuild.py new file mode 100644 index 000000000000..357f8350aabe --- /dev/null +++ b/rust/tools/autobuild.py @@ -0,0 +1,27 @@ + +import os +import json +import subprocess + +""" +run "cargo metadata --format-version=1" +""" + + +def get_cargo_metadata(): + metadata = json.loads(subprocess.check_output( + ["cargo", "metadata", "--format-version=1"])) + return metadata + + +CODEQL_EXTRACTOR_RUST_ROOT = os.environ.get("CODEQL_EXTRACTOR_RUST_ROOT") +CODEQL_PLATFORM = os.environ.get("CODEQL_PLATFORM") +metadata = get_cargo_metadata() +for package in metadata['packages']: + for target in package['targets']: + if 'lib' in target['kind']: + src_path = target['src_path'] + dir = os.path.dirname(src_path) + autobuild = "{root}/tools/{platform}/autobuild".format( + root=CODEQL_EXTRACTOR_RUST_ROOT, platform=CODEQL_PLATFORM) + subprocess.run([autobuild], cwd=dir) diff --git a/rust/tools/autobuild.sh b/rust/tools/autobuild.sh index 6d78ecf6d1f1..483f44c8ca67 100755 --- a/rust/tools/autobuild.sh +++ b/rust/tools/autobuild.sh @@ -3,4 +3,6 @@ set -eu export RUST_BACKTRACE=1 -exec "$CODEQL_EXTRACTOR_RUST_ROOT/tools/$CODEQL_PLATFORM/autobuild" +SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd ) + +exec /usr/bin/env python "${SCRIPT_DIR}/autobuild.py" \ No newline at end of file