Psellos
Life So Short, the Craft So Long to Learn

OCaml on iOS 7 Simulator, Progress Report

August 30, 2014

Some indie gypsy polka-punk developers across the globe have now been asking me about the status of the OCaml-on-iOS-Simulator project. I’ve made some progress here, too, and yesterday I built the first iOS app with OCaml 4.01.0, and ran it successfully in the iPhone Simulator (iOS 7.1).

Gamut app burnt orange screen

To get it working I applied the OCaml 4.00.0 OCaml-on-iOS-Simulator patches to the OCaml 4.01.0 compiler. The patches are quite simple, probably because the ABI of the 32-bit Intel architecture is well standardized. So there’s nothing to change in the code generator.

Then I updated the xsim-build script that builds ocamlopt as a cross-compiler to the Simulator environment. xsim-build is described in Compile OCaml for iOS Simulator (soon to be updated).

There were two small problems with the generated compiler.

First, the clang toolchain tracks the difference between OS X and the Simulator environment. (I wrote about the differences in iOS Simulator Vs. OS X.) You need to tell the compiler you’re compiling for iOS, or you’ll see an error like this at link time:

ld: building for MacOSX, but linking against dylib built for iOS Simulator file

The fix for this is to define a desired minimum version of iOS. What I’m using for now is this command-line flag for clang:

-miphoneos-version-min=6.0

Note that this will change the set of defined preprocessor symbols, and so may require some adaptation in your conditional compilation directives.

Second, there were many warnings like this at link time:

ld: warning: could not create compact unwind for _caml_curry4_2_app

I believe this happens because the OCaml code generator doesn’t generate metadata to support “compact unwind.” This is something I know nothing about. I could learn, but if you, reader, know something about it I’d be very happy to get some help.

For now, things seem to work if you turn off compact unwind with the following undocumented linker flag:

-no_compact_unwind

One cool thing about compiling for the simulator is that you can run the generated code, if it’s simple enough, from the OS X command line. Here’s a session showing how to do it:

$ BIN=/usr/local/ocamlxsim/bin
$ cat howitends.ml
let main () = Printf.printf "You already know how this will end.\n"

let () = main ()
$ $BIN/ocamlopt -o howitends howitends.ml
$ HIDEOUT=/Applications/Xcode.app/Contents/Developer
$ PLAT=$HIDEOUT/Platforms/iPhoneSimulator.platform
$ SDK=/Developer/SDKs/iPhoneSimulator7.1.sdk
$ DYLD_ROOT_PATH=$PLAT$SDK howitends
You already know how this will end.

If you’re interested, there are more details in iOS Simulator Vs. OS X.

Once the compiler was working I built the Gamut app, which does just enough to test whether OCaml is working in the Simulator. As far as I can tell, it’s working perfectly.

I want to try a few more things before releasing this version to the savants and lovers of wisdom. If I don’t find any serious problems, a new release will be coming soon. In the meantime, the elder savants have released a new version of OCaml, 4.02.0, for which we can only thank them. Support for this new release will come in due time.

If you have any comments or encouragement, leave them below or email me at jeffsco@psellos.com.

Posted by: Jeffrey

Comments

blog comments powered by Disqus