Psellos
Life So Short, the Craft So Long to Learn

OCaml 4.01 for iOS 8

December 9, 2014

The current OCamlXARM compiles by default for iOS 7.1, but using it for iOS 8 is not too difficult. The only thing that changes is the name of the SDK, which can be specified with the -ccopt option of ocamlopt.

I’ve been testing OCaml apps on iOS 8.1 recently, and I wrote a script that runs the OCamlXARM version of ocamlopt with the correct options. In fact it lets you specify the revision of iOS that you want to compile for.

If you want to try out OCaml on iOS 8, this script should work for you (ocamloptrev):

#!/bin/bash
#
# ocamloptrev     ocamlopt for specified iOS revision
#
USAGE='ocamloptrev  -rev M.N  other-ocamlopt-options ...'
REV=''
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
        ;;
    *)  ARGS[${#ARGS[*]}]="$1"
        shift 1
        ;;
    esac
done
if [ "$REV" = "" ]; then
    echo "$USAGE" >&2
    exit 1
fi
HIDEOUT=/Applications/Xcode.app/Contents/Developer 
PLT=$HIDEOUT/Platforms/iPhoneOS.platform 
SDK=/Developer/SDKs/iPhoneOS${REV}.sdk 
OCOPTS="-ccopt -isysroot -ccopt $PLT$SDK" 
/usr/local/ocamlxarm/v7/bin/ocamlopt $OCOPTS "${ARGS[@]}"

Let’s make a tiny OCaml program for testing:

$ echo "Printf.printf \"There's a light up above\n\"" > bbjohn.ml

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

$ /usr/local/ocamlxarm/v7/bin/ocamlopt -o bbjohn bbjohn.ml
clang: warning: no such sysroot directory: '/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS7.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 iOS 7.1 SDK. Here’s how to use ocamloptrev (the above script):

$ ocamloptrev -rev 8.1 -o bbjohn -cclib -Wl,-no_pie bbjohn.ml
$ ls -l bbjohn
-rwxr-xr-x+ 1 jeffsco  staff  238272 Dec  9 20:55 bbjohn
$ file bbjohn
bbjohn: Mach-O executable arm

I have run the generated executables under iOS 8.1, and they work for me.

I’m continuing to work with passion, in the evenings, in my metaphorically lonely atelier in the subbasement, on several OCaml-on-iOS projects. In fact one is potentially quite exciting. Thanks for all the support from correspondents as I work through them as fast as I can.

I hope this script will be useful for folks who want to try OCaml on iOS while I’m updating the release 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