From c2be267febcf6fea68645574cee7155f125f9211 Mon Sep 17 00:00:00 2001 From: Paolo Tranquilli Date: Tue, 3 May 2022 09:54:15 +0200 Subject: [PATCH] Swift: enable dynamic mode Providing `--dynamic_mode=fully` (for example setting it in `local.bazelrc`) will now work. All runfiles are now copied in the extractor pack: in dynamic mode, those will be the executable and the dynamic libraries, while in static mode only the executable will be part of the runfiles. Setting the correct `LD_LIBRARY_PATH` in `qltest.sh` then allows to run tests with this pakcage. If we need something more, we can switch to a wrapper script in place of `extractor` in the future. Notice that `LD_LIBRARY_PATH` is also set in static mode, but that has no consequence. --- .gitignore | 3 ++ misc/bazel/pkg_runfiles.bzl | 33 +++++++++++++++++++ misc/bazel/workspace.bzl | 3 +- swift/BUILD.bazel | 14 ++------ swift/extractor/BUILD.bazel | 10 +----- swift/tools/prebuilt/BUILD.bazel | 11 +++++++ .../prebuilt}/BUILD.swift-prebuilt.bazel | 0 swift/tools/qltest.sh | 2 ++ 8 files changed, 54 insertions(+), 22 deletions(-) create mode 100644 misc/bazel/pkg_runfiles.bzl create mode 100644 swift/tools/prebuilt/BUILD.bazel rename swift/{extractor => tools/prebuilt}/BUILD.swift-prebuilt.bazel (100%) diff --git a/.gitignore b/.gitignore index 5642399a5f6..9dd2effe951 100644 --- a/.gitignore +++ b/.gitignore @@ -37,5 +37,8 @@ csharp/extractor/Semmle.Extraction.CSharp.Driver/Properties/launchSettings.json # links created by bazel /bazel-* +# local bazel options +/local.bazelrc + # CLion project files /.clwb diff --git a/misc/bazel/pkg_runfiles.bzl b/misc/bazel/pkg_runfiles.bzl new file mode 100644 index 00000000000..a8f215e8a55 --- /dev/null +++ b/misc/bazel/pkg_runfiles.bzl @@ -0,0 +1,33 @@ +load("@rules_pkg//:mappings.bzl", "pkg_attributes", "pkg_files") + +def _runfiles_group_impl(ctx): + files = [] + for src in ctx.attr.srcs: + rf = src[DefaultInfo].default_runfiles + if rf != None: + files.append(rf.files) + return [ + DefaultInfo( + files = depset(transitive = files), + ), + ] + +_runfiles_group = rule( + implementation = _runfiles_group_impl, + attrs = { + "srcs": attr.label_list(), + }, +) + +def pkg_runfiles(*, name, srcs, **kwargs): + internal_name = "_%s_runfiles" % name + _runfiles_group( + name = internal_name, + srcs = srcs, + ) + kwargs.setdefault("attributes", pkg_attributes(mode = "0755")) + pkg_files( + name = name, + srcs = [internal_name], + **kwargs + ) diff --git a/misc/bazel/workspace.bzl b/misc/bazel/workspace.bzl index c7ac65d3d2a..7b89bff5693 100644 --- a/misc/bazel/workspace.bzl +++ b/misc/bazel/workspace.bzl @@ -22,7 +22,7 @@ def codeql_workspace(repository_name = "codeql"): _swift_prebuilt_version, repo_arch, ), - build_file = "@%s//swift/extractor:BUILD.swift-prebuilt.bazel" % repository_name, + build_file = "@%s//swift/tools/prebuilt:BUILD.swift-prebuilt.bazel" % repository_name, sha256 = sha256, ) @@ -55,4 +55,3 @@ def codeql_workspace(repository_name = "codeql"): "https://github.com/bazelbuild/rules_python/archive/refs/tags/0.8.1.tar.gz", ], ) - diff --git a/swift/BUILD.bazel b/swift/BUILD.bazel index 91779d830d8..ef95c3c006f 100644 --- a/swift/BUILD.bazel +++ b/swift/BUILD.bazel @@ -1,6 +1,7 @@ load("@rules_pkg//:mappings.bzl", "pkg_attributes", "pkg_filegroup", "pkg_files", "strip_prefix") load("@rules_pkg//:install.bzl", "pkg_install") load("//:defs.bzl", "codeql_platform") +load("//misc/bazel:pkg_runfiles.bzl", "pkg_runfiles") filegroup( name = "dbscheme", @@ -45,24 +46,15 @@ pkg_filegroup( visibility = ["//visibility:public"], ) -pkg_files( +pkg_runfiles( name = "extractor", srcs = ["//swift/extractor"], - attributes = pkg_attributes(mode = "0755"), prefix = "tools/" + codeql_platform, ) -alias( - name = "swift-test-sdk", - actual = select({ - "@bazel_tools//src/conditions:%s" % arch: "@swift_prebuilt_%s//:swift-test-sdk" % arch - for arch in ("linux", "darwin_x86_64", "darwin_arm64") - }), -) - pkg_files( name = "swift-test-sdk-arch", - srcs = [":swift-test-sdk"], + srcs = ["//swift/tools/prebuilt:swift-test-sdk"], prefix = "qltest/" + codeql_platform, strip_prefix = strip_prefix.from_pkg(), ) diff --git a/swift/extractor/BUILD.bazel b/swift/extractor/BUILD.bazel index 6ae8be83a59..011183d8dac 100644 --- a/swift/extractor/BUILD.bazel +++ b/swift/extractor/BUILD.bazel @@ -1,13 +1,5 @@ load("//swift:rules.bzl", "swift_cc_binary") -alias( - name = "swift-llvm-support", - actual = select({ - "@bazel_tools//src/conditions:%s" % arch: "@swift_prebuilt_%s//:swift-llvm-support" % arch - for arch in ("linux", "darwin_x86_64", "darwin_arm64") - }), -) - swift_cc_binary( name = "extractor", srcs = [ @@ -18,7 +10,7 @@ swift_cc_binary( ], visibility = ["//swift:__pkg__"], deps = [ - ":swift-llvm-support", + "//swift/tools/prebuilt:swift-llvm-support", "//swift/extractor/trap", ], ) diff --git a/swift/tools/prebuilt/BUILD.bazel b/swift/tools/prebuilt/BUILD.bazel new file mode 100644 index 00000000000..1abd461f9e8 --- /dev/null +++ b/swift/tools/prebuilt/BUILD.bazel @@ -0,0 +1,11 @@ +package(default_visibility = ["//swift:__subpackages__"]) + +[ + alias( + name = name, + actual = select({ + "@bazel_tools//src/conditions:%s" % arch: "@swift_prebuilt_%s//:%s" % (arch, name) + for arch in ("linux", "darwin_x86_64", "darwin_arm64") + }), + ) for name in ("swift-llvm-support", "swift-test-sdk") +] diff --git a/swift/extractor/BUILD.swift-prebuilt.bazel b/swift/tools/prebuilt/BUILD.swift-prebuilt.bazel similarity index 100% rename from swift/extractor/BUILD.swift-prebuilt.bazel rename to swift/tools/prebuilt/BUILD.swift-prebuilt.bazel diff --git a/swift/tools/qltest.sh b/swift/tools/qltest.sh index 22043cf940e..9da3a32b6d7 100755 --- a/swift/tools/qltest.sh +++ b/swift/tools/qltest.sh @@ -4,4 +4,6 @@ mkdir -p "$CODEQL_EXTRACTOR_SWIFT_TRAP_DIR" QLTEST_LOG="$CODEQL_EXTRACTOR_SWIFT_LOG_DIR"/qltest.log +export LD_LIBRARY_PATH="$CODEQL_EXTRACTOR_SWIFT_ROOT/tools/$CODEQL_PLATFORM" + exec "$CODEQL_EXTRACTOR_SWIFT_ROOT/tools/$CODEQL_PLATFORM/extractor" -sdk "$CODEQL_EXTRACTOR_SWIFT_ROOT/qltest/$CODEQL_PLATFORM/sdk" -c *.swift >> $QLTEST_LOG 2>&1