Psellos
Life So Short, the Craft So Long to Learn

OCaml 4.01 for iOS 8 Simulator

December 19, 2014

OCamlXARM compiles for an iOS device, but OCamlXSim compiles for an iOS Simulator. The same ocamloptrev script that compiles OCaml for iOS 8 can also get OCamlXSim to compile OCaml for the iOS 8 Simulator. The only thing that changes is the location of the compiler.

If you want to try out OCaml on the iOS 8 Simulator, here is an update to the script that compiles for either an iOS device or an iOS Simulator (ocamloptrev):

#!/bin/bash
#
# ocamloptrev     ocamlopt for specified iOS revision
#
USAGE='ocamloptrev  -rev M.N  [ -sim ]  other-ocamlopt-options ...'

OCAMLDIR=/usr/local/ocamlxarm/v7
OCAMLSIMDIR=/usr/local/ocamlxsim

REV=''
SIM=n
declare -a ARGS
while [ $# -gt 0 ] ; do
    case $1 in
    -rev)
        if [ $# -gt 1 ]; then
            REV=$2
            shift 2
        else
            echo "$USAGE" >&2
            exit 1
        fi
        ;;
    -sim)
        SIM=y
        shift
        ;;
    *)  ARGS[${#ARGS[*]}]="$1"
        shift 1
        ;;
    esac
done
if [ "$REV" = "" ]; then
    echo "$USAGE" >&2
    exit 1
fi

HIDEOUT=/Applications/Xcode.app/Contents/Developer 

case $SIM in
y)  PLT=$HIDEOUT/Platforms/iPhoneSimulator.platform 
    SDK=/Developer/SDKs/iPhoneSimulator${REV}.sdk 
    OCAMLC=$OCAMLSIMDIR/bin/ocamlopt
    ;;
n)  PLT=$HIDEOUT/Platforms/iPhoneOS.platform 
    SDK=/Developer/SDKs/iPhoneOS${REV}.sdk 
    OCAMLC=$OCAMLDIR/bin/ocamlopt
    ;;
esac

$OCAMLC -ccopt -isysroot -ccopt "$PLT$SDK" "${ARGS[@]}"

To compile for the iOS Simulator, specify -sim along with -rev M.N.

Let’s make a tiny OCaml program for testing:

$ Q="Do you know what it's like on the outside?\\n"
$ echo "Printf.printf \"$Q\"" > ny1941.ml

Here’s what happens if you compile with the current OCamlXSim on a system with the iOS 8.1 SDK:

$ /usr/local/ocamlxsim/bin/ocamlopt -o ny1941 ny1941.ml
clang: warning: no such sysroot directory: '/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator7.1.sdk'
ld: library not found for -lSystem
clang: error: linker command failed with exit code 1 (use -v to see invocation)
File "caml_startup", line 1:
Error: Error during linking

As you can see, it’s trying and failing to use the default iOS Simulator 7.1 SDK. Here’s how to use ocamloptrev (the above script):

$ ocamloptrev -sim -rev 8.1 -o ny1941 ny1941.ml
$ ls -l ny1941
-rwxr-xr-x  1 jeffsco  staff  303364 Dec 19 23:02 ny1941
$ file ny1941
ny1941: Mach-O executable i386
$

You can actually run an iOS simulator app from the OS X command line, though there are many things that don’t work properly.

$ ny1941
Do you know what it's like on the outside?
$

See iOS Simulator Vs. OS X for a description of some differences between the OS X and the iOS Simulator environments.

If you don’t specify -sim, the script compiles for an iOS device as before:

$ ocamloptrev -rev 8.1 -o ny1941 ny1941.ml -cclib -Wl,-no_pie
$ file ny1941
ny1941: Mach-O executable arm
$ 

When not working in the subbasement of my alma mater, I’m working in my cluttered underground workroom on several OCaml-on-iOS projects. Along with holiday joys and the delights of coding in node.js during the day, I’ll keep working through them as fast as I can.

I hope this script will be useful for folks who want to try OCaml on the iOS Simulator while I’m updating my humble patches to the latest versions of everything and keeping all the irons in the fire.

If you have any trouble (or success) with the script, or have any other comments, leave them below or email me at jeffsco@psellos.com.

Posted by: Jeffrey

Comments

blog comments powered by Disqus