Skip to content

Commit

Permalink
Use real rules_python
Browse files Browse the repository at this point in the history
Initial motivation was to use py_proto_library from rules_python, but then a yak came along.

Fixes: bazelbuild#9029

Closes bazelbuild#17545.

PiperOrigin-RevId: 513834100
Change-Id: I11a99381e1169a9fb7a7a3eaa733ddd348ebac2b
  • Loading branch information
comius authored and fweikert committed May 25, 2023
1 parent bd01094 commit d4deb10
Show file tree
Hide file tree
Showing 35 changed files with 78 additions and 72 deletions.
2 changes: 1 addition & 1 deletion BUILD
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Bazel - Google's Build System

load("//tools/distributions:distribution_rules.bzl", "distrib_jar_filegroup")
load("//tools/python:private/defs.bzl", "py_binary")
load("@rules_python//python:defs.bzl", "py_binary")
load("@rules_license//rules:license.bzl", "license")
load("@rules_pkg//pkg:tar.bzl", "pkg_tar")

Expand Down
4 changes: 4 additions & 0 deletions MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ bazel_dep(name = "rules_cc", version = "0.0.6")
bazel_dep(name = "rules_java", version = "5.4.1")
bazel_dep(name = "rules_proto", version = "5.3.0-21.7")
bazel_dep(name = "rules_jvm_external", version = "4.5")
bazel_dep(name = "rules_python", version = "0.19.0")

