Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
2 changes: 1 addition & 1 deletion .github/workflows/test_bazel.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ jobs:
# Default flags, uses from-source protoc
- ""
# still uses from-source protoc unless
# --@com_google_protobuf//bazel/toolchains:prefer_prebuilt_protoc is set
# --@com_google_protobuf//bazel/flags:prefer_prebuilt_protoc is set
- "--incompatible_enable_proto_toolchain_resolution=true"
runs-on: ${{ matrix.runner }}-latest
name: ${{ matrix.continuous-only && inputs.continuous-prefix || '' }} Examples ${{ matrix.runner }} ${{ matrix.bazelversion }}${{ matrix.bzlmod && ' (bzlmod)' || '' }} ${{ matrix.toolchain_resolution && ' (toolchain resolution)' || '' }}
Expand Down
9 changes: 7 additions & 2 deletions BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,12 @@ alias(
visibility = ["//visibility:public"],
)

alias(
name = "timestamp_upb_reflection_proto",
actual = "//src/google/protobuf:timestamp_upb_reflection_proto",
visibility = ["//visibility:public"],
)

alias(
name = "type_upb_reflection_proto",
actual = "//src/google/protobuf:type_upb_reflection_proto",
Expand Down Expand Up @@ -570,7 +576,7 @@ proto_lang_toolchain(
# Set minimal protoc if prefer_prebuilt_protoc.flag_set is true since a prebuilt for protoc_minimal is not available
# TODO: Add a flag to switch between minimal and full protoc with proto_lang_toolchain once we have a minimal protoc binary.
# Setting this attribute to None will make the toolchain to pick the full protoc binary.
"//bazel/toolchains:prefer_prebuilt_protoc.flag_set": None,
"//bazel/flags:prefer_prebuilt_protoc.flag_set": None,
"//conditions:default": "//src/google/protobuf/compiler:protoc_minimal",
}),
runtime = "//src/google/protobuf",
Expand Down Expand Up @@ -671,7 +677,6 @@ internal_py_proto_library(
testonly = 1,
srcs = [":generated_protos"],
default_runtime = "",
protoc = ":protoc",
deps = ["//python:python_common_test_protos"],
)

Expand Down
8 changes: 4 additions & 4 deletions MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ register_toolchains(
)

