Merge pull request #531 from haberman/python-google3

Changes to make Python extension build in google3
pull/13171/head
Joshua Haberman 3 years ago committed by GitHub
commit 3a4c492e79
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 5
      BUILD
  2. 39
      bazel/py_extension.bzl
  3. 2
      bazel/py_proto_library.bzl
  4. 2
      bazel/workspace_deps.bzl
  5. 84
      python/BUILD
  6. 2
      python/protobuf.h

@ -444,7 +444,10 @@ cc_library(
"upb/table_internal.h", "upb/table_internal.h",
"upb/upb.h", "upb/upb.h",
], ],
visibility = ["//tests:__pkg__"], visibility = [
"//python:__pkg__",
"//tests:__pkg__",
],
deps = [ deps = [
":port", ":port",
], ],

@ -0,0 +1,39 @@
load(
"//bazel:build_defs.bzl",
"UPB_DEFAULT_COPTS",
)
def py_extension(name, srcs, deps=[]):
version_script = name + "_version_script.lds"
symbol = "PyInit_" + name
native.genrule(
name = "gen_" + version_script,
outs = [version_script],
cmd = "echo 'message { global: " + symbol + "; local: *; };' > $@",
)
native.cc_binary(
name = name,
srcs = srcs,
copts = UPB_DEFAULT_COPTS + [
# The Python API requires patterns that are ISO C incompatible, like
# casts between function pointers and object pointers.
"-Wno-pedantic",
],
# We use a linker script to hide all symbols except the entry point for
# the module.
linkopts = select({
"@platforms//os:linux": ["-Wl,--version-script,$(location :" + version_script + ")"],
"@platforms//os:macos": [
"-Wl,-exported_symbol",
"-Wl,_" + symbol,
],
}),
linkshared = True,
linkstatic = True,
deps = deps + [
":" + version_script,
"@system_python//:python_headers",
],
)

@ -103,7 +103,7 @@ def _py_proto_library_aspect_impl(target, ctx):
) )
outs_depset = depset(srcs) outs_depset = depset(srcs)
return [ return [
PyInfo(transitive_sources = outs_depset) PyInfo(transitive_sources = outs_depset),
] ]
_py_proto_library_aspect = aspect( _py_proto_library_aspect = aspect(

@ -23,7 +23,7 @@ def upb_deps():
"rm python/google/protobuf/__init__.py", "rm python/google/protobuf/__init__.py",
"rm python/google/protobuf/pyext/__init__.py", "rm python/google/protobuf/pyext/__init__.py",
"rm python/google/protobuf/internal/__init__.py", "rm python/google/protobuf/internal/__init__.py",
] ],
) )
rules_python_version = "740825b7f74930c62f44af95c9a4c1bd428d2c53" # Latest @ 2021-06-23 rules_python_version = "740825b7f74930c62f44af95c9a4c1bd428d2c53" # Latest @ 2021-06-23