single_version_override(
module_name = "rules_jvm_external",
patch_strip = 1,
Expand Down Expand Up @@ -50,3 +52,5 @@ local_path_override(
module_name = "googleapis",
path = "./third_party/googleapis",
)

register_toolchains("@bazel_tools//tools/python:autodetecting_toolchain")
24 changes: 12 additions & 12 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -96,16 +96,6 @@ dist_http_archive(
patch_cmds_win = EXPORT_WORKSPACE_IN_BUILD_BAZEL_FILE_WIN,
)

# This is a mock version of bazelbuild/rules_python that contains only
# @rules_python//python:defs.bzl. It is used by protobuf.
# TODO(#9029): We could potentially replace this with the real @rules_python.
new_local_repository(
name = "rules_python",
build_file = "//third_party/rules_python:BUILD",
path = "./third_party/rules_python",
workspace_file = "//third_party/rules_python:rules_python.WORKSPACE",
)

local_repository(
name = "googleapis",
path = "./third_party/googleapis/",
Expand Down Expand Up @@ -214,6 +204,16 @@ dist_http_archive(
patch_cmds_win = EXPORT_WORKSPACE_IN_BUILD_FILE_WIN,
)

dist_http_archive(
name = "rules_python",
patch_cmds = EXPORT_WORKSPACE_IN_BUILD_BAZEL_FILE,
patch_cmds_win = EXPORT_WORKSPACE_IN_BUILD_BAZEL_FILE_WIN,
)

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

py_repositories()

dist_http_archive(
name = "zstd-jni",
build_file = "//third_party:zstd-jni/zstd-jni.BUILD",
Expand Down Expand Up @@ -722,11 +722,11 @@ maven_install(
"com.google.protobuf:protobuf-java",
"com.google.protobuf:protobuf-javalite",
],
fail_if_repin_required = True,
maven_install_json = "//:maven_install.json",
repositories = [
"https://repo1.maven.org/maven2",
],
maven_install_json = "//:maven_install.json",
fail_if_repin_required = True,
strict_visibility = True,
)

Expand Down
7 changes: 7 additions & 0 deletions distdir_deps.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,13 @@ DIST_DEPS = {
],
"package_version": "4.5",
},
"rules_python": {
"sha256": "ffc7b877c95413c82bfd5482c017edcf759a6250d8b24e82f41f3c8b8d9e287e",
"strip_prefix": "rules_python-0.19.0",
"urls": ["https://github.com/bazelbuild/rules_python/releases/download/0.19.0/rules_python-0.19.0.tar.gz"],
"archive": "rules_python-0.19.0.tar.gz",
"used_in": ["additional_distfiles", "test_WORKSPACE_files"],
},
"desugar_jdk_libs": {
# Commit 5847d6a06302136d95a14b4cbd4b55a9c9f1436e of 2021-03-10
"archive": "5847d6a06302136d95a14b4cbd4b55a9c9f1436e.zip",
Expand Down
2 changes: 1 addition & 1 deletion scripts/docs/BUILD
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
load("//tools/python:private/defs.bzl", "py_binary", "py_library")
load("@rules_python//python:defs.bzl", "py_binary", "py_library")

BUILD_SCM_REV_CMD = "$$(grep BUILD_SCM_REVISION bazel-out/volatile-status.txt | sed 's/^BUILD_SCM_REVISION //')"

Expand Down
2 changes: 1 addition & 1 deletion src/BUILD
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Packaging

load("//tools/python:private/defs.bzl", "py_binary", "py_library")
load("@rules_python//python:defs.bzl", "py_binary", "py_library")
load(":embedded_tools.bzl", "srcsfile")
load(":rule_size_test.bzl", "rule_size_test")
load("//src:release_archive.bzl", "release_archive")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ gen_workspace_stanza(
"rules_cc",
"rules_java",
"rules_proto",
"rules_python",
"remotejdk11_linux",
"remotejdk11_linux_aarch64",
"remotejdk11_linux_ppc64le",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -284,4 +284,10 @@ maybe(
{rules_proto}
)

# Needed only because of proguard_allowlister in java_tools.
maybe(
http_archive,
{rules_python}
)

register_toolchains("@bazel_tools//tools/jdk:all")
11 changes: 3 additions & 8 deletions src/main/protobuf/BUILD
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
load("@com_google_protobuf//:protobuf.bzl", "py_proto_library")
load("@rules_python//python:proto.bzl", "py_proto_library")
load("@rules_proto//proto:defs.bzl", "proto_library")
load("//third_party/grpc:build_defs.bzl", "java_grpc_library")
load("//third_party/grpc/bazel:cc_grpc_library.bzl", "cc_grpc_library")
Expand Down Expand Up @@ -47,10 +47,7 @@ proto_library(

py_proto_library(
name = "analysis_v2_py_proto",
srcs = ["analysis_v2.proto"],
default_runtime = "//third_party/protobuf:protobuf_python",
protoc = "//third_party/protobuf:protoc",
deps = [":build_pb_py"],
deps = [":analysis_v2_proto"],
)

java_proto_library(
Expand Down Expand Up @@ -187,9 +184,7 @@ cc_grpc_library(

py_proto_library(
name = "build_pb_py",
srcs = ["build.proto"],
default_runtime = "//third_party/protobuf:protobuf_python",
protoc = "//third_party/protobuf:protoc",
deps = [":build_proto"],
)

java_library_srcs(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,12 @@
public class DefaultToolsSetup implements ToolsSetup {

private static ImmutableList<String> repos =
ImmutableList.<String>builder().add("rules_cc").add("rules_proto").add("rules_java").build();
ImmutableList.<String>builder()
.add("rules_cc")
.add("rules_proto")
.add("rules_java")
.add("rules_python")
.build();

private ImmutableList<String> getRepositoryOverrides() {
String sharedRepoHome = System.getenv("TEST_REPOSITORY_HOME");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ gen_workspace_stanza(
repos = [
"rules_cc",
"rules_proto",
"rules_python",
],
visibility = ["//visibility:private"],
)
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ public void setup(MockToolsConfig config) throws IOException {
addTool(config, "tools/python/srcs_version.bzl");
addTool(config, "tools/python/toolchain.bzl");
addTool(config, "tools/python/utils.bzl");
addTool(config, "tools/python/private/defs.bzl");
addTool(config, "tools/python/python_bootstrap_template.txt");

config.create(
Expand Down
3 changes: 2 additions & 1 deletion src/test/py/bazel/BUILD
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
load("//:distdir_deps.bzl", "gen_workspace_stanza")
load("//tools/python:private/defs.bzl", "py_library", "py_test")
load("@rules_python//python:defs.bzl", "py_library", "py_test")

package(default_visibility = ["//visibility:private"])

Expand Down Expand Up @@ -43,6 +43,7 @@ gen_workspace_stanza(
repos = [
"rules_license",
"rules_cc",
"rules_python",
],
)

Expand Down
1 change: 1 addition & 0 deletions src/test/py/bazel/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ class TestBase(unittest.TestCase):
'rules_cc',
'rules_java',
'rules_proto',
'rules_python',
'remotejdk11_linux_for_testing',
'remotejdk11_linux_aarch64_for_testing',
'remotejdk11_linux_ppc64le_for_testing',
Expand Down
3 changes: 2 additions & 1 deletion src/test/shell/BUILD
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
load("//:distdir_deps.bzl", "gen_workspace_stanza")
load("//tools/python:private/defs.bzl", "py_test")
load("@rules_python//python:defs.bzl", "py_test")

package(default_visibility = ["//visibility:private"])

Expand Down Expand Up @@ -31,6 +31,7 @@ gen_workspace_stanza(
"rules_license",
"rules_pkg",
"rules_proto",
"rules_python",
],
template = "testenv.sh.tmpl",
visibility = ["//visibility:public"],
Expand Down
15 changes: 1 addition & 14 deletions src/test/shell/bazel/bazel_proto_library_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -48,19 +48,6 @@ function write_workspace() {
mkdir -p "$workspace"
fi

cat >> "$workspace"WORKSPACE << EOF
# TODO(#9029): May require some adjustment if/when we depend on the real
# @rules_python in the real source tree, since this third_party/ package won't
# be available.
new_local_repository(
name = "rules_python",
path = "$(dirname $(rlocation io_bazel/third_party/rules_python/rules_python.WORKSPACE))",
build_file = "$(rlocation io_bazel/third_party/rules_python/BUILD)",
workspace_file = "$(rlocation io_bazel/third_party/rules_python/rules_python.WORKSPACE)",
)
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
EOF
cat $(rlocation io_bazel/src/tests/shell/bazel/rules_proto_stanza.txt) >> "$workspace"WORKSPACE
cat >> "$workspace"WORKSPACE << EOF
load("@rules_proto//proto:repositories.bzl", "rules_proto_dependencies", "rules_proto_toolchains")
Expand All @@ -70,7 +57,7 @@ rules_proto_toolchains()
# @com_google_protobuf//:protoc depends on @io_bazel//third_party/zlib.
new_local_repository(
name = "io_bazel",
path = "$(dirname $(rlocation io_bazel/third_party/rules_python/rules_python.WORKSPACE))/../..",
path = "$(dirname $(rlocation io_bazel/third_party/zlib))/..",
build_file_content = "# Intentionally left empty.",
workspace_file_content = "workspace(name = 'io_bazel')",
)
Expand Down
1 change: 1 addition & 0 deletions src/test/shell/integration/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ gen_workspace_stanza(
out = "rules_proto_stanza.txt",
repos = [
"rules_proto",
"rules_python",
],
)

Expand Down
14 changes: 1 addition & 13 deletions src/test/shell/integration/modify_execution_info_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -151,18 +151,6 @@ Genrule=+requires-a,CppCompile=+requires-b,CppCompile=+requires-c \

function test_modify_execution_info_various_types() {
if [[ "$PRODUCT_NAME" = "bazel" ]]; then
# proto_library requires this external workspace.
cat >> WORKSPACE << EOF
# TODO(#9029): May require some adjustment if/when we depend on the real
# @rules_python in the real source tree, since this third_party/ package won't
# be available.
new_local_repository(
name = "rules_python",
path = "$(dirname $(rlocation io_bazel/third_party/rules_python/rules_python.WORKSPACE))",
build_file = "$(rlocation io_bazel/third_party/rules_python/BUILD)",
workspace_file = "$(rlocation io_bazel/third_party/rules_python/rules_python.WORKSPACE)",
)
EOF
cat "$(rlocation "io_bazel/src/test/shell/integration/rules_proto_stanza.txt")" >>WORKSPACE
cat >> WORKSPACE << EOF
load("@rules_proto//proto:repositories.bzl", "rules_proto_dependencies", "rules_proto_toolchains")
Expand All @@ -172,7 +160,7 @@ rules_proto_toolchains()
# @com_google_protobuf//:protoc depends on @io_bazel//third_party/zlib.
new_local_repository(
name = "io_bazel",
path = "$(dirname $(rlocation io_bazel/third_party/rules_python/rules_python.WORKSPACE))/../..",
path = "$(dirname $(rlocation io_bazel/third_party/zlib))/..",
build_file_content = "# Intentionally left empty.",
workspace_file_content = "workspace(name = 'io_bazel')",
)
Expand Down
9 changes: 9 additions & 0 deletions src/test/shell/testenv.sh.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -561,6 +561,14 @@ load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
EOF
}

function add_rules_python_to_workspace() {
cat >> "$1"<<EOF
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
{rules_python}
EOF
}

function create_workspace_with_default_repos() {
write_workspace_file "${1:-WORKSPACE}" "${2:-main}"
echo "$1"
Expand All @@ -576,6 +584,7 @@ EOF
add_rules_license_to_workspace "WORKSPACE"
add_rules_pkg_to_workspace "WORKSPACE"
add_rules_proto_to_workspace "WORKSPACE"
add_rules_python_to_workspace "WORKSPACE"

maybe_setup_python_windows_workspace
}
Expand Down
2 changes: 1 addition & 1 deletion tools/android/BUILD
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
load("//tools/python:private/defs.bzl", "py_binary", "py_library", "py_test")
load("@rules_python//python:defs.bzl", "py_binary", "py_library", "py_test")

package(default_visibility = ["//tools:__pkg__"])

Expand Down
2 changes: 1 addition & 1 deletion tools/android/BUILD.tools
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
load("//tools/python:private/defs.bzl", "py_binary", "py_library")
load("@rules_python//python:defs.bzl", "py_binary", "py_library")
load(":defs.bzl", "run_ijar", "run_singlejar")

package(default_visibility = ["//visibility:public"])
Expand Down
1 change: 0 additions & 1 deletion tools/aquery_differ/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
# limitations under the License.

load("//tools/python:private/py_test_alias.bzl", "py_test_alias")
load("//tools/python:private/defs.bzl", "py_binary", "py_library")

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

Expand Down
2 changes: 1 addition & 1 deletion tools/build_defs/hash/BUILD
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
load("//tools/python:private/defs.bzl", "py_binary")
load("@rules_python//python:defs.bzl", "py_binary")

licenses(["notice"]) # Apache 2.0

Expand Down
2 changes: 1 addition & 1 deletion tools/build_defs/pkg/BUILD
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-
load("//tools/build_defs/pkg:pkg.bzl", "pkg_tar")
load("//tools/python:private/defs.bzl", "py_binary", "py_library", "py_test")
load("@rules_python//python:defs.bzl", "py_binary", "py_library", "py_test")

licenses(["notice"])

Expand Down
2 changes: 1 addition & 1 deletion tools/build_rules/BUILD
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
load("//tools/python:private/defs.bzl", "py_test")
load("@rules_python//python:defs.bzl", "py_test")

licenses(["notice"]) # Apache 2.0

Expand Down
2 changes: 1 addition & 1 deletion tools/ctexplain/BUILD
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Description:
# Tool for measuring how configuration transitions affect build graph size.
load("//tools/python:private/defs.bzl", "py_binary", "py_library")
load("@rules_python//python:defs.bzl", "py_binary", "py_library")

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

Expand Down
2 changes: 1 addition & 1 deletion tools/jdk/BUILD.tools
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ load(
"java_runtime_version_alias",
"java_toolchain_alias",
)
load("//tools/python:private/defs.bzl", "py_binary", "py_test")
load("@rules_python//python:defs.bzl", "py_binary", "py_test")

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

Expand Down
1 change: 0 additions & 1 deletion tools/mini_tar/BUILD
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# -*- coding: utf-8 -*-
load("//tools/mini_tar:tar.bzl", "mini_tar")
load("//tools/python:private/defs.bzl", "py_binary", "py_test")

licenses(["notice"])

Expand Down
2 changes: 1 addition & 1 deletion tools/python/BUILD
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
load("//tools/python:private/defs.bzl", "py_test")
load("@rules_python//python:defs.bzl", "py_test")
load(":utils.bzl", "expand_pyversion_template")

package(default_visibility = ["//visibility:public"])
Expand Down
3 changes: 1 addition & 2 deletions tools/python/BUILD.tools
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ exports_files([
"srcs_version.bzl",
"toolchain.bzl",
"utils.bzl",
"private/defs.bzl",
"private/py_test_alias.bzl",
# write_file.bzl fortunately doesn't need to be exposed because it's only
# used in this BUILD file.
Expand All @@ -38,7 +37,7 @@ exports_files([
filegroup(
name = "bzl_srcs",
srcs = glob(["*.bzl"]) + [
"private/defs.bzl",
"private/defs.bzl", # TODO(#9029): remove after the uses in bazel/third_party are removed
"private/py_test_alias.bzl",
],
visibility = ["//tools:__pkg__"],
Expand Down
2 changes: 2 additions & 0 deletions tools/python/private/defs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ WORKSPACE, and rely on those repos using the proper @rules_python-qualified
label. (#9029 tracks possibly replacing the mock with the real thing.)
"""

# TODO(#9029): the file can be deleted after the uses in bazel/third_party are removed

_MIGRATION_TAG = "__PYTHON_RULES_MIGRATION_DO_NOT_USE_WILL_BREAK__"

def _add_tags(attrs):
Expand Down
Loading

0 comments on commit d4deb10

Please sign in to comment.