Skip to content

Commit

Permalink
add back version checks
Browse files Browse the repository at this point in the history
  • Loading branch information
Karim Alweheshy committed Mar 1, 2024
1 parent 56f3a5f commit 016fc08
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 27 deletions.
6 changes: 6 additions & 0 deletions MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,12 @@ use_repo(
"tart",
)

rules_ios_bazel_version_deps = use_extension("//rules:module_extensions.bzl", "rules_ios_bazel_version_deps")
use_repo(
rules_ios_bazel_version_deps,
"rules_ios_bazel_version",
)

non_module_dev_deps = use_extension(
"//rules:module_extensions.bzl",
"non_module_dev_deps",
Expand Down
4 changes: 3 additions & 1 deletion rules/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ bzl_library(
srcs = ["transition_support.bzl"],
visibility = ["//visibility:public"],
deps = [
"@build_bazel_apple_support//lib:apple_support",
"//rules/internal:bazel_version",
"@build_bazel_apple_support//configs:platforms",
"@rules_ios_bazel_version//:api",
],
)

Expand Down
6 changes: 6 additions & 0 deletions rules/internal/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
load("@bazel_skylib//:bzl_library.bzl", "bzl_library")

bzl_library(
name = "bazel_version",
srcs = ["bazel_version.bzl"],
visibility = ["//rules:__pkg__"],
)

bzl_library(
name = "objc_provider_utils",
srcs = ["objc_provider_utils.bzl"],
Expand Down
19 changes: 19 additions & 0 deletions rules/internal/bazel_version.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
"Bazel version parsing"

def get_bazel_version(bazel_version = getattr(native, "bazel_version", "")):
"""
Parse the Bazel version into a `struct`.
Args:
bazel_version: String representing the Bazel version.
Returns:
Bazel version represented as a `struct` with the major, minor, and path attributes.
"""
if bazel_version:
parts = bazel_version.split(".")
if len(parts) > 2:
return struct(major = parts[0], minor = parts[1], patch = parts[2])

# Unknown, but don't crash
return struct(major = 0, minor = 0, patch = 0)
8 changes: 8 additions & 0 deletions rules/module_extensions.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

load(
"//rules:repositories.bzl",
"rules_ios_bazel_version",
"rules_ios_dependencies",
"rules_ios_dev_dependencies",
)
Expand All @@ -22,6 +23,13 @@ def _non_module_dev_deps_impl(_):

non_module_dev_deps = module_extension(implementation = _non_module_dev_deps_impl)

def _rules_ios_bazel_version(_):
rules_ios_bazel_version(
name = "rules_ios_bazel_version",
)

rules_ios_bazel_version_deps = module_extension(implementation = _rules_ios_bazel_version)

def _xcode_configure_impl(module_ctx):
for mod in module_ctx.modules:
for xcode_configure_attr in mod.tags.configure:
Expand Down
28 changes: 28 additions & 0 deletions rules/repositories.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -118,13 +118,41 @@ def _rules_ios_bzlmod_dependencies():
sha256 = "8f9ee2dc10c1ae514ee599a8b42ed99fa262b757058f65ad3c384289ff70c4b8",
)

def _rules_ios_bazel_version_impl(ctx):
ctx.file("BUILD.bazel", content = """
load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
bzl_library(
name = "api",
srcs = [
"version.bzl",
],
visibility = ["//visibility:public"],
)
""")

# Write Bazel version to a file
ctx.file("version.bzl", content = "bazel_version = \"{}\"".format(ctx.attr._bazel_version))

rules_ios_bazel_version = repository_rule(
implementation = _rules_ios_bazel_version_impl,
attrs = {
"_bazel_version": attr.string(default = getattr(native, "bazel_version", "")),
},
local = True,
)

def _rules_ios_bzlmod_dev_dependencies():
"""
Fetches repositories that are development dependencies of `rules_ios` and available via bzlmod.
These are only included when using WORKSPACE, when using bzlmod they're loaded in MODULE.bazel
"""

_maybe(
rules_ios_bazel_version,
name = "rules_ios_bazel_version",
)

_maybe(
http_archive,
name = "buildifier_prebuilt",
Expand Down
32 changes: 6 additions & 26 deletions rules/transition_support.bzl
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
"""Starlark transition support for Apple rules."""

load("@rules_ios_bazel_version//:version.bzl", "bazel_version")
load(
"//rules/internal:bazel_version.bzl",
"get_bazel_version",
)
load(
"@build_bazel_apple_support//configs:platforms.bzl",
"CPU_TO_DEFAULT_PLATFORM_NAME"
)

_PLATFORM_TYPE_TO_CPUS_FLAG = {
"ios": "//command_line_option:ios_multi_cpus",
Expand All @@ -13,36 +18,11 @@ _PLATFORM_TYPE_TO_CPUS_FLAG = {
"watchos": "//command_line_option:watchos_cpus",
}

# TODO: Should be loaded from bazel_build_apple_support when we migrate to v1.10.0
# Could not load it directly from bazel_build_apple_support
# As we support multiple versions of rules_apple_support (v2 and v3)
# Where <v3.1.0 don't have the updated dependency. So we copied it
# https://github.com/bazelbuild/apple_support/blob/d87e8b07f3345e750834dbb6ce38c7c7d3b8b44b/configs/platforms.bzl#L108
_CPU_TO_DEFAULT_PLATFORM_NAME = {
"darwin_arm64": "macos_arm64",
"darwin_arm64e": "macos_arm64e",
"darwin_x86_64": "macos_x86_64",
"ios_arm64": "ios_arm64",
"ios_arm64e": "ios_arm64e",
"ios_x86_64": "ios_x86_64",
"ios_sim_arm64": "ios_sim_arm64",
"tvos_arm64": "tvos_arm64",
"tvos_x86_64": "tvos_x86_64",
"tvos_sim_arm64": "tvos_sim_arm64",
"visionos_arm64": "visionos_arm64",
"visionos_sim_arm64": "visionos_sim_arm64",
"visionos_x86_64": "visionos_x86_64",
"watchos_arm64": "watchos_arm64",
"watchos_arm64_32": "watchos_arm64_32",
"watchos_armv7k": "watchos_armv7k",
"watchos_x86_64": "watchos_x86_64",
}

_CPU_TO_DEFAULT_PLATFORM_FLAG = {
cpu: "@build_bazel_apple_support//platforms:{}_platform".format(
platform_name,
)
for cpu, platform_name in _CPU_TO_DEFAULT_PLATFORM_NAME.items()
for cpu, platform_name in CPU_TO_DEFAULT_PLATFORM_NAME.items()
}

_bazel_version = get_bazel_version(bazel_version)
Expand Down

0 comments on commit 016fc08

Please sign in to comment.