如何在预处理时可靠地检测clang的版本? [英] How can I reliably detect the version of clang at preprocessing time?

查看:108
本文介绍了如何在预处理时可靠地检测clang的版本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

显然,与Xcode捆绑在一起的clang不尊重上游 __ clang_major __ __ clang_minor __ 的值,而是报告

Apparently, the clang bundled with Xcode doesn't respect the upstream __clang_major__ and __clang_minor__ values, and instead reports an Xcode user-facing version of some sort.

此处提供了各种MacPorts clang安装值,以供参考。他们似乎尊重上游发行标识符。在Linux上进行测试时,我会得到类似的值。

Here are, for reference, the values for the various MacPorts installs of clang. They seem to respect the upstream release identifiers. I get similar values when testing on Linux.

➜  prohibit-clang-3.2  /opt/local/bin/clang++-mp-3.2 -dM -E -x c /dev/null |
grep __clang_m
#define __clang_major__ 3
#define __clang_minor__ 2

➜  prohibit-clang-3.2  /opt/local/bin/clang++-mp-3.3 -dM -E -x c /dev/null |
grep __clang_m
#define __clang_major__ 3
#define __clang_minor__ 3

➜  prohibit-clang-3.2  /opt/local/bin/clang++-mp-3.4 -dM -E -x c /dev/null |
grep __clang_m
#define __clang_major__ 3
#define __clang_minor__ 4

但是,由于某种原因,Apple提供的clang具有跟踪Xcode版本的 __ clang_major __ __ clang_minor __ 版本,而不是基本的clang修订版:

However, for some reason, the Apple provided clang has __clang_major__ and __clang_minor__ versions that track the Xcode version, not the base clang revision:

➜  prohibit-clang-3.2 
/Applications/Xcode-4.6.3.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang++
-dM -E -x c /dev/null | grep __clang_m
#define __clang_major__ 4
#define __clang_minor__ 2

➜  prohibit-clang-3.2 
/Applications/Xcode-4.6.3.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang++
--version
Apple LLVM version 4.2 (clang-425.0.28) (based on LLVM 3.2svn)
Target: x86_64-apple-darwin12.5.0
Thread model: posix

➜  prohibit-clang-3.2 
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang++
-dM -E -x c /dev/null | grep __clang_m
#define __clang_major__ 5
#define __clang_minor__ 0

➜  prohibit-clang-3.2 
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang++
--version
Apple LLVM version 5.0 (clang-500.2.76) (based on LLVM 3.3svn)
Target: x86_64-apple-darwin12.5.0
Thread model: posix

➜  prohibit-clang-3.2  /usr/bin/clang++ -dM -E -x c /dev/null | grep __clang_m
#define __clang_major__ 5
#define __clang_minor__ 0

太糟糕了,因为这意味着您无法编写如下条件编译行,这些行将在Apple供应的clang和从clang源代码或发行版软件包正常构建的clang上都能正常工作:

This seems pretty awful, because it means you can't write conditional compilation lines like the following that will work accurately across both the Apple vendored clang, and clang built normally from the clang sources or from distro packages:

#if !defined(__clang__) || (__clang_major__ > 3) || ((__clang_major__ == 3) && (__clang_minor__ > 2))
// Thing that doesn't work for clang-3.2 due to an optimization bug.
#endif

我不想将已经很糟糕的预处理程序检查扩展到解释了两种不同的clang版本控制方案,一种是普通的,另一种是Apple。我什至不知道如何可靠地检测到它是 Xcode clang而不是普通clang。

I'd hate to need to extend that already pretty terrible preprocessor check to account for two different clang versioning schemes, one normal and one Apple. I'm not even sure how I could reliably detect that this is 'Xcode clang' rather than normal clang.

有人对如何解决这个问题有任何建议吗?是否有标记传递给Apple的叮当声,指示其报告真实版本?还是苹果注定了我们永远无法可靠地使用 __ clang_major __ __ clang_minor __ ?这些不是我应该在这里使用的宏吗?

Does anyone have any suggestions on how to work around this? Is there a flag to pass to Apple's clang that will instruct it to report its 'true' version? Or has Apple doomed us to never be able to reliably use __clang_major__ and __clang_minor__? Are these not the macros I should be using here?

推荐答案

如果功能检查宏没有涵盖您需要检查的内容,那么您确实需要分别对待供应商发布的内容与开放源LLVM版本的内容截然不同。您可以使用 __ apple_build_version __ 检查特定的Apple LLVM版本。

If the feature checking macros don't cover what you need to check for then you do need to treat vendor releases separately as they may have very different content from the open source LLVM releases. You can use __apple_build_version__ to check for a specific Apple LLVM version.

这篇关于如何在预处理时可靠地检测clang的版本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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