Skip to content

Commit

Permalink
Change rules_proto loads to protobuf
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 681872027
Change-Id: I89725e873bbfe61ab46f6c7483477d4af796db71
  • Loading branch information
comius authored and copybara-github committed Oct 3, 2024
1 parent 74181e7 commit 07af430
Show file tree
Hide file tree
Showing 19 changed files with 78 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -727,9 +727,7 @@ public final class BuildLanguageOptions extends OptionsBase {
documentationCategory = OptionDocumentationCategory.TOOLCHAIN,
effectTags = {OptionEffectTag.LOADING_AND_ANALYSIS},
metadataTags = {OptionMetadataTag.INCOMPATIBLE_CHANGE},
help =
"If true, proto lang rules define toolchains from rules_proto, rules_java, rules_cc"
+ " repositories.")
help = "If true, proto lang rules define toolchains from protobuf repository.")
public boolean incompatibleEnableProtoToolchainResolution;

// Flip when java_single_jar is feature complete
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
load("@protobuf//bazel:proto_library.bzl", "proto_library")
load("@rules_java//java:defs.bzl", "java_proto_library")
load("@rules_proto//proto:defs.bzl", "proto_library")
load("//tools/build_rules:utilities.bzl", "java_library_srcs")

package(default_visibility = ["//src:__pkg__"])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,5 @@
@StarlarkBuiltin(
name = "proto_common",
category = DocCategory.TOP_LEVEL_MODULE,
doc =
"Utilities for protocol buffers. <p>Please consider using"
+ " <code>load(\"@rules_proto//proto:defs.bzl\", \"proto_common\")</code> to load this"
+ " symbol from <a href=\"https://github.com/bazelbuild/rules_proto\">rules_proto</a>."
+ "</p>")
doc = "Private utilities for protocol buffers. Do not use.")
public interface ProtoCommonApi extends StarlarkValue {}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
load("@protobuf//bazel:proto_library.bzl", "proto_library")
load("@rules_java//java:defs.bzl", "java_proto_library")
load("@rules_proto//proto:defs.bzl", "proto_library")
load("//tools/build_rules:utilities.bzl", "java_library_srcs")

package(default_visibility = ["//src:__subpackages__"])
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/google/devtools/build/skyframe/BUILD
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
load("@protobuf//bazel:proto_library.bzl", "proto_library")
load("@rules_java//java:defs.bzl", "java_library")
load("@rules_proto//proto:defs.bzl", "proto_library")
load("//tools/build_rules:utilities.bzl", "java_library_srcs")

