Is there a Fortran compiler/emulator for iOS?
Solution 1:
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:
- Install GCC 4.8 from HPC for Mac OS X into /usr/local
- Install Clang/LLVM 3.5 from llvm.org into /usr/local
- Download the Dragonegg source code from the LLVM website
- Build dragonegg.so and install it to /usr/local/lib/gcc/x86_64-apple-darwin12.5.0/4.8.1/plugin
- Download GCC 4.8.1 sources
- 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
- Install libgfortran.dylib and libgfortran.3.dylib to /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS8.1.sdk/usr/lib
- 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:
- gfortran -O3 -fplugin=dragonegg -fplugin-arg-dragonegg-llvm-codegen-optimize=3 -fplugin-arg-dragonegg-emit-ir -S -o main.s main.f
- llc -mtriple arm64-apple-darwin -filetype=obj -o main.o main.s
- 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.
Solution 2:
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/