Skip to content

Commit

Permalink
Add bzlmod support
Browse files Browse the repository at this point in the history
This commit adds support for bzlmod.
See the MODULE.bazel for changes and enable with `--enable_bzlmod`.
  • Loading branch information
luispadron committed Oct 3, 2023
1 parent b121696 commit a3a74ed
Show file tree
Hide file tree
Showing 9 changed files with 336 additions and 57 deletions.
4 changes: 4 additions & 0 deletions .bazelrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# Enable Bzlmod
# TODO: While supporting Bazel 5 we need to keep experimental flag vs using "--enable_bzlmod"
common --experimental_enable_bzlmod

# We can't create a bzl_library for rules-swift because of its visibility,
# so circumvent by not using the sandbox
build --strategy=Stardoc=standalone
Expand Down
6 changes: 6 additions & 0 deletions .github/workflows/preflight_env.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
#!/bin/bash

# If flag --no-bzlmod is passed, writes a user.bazelrc file to disable Bzlmod.
if [[ "$*" == *--no-bzlmod* ]]; then
echo "build --noexperimental_enable_bzlmod" > user.bazelrc
fi

set -e
echo "Selecting Xcode for environment"

Expand Down
11 changes: 7 additions & 4 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,13 @@ jobs:
steps:
- uses: actions/checkout@v3
- name: Preflight Env
run: .github/workflows/preflight_env.sh
# Dont test Bazel 5 with bzlmod
run: .github/workflows/preflight_env.sh --no-bzlmod
- name: Build and Test
run: |
# iOS tests
# iOS tests without bzlmod on Bazel 5
bazelisk build \
--noexperimental_enable_bzlmod \
--config=ci_with_caches \
--config=ios \
--config=vfs \
Expand Down Expand Up @@ -147,7 +149,8 @@ jobs:
steps:
- uses: actions/checkout@v3
- name: Preflight Env
run: .github/workflows/preflight_env.sh
# TODO: stardoc doesn't currently support bzlmod: https://github.com/bazelbuild/stardoc/issues/117
run: .github/workflows/preflight_env.sh --no-bzlmod
# Note: we need to pass the absolute to the Bazel run
- name: buildifier
run: |
Expand All @@ -166,7 +169,7 @@ jobs:
steps:
- uses: actions/checkout@v3
- name: Preflight Env
run: .github/workflows/preflight_env.sh
run: .github/workflows/preflight_env.sh --no-bzlmod
- name: Run tests
run: ./tests/xcodeproj-tests.sh --run && ./tests/test-tests.sh
- uses: actions/upload-artifact@v2
Expand Down
12 changes: 7 additions & 5 deletions BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
load("@build_bazel_rules_swift//swift/internal:feature_names.bzl", "SWIFT_FEATURE_USE_GLOBAL_INDEX_STORE")
load("//rules:features.bzl", "feature_names")
load("@bazel_skylib//lib:selects.bzl", "selects")
load("@buildifier_prebuilt//:rules.bzl", "buildifier")

sh_binary(
buildifier(
name = "buildifier",
srcs = select({
"@bazel_tools//src/conditions:darwin_x86_64": ["@buildifier.mac.amd64//file"],
"@bazel_tools//src/conditions:darwin_arm64": ["@buildifier.mac.arm64//file"],
}),
exclude_patterns = [
"./.git/*",
"./bazel-*/*",
],
mode = "fix",
)

config_setting(
Expand Down
113 changes: 113 additions & 0 deletions MODULE.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
"""
Defines all the external repositories and dependencies for rules_ios.
"""

# Defines the rules_ios bzlmod module.
# Version is updated during release to the registry.
module(
name = "rules_ios",
version = "0",
bazel_compatibility = [">=5.0.0"],
compatibility_level = 1,
repo_name = "build_bazel_rules_ios",
)

# Declare the bzlmod dependencies needed by rules_ios and users of rules_ios
bazel_dep(
name = "apple_support",
version = "1.6.0",
repo_name = "build_bazel_apple_support",
)
bazel_dep(
name = "rules_apple",
version = "2.3.0",
repo_name = "build_bazel_rules_apple",
)
bazel_dep(
name = "rules_cc",
version = "0.0.6",
)
bazel_dep(
name = "rules_python",
version = "0.24.0",
)
bazel_dep(
name = "rules_swift",
version = "1.8.0",
repo_name = "build_bazel_rules_swift",
)

# Declare the development dependencies needed for rules_ios development
bazel_dep(
name = "buildifier_prebuilt",
version = "6.1.0",
dev_dependency = True,
)
bazel_dep(
name = "rules_pkg",
version = "0.9.1",
dev_dependency = True,
)
bazel_dep(
name = "bazel_skylib",
version = "1.4.1",
dev_dependency = True,
)

# Load non-bzlmod dependencies from rules_ios
non_module_deps = use_extension("//rules:module_extensions.bzl", "non_module_deps")
use_repo(
non_module_deps,
"com_github_yonaskolb_xcodegen",
)

non_module_dev_deps = use_extension(
"//rules:module_extensions.bzl",
"non_module_dev_deps",
dev_dependency = True,
)
use_repo(
non_module_dev_deps,
"GoogleMobileAdsSDK",
"TensorFlowLiteC",
"arm64-to-sim",
"com_github_apple_swiftcollections",
"tart",
)

# Configure Xcode
xcode_configure = use_extension(
"//rules:module_extensions.bzl",
"xcode_configure",
dev_dependency = True,
)
xcode_configure.configure(
remote_xcode_label = "",
xcode_locator_label = "//tools/toolchains/xcode_configure:xcode_locator.m",
)

# Load non-bzlmod dependencies used in this repo from rules_swift
swift_non_module_deps = use_extension("@build_bazel_rules_swift//swift:extensions.bzl", "non_module_deps")
use_repo(
swift_non_module_deps,
"build_bazel_rules_swift_index_import",
"build_bazel_rules_swift_local_config",
)

# Load non-bzlmod dependencies used in this repo from rules_apple
apple_non_module_deps = use_extension("@build_bazel_rules_apple//apple:extensions.bzl", "non_module_deps")
use_repo(
apple_non_module_deps,
"xctestrunner",
)

# Register the Python toolchain
python = use_extension("@rules_python//python/extensions:python.bzl", "python")
python.toolchain(
is_default = True,
python_version = "3.9",
)
use_repo(
python,
"python_versions",
)
31 changes: 14 additions & 17 deletions WORKSPACE
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
# This file marks the root of the Bazel workspace.
# See MODULE.bazel for dependencies and setup.
# It is only used when bzlmod is disabled.
# When bzlmod is enabled WORKSPACE.bzlmod is used.

workspace(name = "build_bazel_rules_ios")

load(
"//rules:repositories.bzl",
"rules_ios_dependencies",
"rules_ios_dev_dependencies",
)

rules_ios_dependencies()

rules_ios_dev_dependencies()

load(
"@build_bazel_rules_apple//apple:repositories.bzl",
"apple_rules_dependencies",
Expand Down Expand Up @@ -55,25 +63,14 @@ load("@io_bazel_stardoc//:setup.bzl", "stardoc_repositories")

stardoc_repositories()

load(
"@bazel_tools//tools/build_defs/repo:http.bzl",
"http_file",
)
# Download prebuilt binaries buildifier
load("@buildifier_prebuilt//:deps.bzl", "buildifier_prebuilt_deps")

# Download offical release of buildifier.mac
http_file(
name = "buildifier.mac.amd64",
executable = True,
sha256 = "c9378d9f4293fc38ec54a08fbc74e7a9d28914dae6891334401e59f38f6e65dc",
urls = ["https://github.com/bazelbuild/buildtools/releases/download/5.1.0/buildifier-darwin-amd64"],
)
buildifier_prebuilt_deps()

http_file(
name = "buildifier.mac.arm64",
executable = True,
sha256 = "745feb5ea96cb6ff39a76b2821c57591fd70b528325562486d47b5d08900e2e4",
urls = ["https://github.com/bazelbuild/buildtools/releases/download/5.1.0/buildifier-darwin-arm64"],
)
load("@buildifier_prebuilt//:defs.bzl", "buildifier_prebuilt_register_toolchains")

buildifier_prebuilt_register_toolchains()

load(
"//tests/ios/frameworks/external-dependency:external_dependency.bzl",
Expand Down
27 changes: 27 additions & 0 deletions WORKSPACE.bzlmod
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# This file marks the root of the Bazel workspace.
# See MODULE.bazel for dependencies and setup.
# It is only used when bzlmod is enabled.
# This file will generally be kept empty in favor of using MODULE.bazel fully.

workspace(name = "build_bazel_rules_ios")

# TODO: stardoc doesn't currently support bzlmod: https://github.com/bazelbuild/stardoc/issues/117
# Once that closes we should depend on it via the WORKSPACE file.
# In the meantime, this is a workaround to at least allow building.
# Note however that updating docs with bzlmod will not work until the above is resolved.
# Until then use: --noenable_bzlmod
load(
"@bazel_tools//tools/build_defs/repo:git.bzl",
"git_repository",
)

git_repository(
name = "io_bazel_stardoc",
commit = "6f274e903009158504a9d9130d7f7d5f3e9421ed",
remote = "https://github.com/bazelbuild/stardoc.git",
shallow_since = "1667581897 -0400",
)

load("@io_bazel_stardoc//:setup.bzl", "stardoc_repositories")

stardoc_repositories()
43 changes: 43 additions & 0 deletions rules/module_extensions.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
"""Definitions for bzlmod module extensions."""

load(
"//rules:repositories.bzl",
"rules_ios_dependencies",
"rules_ios_dev_dependencies",
)
load(
"//tools/toolchains/xcode_configure:xcode_configure.bzl",
_xcode_configure = "xcode_configure",
)

def _non_module_deps_impl(_):
rules_ios_dependencies(
load_bzlmod_dependencies = False,
)

non_module_deps = module_extension(implementation = _non_module_deps_impl)

def _non_module_dev_deps_impl(_):
rules_ios_dev_dependencies(load_bzlmod_dependencies = False)

non_module_dev_deps = module_extension(implementation = _non_module_dev_deps_impl)

def _xcode_configure_impl(module_ctx):
for mod in module_ctx.modules:
for xcode_configure_attr in mod.tags.configure:
_xcode_configure(
remote_xcode_label = xcode_configure_attr.remote_xcode_label,
xcode_locator_label = xcode_configure_attr.xcode_locator_label,
)

xcode_configure = module_extension(
implementation = _xcode_configure_impl,
tag_classes = {
"configure": tag_class(
attrs = {
"remote_xcode_label": attr.string(mandatory = True),
"xcode_locator_label": attr.string(mandatory = True),
},
),
},
)
Loading

0 comments on commit a3a74ed

Please sign in to comment.