11

Since Apple have stopped distributing gfortran with Xcode, how should I compile architecture independent Fortran code? I have Mac OS X Mountain Lion (10.8), and XCode 4.4 installed, with the Command Line Tools package installed.

Apple's Native Compilers

As far as I can tell, the Xcode C / C++ / ObjC compilers use a fork of the GNU compiler collection, with llvm as a backend; the latter I figure enables compiling and optimising "universal" binaries, for both Intel and PPC architectures.

3rd party binary Fortran compilers

HPC

I've only found a single website that distributes a binary version of gfortran specifically for Mountain Lion: the HPC website. However, I failed to get this to compile SciPy, and later saw in SciPy's README that it is "known to generate buggy scipy binaries".

CRAN/R

SciPy's recommended (free) Fortran compiler is the one on CRAN's R server, but this has not been updated for Mountain Lion yet. They provide instructions and a script for Building a Universal Compiler, but, again, this hasn't been updated for Mountain Lion yet..

G95

The G95 project hasn't had an update since 2010, so I didn't try it.. Anyone tried this on Mountain Lion?

MacPorts

I guess this will be the easiest way to get gfortran installed, but port search gfortran comes up with nothing, and I've not had any joy with MacPorts in the past (no offence to MacPorts; it's looks like a very active project, but I've been spoilt with Linux package managers, my favourite manager being aptitude) so on Mac OS X I've compiled software and libraries from source code in the past. Never been a problem 'til now...

Building a Fortran compiler

Having dug around on the internet a lot in the last couple of days, I've found other Fortran compilers, but I've failed to get any to cross-compile universal binaries, or to compile SciPy.

GCC - The Gnu Compiler Collection

I compiled the entire GCC collection (v4.6.3), including autotools, automake, libtool and m4 - like the GCC wiki and this blog describe - but the resulting compilers didn't compile universal binaries, probably because LLVM wasn't used as a backend.

DragonEgg

DragonEgg is a "gcc plugin that replaces GCC's optimisers and code-generators ... with LLVM". This looks interesting, but I don't know how I could use it to compile 'llvm-gfortran-4.x'. Can this be done?

Compatibility

Libraries

The compiler that comes with Xcode is (a fork of?) GCC v4.2. But GCC's current release and development branches are versions 4.6 and 4.7, respectively. Apparently, a GNU license change, or something, stopped Apple from updating to more modern versions of GCC. So, if I was to build dynamic libraries made with GCC's gfortran v4.6, could they then be linked with C code compiled by Xcode's native compiler? At a minimum, I figure resulting Mach-O binaries need both x86_64 and i386 code paths. Do GCC provide backwards compatibility with Apple's (forks of?) GCC? I know gfortran has the -ff2c flag, but is this stable across versions?

Compile flags

The GCC Fortran compiler I built from source didn't support the use of the -arch compile flag. I had been including the flags -arch x86_64 -arch i386 in both CFLAGS and FFLAGS environment variables on earlier OSX versions (Snow Leopard to Lion). Python's distutils, and probably other OSX compilers, expect these flags to work, when configured to build apps or frameworks, using Xcode's universal SDK.

In case you're wondering what compile flags I use, I've uploaded the script I use to pastebin, which I source before I compile anything, using: source ~/.bash_devenv.

