Has anyone come across a Fortran 77 emulator or compiler for iOS devices?

  • 1
    Why would you want one? – Fake Name Feb 16 '12 at 6:14
  • @FakeName Numeric and machine learning libraries, as a few examples. – adib Dec 1 '16 at 2:38

It can be accomplished using the Dragonegg plugin for GCC (frontend), a generic LLVM installation (backend), and the Apple linker. I have been able to successfully build and run a 64-bit Fortran application on iOS 8.1 built on Mac OS X 10.10 using GCC 4.8 and LLVM 3.5. The process is not very straightforward and requires a number of hacks and some scripting, but the high-level steps are:

  1. Install GCC 4.8 from HPC for Mac OS X into /usr/local
  2. Install Clang/LLVM 3.5 from llvm.org into /usr/local
  3. Download the Dragonegg source code from the LLVM website
  4. Build dragonegg.so and install it to /usr/local/lib/gcc/x86_64-apple-darwin12.5.0/4.8.1/plugin
  5. Download GCC 4.8.1 sources
  6. Build libgfortran for arm64 using a combination of Apple clang for C/C++, Dragonegg for Fortran, and some wrapper scripts to piece together the compilers
  7. Install libgfortran.dylib and libgfortran.3.dylib to /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS8.1.sdk/usr/lib
  8. Install libgfortran.3.dylib on the iPhone under /usr/local/lib

Now if you got this far, compile a Fortran source file into an iOS binary using:

  1. gfortran -O3 -fplugin=dragonegg -fplugin-arg-dragonegg-llvm-codegen-optimize=3 -fplugin-arg-dragonegg-emit-ir -S -o main.s main.f
  2. llc -mtriple arm64-apple-darwin -filetype=obj -o main.o main.s
  3. ld -arch arm64 -syslibroot /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS8.1.sdk -lSystem -lgfortran main.o -o main

Precompiled binaries for Dragonegg and libgfortran can be found here: http://download.zeroepoch.com/gfortran-ios/gfortran-ios.tar.bz2

UPDATE: The precompiled binaries archive now contains fat libraries (armv7s, arm64) and some bug fixes to the wrapper scripts.

  • I found this link useful for building dragonegg.so, stackoverflow.com/questions/26009274/… – Eric Nov 21 '14 at 9:45
  • FYI, I was able to compile all of the Fortran benchmarks from SPEC FP for iOS using this method. – Eric Nov 29 '14 at 10:31
  • Could you be more specific with your 6th step, build libgfortran for arm64? I'm trying to find a way to built it but I did not succeed. – Alessandra Siqueira da Silva P Feb 24 '15 at 16:05
  • I had to get this work done quickly so I didn't take notes, but I didn't want to lose the binaries. I just remember I had to hack the source quite a bit. The majority of the trouble was setting the fortran compiler to my wrapper script since gfortran has some fortran libraries inside. – Eric Feb 25 '15 at 18:33
  • Thank you very much for the idea. It works. Regarding the 8th step, I was not able to copy the dylib into the proposed location. I had to change the location like this: install_name_tool -id @executable_path/../Frameworks/libgfortran.3.dylib libgfortran.3.dylib. In XCode a Copy build step is needed. Copy libgfortran.3.dylib and set the destination to Executables. – timomeinen Jan 23 '16 at 16:20

Perhaps it's best done with a pre-compiler translating Fortran to C (and integrate that into Xcode as a separate build step). One such tool is e.g. http://www.greatmigrations.com/tools_gmFortran.aspx

Maybe one can also have a look at the gnu fortran compiler, as far as I remember it also started out as a precompiler translating Fortran to C. http://gcc.gnu.org/fortran/

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .