Skip to content

Commit

Permalink
feat(bazel): make googleapis a Bzlmod module
Browse files Browse the repository at this point in the history
  • Loading branch information
tyler-french committed Dec 30, 2023
1 parent 75c4411 commit 942c6c7
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .kokoro/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,11 @@ CACHE_CMDLINE="--remote_cache=https://storage.googleapis.com/${CACHE_BUCKET} --g
#
# Run build and tests
#
echo "Building and Testing using WORKSPACE dependencies"
${BAZELISK_BIN} --output_user_root=${BAZEL_ROOT} build ${CACHE_CMDLINE} --keep_going //...
${BAZELISK_BIN} --output_user_root=${BAZEL_ROOT} test ${CACHE_CMDLINE} --flaky_test_attempts=3 --keep_going //...

# Restart the Bazel server to prevent sandbox issues.
echo "Building and Testing using Bzlmod dependencies"
${BAZELISK_BIN} shutdown
${BAZELISK_BIN} --output_user_root=${BAZEL_ROOT} build ${CACHE_CMDLINE} --keep_going --enable_bzlmod //...
16 changes: 16 additions & 0 deletions MODULE.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module(
name = "com_google_googleapis",
version = "0.0.0",
)

bazel_dep(name = "protobuf", version = "21.7", repo_name = "com_google_protobuf")
bazel_dep(name = "rules_proto", version = "5.3.0-21.7")

switched_rules = use_extension("//:extensions.bzl", "switched_rules")
switched_rules.use_languages(
cc = True,
go = True,
java = True,
python = True,
)
use_repo(switched_rules, "com_google_googleapis_imports")
2 changes: 2 additions & 0 deletions WORKSPACE.bzlmod
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# When Bzlmod is enabled, this file replaces the content of the original WORKSPACE
# and makes sure no WORKSPACE prefix or suffix are added when Bzlmod is enabled.
59 changes: 59 additions & 0 deletions extensions.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
load(":repository_rules.bzl", "switched_rules_by_language")

_use_languages_tag = tag_class(
attrs = {
"cc": attr.bool(default = False),
"csharp": attr.bool(default = False),
"gapic": attr.bool(default = False),
"go": attr.bool(default = False),
"go_test": attr.bool(default = False),
"grpc": attr.bool(default = False),
"java": attr.bool(default = False),
"nodejs": attr.bool(default = False),
"php": attr.bool(default = False),
"python": attr.bool(default = False),
"ruby": attr.bool(default = False),
},
)

def _switched_rules_impl(ctx):
attrs = {}
for module in ctx.modules:
if not module.is_root:
continue

is_tag_set = False
set_tag_name = ""

for t in module.tags.use_languages:
if is_tag_set:
fail("Multiple use_language tags are set in the root module: '{}' and '{}'. Only one is allowed.".format(set_tag_name, t.name))

is_tag_set = True
set_tag_name = t.name

attrs = {
"cc": t.cc,
"csharp": t.csharp,
"gapic": t.gapic,
"go": t.go,
"go_test": t.go_test,
"grpc": t.grpc,
"java": t.java,
"nodejs": t.nodejs,
"php": t.php,
"python": t.python,
"ruby": t.ruby,
}

switched_rules_by_language(
name = "com_google_googleapis_imports",
**attrs
)

switched_rules = module_extension(
implementation = _switched_rules_impl,
tag_classes = {
"use_languages": _use_languages_tag,
},
)

0 comments on commit 942c6c7

Please sign in to comment.