From 98eab1c0c29465eb6300192891e66ded76353fde Mon Sep 17 00:00:00 2001 From: Joshua Haberman Date: Wed, 18 Aug 2021 17:36:14 -0700 Subject: [PATCH] Fix the build on macos. --- bazel/workspace_defs.bzl | 14 ++++++++++---- python/BUILD | 11 +++++++++-- 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/bazel/workspace_defs.bzl b/bazel/workspace_defs.bzl index 18d985fdad..bb1817d470 100644 --- a/bazel/workspace_defs.bzl +++ b/bazel/workspace_defs.bzl @@ -34,17 +34,23 @@ cc_library( ) """ -def _find_python_dir(repository_ctx): - py_program = "import sysconfig; print(sysconfig.get_config_var('INCLUDEPY'), end='')" - result = repository_ctx.execute(["python3", "-c", py_program]) +_build_defs_file = """ +EXT_SUFFIX = "%s" +""" + +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 def _python_headers_impl(repository_ctx): - path = _find_python_dir(repository_ctx) + path = _get_config_var(repository_ctx, "INCLUDEPY") + ext_suffix = _get_config_var(repository_ctx, "EXT_SUFFIX") repository_ctx.symlink(path, "python") repository_ctx.file("BUILD.bazel", _build_file) + repository_ctx.file("build_defs.bzl", _build_defs_file % ext_suffix) # The python_headers() repository rule exposes Python headers from the system. # diff --git a/python/BUILD b/python/BUILD index 3ec7853ae6..0df0b1d98a 100644 --- a/python/BUILD +++ b/python/BUILD @@ -27,6 +27,10 @@ load( "//bazel:build_defs.bzl", "UPB_DEFAULT_COPTS", ) +load( + "@python_headers//:build_defs.bzl", + "EXT_SUFFIX", +) cc_binary( name = "message", @@ -45,7 +49,10 @@ cc_binary( ], # We use a linker script to hide all symbols except the entry point for # the module. - linkopts = ["-Wl,--version-script,$(location :version_script.lds)"], + 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 = [ @@ -60,7 +67,7 @@ cc_binary( genrule( name = "message_ext", srcs = [":message"], - outs = ["google/protobuf/pyext/_message.cpython-39-x86_64-linux-gnu.so"], + outs = ["google/protobuf/pyext/_message" + EXT_SUFFIX], cmd = "cp $< $@", )