Skip to content

Commit 896e7e1

Browse files
committed
Add load() statements for Python rules in third_party/
This replaces all direct uses of the native Python rules underneath the third_party/ directory with load()s of the internal wrapper macros. These macros are needed for compatibility with `--incompatible_load_python_rules_from_bzl`. Some of the uses are replaced by the file under the tools/ dir, which is already used elsewhere in Bazel. A few uses have to use a new @rules_python repo (see also bazelbuild#9029). Work toward bazelbuild#9006. RELNOTES: None
1 parent 2270c99 commit 896e7e1

File tree

13 files changed

+89
-2
lines changed

13 files changed

+89
-2
lines changed

WORKSPACE

+8
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,14 @@ new_local_repository(
7979
path = "./third_party/protobuf/3.6.1/",
8080
)
8181

82+
# This is a mock version of bazelbuild/rules_python that contains only
83+
# @rules_python//python:defs.bzl. It is used by protobuf.
84+
# TODO(#9029): We could potentially replace this with the real @rules_python.
85+
local_repository(
86+
name = "rules_python",
87+
path = "./third_party/rules_python",
88+
)
89+
8290
local_repository(
8391
name = "googleapis",
8492
path = "./third_party/googleapis/",

third_party/def_parser/BUILD

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# Needed for #9006. Consider replacing label with @rules_python as per #9029 if
2+
# merging from upstream becomes an issue.
3+
load("//tools/python:private/defs.bzl", "py_test")
4+
15
licenses(["notice"]) # 3-clause BSD
26

37
package(

third_party/protobuf/3.6.1/BUILD

+2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ config_setting(
2121

2222
load(":protobuf.bzl", "py_proto_library")
2323
load(":compiler_config_setting.bzl", "create_compiler_config_setting")
24+
# Needed for #9006. Hopefully a future upstream version will include this line.
25+
load("@rules_python//python:defs.bzl", "py_library")
2426

2527
filegroup(
2628
name = "srcs",

third_party/protobuf/3.6.1/protobuf.bzl

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
# Needed for #9006. Hopefully a future upstream version will include this line.
2+
load("@rules_python//python:defs.bzl", "py_library", "py_test")
3+
14
def _GetPath(ctx, path):
25
if ctx.label.workspace_root:
36
return ctx.label.workspace_root + "/" + path
@@ -383,7 +386,7 @@ def py_proto_library(
383386
if default_runtime and not default_runtime in py_libs + deps:
384387
py_libs = py_libs + [default_runtime]
385388

386-
native.py_library(
389+
py_library(
387390
name = name,
388391
srcs = outs + py_extra_srcs,
389392
deps = py_libs + deps,
@@ -406,7 +409,7 @@ def internal_protobuf_py_tests(
406409
"""
407410
for m in modules:
408411
s = "python/google/protobuf/internal/%s.py" % m
409-
native.py_test(
412+
py_test(
410413
name = "py_%s" % m,
411414
srcs = [s],
412415
main = s,

third_party/protobuf/README.md

+5
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@ You will create and merge the following Pull Requests.
4343
copy the `licenses` declaration and the `srcs` filegroup to the corresponding file under
4444
`third_party/protobuf/<new_proto>`.
4545
46+
1. Modify the new protobuf to be compatible with #9006 (until upstream protobuf migrates):
47+
In `third_party/protobuf/<new_proto>/BUILD` and `third_party/protobuf/<new_proto>/protobuf.bzl`,
48+
copy the line that loads from `@rules_python` along with its explanatory comment, from the
49+
corresponding files in the old proto directory. (See also #9019.)
50+
4651
1. In `third_party/protobuf/<new_proto>/BUILD`, in the `srcs` filegroup rule, update the version
4752
number referring to the newly added `srcs` rules.
4853

third_party/py/abseil/BUILD

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# Needed for #9006. Consider replacing label with @rules_python as per #9029 if
2+
# merging from upstream becomes an issue.
3+
load("//tools/python:private/defs.bzl", "py_library")
4+
15
licenses(["notice"])
26

37
filegroup(

third_party/py/concurrent/BUILD

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# Needed for #9006. Consider replacing label with @rules_python as per #9029 if
2+
# merging from upstream becomes an issue.
3+
load("//tools/python:private/defs.bzl", "py_library")
4+
15
licenses(["notice"])
26

37
filegroup(

third_party/py/gflags/BUILD

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# Needed for #9006. Consider replacing label with @rules_python as per #9029 if
2+
# merging from upstream becomes an issue.
3+
load("//tools/python:private/defs.bzl", "py_library")
4+
15
licenses(["notice"])
26

37
filegroup(

third_party/py/mock/BUILD

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# Needed for #9006. Consider replacing label with @rules_python as per #9029 if
2+
# merging from upstream becomes an issue.
3+
load("//tools/python:private/defs.bzl", "py_library")
4+
15
licenses(["notice"])
26

37
filegroup(

third_party/py/six/BUILD

+5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
# Needed for #9006. We can't easily replace this label with a @rules_python
2+
# label (#9029) because this file is used in both @io_bazel and @bazel_tools,
3+
# the latter of which can't reference other repos.
4+
load("//tools/python:private/defs.bzl", "py_library")
5+
16
licenses(["notice"])
27

38
filegroup(

third_party/rules_python/WORKSPACE

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# A stub version of bazelbuild/rules_python that contains only the defs.bzl
2+
# file, which is needed to access native Python rules under
3+
# --incompatible_load_python_rules_from_bzl (#9006).
4+
#
5+
# TODO(#9029): Maybe replace this with the real thing.
6+
7+
workspace(name = "rules_python")

third_party/rules_python/python/BUILD

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# This file intentionally left blank.
+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Copyright 2019 The Bazel Authors. All rights reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
"""This file is selectively copied from //tools/python:private/defs.bzl."""
16+
17+
_MIGRATION_TAG = "__PYTHON_RULES_MIGRATION_DO_NOT_USE_WILL_BREAK__"
18+
19+
def _add_tags(attrs):
20+
if "tags" in attrs and attrs["tags"] != None:
21+
attrs["tags"] += [_MIGRATION_TAG]
22+
else:
23+
attrs["tags"] = [_MIGRATION_TAG]
24+
return attrs
25+
26+
def py_library(**attrs):
27+
native.py_library(**_add_tags(attrs))
28+
29+
def py_binary(**attrs):
30+
native.py_binary(**_add_tags(attrs))
31+
32+
def py_test(**attrs):
33+
native.py_test(**_add_tags(attrs))
34+
35+
def py_runtime(**attrs):
36+
native.py_runtime(**_add_tags(attrs))

0 commit comments

Comments
 (0)