From 6d164984a90356a57c39389a53bf7062c2b86de7 Mon Sep 17 00:00:00 2001 From: Guillaume Maudoux Date: Wed, 19 Jan 2022 17:13:49 +0100 Subject: [PATCH 1/4] Make toolchain registration optional --- CHANGELOG.md | 6 ++++++ README.md | 16 ++++++++++++++++ nixpkgs/nixpkgs.bzl | 9 ++++++++- 3 files changed, 30 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8ab58b7c9..2590c6753 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,12 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/). [Unreleased]: https://github.com/tweag/rules_nixpkgs/compare/v0.8.0...HEAD +### Added + +- nixpkgs_cc_configure gained a flag to disable automatic registration of the + toolchain it creates. + See [#XXX][#XXX] + ## [0.8.0] - 2021-02-11 [0.8.0]: https://github.com/tweag/rules_nixpkgs/compare/v0.7.0...v0.8.0 diff --git a/README.md b/README.md index 5edb05dcb..5ae939071 100644 --- a/README.md +++ b/README.md @@ -429,6 +429,22 @@ default is None Constraints for the target platform. +

+ + + +register + + +optional. +default is True + +

+ +bool, whether the underlying toolchain should be automatically registered with +register_toolchains. Useful when you need a more advanced +toolchain configuration than the one provided here. +

diff --git a/nixpkgs/nixpkgs.bzl b/nixpkgs/nixpkgs.bzl index c16d1d9d1..01552c920 100644 --- a/nixpkgs/nixpkgs.bzl +++ b/nixpkgs/nixpkgs.bzl @@ -622,7 +622,8 @@ def nixpkgs_cc_configure( quiet = False, fail_not_supported = True, exec_constraints = None, - target_constraints = None): + target_constraints = None, + register = True): """Use a CC toolchain from Nixpkgs. No-op if not a nix-based platform. By default, Bazel auto-configures a CC toolchain from commands (e.g. @@ -653,6 +654,7 @@ def nixpkgs_cc_configure( fail_not_supported: bool, Whether to fail if `nix-build` is not available. exec_constraints: Constraints for the execution platform. target_constraints: Constraints for the target platform. + register: Automatically register the created toolchain with the provided *_constraints. Defaults to True. """ nixopts = list(nixopts) @@ -717,6 +719,11 @@ def nixpkgs_cc_configure( fail_not_supported = fail_not_supported, ) + # Stop before generating the toolchain. The only point of not registering it + # is because it is not used. + if (not register): + return + # Generate the `cc_toolchain` workspace. if (exec_constraints == None) != (target_constraints == None): fail("Both exec_constraints and target_constraints need to be provided or none of them.") From 255e318bf878957543922b4be2a6af45a4a78559 Mon Sep 17 00:00:00 2001 From: Guillaume Maudoux Date: Wed, 19 Jan 2022 17:15:35 +0100 Subject: [PATCH 2/4] Add pr number --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2590c6753..10a2112f6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,7 +12,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/). - nixpkgs_cc_configure gained a flag to disable automatic registration of the toolchain it creates. - See [#XXX][#XXX] + See [#179][#179] ## [0.8.0] - 2021-02-11 From 47791ce9f94aa7580a56da4eef6b4a74df9a2107 Mon Sep 17 00:00:00 2001 From: Guillaume Maudoux Date: Tue, 8 Feb 2022 15:18:53 +0100 Subject: [PATCH 3/4] nixpkgs_cc_configure: also create the toolchain when not registering it --- README.md | 3 ++- nixpkgs/nixpkgs.bzl | 18 +++++++----------- 2 files changed, 9 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 5ae939071..235c281bf 100644 --- a/README.md +++ b/README.md @@ -443,7 +443,8 @@ default is True bool, whether the underlying toolchain should be automatically registered with register_toolchains. Useful when you need a more advanced -toolchain configuration than the one provided here. +toolchain configuration than the one provided here and do not want this partial +toolchain to be registered as-is.

diff --git a/nixpkgs/nixpkgs.bzl b/nixpkgs/nixpkgs.bzl index 01552c920..b920d8ba6 100644 --- a/nixpkgs/nixpkgs.bzl +++ b/nixpkgs/nixpkgs.bzl @@ -719,11 +719,6 @@ def nixpkgs_cc_configure( fail_not_supported = fail_not_supported, ) - # Stop before generating the toolchain. The only point of not registering it - # is because it is not used. - if (not register): - return - # Generate the `cc_toolchain` workspace. if (exec_constraints == None) != (target_constraints == None): fail("Both exec_constraints and target_constraints need to be provided or none of them.") @@ -734,12 +729,13 @@ def nixpkgs_cc_configure( target_constraints = target_constraints, ) - maybe( - native.bind, - name = "cc_toolchain", - actual = "@{}//:toolchain".format(name), - ) - native.register_toolchains("@{}_toolchains//:all".format(name)) + if register: + maybe( + native.bind, + name = "cc_toolchain", + actual = "@{}//:toolchain".format(name), + ) + native.register_toolchains("@{}_toolchains//:all".format(name)) def _readlink(repository_ctx, path): return repository_ctx.path(path).realpath From ce1a031a52a3d5a088cb0341e6e77a85855385f1 Mon Sep 17 00:00:00 2001 From: Guillaume Maudoux Date: Wed, 9 Feb 2022 15:33:16 +0100 Subject: [PATCH 4/4] Properly generate readme --- README.md | 7 ++----- nixpkgs/nixpkgs.bzl | 2 +- 2 files changed, 3 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 235c281bf..f16fbd7a4 100644 --- a/README.md +++ b/README.md @@ -239,7 +239,7 @@ Dependencies of `nix_file` if any.
 nixpkgs_cc_configure(name, attribute_path, nix_file, nix_file_content, nix_file_deps, repositories,
                      repository, nixopts, quiet, fail_not_supported, exec_constraints,
-                     target_constraints)
+                     target_constraints, register)
 
Use a CC toolchain from Nixpkgs. No-op if not a nix-based platform. @@ -441,10 +441,7 @@ default is True

-bool, whether the underlying toolchain should be automatically registered with -register_toolchains. Useful when you need a more advanced -toolchain configuration than the one provided here and do not want this partial -toolchain to be registered as-is. +bool, enabled by default, Whether to register (with `register_toolchains`) the generated toolchain and install it as the default cc_toolchain.

diff --git a/nixpkgs/nixpkgs.bzl b/nixpkgs/nixpkgs.bzl index b920d8ba6..061170e67 100644 --- a/nixpkgs/nixpkgs.bzl +++ b/nixpkgs/nixpkgs.bzl @@ -654,7 +654,7 @@ def nixpkgs_cc_configure( fail_not_supported: bool, Whether to fail if `nix-build` is not available. exec_constraints: Constraints for the execution platform. target_constraints: Constraints for the target platform. - register: Automatically register the created toolchain with the provided *_constraints. Defaults to True. + register: bool, enabled by default, Whether to register (with `register_toolchains`) the generated toolchain and install it as the default cc_toolchain. """ nixopts = list(nixopts)