@ -23,23 +23,14 @@
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
load( load("//bazel:py_proto_library.bzl", "py_proto_library")
"//bazel:build_defs.bzl", load("//bazel:py_extension.bzl", "py_extension") # copybara:strip_for_google3
"UPB_DEFAULT_COPTS", load("@rules_python//python:packaging.bzl", "py_wheel")
)
load(
"//bazel:py_proto_library.bzl",
"py_proto_library",
)
load(
"@rules_python//python:packaging.bzl",
"py_wheel",
)
licenses(["notice"]) licenses(["notice"])
cc_binary( py_extension(
name = "message", name = "_message",
srcs = [ srcs = [
"convert.c", "convert.c",
"convert.h", "convert.h",
@ -61,45 +52,38 @@ cc_binary(
"repeated.c", "repeated.c",
"repeated.h", "repeated.h",
], ],
copts = UPB_DEFAULT_COPTS + [
# The Python API requires patterns that are ISO C incompatible, like
# casts between function pointers and object pointers.
"-Wno-pedantic",
],
# We use a linker script to hide all symbols except the entry point for
# the module.
linkopts = select({
"@platforms//os:linux": ["-Wl,--version-script,$(location :version_script.lds)"],
"@platforms//os:macos": [
"-Wl,-exported_symbol",
"-Wl,_PyInit__message",
],
}),
linkshared = True,
linkstatic = True,
deps = [ deps = [
":version_script.lds",
"//:descriptor_upb_proto_reflection", "//:descriptor_upb_proto_reflection",
"//:reflection", "//:reflection",
"//:table",
"//:textformat", "//:textformat",
"//:upb", "//:upb",
"//upb/util:compare", "//upb/util:compare",
"//upb/util:def_to_proto", "//upb/util:def_to_proto",
"//upb/util:required_fields", "//upb/util:required_fields",
"@system_python//:python_headers",
], ],
) )
cc_binary( py_extension(
name = "api_implementation", name = "_api_implementation",
srcs = ["api_implementation.c"],
)
# copybara:strip_for_google3_begin
py_test(
name = "minimal_test",
srcs = [ srcs = [
"api_implementation.c", "minimal_test.py",
],
imports = ["."],
legacy_create_init = False,
deps = [
"//python:message_ext",
"@com_google_protobuf//:python_common_test_protos",
"@com_google_protobuf//:python_specific_test_protos",
"@com_google_protobuf//:python_srcs",
], ],
linkshared = True,
linkstatic = True,
# Enable once linker script is available.
#copts = ["-fvisibility=hidden"],
deps = ["@system_python//:python_headers"],
) )
# Copy the extensions into the location recognized by Python. # Copy the extensions into the location recognized by Python.
@ -108,14 +92,14 @@ EXT_SUFFIX = ".abi3.so"
genrule( genrule(
name = "copy_message", name = "copy_message",
srcs = [":message"], srcs = [":_message"],
outs = ["google/protobuf/pyext/_message" + EXT_SUFFIX], outs = ["google/protobuf/pyext/_message" + EXT_SUFFIX],
cmd = "cp $< $@", cmd = "cp $< $@",
) )
genrule( genrule(
name = "copy_api_implementation", name = "copy_api_implementation",
srcs = [":api_implementation"], srcs = [":_api_implementation"],
outs = ["google/protobuf/internal/_api_implementation" + EXT_SUFFIX], outs = ["google/protobuf/internal/_api_implementation" + EXT_SUFFIX],
cmd = "cp $< $@", cmd = "cp $< $@",
visibility = ["//python:__subpackages__"], visibility = ["//python:__subpackages__"],
@ -136,20 +120,6 @@ py_library(
visibility = ["//python:__subpackages__"], visibility = ["//python:__subpackages__"],
) )
py_test(
name = "minimal_test",
srcs = [
"minimal_test.py",
],
imports = ["."],
legacy_create_init = False,
deps = [
"//python:message_ext",
"@com_google_protobuf//:python_common_test_protos",
"@com_google_protobuf//:python_specific_test_protos",
"@com_google_protobuf//:python_srcs",
],
)
py_proto_library( py_proto_library(
name = "well_known_proto_pb2", name = "well_known_proto_pb2",
@ -189,3 +159,5 @@ py_wheel(
"@com_google_protobuf//:python_srcs", "@com_google_protobuf//:python_srcs",
], ],
) )
# copybara:strip_end

@ -193,7 +193,7 @@ PyObject* PyUpb_Forbidden_New(PyObject* cls, PyObject* args, PyObject* kwds);
static inline void PyUpb_Dealloc(void* self) { static inline void PyUpb_Dealloc(void* self) {
PyTypeObject* tp = Py_TYPE(self); PyTypeObject* tp = Py_TYPE(self);
assert(PyType_GetFlags(tp) & Py_TPFLAGS_HEAPTYPE); assert(PyType_GetFlags(tp) & Py_TPFLAGS_HEAPTYPE);
freefunc tp_free = PyType_GetSlot(tp, Py_tp_free); freefunc tp_free = (freefunc)PyType_GetSlot(tp, Py_tp_free);
tp_free(self); tp_free(self);
Py_DECREF(tp); Py_DECREF(tp);
} }

Loading…
Cancel
Save