From 94280fae2141fa974f6ebca58673e7a646ddcf02 Mon Sep 17 00:00:00 2001 From: Matt Clarkson Date: Wed, 20 Nov 2024 15:17:05 +0000 Subject: [PATCH 1/5] docs(toolchain_local_select): correct example code --- toolchain/local/select/repository.bzl | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/toolchain/local/select/repository.bzl b/toolchain/local/select/repository.bzl index 57258bb..c26005a 100644 --- a/toolchain/local/select/repository.bzl +++ b/toolchain/local/select/repository.bzl @@ -10,14 +10,15 @@ ATTRS = { "map": attr.string_dict( doc = """Local triplet to repository mappings: -``` -toolchian_local_select( +```py +toolchain_local_select( name = "abc", map = { "arm64-linux-gnu": "@abc-arm64-linux-gnu", "arm64-linux-musl": "@abc-arm64-linux-musl", }, ) +``` """, mandatory = True, allow_empty = False, -- GitLab From b9248c83799121f423abf64b9cb59ccd9d043650 Mon Sep 17 00:00:00 2001 From: Matt Clarkson Date: Wed, 20 Nov 2024 15:20:54 +0000 Subject: [PATCH 2/5] refactor: make `rule`s have the correct query `kind`s A rule is assigned the `kind` from the variable name. This change makes sure that `rule` declarations have the correct `kind`. Documentation strings are moved to global private variables as well. --- toolchain/symlink/path/rule.bzl | 4 +++- toolchain/symlink/target/rule.bzl | 4 +++- toolchain/test/rule.bzl | 36 ++++++++++++++++--------------- toolchain/triplet/rule.bzl | 26 ++++++++++++---------- 4 files changed, 40 insertions(+), 30 deletions(-) diff --git a/toolchain/symlink/path/rule.bzl b/toolchain/symlink/path/rule.bzl index 756de99..d88297a 100644 --- a/toolchain/symlink/path/rule.bzl +++ b/toolchain/symlink/path/rule.bzl @@ -97,10 +97,12 @@ def implementation(ctx): runfiles = runfiles, ) -path = rule( +toolchain_symlink_path = rule( doc = DOC, attrs = ATTRS, implementation = implementation, provides = [DefaultInfo], executable = True, ) + +path = toolchain_symlink_path diff --git a/toolchain/symlink/target/rule.bzl b/toolchain/symlink/target/rule.bzl index ba0d005..9c136ff 100644 --- a/toolchain/symlink/target/rule.bzl +++ b/toolchain/symlink/target/rule.bzl @@ -61,9 +61,11 @@ def implementation(ctx): runfiles = runfiles, ) -target = rule( +toolchain_symlink_target = rule( doc = DOC, attrs = ATTRS, implementation = implementation, executable = True, ) + +target = toolchain_symlink_target diff --git a/toolchain/test/rule.bzl b/toolchain/test/rule.bzl index c8d6cee..e5df9f2 100644 --- a/toolchain/test/rule.bzl +++ b/toolchain/test/rule.bzl @@ -1,5 +1,23 @@ visibility("//toolchain/...") +DOC = """Performs a simple test that a toolchain resolved to an executable. + +- Resolves the provided toolchain binary +- Executes with the provided arguments +- Captures `stdout` and `stderr` +- Can do optional `diff` checking of the output + +A common use case is to check that a toolchain can output some help text: + +``` +toolchain_test( + name = "test", + args = ["--help"], + toolchains = [":resolved"], +) +``` +""" + ATTRS = { "stdout": attr.label( doc = """The expected standard output. @@ -73,23 +91,7 @@ def implementation(ctx): ) toolchain_test = rule( - doc = """Performs a simple test that a toolchain resolved to an executable. - -- Resolves the provided toolchain binary -- Executes with the provided arguments -- Captures `stdout` and `stderr` -- Can do optional `diff` checking of the output - -A common use case is to check that a toolchain can output some help text: - -``` -toolchain_test( - name = "test", - args = ["--help"], - toolchains = [":resolved"], -) -``` -""", + doc = DOC, attrs = ATTRS, implementation = implementation, test = True, diff --git a/toolchain/triplet/rule.bzl b/toolchain/triplet/rule.bzl index e1cfa44..435ee67 100644 --- a/toolchain/triplet/rule.bzl +++ b/toolchain/triplet/rule.bzl @@ -2,6 +2,17 @@ load(":TripletInfo.bzl", "TripletInfo") visibility("//toolchain/...") +DOC = """Provides a machine triplet. + +A simple rule that provides a `ToolchainTripletInfo` provider. + +The resulting provider can be used in other rules to understand triplet values. + +Running the target with `bazel run` will result in the triplet being printed. + +The triplet runnable output is particularly useful for the resolved host triplet at `@toolchain_utils//toolchain/triplet:host` +""" + ATTRS = { "value": attr.string( doc = "A triplet value that overrides `name`.", @@ -44,17 +55,8 @@ def implementation(ctx): return [triplet, default] -triplet = rule( - doc = """Provides a machine triplet. - -A simple rule that provides a `ToolchainTripletInfo` provider. - -The resulting provider can be used in other rules to understand triplet values. - -Running the target with `bazel run` will result in the triplet being printed. - -The triplet runnable output is particularly useful for the resolved host triplet at `@toolchain_utils//toolchain/triplet:host` -""", +toolchain_triplet = rule( + doc = DOC, attrs = ATTRS, implementation = implementation, provides = [ @@ -63,3 +65,5 @@ The triplet runnable output is particularly useful for the resolved host triplet ], executable = True, ) + +triplet = toolchain_triplet -- GitLab From 793273a239a088d8db0bcd961149dc047b07f191 Mon Sep 17 00:00:00 2001 From: Matt Clarkson Date: Wed, 20 Nov 2024 16:58:48 +0000 Subject: [PATCH 3/5] chore: upgrade to Bazel `7.4.0` --- .bazelversion | 2 +- MODULE.bazel.lock | 20 ++++++++++---------- e2e/.bazelversion | 2 +- e2e/MODULE.bazel.lock | 20 ++++++++++---------- 4 files changed, 22 insertions(+), 22 deletions(-) diff --git a/.bazelversion b/.bazelversion index 0ee843c..ba7f754 100644 --- a/.bazelversion +++ b/.bazelversion @@ -1 +1 @@ -7.2.0 +7.4.0 diff --git a/MODULE.bazel.lock b/MODULE.bazel.lock index 7edadda..5dfcd16 100644 --- a/MODULE.bazel.lock +++ b/MODULE.bazel.lock @@ -36,8 +36,8 @@ "https://bcr.bazel.build/modules/rules_cc/0.0.9/MODULE.bazel": "836e76439f354b89afe6a911a7adf59a6b2518fafb174483ad78a2a2fde7b1c5", "https://bcr.bazel.build/modules/rules_cc/0.0.9/source.json": "1f1ba6fea244b616de4a554a0f4983c91a9301640c8fe0dd1d410254115c8430", "https://bcr.bazel.build/modules/rules_java/4.0.0/MODULE.bazel": "5a78a7ae82cd1a33cef56dc578c7d2a46ed0dca12643ee45edbb8417899e6f74", - "https://bcr.bazel.build/modules/rules_java/7.6.1/MODULE.bazel": "2f14b7e8a1aa2f67ae92bc69d1ec0fa8d9f827c4e17ff5e5f02e91caa3b2d0fe", - "https://bcr.bazel.build/modules/rules_java/7.6.1/source.json": "8f3f3076554e1558e8e468b2232991c510ecbcbed9e6f8c06ac31c93bcf38362", + "https://bcr.bazel.build/modules/rules_java/7.6.5/MODULE.bazel": "481164be5e02e4cab6e77a36927683263be56b7e36fef918b458d7a8a1ebadb1", + "https://bcr.bazel.build/modules/rules_java/7.6.5/source.json": "a805b889531d1690e3c72a7a7e47a870d00323186a9904b36af83aa3d053ee8d", "https://bcr.bazel.build/modules/rules_jvm_external/4.4.2/MODULE.bazel": "a56b85e418c83eb1839819f0b515c431010160383306d13ec21959ac412d2fe7", "https://bcr.bazel.build/modules/rules_jvm_external/4.4.2/source.json": "a075731e1b46bc8425098512d038d416e966ab19684a10a34f4741295642fc35", "https://bcr.bazel.build/modules/rules_license/0.0.3/MODULE.bazel": "627e9ab0247f7d1e05736b59dbb1b6871373de5ad31c3011880b4133cafd4bd0", @@ -58,27 +58,27 @@ "https://bcr.bazel.build/modules/upb/0.0.0-20220923-a547704/source.json": "f1ef7d3f9e0e26d4b23d1c39b5f5de71f584dd7d1b4ef83d9bbba6ec7a6a6459", "https://bcr.bazel.build/modules/zlib/1.2.11/MODULE.bazel": "07b389abc85fdbca459b69e2ec656ae5622873af3f845e1c9d80fe179f3effa0", "https://bcr.bazel.build/modules/zlib/1.2.12/MODULE.bazel": "3b1a8834ada2a883674be8cbd36ede1b6ec481477ada359cd2d3ddc562340b27", - "https://bcr.bazel.build/modules/zlib/1.3/MODULE.bazel": "6a9c02f19a24dcedb05572b2381446e27c272cd383aed11d41d99da9e3167a72", - "https://bcr.bazel.build/modules/zlib/1.3/source.json": "b6b43d0737af846022636e6e255fd4a96fee0d34f08f3830e6e0bac51465c37c" + "https://bcr.bazel.build/modules/zlib/1.3.1.bcr.3/MODULE.bazel": "af322bc08976524477c79d1e45e241b6efbeb918c497e8840b8ab116802dda79", + "https://bcr.bazel.build/modules/zlib/1.3.1.bcr.3/source.json": "2be409ac3c7601245958cd4fcdff4288be79ed23bd690b4b951f500d54ee6e7d" }, "selectedYankedVersions": {}, "moduleExtensions": { "@@apple_support~//crosstool:setup.bzl%apple_cc_configure_extension": { "general": { "bzlTransitiveDigest": "PjIds3feoYE8SGbbIq2SFTZy3zmxeO2tQevJZNDo7iY=", - "usagesDigest": "aLmqbvowmHkkBPve05yyDNGN7oh7QE9kBADr3QIZTZs=", + "usagesDigest": "+hz7IHWN6A1oVJJWNDB6yZRG+RYhF76wAYItpAeIUIg=", "recordedFileInputs": {}, "recordedDirentsInputs": {}, "envVariables": {}, "generatedRepoSpecs": { - "local_config_apple_cc": { + "local_config_apple_cc_toolchains": { "bzlFile": "@@apple_support~//crosstool:setup.bzl", - "ruleClassName": "_apple_cc_autoconf", + "ruleClassName": "_apple_cc_autoconf_toolchains", "attributes": {} }, - "local_config_apple_cc_toolchains": { + "local_config_apple_cc": { "bzlFile": "@@apple_support~//crosstool:setup.bzl", - "ruleClassName": "_apple_cc_autoconf_toolchains", + "ruleClassName": "_apple_cc_autoconf", "attributes": {} } }, @@ -94,7 +94,7 @@ "@@platforms//host:extension.bzl%host_platform": { "general": { "bzlTransitiveDigest": "xelQcPZH8+tmuOHVjL9vDxMnnQNMlwj0SlvgoqBkm4U=", - "usagesDigest": "meSzxn3DUCcYEhq4HQwExWkWtU4EjriRBQLsZN+Q0SU=", + "usagesDigest": "pCYpDQmqMbmiiPI1p2Kd3VLm5T48rRAht5WdW0X2GlA=", "recordedFileInputs": {}, "recordedDirentsInputs": {}, "envVariables": {}, diff --git a/e2e/.bazelversion b/e2e/.bazelversion index 0ee843c..ba7f754 100644 --- a/e2e/.bazelversion +++ b/e2e/.bazelversion @@ -1 +1 @@ -7.2.0 +7.4.0 diff --git a/e2e/MODULE.bazel.lock b/e2e/MODULE.bazel.lock index 7edadda..5dfcd16 100644 --- a/e2e/MODULE.bazel.lock +++ b/e2e/MODULE.bazel.lock @@ -36,8 +36,8 @@ "https://bcr.bazel.build/modules/rules_cc/0.0.9/MODULE.bazel": "836e76439f354b89afe6a911a7adf59a6b2518fafb174483ad78a2a2fde7b1c5", "https://bcr.bazel.build/modules/rules_cc/0.0.9/source.json": "1f1ba6fea244b616de4a554a0f4983c91a9301640c8fe0dd1d410254115c8430", "https://bcr.bazel.build/modules/rules_java/4.0.0/MODULE.bazel": "5a78a7ae82cd1a33cef56dc578c7d2a46ed0dca12643ee45edbb8417899e6f74", - "https://bcr.bazel.build/modules/rules_java/7.6.1/MODULE.bazel": "2f14b7e8a1aa2f67ae92bc69d1ec0fa8d9f827c4e17ff5e5f02e91caa3b2d0fe", - "https://bcr.bazel.build/modules/rules_java/7.6.1/source.json": "8f3f3076554e1558e8e468b2232991c510ecbcbed9e6f8c06ac31c93bcf38362", + "https://bcr.bazel.build/modules/rules_java/7.6.5/MODULE.bazel": "481164be5e02e4cab6e77a36927683263be56b7e36fef918b458d7a8a1ebadb1", + "https://bcr.bazel.build/modules/rules_java/7.6.5/source.json": "a805b889531d1690e3c72a7a7e47a870d00323186a9904b36af83aa3d053ee8d", "https://bcr.bazel.build/modules/rules_jvm_external/4.4.2/MODULE.bazel": "a56b85e418c83eb1839819f0b515c431010160383306d13ec21959ac412d2fe7", "https://bcr.bazel.build/modules/rules_jvm_external/4.4.2/source.json": "a075731e1b46bc8425098512d038d416e966ab19684a10a34f4741295642fc35", "https://bcr.bazel.build/modules/rules_license/0.0.3/MODULE.bazel": "627e9ab0247f7d1e05736b59dbb1b6871373de5ad31c3011880b4133cafd4bd0", @@ -58,27 +58,27 @@ "https://bcr.bazel.build/modules/upb/0.0.0-20220923-a547704/source.json": "f1ef7d3f9e0e26d4b23d1c39b5f5de71f584dd7d1b4ef83d9bbba6ec7a6a6459", "https://bcr.bazel.build/modules/zlib/1.2.11/MODULE.bazel": "07b389abc85fdbca459b69e2ec656ae5622873af3f845e1c9d80fe179f3effa0", "https://bcr.bazel.build/modules/zlib/1.2.12/MODULE.bazel": "3b1a8834ada2a883674be8cbd36ede1b6ec481477ada359cd2d3ddc562340b27", - "https://bcr.bazel.build/modules/zlib/1.3/MODULE.bazel": "6a9c02f19a24dcedb05572b2381446e27c272cd383aed11d41d99da9e3167a72", - "https://bcr.bazel.build/modules/zlib/1.3/source.json": "b6b43d0737af846022636e6e255fd4a96fee0d34f08f3830e6e0bac51465c37c" + "https://bcr.bazel.build/modules/zlib/1.3.1.bcr.3/MODULE.bazel": "af322bc08976524477c79d1e45e241b6efbeb918c497e8840b8ab116802dda79", + "https://bcr.bazel.build/modules/zlib/1.3.1.bcr.3/source.json": "2be409ac3c7601245958cd4fcdff4288be79ed23bd690b4b951f500d54ee6e7d" }, "selectedYankedVersions": {}, "moduleExtensions": { "@@apple_support~//crosstool:setup.bzl%apple_cc_configure_extension": { "general": { "bzlTransitiveDigest": "PjIds3feoYE8SGbbIq2SFTZy3zmxeO2tQevJZNDo7iY=", - "usagesDigest": "aLmqbvowmHkkBPve05yyDNGN7oh7QE9kBADr3QIZTZs=", + "usagesDigest": "+hz7IHWN6A1oVJJWNDB6yZRG+RYhF76wAYItpAeIUIg=", "recordedFileInputs": {}, "recordedDirentsInputs": {}, "envVariables": {}, "generatedRepoSpecs": { - "local_config_apple_cc": { + "local_config_apple_cc_toolchains": { "bzlFile": "@@apple_support~//crosstool:setup.bzl", - "ruleClassName": "_apple_cc_autoconf", + "ruleClassName": "_apple_cc_autoconf_toolchains", "attributes": {} }, - "local_config_apple_cc_toolchains": { + "local_config_apple_cc": { "bzlFile": "@@apple_support~//crosstool:setup.bzl", - "ruleClassName": "_apple_cc_autoconf_toolchains", + "ruleClassName": "_apple_cc_autoconf", "attributes": {} } }, @@ -94,7 +94,7 @@ "@@platforms//host:extension.bzl%host_platform": { "general": { "bzlTransitiveDigest": "xelQcPZH8+tmuOHVjL9vDxMnnQNMlwj0SlvgoqBkm4U=", - "usagesDigest": "meSzxn3DUCcYEhq4HQwExWkWtU4EjriRBQLsZN+Q0SU=", + "usagesDigest": "pCYpDQmqMbmiiPI1p2Kd3VLm5T48rRAht5WdW0X2GlA=", "recordedFileInputs": {}, "recordedDirentsInputs": {}, "envVariables": {}, -- GitLab From 1d48d2e43ff1cbe1d2746aa7c7b7b86363fad2a0 Mon Sep 17 00:00:00 2001 From: Matt Clarkson Date: Wed, 20 Nov 2024 17:17:42 +0000 Subject: [PATCH 4/5] fix(toolchain_local_select): use `label_keyed_string_dict` for `map` attribute BREAKING CHANGE: `toolchain_local_select#map` is now flipped taking the labels as they keys rather than as the values. Previous releases had the following setup: ```py download_file( name = "abc-arm64-linux-gnu", output = "abc", ) download_file( name = "abc-arm64-linux-musl", output = "abc", ) toolchain_local_select( name = "abc", map = { "arm64-linux-gnu": "@abc-arm64-linux-gnu", "arm64-linux-musl": "@abc-arm64-linux-musl", }, ) ``` This prevented eagarly overfetching all the repositories that would be selected. As of Bazel 7.4.0+[1], it no longer overfetches so the `map` attribute has been switched to a `label_keyed_string_dict`: ```py toolchain_local_select( name = "abc", map = { "@abc-arm64-linux-gnu": "arm64-linux-gnu", "@abc-arm64-linux-musl": "arm64-linux-musl", }, ) ``` The repository rule will _only_ fetch the repostory label that has been selected due to the triplet value matching. Whilst this is a breaking change, it will introduce a regression on Bazel 7.3 and below which will overfetch the repositories. It is *highly* recommended to use Bazel 7.4 and above. [1]: https://github.com/bazelbuild/bazel/commit/6fabb1fc6869a204373e5ee0adde696a659415dd --- e2e/MODULE.bazel | 14 +++++++------- toolchain/local/select/repository.bzl | 20 +++++++------------- 2 files changed, 14 insertions(+), 20 deletions(-) diff --git a/e2e/MODULE.bazel b/e2e/MODULE.bazel index 4224456..f01aa1a 100644 --- a/e2e/MODULE.bazel +++ b/e2e/MODULE.bazel @@ -62,13 +62,13 @@ select = use_repo_rule("@toolchain_utils//toolchain/local/select:defs.bzl", "too select( name = "fixture", map = { - "amd64-linux-gnu": "@fixture-amd64-linux-gnu", - "arm64-linux-gnu": "@fixture-arm64-linux-gnu", - "arm-linux-musl": "@fixture-arm-linux-musl", - "windows-ucrt": "@fixture-windows-ucrt", - "windows-cygwin": "@fixture-windows-cygwin", - "arm64-macos-darwin": "@fixture-arm64-macos-darwin", - "amd64-macos-darwin": "@fixture-amd64-macos-darwin", + "@fixture-amd64-linux-gnu": "amd64-linux-gnu", + "@fixture-arm64-linux-gnu": "arm64-linux-gnu", + "@fixture-arm-linux-musl": "arm-linux-musl", + "@fixture-windows-ucrt": "windows-ucrt", + "@fixture-windows-cygwin": "windows-cygwin", + "@fixture-arm64-macos-darwin": "arm64-macos-darwin", + "@fixture-amd64-macos-darwin": "amd64-macos-darwin", }, ) diff --git a/toolchain/local/select/repository.bzl b/toolchain/local/select/repository.bzl index c26005a..bb0386e 100644 --- a/toolchain/local/select/repository.bzl +++ b/toolchain/local/select/repository.bzl @@ -1,21 +1,20 @@ load("//toolchain/repository:root.bzl", "root") -load("//toolchain:separator.bzl", "SEPARATOR") -load(":resolve.bzl", resolve = "key") +load(":resolve.bzl", resolve = "value") visibility("//toolchain/...") DOC = "Selects and symlinks a repository based on the local machine triplet." ATTRS = { - "map": attr.string_dict( - doc = """Local triplet to repository mappings: + "map": attr.label_keyed_string_dict( + doc = """A map of repository labels against the host triplet: ```py toolchain_local_select( name = "abc", map = { - "arm64-linux-gnu": "@abc-arm64-linux-gnu", - "arm64-linux-musl": "@abc-arm64-linux-musl", + "@abc-arm64-linux-gnu": "arm64-linux-gnu", + "@abc-arm64-linux-musl": "arm64-linux-musl", }, ) ``` @@ -34,16 +33,11 @@ toolchain_local_select( ), } -def canonical(rctx, label): - prefix = SEPARATOR.join(rctx.name.split(SEPARATOR)[:-1]) - return Label("@@{}{}{}".format(prefix, SEPARATOR, label.removeprefix("@"))) - def implementation(rctx): label = resolve(rctx.attr.map, no_match_error = rctx.attr.no_match_error) - canon = canonical(rctx, label) - path = root(rctx, canon) + path = root(rctx, label) if not path: - fail("Missing `{}` for `{}`".format(canon, rctx.name)) + fail("Missing `{}` for `{}`".format(label, rctx.name)) rctx.delete(".") rctx.symlink(path, ".") -- GitLab From 7b3155446125e601a96e551b30fe024011ed3bf8 Mon Sep 17 00:00:00 2001 From: Matt Clarkson Date: Wed, 20 Nov 2024 17:38:30 +0000 Subject: [PATCH 5/5] test: detect overfetching --- e2e/MODULE.bazel | 5 +++++ e2e/test/fixture/repository.bzl | 4 ++++ 2 files changed, 9 insertions(+) diff --git a/e2e/MODULE.bazel b/e2e/MODULE.bazel index f01aa1a..d21ff29 100644 --- a/e2e/MODULE.bazel +++ b/e2e/MODULE.bazel @@ -57,6 +57,10 @@ fixture( name = "fixture-amd64-macos-darwin", ) +fixture( + name = "fixture-never", +) + select = use_repo_rule("@toolchain_utils//toolchain/local/select:defs.bzl", "toolchain_local_select") select( @@ -69,6 +73,7 @@ select( "@fixture-windows-cygwin": "windows-cygwin", "@fixture-arm64-macos-darwin": "arm64-macos-darwin", "@fixture-amd64-macos-darwin": "amd64-macos-darwin", + "@fixture-never": "never", }, ) diff --git a/e2e/test/fixture/repository.bzl b/e2e/test/fixture/repository.bzl index 69c9fb1..33d7784 100644 --- a/e2e/test/fixture/repository.bzl +++ b/e2e/test/fixture/repository.bzl @@ -15,6 +15,10 @@ ATTRS = { } def implementation(rctx): + version = tuple([int(v) for v in native.bazel_version.split(".")[:2]]) + if rctx.name.endswith("never") and version >= (7, 4): + fail("The `{}` repository should not be fetched. This is a case where Bazel is over-fetching.") + map = {l: "{}/{}".format(l.package, l.name) for l in rctx.attr.srcs} for label, path in map.items(): -- GitLab