From 15bb846a5fb9a00748219475e4a2d436181e5e1a Mon Sep 17 00:00:00 2001 From: Paolo Tranquilli Date: Mon, 29 Apr 2024 12:17:47 +0200 Subject: [PATCH] Go: add workaround for extractor pack windows installer --- go/BUILD.bazel | 23 +++++++++++++++++++++-- go/create_extractor_pack.py | 16 +++++++++++++--- 2 files changed, 34 insertions(+), 5 deletions(-) diff --git a/go/BUILD.bazel b/go/BUILD.bazel index 41113d45f518..4251ebd228f2 100644 --- a/go/BUILD.bazel +++ b/go/BUILD.bazel @@ -3,6 +3,7 @@ load("@gazelle//:def.bzl", "gazelle") load("@rules_go//go:def.bzl", "go_cross_binary") load("@rules_pkg//pkg:install.bzl", "pkg_install") load("@rules_pkg//pkg:mappings.bzl", "pkg_attributes", "pkg_filegroup", "pkg_files") +load("@rules_pkg//pkg:zip.bzl", "pkg_zip") load("//:defs.bzl", "codeql_platform") # following is needed for running gazelle on macOS @@ -92,15 +93,33 @@ pkg_filegroup( ) pkg_install( - name = "_create_extractor_pack", + name = "_extractor-pack-installer", srcs = [":extractor-pack"], ) +# rules_pkg installer is currently broken on Windows +# see https://github.com/bazelbuild/rules_pkg/issues/387 +# for now, work around it using an archive +pkg_zip( + name = "_extractor-pack-zip", + srcs = [":extractor-pack"], +) + +alias( + name = "_create-extractor-pack-arg", + actual = select({ + "@platforms//os:windows": ":_extractor-pack-zip", + "//conditions:default": ":_extractor-pack-installer", + }), +) + py_binary( name = "create-extractor-pack", srcs = ["create_extractor_pack.py"], + args = ["$(rlocationpath :_create-extractor-pack-arg)"], + data = [":_create-extractor-pack-arg"], main = "create_extractor_pack.py", - deps = [":_create_extractor_pack"], + deps = ["@rules_python//python/runfiles"], ) native_binary( diff --git a/go/create_extractor_pack.py b/go/create_extractor_pack.py index 08665a2d8dc9..a1154a777d85 100644 --- a/go/create_extractor_pack.py +++ b/go/create_extractor_pack.py @@ -2,7 +2,9 @@ import pathlib import shutil import sys -from go._create_extractor_pack_install_script import main +import subprocess +import zipfile +from python.runfiles import runfiles try: workspace_dir = pathlib.Path(os.environ['BUILD_WORKSPACE_DIRECTORY']) @@ -11,6 +13,14 @@ sys.exit(1) dest_dir = workspace_dir / 'go' / 'build' / 'codeql-extractor-go' +installer_or_zip = pathlib.Path(runfiles.Create().Rlocation(sys.argv[1])) + shutil.rmtree(dest_dir, ignore_errors=True) -os.environ['DESTDIR'] = str(dest_dir) -main(sys.argv) + +if installer_or_zip.suffix == '.zip': + dest_dir.mkdir() + with zipfile.ZipFile(installer_or_zip) as pack: + pack.extractall(dest_dir) +else: + os.environ['DESTDIR'] = str(dest_dir) + subprocess.check_call([installer_or_zip])