diff --git a/setup.py b/setup.py index 48bfefe4dba..5d50a608bf9 100644 --- a/setup.py +++ b/setup.py @@ -236,6 +236,21 @@ def check_linker_need_libatomic(): return cpp_test.returncode == 0 +# When building extensions for macOS on a system running macOS 10.14 or newer, +# make sure they target macOS 10.14 or newer to use C++17 stdlib properly. +# This overrides the default behavior of distutils, which targets the macOS +# version Python was built on. You can further customize the target macOS +# version by setting the MACOSX_DEPLOYMENT_TARGET environment variable before +# running setup.py. +if sys.platform == "darwin": + if "MACOSX_DEPLOYMENT_TARGET" not in os.environ: + target_ver = sysconfig.get_config_var("MACOSX_DEPLOYMENT_TARGET") + if target_ver == "" or tuple(int(p) for p in target_ver.split(".")) < ( + 10, + 14, + ): + os.environ["MACOSX_DEPLOYMENT_TARGET"] = "10.14" + # There are some situations (like on Windows) where CC, CFLAGS, and LDFLAGS are # entirely ignored/dropped/forgotten by distutils and its Cygwin/MinGW support. # We use these environment variables to thus get around that without locking diff --git a/src/python/grpcio_observability/setup.py b/src/python/grpcio_observability/setup.py index 344c802b072..efe4cad8a22 100644 --- a/src/python/grpcio_observability/setup.py +++ b/src/python/grpcio_observability/setup.py @@ -138,6 +138,21 @@ class BuildExt(build_ext.build_ext): return filename +# When building extensions for macOS on a system running macOS 10.14 or newer, +# make sure they target macOS 10.14 or newer to use C++17 stdlib properly. +# This overrides the default behavior of distutils, which targets the macOS +# version Python was built on. You can further customize the target macOS +# version by setting the MACOSX_DEPLOYMENT_TARGET environment variable before +# running setup.py. +if sys.platform == "darwin": + if "MACOSX_DEPLOYMENT_TARGET" not in os.environ: + target_ver = sysconfig.get_config_var("MACOSX_DEPLOYMENT_TARGET") + if target_ver == "" or tuple(int(p) for p in target_ver.split(".")) < ( + 10, + 14, + ): + os.environ["MACOSX_DEPLOYMENT_TARGET"] = "10.14" + # There are some situations (like on Windows) where CC, CFLAGS, and LDFLAGS are # entirely ignored/dropped/forgotten by distutils and its Cygwin/MinGW support. # We use these environment variables to thus get around that without locking diff --git a/tools/distrib/python/grpcio_tools/setup.py b/tools/distrib/python/grpcio_tools/setup.py index 5e19b6314ed..f40c6d738e3 100644 --- a/tools/distrib/python/grpcio_tools/setup.py +++ b/tools/distrib/python/grpcio_tools/setup.py @@ -157,6 +157,21 @@ class BuildExt(build_ext.build_ext): build_ext.build_ext.build_extensions(self) +# When building extensions for macOS on a system running macOS 10.14 or newer, +# make sure they target macOS 10.14 or newer to use C++17 stdlib properly. +# This overrides the default behavior of distutils, which targets the macOS +# version Python was built on. You can further customize the target macOS +# version by setting the MACOSX_DEPLOYMENT_TARGET environment variable before +# running setup.py. +if sys.platform == "darwin": + if "MACOSX_DEPLOYMENT_TARGET" not in os.environ: + target_ver = sysconfig.get_config_var("MACOSX_DEPLOYMENT_TARGET") + if target_ver == "" or tuple(int(p) for p in target_ver.split(".")) < ( + 10, + 14, + ): + os.environ["MACOSX_DEPLOYMENT_TARGET"] = "10.14" + # There are some situations (like on Windows) where CC, CFLAGS, and LDFLAGS are # entirely ignored/dropped/forgotten by distutils and its Cygwin/MinGW support. # We use these environment variables to thus get around that without locking