Skip to content

Commit

Permalink
Ensure nixpkgs will work with Bazel build from source
Browse files Browse the repository at this point in the history
This commit make sure, that the checks placed for certain Bazel version will treat Bazel build from source gracefuly
  • Loading branch information
AleksanderGondek authored and avdv committed May 31, 2022
1 parent 38124e4 commit 4b53b7f
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 4 deletions.
26 changes: 26 additions & 0 deletions core/util.bzl
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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:
treshold_met, from_source_version: bool, bool: tuple where
first item states if the treshold was met, the second indicates
if obtained bazel version is empty string (indicating from source build)
"""
treshold_met = False
from_source_version = False

bazel_version = versions.get()
if not bazel_version:
from_source_version = True
else:
treshold_met = versions.is_at_least(threshold, bazel_version)

return (
treshold_met,
from_source_version,
)
5 changes: 3 additions & 2 deletions toolchains/cc/cc.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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",
)
Expand Down Expand Up @@ -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),
Expand Down
5 changes: 3 additions & 2 deletions toolchains/python/python.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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",
)
Expand Down Expand Up @@ -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 <nixpkgs> {{ config = {{}}; overlays = []; }};
Expand Down

0 comments on commit 4b53b7f

Please sign in to comment.