Skip to content

Commit

Permalink
Merge pull request grpc#24992 from gnossen/fix_grpc_tools_so_mac_symbols
Browse files Browse the repository at this point in the history
Fix visibility of libprotobuf symbols in protoc_compiler.so on Mac
  • Loading branch information
gnossen authored and ravidcottopia committed Dec 29, 2020
1 parent 64c6cfc commit 6f4c8b3
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions tools/distrib/python/grpcio_tools/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@
import protoc_lib_deps
import grpc_version

_EXT_INIT_SYMBOL = None
if sys.version_info[0] == 2:
_EXT_INIT_SYMBOL = "init_protoc_compiler"
else:
_EXT_INIT_SYMBOL = "PyInit__protoc_compiler"

_parallel_compile_patch.monkeypatch_compile_maybe()

CLASSIFIERS = [
Expand Down Expand Up @@ -118,6 +124,28 @@ def check_linker_need_libatomic():
EXTRA_ENV_COMPILE_ARGS += ' -fno-wrapv -frtti'
if EXTRA_ENV_LINK_ARGS is None:
EXTRA_ENV_LINK_ARGS = ''
# NOTE(rbellevi): Clang on Mac OS will make all static symbols (both
# variables and objects) global weak symbols. When a process loads the
# protobuf wheel's shared object library before loading *this* C extension,
# the runtime linker will prefer the protobuf module's version of symbols.
# This results in the process using a mixture of symbols from the protobuf
# wheel and this wheel, which may be using different versions of
# libprotobuf. In the case that they *are* using different versions of
# libprotobuf *and* there has been a change in data layout (or in other
# invariants) segfaults, data corruption, or "bad things" may happen.
#
# This flag ensures that on Mac, the only global symbol is the one loaded by
# the Python interpreter. The problematic global weak symbols become local
# weak symbols. This is not required on Linux since the compiler does not
# produce global weak symbols. This is not required on Windows as our ".pyd"
# file does not contain any symbols.
#
# Finally, the leading underscore here is part of the Mach-O ABI. Unlike
# more modern ABIs (ELF et al.), Mach-O prepends an underscore to the names
# of C functions.
if "darwin" in sys.platform:
EXTRA_ENV_LINK_ARGS += ' -Wl,-exported_symbol,_{}'.format(
_EXT_INIT_SYMBOL)
if "linux" in sys.platform or "darwin" in sys.platform:
EXTRA_ENV_LINK_ARGS += ' -lpthread'
if check_linker_need_libatomic():
Expand Down

0 comments on commit 6f4c8b3

Please sign in to comment.