All Projects → ocaml-cross → opam-cross-ios

ocaml-cross / opam-cross-ios

Licence: other
An OCaml cross-toolchain for iOS and several useful libraries

Programming Languages

shell
77523 projects

Projects that are alternatives of or similar to opam-cross-ios

Glsl Optimizer
GLSL optimizer based on Mesa's GLSL compiler. Used to be used in Unity for mobile shader optimization.
Stars: ✭ 1,506 (+2496.55%)
Mutual labels:  cross-compiler
conex
establish trust in community repositories, cryptographically signed by library authors
Stars: ✭ 32 (-44.83%)
Mutual labels:  opam
docker-darling
Experimental docker container with Darling for macOS runtime
Stars: ✭ 38 (-34.48%)
Mutual labels:  cross-compiler
Mingw W64 Build
MinGW-w64 build script
Stars: ✭ 124 (+113.79%)
Mutual labels:  cross-compiler
opam-coq-archive
Archive for all Coq related OPAM packages organized in various repositories
Stars: ✭ 101 (+74.14%)
Mutual labels:  opam
chalk
🖍️🎨Composable and simple terminal highlighting package for OCaml.
Stars: ✭ 29 (-50%)
Mutual labels:  opam
Opam Cross Windows
An OCaml cross-toolchain for Windows and several useful libraries
Stars: ✭ 86 (+48.28%)
Mutual labels:  cross-compiler
CrossMobile
Create native iOS, Android, Windows Phone and Desktop applications in Java. Write it once, and produce sophisticated multiplatform applications.
Stars: ✭ 48 (-17.24%)
Mutual labels:  cross-compiler
docker-ocaml
Minimum docker images for OCaml+OPAM
Stars: ✭ 15 (-74.14%)
Mutual labels:  opam
xcross
"Zero Setup" cross-compilation for C/C++. Supports numerous architectures, build systems, C standard libraries, vcpkg, and Conan.
Stars: ✭ 29 (-50%)
Mutual labels:  cross-compiler
Proton Clang
Proton Clang toolchains builds in the form of a continuously updating Git repository. Clone with --depth=1.
Stars: ✭ 126 (+117.24%)
Mutual labels:  cross-compiler
Ataraxia
Simple and lightweight source-based multi-platform Linux distribution with musl libc.
Stars: ✭ 226 (+289.66%)
Mutual labels:  cross-compiler
ocamlbyexample
Learn Ocaml by reading code examples
Stars: ✭ 18 (-68.97%)
Mutual labels:  opam
Yolov3 On Android
Build an Android App for deploying YOLO V3 source code on mobile phone directly.
Stars: ✭ 113 (+94.83%)
Mutual labels:  cross-compiler
xs-opam
Opam repository for OCaml libraries to build Citrix Hypervisor toolstack components
Stars: ✭ 14 (-75.86%)
Mutual labels:  opam
Opam Cross Android
An OCaml cross-toolchain for Android and several useful libraries
Stars: ✭ 93 (+60.34%)
Mutual labels:  cross-compiler
mkocaml
🔨 Tool to generate OCaml projects
Stars: ✭ 59 (+1.72%)
Mutual labels:  opam
ocaml-semver
Semantic version handling for OCaml
Stars: ✭ 16 (-72.41%)
Mutual labels:  opam
build-gcc
Shell scripts to build various gcc cross-compilers (primarily djgpp)
Stars: ✭ 34 (-41.38%)
Mutual labels:  cross-compiler
Mirage
MirageOS is a library operating system that constructs unikernels
Stars: ✭ 1,707 (+2843.1%)
Mutual labels:  opam

opam-cross-ios

This repository contains an up-to-date iOS toolchain featuring OCaml 4.04.0, as well as some commonly used packages.

The supported build systems is OS X 10.9 and later. The supported target systems are 32-bit and 64-bit x86 iPhone simulator and ARM iPhone.

If you need support for other platforms or versions, please open an issue.

Installation

Add this repository to OPAM:

opam repository add ios git://github.com/whitequark/opam-cross-ios

Switch to 32-bit compiler when compiling for 32-bit targets:

opam switch 4.04.0+32bit
eval `opam config env`

Otherwise, use a regular compiler; its version must match the version of the cross-compiler:

opam switch 4.04.0
eval `opam config env`

Pin some prerequisite packages that don't yet have fixes merged upstream:

opam pin add ocamlbuild https://github.com/ocaml/ocamlbuild.git
opam pin add topkg https://github.com/whitequark/topkg.git#ios

Configure the compiler for 32-bit ARM:

ARCH=arm SUBARCH=armv7s PLATFORM=iPhoneOS SDK=9.3 VER=8.0 \
  opam install conf-ios

... for 64-bit ARM

ARCH=arm64 SUBARCH=arm64 PLATFORM=iPhoneOS SDK=9.3 VER=8.0 \
  opam install conf-ios

... for 32-bit x86:

ARCH=i386 SUBARCH=i386 PLATFORM=iPhoneSimulator SDK=9.3 VER=8.0 \
  opam install conf-ios

... for 64-bit x86:

ARCH=amd64 SUBARCH=x86_64 PLATFORM=iPhoneSimulator SDK=9.3 VER=8.0 \
  opam install conf-ios

Some options can be further tweaked:

  • SUBARCH (when ARCH=arm) specifies the ARM architecture version, one of armv6, armv7, and armv7s;
  • SDK specifies the SDK being used as well as the minimum iOS version on which the compiled code will run;
  • VER specifies the value of the -miphoneos-version-min compiler switch.

The options above (ARCH, SUBARCH, PLATFORM, SDK) are recorded inside the conf-ios package, so make sure to reinstall that package if you wish to switch to a different toolchain. Otherwise, it is not necessary to supply them while upgrading the ocaml-ios* packages.

