OCaml + Termux

OCaml native on android!

NB since termux updated to binutils >= 2.29 only OCaml versions >= 4.06.0 will work (thx Kyle Stemen).

Easy way

Use pre-built OPAM for aarch64 to build OCaml

  1. Install OPAM

    echo "deb [arch=all,aarch64] http://ygrek.org/files/debian/termux ./" >> "$PREFIX/usr/etc/apt/sources.list"
    apt-get update # repository is not signed for now :]
    apt install opam
  2. Install OCaml

    apt install make clang
    opam init --comp=4.06.1+termux

True way

Build OCaml to build OPAM to build OCaml

NB termux lacks /bin/sh (and all other standard unix file paths for that matter), so the main problem during builds is hardcoded shell path in shebangs. To overcome it - remember to use sh ./script instead of ./script.

  1. Remove opam and ocaml packages if present, remove ~/.opam to have clean state

  2. Prepare proper build environment. NB termux comes with busybox, hence many utilities are present, but with limited features, need to install separate packages.

    apt install coreutils clang curl dash diffutils grep m4 make ncurses patch pkg-config

  3. Build OCaml

    mkdir ~/tmp
    export TMPDIR="$HOME/tmp" # add to ~/.profile
    git clone https://github.com/ygrek/ocaml.git -b termux-4.06.1
    cd ocaml
    sh ./configure -prefix "$PREFIX"
    make world.opt install
  4. Build OPAM

    curl -LO https://github.com/ocaml/opam/releases/download/1.2.2/opam-full-1.2.2.tar.gz
    tar -xzf opam-full-1.2.2.tar.gz
    cd opam-full-1.2.2/
    sed -i 's|/bin/sh|sh|' src/core/opamSystem.ml OCamlMakefile
    CONFIG_SHELL=sh sh ./configure -prefix "$PREFIX"
    OCAMLPARAM="safe-string=0,_" make lib-ext all install
  5. Init OPAM (will use compiler built in step 3 as system switch)

    opam init
  6. Install OCaml via OPAM and remove system OCaml (built in step 3) to avoid confusion with OPAM switches

    opam sw 4.06.1+termux
    rm "$PREFIX"/usr/man/man1/ocaml*
    rm "$PREFIX"/usr/bin/ocaml*
    rm -rf /data/data/com.termux/files/usr/lib/ocaml
    opam sw remove system