# Define toolchains that use pre-built protoc binaries.
prebuilt_protoc = use_extension("//bazel/private:prebuilt_protoc_extension.bzl", "protoc")
prebuilt_protoc = use_extension("//bazel/private/oss/toolchains/prebuilt:protoc_extension.bzl", "protoc")
use_repo(
prebuilt_protoc,
"prebuilt_protoc.linux_aarch_64",
Expand All @@ -135,12 +135,12 @@ use_repo(
)

# However this registration only matters if the config_setting for prefer_prebuilt_protoc is true,
# using --@protobuf//bazel/toolchains:prefer_prebuilt_protoc
register_toolchains("//bazel/private/toolchains/prebuilt:all")
# using --@protobuf//bazel/flags:prefer_prebuilt_protoc
register_toolchains("//bazel/private/oss/toolchains/prebuilt:all")

# From-source protobuf toolchains
# Fallback if nothing is already registered
register_toolchains("//bazel/private/toolchains:all")
register_toolchains("//bazel/private/oss/toolchains:all")

SUPPORTED_PYTHON_VERSIONS = [
"3.10",
Expand Down
57 changes: 32 additions & 25 deletions bazel/BUILD.bazel → bazel/BUILD
Original file line number Diff line number Diff line change
@@ -1,36 +1,32 @@
# Copyright (c) 2009-2021, Google LLC
# All rights reserved.
#
# Use of this source code is governed by a BSD-style
# license that can be found in the LICENSE file or at
# https://developers.google.com/open-source/licenses/bsd

load("@bazel_skylib//:bzl_library.bzl", "bzl_library")

licenses(["notice"])
package(default_applicable_licenses = ["//:license"])

bzl_library(
name = "proto_library_bzl",
srcs = ["proto_library.bzl"],
visibility = ["//visibility:public"],
deps = [
"//bazel/private:proto_library_rule_bzl",
"@proto_bazel_features//:features",
],
)

bzl_library(
name = "cc_proto_library_bzl",
srcs = ["cc_proto_library.bzl"],
visibility = ["//visibility:public"],
deps = ["//bazel/private:bazel_cc_proto_library_bzl"],
deps = [
"//bazel/private/oss:cc_proto_library_bzl",
],
)

bzl_library(
name = "java_proto_library_bzl",
srcs = ["java_proto_library.bzl"],
visibility = ["//visibility:public"],
deps = ["//bazel/private:bazel_java_proto_library_rule_bzl"],
deps = [
"//bazel/private:java_proto_library_bzl",
],
)

bzl_library(
Expand All @@ -45,23 +41,24 @@ bzl_library(
srcs = ["py_proto_library.bzl"],
visibility = ["//visibility:public"],
deps = [
"//bazel/common:proto_common_bzl",
"//bazel/common:proto_info_bzl",
"//bazel/private:toolchain_helpers_bzl",
"@rules_python//python:py_info_bzl",
"//bazel/private/oss:py_proto_library_bzl",
],
)

bzl_library(
name = "upb_proto_library_bzl",
srcs = [
"upb_c_proto_library.bzl",
"upb_minitable_proto_library.bzl",
"upb_proto_library.bzl",
"upb_proto_reflection_library.bzl",
],
name = "upb_c_proto_library_bzl",
srcs = ["upb_c_proto_library.bzl"],
deprecation = "Use upb/bazel:upb_proto_library_bzl instead",
visibility = ["//visibility:public"],
deps = ["//bazel/private:upb_proto_library_internal_bzl"],
deps = ["//upb/bazel:upb_proto_library_bzl"],
)

bzl_library(
name = "upb_proto_reflection_library_bzl",
srcs = ["upb_proto_reflection_library.bzl"],
deprecation = "Use upb/bazel:upb_proto_library_bzl instead",
visibility = ["//visibility:public"],
deps = ["//upb/bazel:upb_proto_library_bzl"],
)

bzl_library(
Expand All @@ -75,13 +72,23 @@ filegroup(
name = "for_bazel_tests",
testonly = True,
srcs = [
"BUILD.bazel",
"BUILD",
":cc_proto_library_bzl",
":java_lite_proto_library_bzl",
":proto_library_bzl",
":py_proto_library_bzl",
":upb_c_proto_library_bzl",
":upb_proto_reflection_library_bzl",
"//bazel/common:for_bazel_tests",
"//bazel/flags:for_bazel_tests",
"//bazel/toolchains:for_bazel_tests",
],
visibility = ["//bazel/private:__pkg__"],
visibility = [
"//bazel/private:__pkg__",
],
)

alias(
name = "prefer_prebuilt_protoc",
actual = "//bazel/flags:prefer_prebuilt_protoc",
)
9 changes: 8 additions & 1 deletion bazel/cc_proto_library.bzl
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
# Copyright (c) 2009-2024, Google LLC
# All rights reserved.
#
# Use of this source code is governed by a BSD-style
# license that can be found in the LICENSE file or at
# https://developers.google.com/open-source/licenses/bsd

"""cc_proto_library rule"""

load("//bazel/private:bazel_cc_proto_library.bzl", _cc_proto_library = "cc_proto_library") # buildifier: disable=bzl-visibility
load("//bazel/private/oss:cc_proto_library.bzl", _cc_proto_library = "cc_proto_library")

def cc_proto_library(**kwattrs):
# Only use Starlark rules when they are removed from Bazel
Expand Down
105 changes: 105 additions & 0 deletions bazel/flags/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
load("@bazel_skylib//rules:common_settings.bzl", "bool_flag", "string_flag", "string_list_flag")

package(
default_applicable_licenses = ["//:license"],
default_visibility = ["//visibility:public"],
)

bzl_library(
name = "flags_bzl",
srcs = ["flags.bzl"],
deps = [
"@bazel_skylib//rules:common_settings",
],
)

filegroup(
name = "for_bazel_tests",
testonly = True,
srcs = [
"BUILD",
"flags.bzl",
"//bazel/flags/cc:BUILD",
"//bazel/flags/java:BUILD",
],
)

bool_flag(
name = "experimental_proto_descriptor_sets_include_source_info",
build_setting_default = False,
scope = "universal",
)

label_flag(
name = "proto_compiler",
build_setting_default = "@bazel_tools//tools/proto:protoc",
)

# When set to true, we will use a prebuilt protoc binary from GitHub if it's available.
bool_flag(
name = "prefer_prebuilt_protoc",
# TODO: this should be True after the feature is vetted with some adoption
build_setting_default = False,
visibility = ["//bazel:__pkg__"],
)

config_setting(
name = "prefer_prebuilt_protoc.flag_set",
flag_values = {":prefer_prebuilt_protoc": "true"},
visibility = [
"//bazel/private/oss/toolchains/prebuilt:__pkg__",
# Needed by cc_toolchain to switch between minimal and full protoc
"//:__pkg__",
],
)

# The public API users set to disable the validation action failing.
bool_flag(
name = "allow_nonstandard_protoc",
build_setting_default = False,
)

config_setting(
name = "allow_nonstandard_protoc.flag_set",
flag_values = {":allow_nonstandard_protoc": "true"},
visibility = ["//bazel/private/oss/toolchains/prebuilt:__pkg__"],
)

# TODO: deprecate this flag.
string_flag(
name = "strict_proto_deps",
build_setting_default = "error",
scope = "universal",
values = [
"off",
"OFF",
"warn",
"WARN",
"error",
"ERROR",
"strict",
"STRICT",
"default",
"DEFAULT",
],
)

# TODO: deprecate this flag.
string_flag(
name = "strict_public_imports",
build_setting_default = "off",
scope = "universal",
values = [
"off",
"OFF",
"warn",
"WARN",
"error",
"ERROR",
"strict",
"STRICT",
"default",
"DEFAULT",
],
)
32 changes: 32 additions & 0 deletions bazel/flags/cc/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
load("@bazel_skylib//rules:common_settings.bzl", "bool_flag", "string_list_flag")
load("@rules_shell//shell:sh_binary.bzl", "sh_binary")

package(
default_applicable_licenses = ["//:license"],
default_visibility = ["//visibility:public"],
)

exports_files(["BUILD"])

label_flag(
name = "proto_toolchain_for_cc",
build_setting_default = "@bazel_tools//tools/proto:cc_toolchain",
)

string_list_flag(
name = "protocopt",
build_setting_default = [],
scope = "universal",
)

string_list_flag(
name = "cc_proto_library_header_suffixes",
build_setting_default = [".pb.h"],
scope = "universal",
)

string_list_flag(
name = "cc_proto_library_source_suffixes",
build_setting_default = [".pb.cc"],
scope = "universal",
)
File renamed without changes.
73 changes: 73 additions & 0 deletions bazel/flags/flags.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
"""An indirection layer for referencing flags whether they are native or defined in Starlark."""

load("@bazel_skylib//rules:common_settings.bzl", "BuildSettingInfo")

visibility([
"//third_party/grpc/bazel",
"//bazel/private/...",
"//bazel/flags",
])

# Maps flag names to their native reference
_FLAGS = {
"protocopt": struct(
native = lambda ctx: getattr(ctx.fragments.proto, "experimental_protoc_opts"),
default = [],
),
"experimental_proto_descriptor_sets_include_source_info": struct(
native = lambda ctx: getattr(ctx.attr, "_experimental_proto_descriptor_sets_include_source_info_native")[BuildSettingInfo].value,
default = False,
),
"proto_compiler": struct(native = lambda ctx: getattr(ctx.attr, "_proto_compiler_native")[BuildSettingInfo].value, default = "@bazel_tools//tools/proto:protoc"),
"strict_proto_deps": struct(
native = lambda ctx: getattr(ctx.attr, "_strict_proto_deps_native")[BuildSettingInfo].value,
default = "error",
),
"strict_public_imports": struct(
native = lambda ctx: getattr(ctx.attr, "_strict_public_imports_native")[BuildSettingInfo].value,
default = "off",
),
"cc_proto_library_header_suffixes": struct(
native = lambda ctx: getattr(ctx.fragments.proto, "cc_proto_library_header_suffixes"),
default = [".pb.h"],
),
"cc_proto_library_source_suffixes": struct(
native = lambda ctx: getattr(ctx.fragments.proto, "cc_proto_library_source_suffixes"),
default = [".pb.cc"],
),
"proto_toolchain_for_java": struct(
native = lambda ctx: getattr(ctx.attr, "_aspect_java_proto_toolchain"),
default = "@bazel_tools//tools/proto:java_toolchain",
),
"proto_toolchain_for_javalite": struct(
native = lambda ctx: getattr(ctx.attr, "_aspect_proto_toolchain_for_javalite"),
default = "@bazel_tools//tools/proto:javalite_toolchain",
),
"proto_toolchain_for_cc": struct(
native = lambda ctx: getattr(ctx.attr, "_aspect_cc_proto_toolchain"),
default = "@bazel_tools//tools/proto:cc_toolchain",
),
}

def get_flag_value(ctx, flag_name):
"""Returns the value of the given flag in Starlark if it's set, otherwise reads the Java flag value.

Args:
ctx: The rule context.
flag_name: The name of the flag to get the value for.

Returns:
The value of the flag.
"""

# We probably got here from toolchains.find_toolchain. Leave the attribute alone.
if flag_name not in _FLAGS:
return getattr(ctx.attr, "_" + flag_name)

# Label flags don't have a BuildSettingInfo, just get the value.
if "toolchain" in flag_name and getattr(ctx.attr, "_" + flag_name).label != _FLAGS[flag_name].default:
return getattr(ctx.attr, "_" + flag_name)
elif getattr(ctx.attr, "_" + flag_name)[BuildSettingInfo].value != _FLAGS[flag_name].default:
return getattr(ctx.attr, "_" + flag_name)[BuildSettingInfo].value
else:
return _FLAGS[flag_name].native(ctx)
Loading
Loading