Added py_extension().

pull/13171/head
Joshua Haberman 3 years ago
parent c16539bb55
commit 711885db8f
  1. 39
      bazel/py_extension.bzl
  2. 2
      bazel/py_proto_library.bzl
  3. 18
      bazel/workspace_defs.bzl
  4. 4
      bazel/workspace_deps.bzl
  5. 28
      python/BUILD

@ -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)
return [
PyInfo(transitive_sources = outs_depset)
PyInfo(transitive_sources = outs_depset),
]
_py_proto_library_aspect = aspect(

@ -53,17 +53,17 @@ toolchain(
"""
def _get_config_var(repository_ctx, name):
py_program = "import sysconfig; print(sysconfig.get_config_var('%s'), end='')"
result = repository_ctx.execute(["python3", "-c", py_program % (name)])
if result.return_code != 0:
fail("No python3 executable available on the system")
return result.stdout
py_program = "import sysconfig; print(sysconfig.get_config_var('%s'), end='')"
result = repository_ctx.execute(["python3", "-c", py_program % (name)])
if result.return_code != 0:
fail("No python3 executable available on the system")
return result.stdout
def _python_headers_impl(repository_ctx):
path = _get_config_var(repository_ctx, "INCLUDEPY")
repository_ctx.symlink(path, "python")
python3 = repository_ctx.which("python3")
repository_ctx.file("BUILD.bazel", _build_file % python3)
path = _get_config_var(repository_ctx, "INCLUDEPY")
repository_ctx.symlink(path, "python")
python3 = repository_ctx.which("python3")
repository_ctx.file("BUILD.bazel", _build_file % python3)
# The system_python() repository rule exposes Python headers from the system.
#

@ -23,10 +23,10 @@ def upb_deps():
"rm python/google/protobuf/__init__.py",
"rm python/google/protobuf/pyext/__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
maybe(
http_archive,

@ -31,6 +31,10 @@ load(
"//bazel:py_proto_library.bzl",
"py_proto_library",
)
load(
"//bazel:py_extension.bzl",
"py_extension",
)
load(
"@rules_python//python:packaging.bzl",
"py_wheel",
@ -38,8 +42,8 @@ load(
licenses(["notice"])
cc_binary(
name = "message",
py_extension(
name = "_message",
srcs = [
"convert.c",
"convert.h",
@ -61,24 +65,7 @@ cc_binary(
"repeated.c",
"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 = [
":version_script.lds",
"//:descriptor_upb_proto_reflection",
"//:reflection",
"//:textformat",
@ -86,7 +73,6 @@ cc_binary(
"//upb/util:compare",
"//upb/util:def_to_proto",
"//upb/util:required_fields",
"@system_python//:python_headers",
],
)
@ -108,7 +94,7 @@ EXT_SUFFIX = ".abi3.so"
genrule(
name = "copy_message",
srcs = [":message"],
srcs = [":_message"],
outs = ["google/protobuf/pyext/_message" + EXT_SUFFIX],
cmd = "cp $< $@",
)

Loading…
Cancel
Save