Skip to content

Commit b3cbea1

Browse files
authored
[Bazel] Move Python rules to //python (#9952)
1 parent 1978a6f commit b3cbea1

File tree

8 files changed

+507
-367
lines changed

8 files changed

+507
-367
lines changed

BUILD.bazel

Lines changed: 29 additions & 280 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
11
# Bazel (https://bazel.build/) BUILD file for Protobuf.
22

33
load("@bazel_skylib//rules:common_settings.bzl", "string_flag")
4-
load("@rules_cc//cc:defs.bzl", "cc_binary", "cc_library", "cc_proto_library","cc_test")
4+
load("@rules_cc//cc:defs.bzl", "cc_binary", "cc_library", "cc_proto_library", "cc_test")
55
load("@rules_pkg//:mappings.bzl", "pkg_files", "strip_prefix")
66
load("@rules_proto//proto:defs.bzl", "proto_lang_toolchain", "proto_library")
7-
load("@rules_python//python:defs.bzl", "py_library")
87
load("@rules_java//java:defs.bzl", "java_binary", "java_lite_proto_library", "java_proto_library")
98
load("//build_defs:cpp_opts.bzl", "COPTS", "LINK_OPTS")
109
load(
1110
":protobuf.bzl",
1211
"adapt_proto_library",
13-
"internal_copied_filegroup",
14-
"internal_protobuf_py_tests",
1512
"py_proto_library",
1613
)
1714

