Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: toolchainize language runtimes #14590

Closed
wants to merge 13 commits into from
1 change: 1 addition & 0 deletions .bazelignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# These are fetched as external repositories.
third_party/abseil-cpp
third_party/googletest
third_party/utf8_range
thesayyn marked this conversation as resolved.
Show resolved Hide resolved
_build/
3 changes: 3 additions & 0 deletions .bazelrc
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,6 @@ build:ubsan --copt=-fno-sanitize=function --copt=-fno-sanitize=vptr
# TODO: migrate all dependencies from WORKSPACE to MODULE.bazel
# https://github.com/protocolbuffers/protobuf/issues/14313
common --noenable_bzlmod

# Enable proto toolchain resolution
common --incompatible_enable_proto_toolchain_resolution
1 change: 1 addition & 0 deletions .bazelversion
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
5a24c8a3c6cd336ff69ce14fd1c95d50589ec27a
thesayyn marked this conversation as resolved.
Show resolved Hide resolved
52 changes: 51 additions & 1 deletion BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
load("@rules_cc//cc:defs.bzl", "cc_binary", "cc_library", "cc_proto_library")
load("@rules_java//java:defs.bzl", "java_lite_proto_library", "java_proto_library")
load("@rules_pkg//:mappings.bzl", "pkg_files", "strip_prefix")
load("@rules_proto//proto:defs.bzl", "proto_lang_toolchain", "proto_library")
load("@rules_proto//proto:defs.bzl", "proto_lang_toolchain", "proto_toolchain", "proto_library")
load("//build_defs:cpp_opts.bzl", "COPTS", "LINK_OPTS")
load(":protobuf.bzl", "internal_objc_proto_library", "internal_php_proto_library", "internal_py_proto_library", "internal_ruby_proto_library")

Expand Down Expand Up @@ -194,6 +194,18 @@ cc_binary(
deps = ["//src/google/protobuf/compiler:protoc_lib"],
)

proto_toolchain(
thesayyn marked this conversation as resolved.
Show resolved Hide resolved
name = "protoc_source_toolchain",
thesayyn marked this conversation as resolved.
Show resolved Hide resolved
proto_compiler = ":protoc"
)

toolchain(
name = "protoc_toolchain",
toolchain = ":protoc_source_toolchain",
toolchain_type = "@rules_proto//proto:toolchain_type",
)


################################################################################
# C++ runtime
################################################################################
Expand Down Expand Up @@ -288,6 +300,19 @@ alias(
visibility = ["//visibility:public"],
)

toolchain(
name = "javalite_source_toolchain",
toolchain = ":javalite_toolchain",
toolchain_type = "@rules_java//java/proto:lite_toolchain_type",
thesayyn marked this conversation as resolved.
Show resolved Hide resolved
)

toolchain(
name = "java_source_toolchain",
toolchain = ":java_toolchain",
toolchain_type = "@rules_java//java/proto:toolchain_type",
)


################################################################################
# Python support
################################################################################
Expand Down Expand Up @@ -328,6 +353,25 @@ alias(
visibility = ["//visibility:public"],
)

proto_lang_toolchain(
thesayyn marked this conversation as resolved.
Show resolved Hide resolved
name = "python_toolchain",
command_line = "--python_out=%s",
progress_message = "Generating Python proto_library %{label}",
runtime = ":protobuf_python",
visibility = ["//visibility:public"],
)

toolchain(
name = "python_source_toolchain",
toolchain = ":python_toolchain",
toolchain_type = "@rules_python//python/proto:toolchain_type",
)


################################################################################
# CC support
################################################################################

proto_lang_toolchain(
name = "cc_toolchain",
blacklisted_protos = [
Expand All @@ -339,6 +383,12 @@ proto_lang_toolchain(
visibility = ["//visibility:public"],
)

toolchain(
name = "cc_source_toolchain",
toolchain = ":cc_toolchain",
toolchain_type = "@rules_cc//cc/proto:toolchain_type",
)

################################################################################
# Objective-C support
################################################################################
Expand Down
15 changes: 14 additions & 1 deletion WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,20 @@ local_repository(
)

# Load common dependencies first to ensure we use the correct version
load("//:protobuf_deps.bzl", "PROTOBUF_MAVEN_ARTIFACTS", "protobuf_deps")
load("//:protobuf_deps.bzl", "PROTOBUF_MAVEN_ARTIFACTS", "protobuf_deps", "protobuf_register_toolchains")

protobuf_deps()
protobuf_register_toolchains()

# Setup rules_java
load("@rules_java//java:repositories.bzl", "rules_java_dependencies", "rules_java_toolchains")
rules_java_dependencies()
rules_java_toolchains()

# Setup rules_python
load("@rules_python//python:repositories.bzl", "py_repositories")
py_repositories()


# Bazel platform rules.
http_archive(
Expand Down Expand Up @@ -201,3 +212,5 @@ crates_repository(

load("@crate_index//:defs.bzl", "crate_repositories")
crate_repositories()


10 changes: 2 additions & 8 deletions bazel/py_proto_library.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ def _py_proto_library_aspect_impl(target, ctx):
transitive = [proto_info.transitive_descriptor_sets],
),
outputs = srcs,
executable = ctx.executable._protoc,
executable = ctx.toolchains["@rules_python//python/proto:toolchain_type"].proto.proto_compiler,
arguments = [
"--python_out=" + _get_real_root(ctx, srcs[0]),
"--descriptor_set_in=" + ctx.configuration.host_path_separator.join([f.path for f in transitive_sets]),
Expand All @@ -112,18 +112,12 @@ def _py_proto_library_aspect_impl(target, ctx):
]

_py_proto_library_aspect = aspect(
attrs = {
"_protoc": attr.label(
executable = True,
cfg = "exec",
default = "//:protoc",
),
},
implementation = _py_proto_library_aspect_impl,
provides = [
PyInfo,
],
attr_aspects = ["deps"],
toolchains = ["@rules_python//python/proto:toolchain_type"]
)

py_proto_library = rule(
Expand Down
2 changes: 1 addition & 1 deletion java/osgi/osgi.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ def _osgi_jar_impl(ctx):
source_jars = source_jars.to_list()
if len(source_jars) > 1:
fail("osgi_jar rule doesn't know how to deal with more than one source jar.")
source_jar = target_java_output.source_jars[0]
source_jar = target_java_output.source_jars.to_list()[0]

output_jar = ctx.outputs.output_jar

Expand Down
39 changes: 23 additions & 16 deletions protobuf_deps.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -80,33 +80,31 @@ def protobuf_deps():
_github_archive(
name = "rules_cc",
repo = "https://github.com/bazelbuild/rules_cc",
commit = "818289e5613731ae410efb54218a4077fb9dbb03",
sha256 = "0adbd6f567291ad526e82c765e15aed33cea5e256eeba129f1501142c2c56610",
commit = "c8c38f8c710cbbf834283e4777916b68261b359c",
)

if not native.existing_rule("rules_java"):
http_archive(
_github_archive(
name = "rules_java",
url = "https://github.com/bazelbuild/rules_java/releases/download/6.0.0/rules_java-6.0.0.tar.gz",
sha256 = "469b7f3b580b4fcf8112f4d6d0d5a4ce8e1ad5e21fee67d8e8335d5f8b3debab",
repo = "https://github.com/bazelbuild/rules_java",
commit = "b14972c2fd5211d0bed50ac291e4e0785f247f47",
)


if not native.existing_rule("rules_proto"):
http_archive(
name = "rules_proto",
sha256 = "dc3fb206a2cb3441b485eb1e423165b231235a1ea9b031b4433cf7bc1fa460dd",
strip_prefix = "rules_proto-5.3.0-21.7",
urls = [
"https://github.com/bazelbuild/rules_proto/archive/refs/tags/5.3.0-21.7.tar.gz",
],
sha256 = "903af49528dc37ad2adbb744b317da520f133bc1cbbecbdd2a6c546c9ead080b",
strip_prefix = "rules_proto-6.0.0-rc0",
url = "https://github.com/bazelbuild/rules_proto/releases/download/6.0.0-rc0/rules_proto-6.0.0-rc0.tar.gz",
)

if not native.existing_rule("rules_python"):
_github_archive(
http_archive(
name = "rules_python",
repo = "https://github.com/bazelbuild/rules_python",
commit = "02b521fce3c7b36b05813aa986d72777cc3ee328", # 0.24.0
sha256 = "f9e4f6acf82449324d56669bda4bdb28b48688ad2990d8b39fa5b93ed39c9ad1",
sha256 = "9d04041ac92a0985e344235f5d946f71ac543f1b1565f2cdbc9a2aaee8adf55b",
strip_prefix = "rules_python-0.26.0",
url = "https://github.com/bazelbuild/rules_python/releases/download/0.26.0/rules_python-0.26.0.tar.gz",
)

if not native.existing_rule("rules_ruby"):
Expand All @@ -121,8 +119,8 @@ def protobuf_deps():
_github_archive(
name = "rules_jvm_external",
repo = "https://github.com/bazelbuild/rules_jvm_external",
commit = "906875b0d5eaaf61a8ca2c9c3835bde6f435d011",
sha256 = "744bd7436f63af7e9872948773b8b106016dc164acb3960b4963f86754532ee7",
commit = "be4bc31ad00a9c450a98ef87aa45b2199b4fdd58",
sha256 = "50ca1c0e976bb52f02c464a1e901d494e3b5681bd0d4bfe40799f2353933da9f"
)

if not native.existing_rule("rules_pkg"):
Expand Down Expand Up @@ -178,3 +176,12 @@ def protobuf_deps():
name = "nuget_python_x86-64_3.10.0",
sha256 = "4474c83c25625d93e772e926f95f4cd398a0abbb52793625fa30f39af3d2cc00",
)


def protobuf_register_toolchains():
"registers source toolchain"
native.register_toolchains("//:protoc_toolchain")
thesayyn marked this conversation as resolved.
Show resolved Hide resolved
native.register_toolchains("//:cc_source_toolchain")
native.register_toolchains("//:javalite_source_toolchain")
native.register_toolchains("//:java_source_toolchain")
native.register_toolchains("//:python_source_toolchain")
Loading