This repository contains a 32 bit and 64 bit cross-compiler for OCaml
on iOS SDK 7.0-9.2,
arm64 respectively. Do read this
README, it has instructions on how to install, compile and codesigning
for jailbroken phones.
You do not need to have a jailbroken iOS device to run OCaml, you can use the compiler to make an object file out of the OCaml code and then use OCaml as a C library from your Objective-C programs.
To run OCaml on jailbroken phones, that is to make arbitrary pure OCaml
executables, you need to use
ldid to sign your binary.
Installation, either 32-bit and 64-bit
First create a fresh switch, it must be 4.02.3:
$ opam switch ios -A 4.02.3
Then add this repository with:
$ opam repository add ios git://github.com/fxfactorial/opam-ios
Now you have a choice, do you want the 32 bit for armv7 or 64-bit compiled for arm64 cross-compiler.
$ opam install ios-thirty-two-bit
$ opam install ios-sixty-four-bit
Now you can compile OCaml that will run on the iPhone!
Example for jailbroken iDevices
Here is a stupid server, mostly useful to show how one can use the Unix module with no problem on the iPhone.
open UnixLabels let () = let sock = socket ~domain:PF_INET ~kind:SOCK_STREAM ~protocol:0 in bind sock ~addr:(ADDR_INET (inet_addr_any, 3000)); print_endline "About to listen"; listen sock 5; let resp = "Thanks for the Test!\n" in let buf = String.create 255 in while true do let (listen_sock, listen_addr) = accept sock in (ignore (read listen_sock ~buf ~pos:0 ~len:255)); print_endline buf; (ignore @@ write listen_sock ~buf:resp ~pos:0 ~len:(String.length resp - 1)); Unix.close listen_sock done
Compile with (same for 32-bit, 64-bit):
$ ocamlopt -o The_server unix.cmxa server.ml
Now you need to code sign the binary. You can use
brew or Apple’s native
$ ldid -S your_binary
first need to get your developer hash account:
$ security find-identity -pcodesigning -v 1) XXXXX "Some Account: Fool bar" ...
And now use
XXXXX to sign your binary:
$ codesign --deep --sign XXXXX The_server
(You can also try using
ldid on the device directly, its in Cydia).
And here’s an example of it working: (The invocation uses ocamloptrev, this was an old version created from a previous verion offered by this repository but the Unix code is exactly the same)
For an example of calling OCaml from Objective-C, see this blog post
This won’t work with other opam packages just yet, I am still working on that. The only thing you’ll have at your disposal for now is the OCaml standard library, which does include unix.
The real heroes are Gerd Stolpmann, awesome guys at Psellos.com; special shout out to Jeffrey Scofield.
Also thanks to @whitequark for his
opam-android repo. Although I
ended up going in a different approach, his repo was a great starting
point for me.