@@ -538,9 +535,15 @@ RELATIVE_TEST_PROTOS = [
538535

539536
TEST_PROTOS = ["src/" + s for s in RELATIVE_TEST_PROTOS]
540537

538+
filegroup(
539+
name = "test_proto_srcs",
540+
srcs = TEST_PROTOS,
541+
visibility = ["//:__subpackages__"],
542+
)
543+
541544
proto_library(
542545
name = "test_protos",
543-
srcs = TEST_PROTOS,
546+
srcs = [":test_proto_srcs"],
544547
strip_import_prefix = "src",
545548
visibility = ["//:__subpackages__"],
546549
deps = [
@@ -819,264 +822,40 @@ alias(
819822
# Python support
820823
################################################################################
821824

822-
py_library(
825+
alias(
826+
name = "protobuf_python",
827+
actual = "//python:protobuf_python",
828+
visibility = ["//visibility:public"],
829+
)
830+
831+
alias(
823832
name = "python_srcs",
824-
srcs = glob(
825-
[
826-
"python/google/protobuf/**/*.py",
827-
],
828-
),
829-
imports = ["python"],
830-
srcs_version = "PY2AND3",
833+
actual = "//python:python_srcs",
831834
visibility = ["@upb//:__subpackages__"],
832835
)
833836

834-
py_library(
837+
alias(
835838
name = "python_test_srcs",
836-
srcs = glob(
837-
[
838-
"python/google/protobuf/internal/*_test.py",
839-
"python/google/protobuf/internal/test_util.py",
840-
],
841-
),
842-
imports = ["python"],
843-
srcs_version = "PY3",
839+
actual = "//python:python_test_srcs",
844840
visibility = ["@upb//:__subpackages__"],
845841
)
846842

847-
cc_binary(
848-
name = "python/google/protobuf/internal/_api_implementation.so",
849-
srcs = ["python/google/protobuf/internal/api_implementation.cc"],
850-
copts = COPTS + [
851-
"-DPYTHON_PROTO2_CPP_IMPL_V2",
852-
],
853-
linkshared = 1,
854-
linkstatic = 1,
855-
tags = [
856-
# Exclude this target from wildcard expansion (//...) because it may
857-
# not even be buildable. It will be built if it is needed according
858-
# to :use_fast_cpp_protos.
859-
# https://docs.bazel.build/versions/master/be/common-definitions.html#common-attributes
860-
"manual",
861-
],
862-
deps = select({
863-
"//conditions:default": [],
864-
":use_fast_cpp_protos": ["//external:python_headers"],
865-
}),
866-
)
867-
868-
cc_binary(
869-
name = "python/google/protobuf/pyext/_message.so",
870-
srcs = glob([
871-
"python/google/protobuf/pyext/*.cc",
872-
"python/google/protobuf/pyext/*.h",
873-
]),
874-
copts = COPTS + [
875-
"-DGOOGLE_PROTOBUF_HAS_ONEOF=1",
876-
] + select({
877-
"//conditions:default": [],
878-
":allow_oversize_protos": ["-DPROTOBUF_PYTHON_ALLOW_OVERSIZE_PROTOS=1"],
879-
}),
880-
includes = [
881-
"python/",
882-
"src/",
883-
],
884-
linkshared = 1,
885-
linkstatic = 1,
886-
tags = [
887-
# Exclude this target from wildcard expansion (//...) because it may
888-
# not even be buildable. It will be built if it is needed according
889-
# to :use_fast_cpp_protos.
890-
# https://docs.bazel.build/versions/master/be/common-definitions.html#common-attributes
891-
"manual",
892-
],
893-
deps = [
894-
":protobuf",
895-
":proto_api",
896-
] + select({
897-
"//conditions:default": [],
898-
":use_fast_cpp_protos": ["//external:python_headers"],
899-
}),
900-
)
901-
902-
config_setting(
903-
name = "use_fast_cpp_protos",
904-
values = {
905-
"define": "use_fast_cpp_protos=true",
906-
},
907-
visibility = [
908-
# Public, but Protobuf only visibility.
909-
"//:__subpackages__",
910-
],
911-
)
912-
913-
config_setting(
914-
name = "allow_oversize_protos",
915-
values = {
916-
"define": "allow_oversize_protos=true",
917-
},
918-
visibility = [
919-
# Public, but Protobuf only visibility.
920-
"//:__subpackages__",
921-
],
922-
)
923-
924-
# Copy the builtin proto files from src/google/protobuf to
925-
# python/google/protobuf. This way, the generated Python sources will be in the
926-
# same directory as the Python runtime sources. This is necessary for the
927-
# modules to be imported correctly since they are all part of the same Python
928-
# package.
929-
internal_copied_filegroup(
930-
name = "protos_python",
931-
srcs = [
932-
"src/google/protobuf/any.proto",
933-
"src/google/protobuf/api.proto",
934-
"src/google/protobuf/compiler/plugin.proto",
935-
"src/google/protobuf/descriptor.proto",
936-
"src/google/protobuf/duration.proto",
937-
"src/google/protobuf/empty.proto",
938-
"src/google/protobuf/field_mask.proto",
939-
"src/google/protobuf/source_context.proto",
940-
"src/google/protobuf/struct.proto",
941-
"src/google/protobuf/timestamp.proto",
942-
"src/google/protobuf/type.proto",
943-
"src/google/protobuf/wrappers.proto",
944-
],
945-
dest = "python",
946-
strip_prefix = "src",
947-
)
948-
949-
py_proto_library(
843+
alias(
950844
name = "well_known_types_py_pb2",
951-
srcs = [
952-
"python/google/protobuf/any.proto",
953-
"python/google/protobuf/api.proto",
954-
"python/google/protobuf/compiler/plugin.proto",
955-
"python/google/protobuf/descriptor.proto",
956-
"python/google/protobuf/duration.proto",
957-
"python/google/protobuf/empty.proto",
958-
"python/google/protobuf/field_mask.proto",
959-
"python/google/protobuf/source_context.proto",
960-
"python/google/protobuf/struct.proto",
961-
"python/google/protobuf/timestamp.proto",
962-
"python/google/protobuf/type.proto",
963-
"python/google/protobuf/wrappers.proto",
964-
],
965-
include = "python",
966-
default_runtime = "",
967-
protoc = ":protoc",
968-
srcs_version = "PY2AND3",
845+
actual = "//python:well_known_types_py_pb2",
969846
visibility = ["@upb//:__subpackages__"],
970847
)
971848

972-
py_library(
973-
name = "protobuf_python",
974-
data = select({
975-
"//conditions:default": [],
976-
":use_fast_cpp_protos": [
977-
":python/google/protobuf/internal/_api_implementation.so",
978-
":python/google/protobuf/pyext/_message.so",
979-
],
980-
}),
981-
deps = [
982-
":python_srcs",
983-
":well_known_types_py_pb2",
984-
],
985-
)
986-
987-
# Copy the test proto files from src/google/protobuf to
988-
# python/google/protobuf. This way, the generated Python sources will be in the
989-
# same directory as the Python runtime sources. This is necessary for the
990-
# modules to be imported correctly by the tests since they are all part of the
991-
# same Python package.
992-
internal_copied_filegroup(
993-
name = "protos_python_test",
994-
srcs = LITE_TEST_PROTOS + TEST_PROTOS,
995-
dest = "python",
996-
strip_prefix = "src",
997-
)
998-
999-
# TODO(dzc): Remove this once py_proto_library can have labels in srcs, in
1000-
# which case we can simply add :protos_python_test in srcs.
1001-
COPIED_LITE_TEST_PROTOS = ["python/" + s for s in RELATIVE_LITE_TEST_PROTOS]
1002-
1003-
COPIED_TEST_PROTOS = ["python/" + s for s in RELATIVE_TEST_PROTOS]
1004-
1005-
py_proto_library(
849+
alias(
1006850
name = "python_common_test_protos",
1007-
srcs = COPIED_LITE_TEST_PROTOS + COPIED_TEST_PROTOS,
1008-
include = "python",
1009-
default_runtime = "",
1010-
protoc = ":protoc",
1011-
srcs_version = "PY2AND3",
851+
actual = "//python:python_common_test_protos",
1012852
visibility = ["//visibility:public"],
1013-
deps = [":well_known_types_py_pb2"],
1014853
)
1015854

1016-
py_proto_library(
855+
alias(
1017856
name = "python_specific_test_protos",
1018-
srcs = glob([
1019-
"python/google/protobuf/internal/*.proto",
1020-
"python/google/protobuf/internal/import_test_package/*.proto",
1021-
]),
1022-
include = "python",
1023-
default_runtime = ":protobuf_python",
1024-
protoc = ":protoc",
1025-
srcs_version = "PY2AND3",
857+
actual = "//python:python_specific_test_protos",
1026858
visibility = ["//visibility:public"],
1027-
deps = [":python_common_test_protos"],
1028-
)
1029-
1030-
py_library(
1031-
name = "python_tests",
1032-
srcs = glob(
1033-
[
1034-
"python/google/protobuf/internal/*_test.py",
1035-
"python/google/protobuf/internal/test_util.py",
1036-
"python/google/protobuf/internal/import_test_package/__init__.py",
1037-
],
1038-
),
1039-
imports = ["python"],
1040-
srcs_version = "PY2AND3",
1041-
deps = [
1042-
":protobuf_python",
1043-
":python_common_test_protos",
1044-
":python_specific_test_protos",
1045-
],
1046-
)
1047-
1048-
internal_protobuf_py_tests(
1049-
name = "python_tests_batch",
1050-
data = glob([
1051-
"src/google/protobuf/**/*",
1052-
]),
1053-
modules = [
1054-
"descriptor_database_test",
1055-
"descriptor_pool_test",
1056-
"descriptor_test",
1057-
"generator_test",
1058-
"json_format_test",
1059-
"message_factory_test",
1060-
"message_test",
1061-
"proto_builder_test",
1062-
"reflection_test",
1063-
"service_reflection_test",
1064-
"symbol_database_test",
1065-
"text_encoding_test",
1066-
"text_format_test",
1067-
"unknown_fields_test",
1068-
"wire_format_test",
1069-
],
1070-
deps = [":python_tests"],
1071-
)
1072-
1073-
cc_library(
1074-
name = "proto_api",
1075-
hdrs = ["python/google/protobuf/proto_api.h"],
1076-
visibility = ["//visibility:public"],
1077-
deps = [
1078-
"//external:python_headers",
1079-
],
1080859
)
1081860

1082861
proto_lang_toolchain(
@@ -1122,25 +901,21 @@ genrule(
1122901
name = "generated_protos",
1123902
srcs = ["src/google/protobuf/unittest_import.proto"],
1124903
outs = ["unittest_gen_import.proto"],
1125-
cmd = "cat $(SRCS) | sed 's|google/|src/google/|' > $(OUTS)",
904+
cmd = "cat $(SRCS) > $(OUTS)",
1126905
)
1127906

1128907
proto_library(
1129908
name = "generated_protos_proto",
1130-
srcs = [
1131-
"src/google/protobuf/unittest_import_public.proto",
1132-
"unittest_gen_import.proto",
1133-
],
909+
srcs = [":generated_protos"],
910+
deps = [":generic_test_protos"],
1134911
)
1135912

1136913
py_proto_library(
1137914
name = "generated_protos_py",
1138-
srcs = [
1139-
"src/google/protobuf/unittest_import_public.proto",
1140-
"unittest_gen_import.proto",
1141-
],
915+
srcs = [":generated_protos"],
1142916
default_runtime = "",
1143917
protoc = ":protoc",
918+
deps = ["//python:python_common_test_protos"],
1144919
)
1145920

1146921
################################################################################
@@ -1361,29 +1136,3 @@ pkg_files(
13611136
],
13621137
visibility = ["//pkg:__pkg__"],
13631138
)
1364-
1365-
# Python runtime
1366-
pkg_files(
1367-
name = "python_dist_files",
1368-
srcs = glob([
1369-
"python/google/**/*.proto",
1370-
"python/google/**/*.py",
1371-
"python/google/protobuf/internal/*.cc",
1372-
"python/google/protobuf/pyext/*.cc",
1373-
"python/google/protobuf/pyext/*.h",
1374-
]) + [
1375-
"python/MANIFEST.in",
1376-
"python/README.md",
1377-
"python/google/protobuf/proto_api.h",
1378-
"python/google/protobuf/pyext/README",
1379-
"python/google/protobuf/python_protobuf.h",
1380-
"python/mox.py",
1381-
"python/release.sh",
1382-
"python/setup.cfg",
1383-
"python/setup.py",
1384-
"python/stubout.py",
1385-
"python/tox.ini",
1386-
],
1387-
strip_prefix = strip_prefix.from_root(""),
1388-
visibility = ["//pkg:__pkg__"],
1389-
)

Makefile.am

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -993,6 +993,7 @@ php_EXTRA_DIST= \
993993

994994
# Note: please keep this in sync with the python_dist_files rule in BUILD.bazel.
995995
python_EXTRA_DIST= \
996+
python/BUILD.bazel \
996997
python/MANIFEST.in \
997998
python/google/__init__.py \
998999
python/google/protobuf/__init__.py \
@@ -1107,6 +1108,7 @@ python_EXTRA_DIST= \
11071108
python/google/protobuf/text_format.py \
11081109
python/google/protobuf/unknown_fields.py \
11091110
python/google/protobuf/util/__init__.py \
1111+
python/internal.bzl \
11101112
python/release.sh \
11111113
python/mox.py \
11121114
python/setup.cfg \

0 commit comments

Comments
 (0)