# Description:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ def _rule_impl(ctx):
proto_lang_toolchain = rule(
_rule_impl,
doc = """
<p>If using Bazel, please load the rule from <a href="https://github.com/bazelbuild/rules_proto">
https://github.com/bazelbuild/rules_proto</a>.
<p>If using Bazel, please load the rule from <a href="https://github.com/google/protobuf">
https://github.com/google/protobuf</a>.
<p>Specifies how a LANG_proto_library rule (e.g., <code>java_proto_library</code>) should invoke the
proto-compiler.
Expand Down
4 changes: 2 additions & 2 deletions src/main/starlark/builtins_bzl/common/proto/proto_library.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -235,8 +235,8 @@ proto_library = rule(
# TODO(b/311576642): proto_common docs are missing
# TODO(b/311576642): ProtoInfo link doesn't work and docs are missing
doc = """
<p>If using Bazel, please load the rule from <a href="https://github.com/bazelbuild/rules_proto">
https://github.com/bazelbuild/rules_proto</a>.
<p>If using Bazel, please load the rule from <a href="https://github.com/google/protobuf">
https://github.com/google/protobuf</a>.
<p>Use <code>proto_library</code> to define libraries of protocol buffers which
may be used from multiple languages. A <code>proto_library</code> may be listed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Proto Semantics
"""

semantics = struct(
PROTO_TOOLCHAIN = "@rules_proto//proto:toolchain_type",
PROTO_TOOLCHAIN = "@protobuf//bazel/private:proto_toolchain_type",
# This constant is used in ProtoCompileActionBuilder to generate an error message that's
# displayed when a strict proto deps violation occurs.
#
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,7 @@ private static void createNetProto2(MockToolsConfig config) throws IOException {
}

public static void setupWorkspace(MockToolsConfig config) throws IOException {
// TODO - ilist@: Remove after Google proto_library doesn't depend on rules_proto
config.create(
"third_party/bazel_rules/rules_proto/proto/BUILD",
"""
Expand All @@ -367,25 +368,24 @@ public static void setupWorkspace(MockToolsConfig config) throws IOException {
)
""");
config.create(
"third_party/bazel_rules/rules_proto/proto/defs.bzl",
"protobuf_workspace/bazel/private/BUILD",
"""
load(":proto_lang_toolchain.bzl", _proto_lang_toolchain = "proto_lang_toolchain")
def _add_tags(kargs):
if "tags" in kargs:
kargs["tags"] += ["__PROTO_RULES_MIGRATION_DO_NOT_USE_WILL_BREAK__"]
else:
kargs["tags"] = ["__PROTO_RULES_MIGRATION_DO_NOT_USE_WILL_BREAK__"]
return kargs
licenses(["notice"])
toolchain_type(
name = "proto_toolchain_type",
visibility = ["//visibility:public"],
)
""");
config.create("protobuf_workspace/bazel/BUILD");
config.create(
"protobuf_workspace/bazel/proto_library.bzl",
"""
def proto_library(**kargs):
native.proto_library(**_add_tags(kargs))
def proto_lang_toolchain(**kargs):
_proto_lang_toolchain(**_add_tags(kargs))
native.proto_library(**kargs)
""");
config.create(
"third_party/bazel_rules/rules_proto/proto/proto_toolchain.bzl",
"protobuf_workspace/bazel/toolchains/proto_toolchain.bzl",
"load(':proto_toolchain_rule.bzl', _proto_toolchain_rule = 'proto_toolchain')",
"def proto_toolchain(*, name, proto_compiler, exec_compatible_with = []):",
" _proto_toolchain_rule(name = name, proto_compiler = proto_compiler)",
Expand All @@ -396,8 +396,9 @@ def proto_lang_toolchain(**kargs):
" target_compatible_with = [],",
" toolchain = name,",
" )");
config.create("protobuf_workspace/bazel/toolchains/BUILD");
config.create(
"third_party/bazel_rules/rules_proto/proto/proto_toolchain_rule.bzl",
"protobuf_workspace/bazel/toolchains/proto_toolchain_rule.bzl",
"""
ProtoLangToolchainInfo = proto_common_do_not_use.ProtoLangToolchainInfo
Expand All @@ -419,7 +420,7 @@ def _impl(ctx):
mnemonic = ctx.attr.mnemonic,
allowlist_different_package = None,
toolchain_type =
"//third_party/bazel_rules/rules_proto/proto:toolchain_type",
"//protobuf/bazel/private:proto_toolchain_type",
),
),
]
Expand All @@ -446,7 +447,7 @@ def _impl(ctx):
)
""");
config.create(
"third_party/bazel_rules/rules_proto/proto/proto_lang_toolchain.bzl",
"protobuf_workspace/bazel/toolchains/proto_lang_toolchain.bzl",
"""
def proto_lang_toolchain(
*,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@
public class TestConstants {

public static final String LOAD_PROTO_LIBRARY =
"load('@rules_proto//proto:defs.bzl', 'proto_library')";
public static final String PROTO_TOOLCHAIN = "@rules_proto//proto:toolchain_type";
"load('@protobuf//bazel:proto_library.bzl', 'proto_library')";
public static final String PROTO_TOOLCHAIN = "@protobuf//bazel/private:proto_toolchain_type";
public static final String LOAD_PROTO_TOOLCHAIN =
"load('@rules_proto//proto:proto_toolchain.bzl', 'proto_toolchain')";
"load('@protobuf//bazel/toolchains:proto_toolchain.bzl', 'proto_toolchain')";
public static final String LOAD_PROTO_LANG_TOOLCHAIN =
"load('@rules_proto//proto:defs.bzl', 'proto_lang_toolchain')";
"load('@protobuf//bazel/toolchains:proto_lang_toolchain.bzl', 'proto_lang_toolchain')";

private TestConstants() {
}
Expand Down
1 change: 1 addition & 0 deletions src/test/shell/bazel/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ filegroup(
":test-deps-wo-bazel",
"//src:bazel",
"//src/test/shell:bin/bazel",
"//third_party/protobuf:remove_rules_rust.patch",
],
visibility = [
"//src/main/starlark/tests/builtins_bzl:__subpackages__",
Expand Down
2 changes: 2 additions & 0 deletions src/test/shell/bazel/bazel_java17_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ override_java_tools "${RULES_JAVA_REPO_NAME}" "${JAVA_TOOLS_ZIP}" "${JAVA_TOOLS_
add_to_bazelrc "build --java_runtime_version=11"
add_to_bazelrc "build --tool_java_runtime_version=11"

add_protobuf "MODULE.bazel"

# Java source files version shall match --java_language_version_flag version.
function test_java17_text_block() {
mkdir -p java/main
Expand Down
1 change: 1 addition & 0 deletions src/test/shell/bazel/bazel_java_tools_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ EOF
# Dependencies of java_tools
add_platforms "MODULE.bazel"
add_rules_cc "MODULE.bazel"
# TODO - ilist@: point java_tools to protobuf; requires coordinated release of rules_java and java_tools
add_rules_proto "MODULE.bazel"
add_rules_license "MODULE.bazel"
}
Expand Down
31 changes: 15 additions & 16 deletions src/test/shell/bazel/bazel_proto_library_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ source "${CURRENT_DIR}/../integration_test_setup.sh" \
|| { echo "integration_test_setup.sh not found!" >&2; exit 1; }

function set_up() {
add_rules_proto "MODULE.bazel"
add_protobuf "MODULE.bazel"
}

# Creates directories and files with the structure:
Expand Down Expand Up @@ -53,7 +53,7 @@ function write_setup() {
extra_attribute=$2
include_macro=$3
if [ "${include_macro}" -eq "" ]; then
include_macro="load('@rules_proto//proto:defs.bzl', 'proto_library')"
include_macro="load('@protobuf//bazel:proto_library.bzl', 'proto_library')"
fi

cat > x/person/BUILD << EOF
Expand Down Expand Up @@ -134,7 +134,7 @@ function write_regression_setup() {
touch proto_library/BUILD

cat > proto_library/src/BUILD << EOF
load("@rules_proto//proto:defs.bzl", "proto_library")
load("@protobuf//bazel:proto_library.bzl", "proto_library")
proto_library(
name = "all",
srcs = glob(["*.proto"]),
Expand Down Expand Up @@ -203,7 +203,7 @@ function write_workspaces_setup() {
touch a/b/BUILD
cat > a/b/src/BUILD <<EOF
package(default_visibility = ["//visibility:public"])
load("@rules_proto//proto:defs.bzl", "proto_library")
load("@protobuf//bazel:proto_library.bzl", "proto_library")
proto_library(
name = "all_protos",
srcs = glob(["*.proto"]),
Expand Down Expand Up @@ -243,7 +243,7 @@ EOF

cat > c/d/src/BUILD <<EOF
package(default_visibility = ["//visibility:public"])
load("@rules_proto//proto:defs.bzl", "proto_library")
load("@protobuf//bazel:proto_library.bzl", "proto_library")
proto_library(
name = "all_protos",
srcs = glob(["*.proto"]),
Expand Down Expand Up @@ -282,7 +282,7 @@ export_files(["proto_library_macro.bzl])
EOF

cat > macros/proto_library_macro.bzl << EOF
load("@rules_proto//proto:defs.bzl", "proto_library")
load("@protobuf//bazel:proto_library.bzl", "proto_library")
def proto_library_macro(name, srcs, deps = []):
proto_library(
name = name,
Expand Down Expand Up @@ -333,7 +333,6 @@ EOF
############# TESTS #############

function test_legacy_proto_library_include_well_known_protos() {
add_protobuf "MODULE.bazel"
mkdir -p a
cat > a/BUILD <<EOF
proto_library(
Expand Down Expand Up @@ -491,7 +490,7 @@ EOF
function test_cc_proto_library() {
mkdir -p a
cat > a/BUILD <<EOF
load("@rules_proto//proto:defs.bzl", "proto_library")
load("@protobuf//bazel:proto_library.bzl", "proto_library")
proto_library(name='p', srcs=['p.proto'])
cc_proto_library(name='cp', deps=[':p'])
cc_library(name='c', srcs=['c.cc'], deps=[':cp'])
Expand Down Expand Up @@ -519,7 +518,7 @@ EOF
function test_cc_proto_library_with_toolchain_resolution() {
mkdir -p a
cat > a/BUILD <<EOF
load("@rules_proto//proto:defs.bzl", "proto_library")
load("@protobuf//bazel:proto_library.bzl", "proto_library")
proto_library(name='p', srcs=['p.proto'])
cc_proto_library(name='cp', deps=[':p'])
cc_library(name='c', srcs=['c.cc'], deps=[':cp'])
Expand Down Expand Up @@ -547,7 +546,7 @@ EOF
function test_cc_proto_library_import_prefix_stripping() {
mkdir -p a/dir
cat > a/BUILD <<EOF
load("@rules_proto//proto:defs.bzl", "proto_library")
load("@protobuf//bazel:proto_library.bzl", "proto_library")
proto_library(name='p', srcs=['dir/p.proto'], strip_import_prefix='/a')
cc_proto_library(name='cp', deps=[':p'])
cc_library(name='c', srcs=['c.cc'], deps=[':cp'])
Expand Down Expand Up @@ -586,7 +585,7 @@ EOF

mkdir -p e/f/bad
cat > e/f/BUILD <<EOF
load("@rules_proto//proto:defs.bzl", "proto_library")
load("@protobuf//bazel:proto_library.bzl", "proto_library")
proto_library(
name = "f",
strip_import_prefix = "bad",
Expand All @@ -607,7 +606,7 @@ EOF

mkdir -p g/bad
cat > g/BUILD << EOF
load("@rules_proto//proto:defs.bzl", "proto_library")
load("@protobuf//bazel:proto_library.bzl", "proto_library")
proto_library(
name = 'g',
strip_import_prefix = "/g/bad",
Expand All @@ -628,7 +627,7 @@ EOF

mkdir -p h
cat > h/BUILD <<EOF
load("@rules_proto//proto:defs.bzl", "proto_library")
load("@protobuf//bazel:proto_library.bzl", "proto_library")
proto_library(
name = "h",
srcs = ["h.proto"],
Expand Down Expand Up @@ -685,7 +684,7 @@ EOF

mkdir -p e/f/good
cat > e/f/BUILD <<EOF
load("@rules_proto//proto:defs.bzl", "proto_library")
load("@protobuf//bazel:proto_library.bzl", "proto_library")
proto_library(
name = "f",
srcs = ["good/f.proto"],
Expand Down Expand Up @@ -722,7 +721,7 @@ EOF

mkdir -p g/good
cat > g/BUILD << EOF
load("@rules_proto//proto:defs.bzl", "proto_library")
load("@protobuf//bazel:proto_library.bzl", "proto_library")
proto_library(
name = 'g',
srcs = ['good/g.proto'],
Expand All @@ -741,7 +740,7 @@ EOF

mkdir -p h
cat > h/BUILD <<EOF
load("@rules_proto//proto:defs.bzl", "proto_library")
load("@protobuf//bazel:proto_library.bzl", "proto_library")
proto_library(
name = "h",
srcs = ["h.proto"],
Expand Down
17 changes: 17 additions & 0 deletions src/test/shell/testenv.sh
Original file line number Diff line number Diff line change
Expand Up @@ -583,6 +583,7 @@ function add_rules_python() {
add_bazel_dep "rules_python" "$1"
}

# Needed only for java_tools
function add_rules_proto() {
add_bazel_dep "rules_proto" "$1"
}
Expand All @@ -593,6 +594,22 @@ function add_rules_license() {

function add_protobuf() {
add_bazel_dep "protobuf" "$1"
mkdir -p third_party/protobuf
touch third_party/protobuf/BUILD
cp "$(rlocation io_bazel/third_party/protobuf/remove_rules_rust.patch)" third_party/protobuf/remove_rules_rust.patch
cat >> "$1" <<EOF
archive_override(
module_name = "protobuf",
integrity = "sha256-zF1Z3SMnHqcP1QKIeAoGGZDEARNXRWRgZi70eKldVlc=",
patch_strip = 1,
# Temporarily patch out rules_rust stuff from protobuf. Not just because we don't need it,
# but also because it introduces huge dependency bloat: rules_rust -> aspect_rules_js ->
# aspect_rules_lint -> rules_buf.
patches = ["//third_party/protobuf:remove_rules_rust.patch"],
strip_prefix = "protobuf-3b62052186d39775090fb074adcba078ea622f54",
urls = ["https://github.com/protocolbuffers/protobuf/archive/3b62052186d39775090fb074adcba078ea622f54.zip"],
)
EOF
}

function add_rules_testing() {
Expand Down
Loading

0 comments on commit 07af430

Please sign in to comment.