From 6e33f979c7189e0e4fcb3c4d4f830a22e617147e Mon Sep 17 00:00:00 2001 From: Paolo Tranquilli Date: Tue, 19 Nov 2024 10:25:31 +0100 Subject: [PATCH 1/4] Bazel: add an `installer` shortcut to `codeql_pack` This makes the first `codeql_pack` in a package add an `installer` target aliasing the `-installer` one. This makes it so that one can for example do `bazel run //rust:installer` instead of the stuttering `bazel run //rust:rust-installer`. If a bazel package defines multiple `codeql_pack` targets, the first one only will get the `installer` alias. --- actions/BUILD.bazel | 2 +- misc/bazel/pkg.bzl | 7 +++++-- rust/BUILD.bazel | 2 +- 3 files changed, 7 insertions(+), 4 deletions(-) diff --git a/actions/BUILD.bazel b/actions/BUILD.bazel index 643d40897185..72313410642f 100644 --- a/actions/BUILD.bazel +++ b/actions/BUILD.bazel @@ -11,10 +11,10 @@ package(default_visibility = ["//visibility:public"]) pack_prefix = "/".join(parts), ) for parts in ( + ["actions"], [ "experimental", "actions", ], - ["actions"], ) ] diff --git a/misc/bazel/pkg.bzl b/misc/bazel/pkg.bzl index 62c2ee199459..104393605415 100644 --- a/misc/bazel/pkg.bzl +++ b/misc/bazel/pkg.bzl @@ -448,9 +448,10 @@ def codeql_pack( contain the `{CODEQL_PLATFORM}` marker. All files in the pack will be prefixed with `name`, unless `pack_prefix` is set, then is used instead. - This rule also provides a convenient installer target, with a path governed by `install_dest`. + This rule also provides a convenient installer target named `-installer`, with a path governed by `install_dest`. This installer is used for installing this pack into the source-tree, relative to the directory where the rule is used. - See `codeql_pack_install` for more details. + See `codeql_pack_install` for more details. The first `codeql_pack` defined in a bazel package also aliases this + installer target with the `installer` name as a shortcut. This function does not accept `visibility`, as packs are always public to make it easy to define pack groups. """ @@ -474,6 +475,8 @@ def codeql_pack( visibility = ["//visibility:public"], ) _codeql_pack_install(internal("installer"), [name], install_dest = install_dest, apply_pack_prefix = False) + if not native.existing_rule("installer"): + native.alias(name = "installer", actual = internal("installer")) strip_prefix = _strip_prefix diff --git a/rust/BUILD.bazel b/rust/BUILD.bazel index 848a5dbb78a6..4c0bb1721f1e 100644 --- a/rust/BUILD.bazel +++ b/rust/BUILD.bazel @@ -56,10 +56,10 @@ codeql_pkg_files( pack_prefix = "/".join(parts), ) for parts in ( + ["rust"], [ "experimental", "rust", ], - ["rust"], ) ] From 40b8406d62bbb2b0876dd2068b17ba4204ecf075 Mon Sep 17 00:00:00 2001 From: Paolo Tranquilli Date: Tue, 19 Nov 2024 10:32:21 +0100 Subject: [PATCH 2/4] Bazel: use installer shortcut in docs and workflows --- .github/workflows/cpp-swift-analysis.yml | 2 +- rust/README.md | 2 +- swift/README.md | 2 +- swift/actions/build-and-test/action.yml | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/.github/workflows/cpp-swift-analysis.yml b/.github/workflows/cpp-swift-analysis.yml index 4b923f1b39c6..ef38cb893933 100644 --- a/.github/workflows/cpp-swift-analysis.yml +++ b/.github/workflows/cpp-swift-analysis.yml @@ -48,7 +48,7 @@ jobs: - name: "Build Swift extractor using Bazel" run: | bazel clean --expunge - bazel run //swift:create-extractor-pack --nouse_action_cache --noremote_accept_cached --noremote_upload_local_results --spawn_strategy=local + bazel run //swift:installer --nouse_action_cache --noremote_accept_cached --noremote_upload_local_results --spawn_strategy=local bazel shutdown - name: Perform CodeQL Analysis diff --git a/rust/README.md b/rust/README.md index 87c7072c6758..4be6372f266b 100644 --- a/rust/README.md +++ b/rust/README.md @@ -13,7 +13,7 @@ If you don't have the `semmle-code` repo you may need to install Bazel manually, This approach uses a released `codeql` version and is simpler to use for QL development. From your `semmle-code` directory run: ```bash -bazel run @codeql//rust:rust-installer +bazel run @codeql//rust:installer ``` You now need to create a [per-user CodeQL configuration file](https://docs.github.com/en/code-security/codeql-cli/using-the-advanced-functionality-of-the-codeql-cli/specifying-command-options-in-a-codeql-configuration-file#using-a-codeql-configuration-file) and specify the option: ``` diff --git a/swift/README.md b/swift/README.md index a39c0ea55788..79970d90d964 100644 --- a/swift/README.md +++ b/swift/README.md @@ -13,7 +13,7 @@ brew install bazelisk then from the `ql` directory run ```bash -bazel run //swift:create-extractor-pack +bazel run //swift:installer ``` If you are running on macOS and you encounter errors mentioning `XXX is unavailable: introduced in macOS YY.ZZ`, diff --git a/swift/actions/build-and-test/action.yml b/swift/actions/build-and-test/action.yml index 2522f545c05f..16a8e02a7a1c 100644 --- a/swift/actions/build-and-test/action.yml +++ b/swift/actions/build-and-test/action.yml @@ -48,7 +48,7 @@ runs: - name: Build Swift extractor shell: bash run: | - bazel run //swift:create-extractor-pack + bazel run //swift:installer - name: Run codegen tests if : ${{ github.event_name == 'pull_request' }} shell: bash From e8677b46d531f839bdb0339fbfc1ef0a92af13f6 Mon Sep 17 00:00:00 2001 From: Paolo Tranquilli Date: Tue, 19 Nov 2024 10:33:53 +0100 Subject: [PATCH 3/4] Bazel: rename shortcut to `install` --- .github/workflows/cpp-swift-analysis.yml | 2 +- misc/bazel/pkg.bzl | 6 +++--- rust/README.md | 2 +- swift/README.md | 2 +- swift/actions/build-and-test/action.yml | 2 +- 5 files changed, 7 insertions(+), 7 deletions(-) diff --git a/.github/workflows/cpp-swift-analysis.yml b/.github/workflows/cpp-swift-analysis.yml index ef38cb893933..3af9c3c5e4ea 100644 --- a/.github/workflows/cpp-swift-analysis.yml +++ b/.github/workflows/cpp-swift-analysis.yml @@ -48,7 +48,7 @@ jobs: - name: "Build Swift extractor using Bazel" run: | bazel clean --expunge - bazel run //swift:installer --nouse_action_cache --noremote_accept_cached --noremote_upload_local_results --spawn_strategy=local + bazel run //swift:install --nouse_action_cache --noremote_accept_cached --noremote_upload_local_results --spawn_strategy=local bazel shutdown - name: Perform CodeQL Analysis diff --git a/misc/bazel/pkg.bzl b/misc/bazel/pkg.bzl index 104393605415..8b4fcbdbd862 100644 --- a/misc/bazel/pkg.bzl +++ b/misc/bazel/pkg.bzl @@ -451,7 +451,7 @@ def codeql_pack( This rule also provides a convenient installer target named `-installer`, with a path governed by `install_dest`. This installer is used for installing this pack into the source-tree, relative to the directory where the rule is used. See `codeql_pack_install` for more details. The first `codeql_pack` defined in a bazel package also aliases this - installer target with the `installer` name as a shortcut. + installer target with the `install` name as a shortcut. This function does not accept `visibility`, as packs are always public to make it easy to define pack groups. """ @@ -475,8 +475,8 @@ def codeql_pack( visibility = ["//visibility:public"], ) _codeql_pack_install(internal("installer"), [name], install_dest = install_dest, apply_pack_prefix = False) - if not native.existing_rule("installer"): - native.alias(name = "installer", actual = internal("installer")) + if not native.existing_rule("install"): + native.alias(name = "install", actual = internal("installer")) strip_prefix = _strip_prefix diff --git a/rust/README.md b/rust/README.md index 4be6372f266b..2f9a7503abb7 100644 --- a/rust/README.md +++ b/rust/README.md @@ -13,7 +13,7 @@ If you don't have the `semmle-code` repo you may need to install Bazel manually, This approach uses a released `codeql` version and is simpler to use for QL development. From your `semmle-code` directory run: ```bash -bazel run @codeql//rust:installer +bazel run @codeql//rust:install ``` You now need to create a [per-user CodeQL configuration file](https://docs.github.com/en/code-security/codeql-cli/using-the-advanced-functionality-of-the-codeql-cli/specifying-command-options-in-a-codeql-configuration-file#using-a-codeql-configuration-file) and specify the option: ``` diff --git a/swift/README.md b/swift/README.md index 79970d90d964..b5be374b5e70 100644 --- a/swift/README.md +++ b/swift/README.md @@ -13,7 +13,7 @@ brew install bazelisk then from the `ql` directory run ```bash -bazel run //swift:installer +bazel run //swift:install ``` If you are running on macOS and you encounter errors mentioning `XXX is unavailable: introduced in macOS YY.ZZ`, diff --git a/swift/actions/build-and-test/action.yml b/swift/actions/build-and-test/action.yml index 16a8e02a7a1c..4048560640b2 100644 --- a/swift/actions/build-and-test/action.yml +++ b/swift/actions/build-and-test/action.yml @@ -48,7 +48,7 @@ runs: - name: Build Swift extractor shell: bash run: | - bazel run //swift:installer + bazel run //swift:install - name: Run codegen tests if : ${{ github.event_name == 'pull_request' }} shell: bash From 918b0bfc53805ba403105857a6e37c352880a0b3 Mon Sep 17 00:00:00 2001 From: Paolo Tranquilli Date: Tue, 19 Nov 2024 12:48:36 +0100 Subject: [PATCH 4/4] Bazel: add `experimental` to `codeql_pack` --- actions/BUILD.bazel | 21 ++++---------- misc/bazel/pkg.bzl | 67 ++++++++++++++++++++++++++++++++++++++------- rust/BUILD.bazel | 25 ++++++----------- 3 files changed, 70 insertions(+), 43 deletions(-) diff --git a/actions/BUILD.bazel b/actions/BUILD.bazel index 72313410642f..b1af486543c6 100644 --- a/actions/BUILD.bazel +++ b/actions/BUILD.bazel @@ -2,19 +2,8 @@ load("//misc/bazel:pkg.bzl", "codeql_pack") package(default_visibility = ["//visibility:public"]) -[ - codeql_pack( - name = "-".join(parts), - srcs = [ - "//actions/extractor", - ], - pack_prefix = "/".join(parts), - ) - for parts in ( - ["actions"], - [ - "experimental", - "actions", - ], - ) -] +codeql_pack( + name = "actions", + srcs = ["//actions/extractor"], + experimental = True, +) diff --git a/misc/bazel/pkg.bzl b/misc/bazel/pkg.bzl index 8b4fcbdbd862..d455e99c7493 100644 --- a/misc/bazel/pkg.bzl +++ b/misc/bazel/pkg.bzl @@ -429,6 +429,8 @@ def codeql_pack( arch_overrides = None, pack_prefix = None, install_dest = "extractor-pack", + installer_alias = "install", + experimental = False, **kwargs): """ Define a codeql pack. @@ -448,22 +450,64 @@ def codeql_pack( contain the `{CODEQL_PLATFORM}` marker. All files in the pack will be prefixed with `name`, unless `pack_prefix` is set, then is used instead. - This rule also provides a convenient installer target named `-installer`, with a path governed by `install_dest`. - This installer is used for installing this pack into the source-tree, relative to the directory where the rule is used. - See `codeql_pack_install` for more details. The first `codeql_pack` defined in a bazel package also aliases this - installer target with the `install` name as a shortcut. + This rule also provides a convenient installer target named `-installer`, with a path governed by `install_dest`, + unless `install_dest == None`. This installer is used for installing this pack into the source-tree, relative to the + directory where the rule is used. See `codeql_pack_install` for more details. If present, `installer_alias` is used + to define a shorthand alias for `-installer`. Be sure to change `installer_alias` or set it to `None` if a + bazel package defines multiple `codeql_pack`s. + + If `experimental = True`, a second `codeql_pack` named `-experimental` is defined alongside the primary one with + an `experimental` pack prefix and no installer, intended to be used when packaging the full distribution. This function does not accept `visibility`, as packs are always public to make it easy to define pack groups. """ - internal = _make_internal(name) - zips = zips or {} if pack_prefix == None: pack_prefix = name + _codeql_pack_impl( + name, + srcs, + zips, + arch_overrides, + pack_prefix, + install_dest, + installer_alias, + pkg_filegroup_kwargs = kwargs, + ) + if experimental: + _codeql_pack_impl( + "%s-experimental" % name, + srcs, + zips, + arch_overrides, + pack_prefix = "experimental/%s" % pack_prefix, + install_dest = None, + installer_alias = None, + pkg_filegroup_kwargs = kwargs, + ) + + # TODO: remove this after internal repo update + native.alias( + name = "experimental-%s" % name, + actual = "%s-experimental" % name, + visibility = ["//visibility:public"], + ) + +def _codeql_pack_impl( + name, + srcs, + zips, + arch_overrides, + pack_prefix, + install_dest, + installer_alias, + pkg_filegroup_kwargs): + internal = _make_internal(name) + zips = zips or {} pkg_filegroup( name = internal("all"), srcs = srcs, visibility = ["//visibility:private"], - **kwargs + **pkg_filegroup_kwargs ) _codeql_pack_info( name = name, @@ -474,9 +518,12 @@ def codeql_pack( # packs are always public, so that we can easily bundle them into groups visibility = ["//visibility:public"], ) - _codeql_pack_install(internal("installer"), [name], install_dest = install_dest, apply_pack_prefix = False) - if not native.existing_rule("install"): - native.alias(name = "install", actual = internal("installer")) + if install_dest: + _codeql_pack_install(internal("installer"), [name], install_dest = install_dest, apply_pack_prefix = False) + + # TODO: remove deprecated `native.existing_rule(installer_alias)` after internal repo update + if installer_alias and not native.existing_rule(installer_alias): + native.alias(name = installer_alias, actual = internal("installer")) strip_prefix = _strip_prefix diff --git a/rust/BUILD.bazel b/rust/BUILD.bazel index 4c0bb1721f1e..aee5d77aa643 100644 --- a/rust/BUILD.bazel +++ b/rust/BUILD.bazel @@ -46,20 +46,11 @@ codeql_pkg_files( ], ) -[ - codeql_pack( - name = "-".join(parts), - srcs = [ - ":root-files", - ":tools", - ], - pack_prefix = "/".join(parts), - ) - for parts in ( - ["rust"], - [ - "experimental", - "rust", - ], - ) -] +codeql_pack( + name = "rust", + srcs = [ + ":root-files", + ":tools", + ], + experimental = True, +)