diff --git a/core/BUILD.bazel b/core/BUILD.bazel index 8b61b92d..74cb7a80 100644 --- a/core/BUILD.bazel +++ b/core/BUILD.bazel @@ -30,5 +30,6 @@ bzl_library( deps = [ ":bazel_tools", "@bazel_skylib//lib:paths", + "@bazel_skylib//lib:versions", ], ) diff --git a/core/util.bzl b/core/util.bzl index 4bfb702e..eff10e76 100644 --- a/core/util.bzl +++ b/core/util.bzl @@ -1,5 +1,6 @@ load("@bazel_tools//tools/cpp:lib_cc_configure.bzl", "get_cpu_value") load("@bazel_skylib//lib:paths.bzl", "paths") +load("@bazel_skylib//lib:versions.bzl", "versions") def is_supported_platform(repository_ctx): return repository_ctx.which("nix-build") != None @@ -287,3 +288,28 @@ def expand_location(repository_ctx, string, labels, attr = None): fail("Internal error: Unknown location expansion command '{}'.".format(command), attr) return result + +def is_bazel_version_at_least(threshold): + """ Check if current bazel version is higer or equals to a threshold. + + Args: + threshold: string: minimum desired version of Bazel + + Returns: + threshold_met, from_source_version: bool, bool: tuple where + first item states if the threshold was met, the second indicates + if obtained bazel version is empty string (indicating from source build) + """ + threshold_met = False + from_source_version = False + + bazel_version = versions.get() + if not bazel_version: + from_source_version = True + else: + threshold_met = versions.is_at_least(threshold, bazel_version) + + return ( + threshold_met, + from_source_version, + ) diff --git a/toolchains/cc/cc.bzl b/toolchains/cc/cc.bzl index d6bb19bd..bd81f1be 100644 --- a/toolchains/cc/cc.bzl +++ b/toolchains/cc/cc.bzl @@ -15,10 +15,10 @@ load( "write_builtin_include_directory_paths", ) load("@bazel_skylib//lib:sets.bzl", "sets") -load("@bazel_skylib//lib:versions.bzl", "versions") load("@rules_nixpkgs_core//:nixpkgs.bzl", "nixpkgs_package") load( "@rules_nixpkgs_core//:util.bzl", + "is_bazel_version_at_least", "ensure_constraints", "execute_or_fail", ) @@ -134,7 +134,8 @@ def _nixpkgs_cc_toolchain_config_impl(repository_ctx): # A module map is required for clang starting from Bazel version 3.3.0. # https://github.com/bazelbuild/bazel/commit/8b9f74649512ee17ac52815468bf3d7e5e71c9fa - needs_module_map = info.is_clang and versions.is_at_least("3.3.0", versions.get()) + bazel_version_match, bazel_from_source = is_bazel_version_at_least("3.3.0") + needs_module_map = info.is_clang and (bazel_version_match or bazel_from_source) if needs_module_map: generate_system_module_map = [ repository_ctx.path(repository_ctx.attr._generate_system_module_map), diff --git a/toolchains/python/python.bzl b/toolchains/python/python.bzl index 12eb92f1..9df1f274 100644 --- a/toolchains/python/python.bzl +++ b/toolchains/python/python.bzl @@ -7,13 +7,13 @@ Rules for importing a Python toolchain from Nixpkgs. * [nixpkgs_python_configure](#nixpkgs_python_configure) """ -load("@bazel_skylib//lib:versions.bzl", "versions") load( "@rules_nixpkgs_core//:nixpkgs.bzl", "nixpkgs_package", ) load( "@rules_nixpkgs_core//:util.bzl", + "is_bazel_version_at_least", "ensure_constraints", "label_string", ) @@ -56,7 +56,8 @@ _nixpkgs_python_toolchain = repository_rule( ) def _python_nix_file_content(attribute_path, bin_path, version): - add_shebang = versions.is_at_least("4.2.0", versions.get()) + bazel_version_match, bazel_from_source = is_bazel_version_at_least("4.2.0") + add_shebang = bazel_version_match or bazel_from_source return """ with import {{ config = {{}}; overlays = []; }};