OS X Python:我可以显式设置扩展名MACOSX_DEPLOYMENT_TARGET吗? [英] OS X Python: can I explicitly set MACOSX_DEPLOYMENT_TARGET for extensions?

查看:210
本文介绍了OS X Python:我可以显式设置扩展名MACOSX_DEPLOYMENT_TARGET吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在最新版本的OS X(即10.11 El Capitan)下使用Python的 python.org 框架构建的Python.我需要构建一些依赖于最新版本的编译器的扩展(例如C ++-11功能).但是,python.org python也可以在较旧的系统上运行,以实现向后兼容.

I use the python.org framework build of Python under recent versions of OS X (i.e., 10.11 El Capitan). I need to build some extensions that rely on recent versions of compilers (e.g., C++-11 features). However the python.org python is built to work on older systems as well, for backward compatibility.

因此,它具有环境变量 MACOSX_DEPLOYMENT_TARGET = 10.6 .这意味着扩展默认情况下是使用工具链构建的,我认为该工具链模仿 gcc-4.2 ,特别是在搜索的 stdlib 方面.

Hence, it has the environment variable MACOSX_DEPLOYMENT_TARGET=10.6. This means that extensions are built by default with a toolchain that, I think, mimics gcc-4.2, in particular in terms of what stdlib it searches.

过去,我已通过以下方法解决此问题:安装带有自制程序的较新的编译器,并在安装前显式设置 CC CXX 等.

In the past, I have fixed this by installing more recent compilers with homebrew and explicitly setting CC, CXX, etc before installation.

但是,我尝试仅设置 MACOSX_DEPLOYMENT_TARGET = 10.11 ,这似乎可行.这样安全吗?有什么缺点吗?(我不需要分发这些构建,而只在本地使用它们?)

However, I have tried just setting MACOSX_DEPLOYMENT_TARGET=10.11, and that seems to work. Is this safe? Are there any downsides? (I don't need to distribute these builds, just use them locally?)

推荐答案

为最近的Python版本构建的python.org OS X 64位/32位框架是使用 MACOSX_DEPLOYMENT_TARGET 设置为10.6构建的.并基于Mac OS X 10.6构建,以确保与各种OS X版本兼容.当前范围是从10.6雪豹到10.11 El Capitan.使用Python的内置Distutils或使用Distutils的高级工具(例如 pip )构建C或C ++扩展模块时,默认情况下,编译和链接环境的部署目标设置为解释器内部版本,因​​此在本例中为 10.6 ,以尝试生成与解释器内部版本相同版本的OS X发行版的扩展模块.几乎不需要更改它,因为Apple通常非常擅长保持Python本身使用的系统库和框架的向后兼容性.但是,您发现,可以通过在构建扩展模块之前设置 MACOSX_DEPLOYMENT_TARGET 环境变量来将部署目标覆盖到较新的版本.(Distutils检查并且不允许将部署目标设置为比解释器构建版本更早的版本.)Distutils还通过设置相应的标准"环境变量来覆盖其他各种构建值,例如 CC CXX CFLAGS LDSHARED 等.

The python.org OS X 64-bit/32-bit framework builds for recent Python releases were built with MACOSX_DEPLOYMENT_TARGET set to 10.6 and built on Mac OS X 10.6 to ensure compatibility with a wide range of OS X releases. Currently that range is from 10.6 Snow Leopard through 10.11 El Capitan. When building C or C++ extension modules using Python's build-in Distutils or high-level tools that use Distutils (such as pip), by default the deployment target for the compilation and link environment is set to that of the interpreter build, so in this case 10.6, to try to produce extension modules that will work with the same range of OS X releases as the interpreter build itself. There is seldom a need to change this since Apple generally is very good about maintaining backwards compatibility for the system libraries and frameworks used by Python itself. However, as you discovered, you can override the deployment target to a newer release by setting the MACOSX_DEPLOYMENT_TARGET environment variable prior to building the extension module. (Distutils checks for and does not allow setting the deployment target to a release older than that used for the interpreter build.) Distutils also honors overriding various other build values by setting the corresponding "standard" environment variables, such as CC, CXX, CFLAGS, LDSHARED, etc.

您可能需要更改部署目标的一种情况是,如果您正在处理C ++代码.正如其他地方广泛讨论的那样,在最近的版本中,Apple 一直在从基于 GCC 的 libstdc++ 迁移到 C++ 程序的 Clang/LLVM libc++ 标准库.苹果一直在出货.Python解释器及其提供的标准库完全不使用C ++,因此此问题不会影响Python本身.但是,如果您使用自己的或通过第三方程序包(例如,从PyPI下载)使用C ++编写的扩展模块(或链接到用C ++编写的第三方库),则可以需要注意,所有C ++代码都是使用相同的C ++标准库构建的,否则,请注意不同的C ++模块不会共享对象.我在这种C ++情况下没有亲身经验,因此我不确定什么是避免出现任何问题的最佳方法.但是快速检查可能是在所有扩展模块及其链接的共享库和框架上使用Apple的 otool 命令行实用程序,以找到对 libstdc ++ 和 libc ++ ,所以类似于从检查以下内容的输出开始:

One case where you might need to change the deployment target is if you are dealing with C++ code. As has been widely discussed elsewhere, in recent releases Apple has been in the process of migrating from the GCC-based libstdc++ to the Clang/LLVM libc++ standard library for C++ programs. Apple has been shipping both. The Python interpreter and its supplied standard library do not use C++ at all so this issue does not affect Python itself. But if you use extension modules that are written in C++ (or link to a third-party library written in C++), either your own or from a third-party package (downloaded from PyPI, for example), you may need to be careful that all of the C++ code is either built using the same C++ standard library or, if not, that the differing C++ modules do not share objects. I don't have personal experience with such C++ situations so I'm not sure what is the best way to avoid any problems that might arise. But a quick-and-dirty check might be to use Apple's otool command line utility on all extension modules and the shared libraries and frameworks they link with to find all references to either libstdc++ and libc++, so something like starting with inspecting the output from:

find -E . -regex '(.*\.so)|(.*\.dylib)' -exec  otool -L '{}' ';'

这篇关于OS X Python:我可以显式设置扩展名MACOSX_DEPLOYMENT_TARGET吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