If desired, request the compiler to be built with flambda optimizers:

opam install conf-flambda-ios

Install the compiler and some packages:

opam install ocaml-ios re-ios

Write some code using them:

let () =
  let regexp = Re_pcre.regexp {|\b([a-z]+)\b|} in
  let result = Re.exec regexp "Hello, world!" in
  Format.printf "match: %s\n" (Re.get result 1)

Make an object file out of it, link libasmrun.a to your final executable, and link it with your iOS project (you'll need to call caml_startup(argv) to run OCaml code; see this article):

ocamlfind -toolchain ios ocamlopt -package re.pcre -linkpkg -output-complete-obj test_pcre.ml -o test_pcre.o

With opam-ios, cross-compilation is easy!

Managing deployment targets

Generally, any native iOS library would have to be compiled four times: for 32-bit and 64-bit device and simulator. OPAM offers no help here; due to the way OPAM packages currently work, the only realistic option is to create four switches, one switch per target, and build everything four times. To assist with this, a script called ioscaml.sh is distributed in this repository.

The script is supposed to be loaded into a running shell by sourcing it and it defines several functions:

  • ioscaml_create_switches creates four OPAM switches with predefined names;
  • ioscaml_foreach cmd... runs cmd... in every OPAM switch;
  • SDK=9.3 VER=8.0 ioscaml_configure installs conf-ios with appropriate parameters and specified SDK version as well as -miphoneos-version-min in every switch;
  • ioscaml_ocamlbuild runs ocamlbuild once with every OPAM switch selected and places the build products in _build_arm for 32-bit iOS, _build_arm64 for 64-bit iOS, _build_i386 for 32-bit simulator, and _build_amd64 for 64-bit simulator.

A typical workflow would be as follows:

  • ioscaml_create_switches to create the switches and build the host compilers;
  • SDK=9.3 VER=8.0 ioscaml_foreach ioscaml_configure to configure the cross-compiler in the switches;
  • ioscaml_foreach opam pin ... to pin the necessary dependencies;
  • ioscaml_foreach opam install re-ios ... to install the dependencies of your library;
  • ioscaml_foreach ioscaml_ocamlbuild libiosthing.o to build your library.

Porting packages

OCaml packages often have components that execute at compile-time (camlp4 or ppx syntax extensions, cstubs, OASIS, ...). Thus, it is not possible to just blanketly cross-compile every package in the OPAM repository; sometimes you would even need a cross-compiled and a non-cross-compiled package at once. The package definitions also often need package-specific modification in order to work.

As a result, if you want a package to be cross-compiled, you have to copy the definition from opam-repository, rename the package to add -ios suffix while updating any dependencies it could have, and update the build script. Don't forget to add ocaml-ios as a dependency!

Findlib 1.5.4 adds a feature that makes porting packages much simpler; namely, an OCAMLFIND_TOOLCHAIN environment variable that is equivalent to the -toolchain command-line flag. Now it is not necessary to patch the build systems of the packages to select the iOS toolchain; it is often enough to add ["env" "OCAMLFIND_TOOLCHAIN=ios" make ...] to the build command in the opam file.

Note that iOS does not support dynamic linking, and so package build systems should be instructed to not build plugins (*.cmxs).

For projects using OASIS, the following steps will work:

build: [
  ["env" "OCAMLFIND_TOOLCHAIN=ios"
   "ocaml" "setup.ml" "-configure" "--prefix" "%{prefix}%/ios-sysroot"
                                   "--override" "native_dynlink" "false"]
  ["env" "OCAMLFIND_TOOLCHAIN=ios" "ocaml" "setup.ml" "-build"]
]
install: [
  ["env" "OCAMLFIND_TOOLCHAIN=ios" "ocaml" "setup.ml" "-install"]
]
remove: [["ocamlfind" "-toolchain" "ios" "remove" "pkg"]]
depends: ["ocaml-ios" ...]

For projects installing the files via OPAM's .install files (e.g. topkg), the following steps will work:

build: [["ocaml" "pkg/pkg.ml" "build" "--pinned" "%{pinned}%" "--toolchain" "ios" ]]
install: [["opam-installer" "--prefix=%{prefix}%/ios-sysroot" "pkg.install"]]
remove: [["ocamlfind" "-toolchain" "ios" "remove" "pkg"]]
depends: ["ocaml-ios" ...]

The output of the configure script will be entirely wrong, referring to the host configuration rather than target configuration. Thankfully, it is not actually used in the build process itself, so it doesn't matter.

Internals

The aim of this repository is to build a cross-compiler while altering the original codebase in the minimal possible way. There are no attempts to alter the configure script; rather, the configuration is provided directly. The resulting cross-compiler has several interesting properties:

  • All paths to the iOS toolchain are embedded inside ocamlc and ocamlopt; thus, no knowledge of the iOS toolchain is required even for packages that have components in C, provided they use the OCaml driver to compile the C code. (This is usually the case.)
  • The build system makes several assumptions that are not strictly valid while cross-compiling, mainly the fact that the bytecode the cross-compiler has just built can be ran by the ocamlrun on the build system. Thus, the requirement for a 32-bit build compiler for 32-bit targets, as well as for the matching versions.
  • The .opt versions of the compiler are built using itself, which doesn't work while cross-compiling, so all provided tools are bytecode-based.

Acknowledgements

The OCaml cross-compiler in opam-cross-ios is based on a patchset by Gerd Stolpmann.

License

All files contained in this repository are licensed under the CC0 1.0 Universal license.

References

See also opam-cross-windows and opam-cross-android.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].