Working LDC iOS (iPhone) on github

Dan Olson zans.is.for.cans at yahoo.com
Fri Feb 14 01:02:20 PST 2014


I think the LDC + iOS stuff it is working well enough that others might
have fun with it too.  I made a fork of ldc/druntime on github and
pushed up changes into branch ios-2.064.  It is based on ldc merge-2.064
branch.  You will find very few changes were needed to get this far.  It
has been mostly a game of finding the right recipe and stubbing out
missing functions for iOS.

What can you do with it?  Well, as far as I know, most of D 2.064
appears to work with the exception of exceptions (that came out nice)
and thread locals.  I am sure there are many pits and holes to fall into
though.

In general:
- TLS support not available so I disabled for now
- Threads "work", but there are unfinished pieces and a wumpus hiding
  here (e.g. not sure what gc will do when it triggers)
- You can throw exceptions, but they have a tragic ending
- debug info does not work and -g causes link warnings
- things are hardcoded for iOS in a few places, so this branch may not
  work as a native compiler).  For example, I had to hardcode real type
  as a 64-bit double (dmd uses host's long double type for real)

To compile the C portions of druntime/phobos, you will need an iPhone
SDK downloaded from Apple, which means you need to use OSX (mac) as
the host.  To actually run on hardware (iPhone 4 in my case) I signed up
as an iOS developer.  D objs and libs can then be added to an Xcode
project and run in the debugger.

Follow normal ldc build instructions on D wiki with some differences
below.

http://wiki.dlang.org/Building_LDC_from_source.

Must use llvm-3.4 or newer.  I am confguring llvm with all targets
enabled (llvm/configure with no options).

$ git clone --recursive https://github.com/smolt/ldc.git
$ mkdir thumb7-ios-ldc
$ cd thumb7-ios-ldc
$ cmake -DD_FLAGS='-w;-d;-mtriple=thumbv7-apple-ios5.0.0;-mcpu=cortex-a8;-disable-fp-elim;-float-abi=softfp;-vectorize-slp' -DD_FLAGS_DEBUG='' -DD_FLAGS_RELEASE='-Os;-release' -DTARGET_C_FLAGS='-target armv7-apple-darwin -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS7.0.sdk' ../ldc
$ make

Then to compile a simple program with D as the main:

$ cat <<XYZZY >hello.d
import std.stdio;

void main()
{
    writeln("Hello from D");
}
XYZZY

$ thumb7-ios-ldc/bin/ldc2 -mtriple=thumbv7-apple-ios5.0.0 -disable-fp-elim -float-abi=softfp -Os -c hello.d

Make a simple (empty) Xcode project for iOS and remove the objc main.m
(or disabled it).  Then add the resulting hello.o plus
thumb7-ios-ldc-3.4/lib/libphobos-ldc-debug.a.  There are no debug
symbols but at least asserts are enabled, which I think is a good thing
in this new world.  Build and run.

Alternatively, you can use the objc main() and have it call an extern
(C) D function.  But remember to call rt_init() first thing.  You can do
it the objc main() as in:

int main()
{
   extern int rt_init();
   extern void someDfunc();
   if (rt_init()) {
      someDfunc();
   }

   ...

}

I hope it works for somebody besides me.  Thanks to everyone who helped
so far.
-- 
Dan


More information about the digitalmars-d-ldc mailing list