The Ideal OSX Fortran Compiler

  • Create ppc and intel (32 and 64bit) universal binaries, specified by using the -arch flags.
  • Makes binaries compatible with XCode's linker.
  • Compiles SciPy, giving no errors (compatible with numpy's distutils and f2py).

I don't use Xcode so much, but integration with it would surely benefit other users. Even Intel are still having problems integrating ifort into Xcode 4.4, so this is not something I expect to work..

If you read all the above, then thank you! You can probably tell that I'm not averse to building my own Fortran compiler from source, but is it even possible? Have I missed something? A configure flag maybe? And if such a compiler is not available yet, then why not?!

(Update:) Apple's GCC

Apple provide the source code for their patched version of GCC, at opensource.apple.com. This actually includes the source code for gfortran, but what do you know - it doesn't compile (easily). I'm in the process of writing a build script to get this to work. Unfortunately, I've had to apply a couple of patches, and learn about "the Apple way" of building GNU software. This is the way to go I think. Any reasons why it shouldn't be? I'll update with an answer if I get it to work...

4

5 回答 5

4

从http://r.research.att.com/tools/gcc-42-5666.3-darwin11.pkg安装 gfortran 后,我设法编译,如此所述。不过,我不得不尝试打开包裹几次。第一次说只能安装 App Store 中的应用程序。安装 gfortran 后,python setup.py build工作python setup.py install正常。scipy 的单元测试虽然给出了相当多的失败率,但不确定它是否正常。

Ran 5481 tests in 82.079s

FAILED (KNOWNFAIL=13, SKIP=42, errors=11, failures=72)
<nose.result.TextTestResult run=5481 errors=11 failures=72>
于 2012-10-03T14:01:29.213 回答
4

如果您还没有注意到这一点:在较新版本的 Xcode 中,您必须通过以下方式显式安装命令行工具:

首选项 -> 下载 -> 组件

然后单击命令行工具的“安装”按钮。这包括 gfortran:

> gfortran -v
Using built-in specs.
Target: i686-apple-darwin10
Thread model: posix
gcc version 4.2.1 (Apple Inc. build 5664)

诚然,这并不能解决我所有的 fortran 需求(在某些情况下,“./configure”脚本会抱怨它们不能“编译一个简单的 fotran 程序”)。

于 2012-10-16T10:02:18.857 回答
2

您可以使用 brew (或Homebrew)来安装 gfortran。

$ 酿造安装 gfortran

于 2013-07-27T11:30:49.423 回答
2

我知道你说你不喜欢 MacPorts,但如果你安装了这个gcc48端口,它实际上包括gfortran(尽管你还必须做sudo port select --set gcc mp-gcc48它来设置名为 的符号链接gfortran)。

此外,FWIW,MacPorts 选项不一定是二进制文件 - MacPorts 实际上可以从源代码构建它,这就是为什么有时需要一段时间。另一方面,它有时似乎也会从某个地方获取存档的二进制文件,但我认为这取决于 portfile 的原始作者上传的内容。

于 2013-11-01T14:32:02.450 回答
1

我最终编译了苹果开发者工具源代码页面提供的源代码 gfortran 。现在这似乎工作正常 - 我已经成功编译了 x86-64 和 i386/i686 LAPACK、ATLAS 和 BLAS fortran 库 - 但是make -k test在构建目录中运行时,有一些 ranlib 测试失败。(如果有人愿意,我可以提供有关该 pastebin 或其他地方的更多信息......)

构建过程

问完这个问题,我下载了苹果的llvmgcc42 源代码 tar 存档,里面包含了 llvm/gcc C、C++、ObjC 和 fortran 编译器的源代码,并花了一些时间尝试编译 gfortran 的通用版本。在我的四核 2.8GHz Mac Pro 上构建大约需要 30-60 分钟,并且变得相当复杂,所以我为它编写了一组构建脚本,我已经在github.com上分享了这些脚本。

……

如果有人想要一份副本,我会暂时在此处保留我的构建的 tar 存档。(2012 年 9 月 26 日更新)它只有在安装了前缀 /usr/local/ 时才有效,除非您install_name_tool在可执行文件和 dylib 上运行,否则将前缀从 /usr/local 更改为您想要放置的任何位置它。您可以使用(有关原因的更多信息在这里)测试install_name 。otool -L filename

我现在使用的最终版本还包括对 gcc/fortran 和 libgfortran 目录的更新,这是我从 GNU GCC 4.2.4 获得的。我从本地GCC 的镜像中获得这些资源。4.2.1 和 4.2.4 之间只有微小的变化,构建脚本包括升级代码所需的补丁。

我编写的build-gfortran.sh 脚本会下载缺少的依赖项(mpfrgmp),编译和交叉编译它们,使用依赖于架构的预处理器宏修补不同的头文件,并运行 lipo 来创建通用二进制文件和库,最终同时支持 i386 和x86_64 架构。llvmCore 和 GCC 的过程类似。我主要从 Apple 的 llvmgcc42 提供的build_llvmbuild_gcc bash 脚本中复制代码,但其中一些必须修改,包括一些lipoinstall_name_tool命令。

使用 Xcode 编译 Apple 的 gcc 的官方方法gnumake对我不起作用。我认为这应该通过将“fortran”添加到 build_gcc 中的 LANGUAGES 变量来工作。

关于编译 Scipy,仍然无法完美地构建该建筑物。我不得不使用 clang 和 clang++ 作为 C/C++ 编译器,否则我会收到 EXC_BAD_ACCESS malloc 错误。没有尝试过我构建的 gcc/g++ 编译器,只是使用了系统编译器。这是在 Scipy安装页面上为 Lion 报告的。我有 11 个错误和 1 个失败,它们都是由相同的 3 个函数调用(_fitpack._bspleval、numeric.asarray、testing.utils.chk_same_position)引发的。认为这很好,但我希望每个测试都能通过......

于 2012-09-17T13:22:28.810 回答