Skip to content

Commit c140e54

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`. Work toward bazelbuild#9006. RELNOTES: None
1 parent 299e3f0 commit c140e54

File tree

10 files changed

+84
-2
lines changed

10 files changed

+84
-2
lines changed

WORKSPACE

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

82+
# Used by protobuf.
83+
# TODO(#9029): This should be replaced by the real bazelbuild/rules_python.
84+
# Also, all non-@bazel_tools uses of Python rules in the Bazel source tree
85+
# should be updated to reference @rules_python//python:defs.bzl.
86+
local_repository(
87+
name = "rules_python",
88+
path = "./third_party/rules_python",
89+
)
90+
8291
local_repository(
8392
name = "googleapis",
8493
path = "./third_party/googleapis/",

third_party/def_parser/BUILD

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
load("//tools/python:private/defs.bzl", "py_test")
2+
13
licenses(["notice"]) # 3-clause BSD
24

35
package(

third_party/protobuf/3.6.1/BUILD

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ config_setting(
2121

2222
load(":protobuf.bzl", "py_proto_library")
2323
load(":compiler_config_setting.bzl", "create_compiler_config_setting")
24+
load("@rules_python//python:defs.bzl", "py_library")
2425

2526
filegroup(
2627
name = "srcs",

third_party/protobuf/3.6.1/protobuf.bzl

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
load("@rules_python//python:defs.bzl", "py_library", "py_test")
2+
13
def _GetPath(ctx, path):
24
if ctx.label.workspace_root:
35
return ctx.label.workspace_root + "/" + path
@@ -383,7 +385,7 @@ def py_proto_library(
383385
if default_runtime and not default_runtime in py_libs + deps:
384386
py_libs = py_libs + [default_runtime]
385387

386-
native.py_library(
388+
py_library(
387389
name = name,
388390
srcs = outs + py_extra_srcs,
389391
deps = py_libs + deps,
@@ -406,7 +408,7 @@ def internal_protobuf_py_tests(
406408
"""
407409
for m in modules:
408410
s = "python/google/protobuf/internal/%s.py" % m
409-
native.py_test(
411+
py_test(
410412
name = "py_%s" % m,
411413
srcs = [s],
412414
main = s,
+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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 is a Bazel-internal file; do not load() it!
16+
17+
The incompatible change `--incompatible_load_python_rules_from_bzl` (#9006)
18+
makes it so the four native Python rules cannot be used unless a magic tag is
19+
present. It is intended that only `@rules_python` (bazelbuild/rules_python)
20+
uses this tag, and all other uses access the native rules via the wrapper
21+
macros defined in `@rules_python`.
22+
23+
However, `@bazel_tools` is not allowed to depend on any other repos. Therefore,
24+
we replicate the behavior of `@rules_python`'s wrapper macros in this file, for
25+
use by Bazel only.
26+
27+
This gets a bit tricky with the third_party/ directory. Some of its
28+
subdirectories are full-blown workspaces that cannot directly reference this
29+
file's label. For these cases we currently copy this file into those trees
30+
under the name `python_defs.bzl`.
31+
32+
TODO(#9029): Currently all uses of Python in Bazel's source tree use this file,
33+
but in principle only `@bazel_tools` needs to. The rest should use the real
34+
`@rules_python`, or for third_party/ packages that get mirrored into
35+
`@bazel_tools`, a dummy version of the repo that contains only this file under
36+
label `@rules_python//python:defs.bzl`.
37+
"""
38+
39+
_MIGRATION_TAG = "__PYTHON_RULES_MIGRATION_DO_NOT_USE_WILL_BREAK__"
40+
41+
def _add_tags(attrs):
42+
if "tags" in attrs and attrs["tags"] != None:
43+
attrs["tags"] += [_MIGRATION_TAG]
44+
else:
45+
attrs["tags"] = [_MIGRATION_TAG]
46+
return attrs
47+
48+
def py_library(**attrs):
49+
native.py_library(**_add_tags(attrs))
50+
51+
def py_binary(**attrs):
52+
native.py_binary(**_add_tags(attrs))
53+
54+
def py_test(**attrs):
55+
native.py_test(**_add_tags(attrs))
56+
57+
def py_runtime(**attrs):
58+
native.py_runtime(**_add_tags(attrs))

third_party/py/abseil/BUILD

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
load("//tools/python:private/defs.bzl", "py_library")
2+
13
licenses(["notice"])
24

35
filegroup(

third_party/py/concurrent/BUILD

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
load("//tools/python:private/defs.bzl", "py_library")
2+
13
licenses(["notice"])
24

35
filegroup(

third_party/py/gflags/BUILD

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
load("//tools/python:private/defs.bzl", "py_library")
2+
13
licenses(["notice"])
24

35
filegroup(

third_party/py/mock/BUILD

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
load("//tools/python:private/defs.bzl", "py_library")
2+
13
licenses(["notice"])
24

35
filegroup(

third_party/py/six/BUILD

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
load("//tools/python:private/defs.bzl", "py_library")
2+
13
licenses(["notice"])
24

35
filegroup(

0 commit comments

Comments
 (0)