From 14ac94d923b80650e0df55bed17be5efa0e4becd Mon Sep 17 00:00:00 2001 From: Esun Kim Date: Wed, 30 Oct 2024 13:34:47 -0700 Subject: [PATCH] [Build] Override MACOSX_DEPLOYMENT_TARGET for gRPC Python (#37997) CPython uses this [logic](https://github.com/pypa/cibuildwheel/blob/b98602705f5449b8e221a1849a8dc18e8d8b14db/cibuildwheel/macos.py#L302-L323) to determine `MACOSX_DEPLOYMENT_TARGET` and the lowest possible value is `10.9` (Mavericks) as of today. But we need to use at least `10.14` (Mojave) to get prepared for upcoming C++17 upgrade (scheduled in 2024-11). This `10.14` version should be safe to use as i) Apple stopped supporting any versions older than 12.0 (https://endoflife.date/macos) and ii) this version is older than officially support OSX version (11.0) of gRPC Core/C++ (https://github.com/google/oss-policies-info/blob/main/foundational-cxx-support-matrix.md) Closes #37997 PiperOrigin-RevId: 691535457 --- setup.py | 15 +++++++++++++++ src/python/grpcio_observability/setup.py | 15 +++++++++++++++ tools/distrib/python/grpcio_tools/setup.py | 15 +++++++++++++++ 3 files changed, 45 insertions(+) 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