Commit 60a0d41a authored by Jisi Liu's avatar Jisi Liu

Merge pull request #1233 from davidzchen/python-path

Remove hack for building Python support with Bazel.
parents fb714b36 985c9684
...@@ -22,7 +22,6 @@ load( ...@@ -22,7 +22,6 @@ load(
"protobuf", "protobuf",
"cc_proto_library", "cc_proto_library",
"py_proto_library", "py_proto_library",
"internal_copied_filegroup",
"internal_protobuf_py_tests", "internal_protobuf_py_tests",
) )
...@@ -484,25 +483,7 @@ java_library( ...@@ -484,25 +483,7 @@ java_library(
# Python support # Python support
################################################################################ ################################################################################
# Hack: py_library(
# protoc generated files contain imports like:
# "from google.protobuf.xxx import yyy"
# However, the sources files of the python runtime are not directly under
# "google/protobuf" (they are under python/google/protobuf). We workaround
# this by copying runtime source files into the desired location to workaround
# the import issue. Ideally py_library should support something similiar to the
# "include" attribute in cc_library to inject the PYTHON_PATH for all libraries
# that depend on the target.
#
# If you use python protobuf as a third_party library in your bazel managed
# project:
# 1) Please import the whole package to //google/protobuf in your
# project. Otherwise, bazel disallows generated files out of the current
# package, thus we won't be able to copy protobuf runtime files into
# //google/protobuf/.
# 2) The runtime also requires "six" for Python2/3 compatibility, please see the
# WORKSPACE file and bind "six" to your workspace as well.
internal_copied_filegroup(
name = "python_srcs", name = "python_srcs",
srcs = glob( srcs = glob(
[ [
...@@ -514,7 +495,7 @@ internal_copied_filegroup( ...@@ -514,7 +495,7 @@ internal_copied_filegroup(
"python/google/protobuf/internal/test_util.py", "python/google/protobuf/internal/test_util.py",
], ],
), ),
include = "python", imports = ["python"],
) )
cc_binary( cc_binary(
...@@ -527,7 +508,7 @@ cc_binary( ...@@ -527,7 +508,7 @@ cc_binary(
linkstatic = 1, linkstatic = 1,
deps = select({ deps = select({
"//conditions:default": [], "//conditions:default": [],
":use_fast_cpp_protos": ["//util/python:python_headers"], ":use_fast_cpp_protos": ["//external:python_headers"],
}), }),
) )
...@@ -553,7 +534,7 @@ cc_binary( ...@@ -553,7 +534,7 @@ cc_binary(
":protobuf", ":protobuf",
] + select({ ] + select({
"//conditions:default": [], "//conditions:default": [],
":use_fast_cpp_protos": ["//util/python:python_headers"], ":use_fast_cpp_protos": ["//external:python_headers"],
}), }),
) )
...@@ -584,23 +565,14 @@ py_proto_library( ...@@ -584,23 +565,14 @@ py_proto_library(
}), }),
default_runtime = "", default_runtime = "",
protoc = ":protoc", protoc = ":protoc",
py_extra_srcs = [":python_srcs"], py_libs = [
py_libs = ["//external:six"], ":python_srcs",
"//external:six"
],
srcs_version = "PY2AND3", srcs_version = "PY2AND3",
visibility = ["//visibility:public"], visibility = ["//visibility:public"],
) )
internal_copied_filegroup(
name = "python_test_srcs",
srcs = glob(
[
"python/google/protobuf/internal/*_test.py",
"python/google/protobuf/internal/test_util.py",
],
),
include = "python",
)
py_proto_library( py_proto_library(
name = "python_common_test_protos", name = "python_common_test_protos",
srcs = LITE_TEST_PROTOS + TEST_PROTOS, srcs = LITE_TEST_PROTOS + TEST_PROTOS,
...@@ -624,7 +596,13 @@ py_proto_library( ...@@ -624,7 +596,13 @@ py_proto_library(
py_library( py_library(
name = "python_tests", name = "python_tests",
srcs = [":python_test_srcs"], srcs = glob(
[
"python/google/protobuf/internal/*_test.py",
"python/google/protobuf/internal/test_util.py",
],
),
imports = ["python"],
srcs_version = "PY2AND3", srcs_version = "PY2AND3",
deps = [ deps = [
":protobuf_python", ":protobuf_python",
......
...@@ -12,6 +12,11 @@ new_http_archive( ...@@ -12,6 +12,11 @@ new_http_archive(
build_file = "six.BUILD", build_file = "six.BUILD",
) )
bind(
name = "python_headers",
actual = "//util/python:python_headers",
)
bind( bind(
name = "gtest", name = "gtest",
actual = "@gmock_archive//:gtest", actual = "@gmock_archive//:gtest",
......
...@@ -117,10 +117,10 @@ def cc_proto_library( ...@@ -117,10 +117,10 @@ def cc_proto_library(
deps=[], deps=[],
cc_libs=[], cc_libs=[],
include=None, include=None,
protoc="//google/protobuf:protoc", protoc="//:protoc",
internal_bootstrap_hack=False, internal_bootstrap_hack=False,
use_grpc_plugin=False, use_grpc_plugin=False,
default_runtime="//google/protobuf:protobuf", default_runtime="//:protobuf",
**kargs): **kargs):
"""Bazel rule to create a C++ protobuf library from proto source files """Bazel rule to create a C++ protobuf library from proto source files
...@@ -199,35 +199,6 @@ def cc_proto_library( ...@@ -199,35 +199,6 @@ def cc_proto_library(
includes=includes, includes=includes,
**kargs) **kargs)
def internal_copied_filegroup(
name,
srcs,
include,
**kargs):
"""Bazel rule to fix sources file to workaround with python path issues.
Args:
name: the name of the internal_copied_filegroup rule, which will be the
name of the generated filegroup.
srcs: the source files to be copied.
include: the expected import root of the source.
**kargs: extra arguments that will be passed into the filegroup.
"""
outs = [_RelativeOutputPath(s, include) for s in srcs]
native.genrule(
name=name+"_genrule",
srcs=srcs,
outs=outs,
cmd=" && ".join(["cp $(location %s) $(location %s)" %
(s, _RelativeOutputPath(s, include))
for s in srcs]))
native.filegroup(
name=name,
srcs=outs,
**kargs)
def py_proto_library( def py_proto_library(
name, name,
srcs=[], srcs=[],
...@@ -235,8 +206,8 @@ def py_proto_library( ...@@ -235,8 +206,8 @@ def py_proto_library(
py_libs=[], py_libs=[],
py_extra_srcs=[], py_extra_srcs=[],
include=None, include=None,
default_runtime="//google/protobuf:protobuf_python", default_runtime="//:protobuf_python",
protoc="//google/protobuf:protoc", protoc="//:protoc",
**kargs): **kargs):
"""Bazel rule to create a Python protobuf library from proto source files """Bazel rule to create a Python protobuf library from proto source files
...@@ -276,15 +247,6 @@ def py_proto_library( ...@@ -276,15 +247,6 @@ def py_proto_library(
visibility=["//visibility:public"], visibility=["//visibility:public"],
) )
if include != None:
# Copy the output files to the desired location to make the import work.
internal_copied_filegroup_name=name + "_internal_copied_filegroup"
internal_copied_filegroup(
name=internal_copied_filegroup_name,
srcs=outs,
include=include)
outs=[internal_copied_filegroup_name]
if default_runtime and not default_runtime in py_libs + deps: if default_runtime and not default_runtime in py_libs + deps:
py_libs += [default_runtime] py_libs += [default_runtime]
...@@ -292,6 +254,7 @@ def py_proto_library( ...@@ -292,6 +254,7 @@ def py_proto_library(
name=name, name=name,
srcs=outs+py_extra_srcs, srcs=outs+py_extra_srcs,
deps=py_libs+deps, deps=py_libs+deps,
imports=includes,
**kargs) **kargs)
def internal_protobuf_py_tests( def internal_protobuf_py_tests(
...@@ -308,8 +271,7 @@ def internal_protobuf_py_tests( ...@@ -308,8 +271,7 @@ def internal_protobuf_py_tests(
""" """
for m in modules: for m in modules:
s = _RelativeOutputPath( s = "python/google/protobuf/internal/%s.py" % m
"python/google/protobuf/internal/%s.py" % m, "python")
native.py_test( native.py_test(
name="py_%s" % m, name="py_%s" % m,
srcs=[s], srcs=[s],
......
# This is a placeholder for python headers. Projects needing to use # This is a placeholder for python headers. Projects needing to use
# fast cpp protos in protobuf's python interface should build with # fast cpp protos in protobuf's python interface should build with
# --define=use_fast_cpp_protos=true, and in addition, provide # --define=use_fast_cpp_protos=true, and in addition, provide
# //util/python:python_headers dependency that in turn provides Python.h. # //external:python_headers dependency that in turn provides Python.h.
#
# Projects that include protobuf using a Bazel external repository will need to
# add a workspace rule to their WORKSPACE files to add an external workspace
# that includes the Python headers. For example, the protobuf WORKSPACE file
# includes the following local_repository rule that points to this directory:
#
# new_local_repository(
# name = "python",
# path = __workspace_dir__ + "/util/python",
# )
cc_library( cc_library(
name = "python_headers", name = "python_headers",
visibility = ["//visibility:public"], visibility = ["//visibility:public"],
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment