summaryrefslogtreecommitdiff
path: root/HOWTO/INSTALL-IOS.md
blob: cdb0bab137544dfa6955f127572c64cabad406d3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
Cross Compiling Erlang/OTP - IOS
====================================

Introduction
------------

This document describes how to cross compile Erlang/OTP to iOS platforms. iOS has the peculiarity that it does not support the creation of shared libraries. So the build process needs to be amended to generate statically linkable and executable Erlang binaries.

### Get Xcode Tools ###

iOS binaries can only be cross-compiled from Apple macOS computers. For compilation the Xcode tools must be used.
### Environment Variables ###

In order to inform the build system that it should generate the static linkable `libbeam.a` add the following environment variable during configuration and compilation:

    $ export RELEASE_LIBBEAM=yes

### Configure Erlang/OTP ###

    To build without without OpenSSL support, run `configure` like this:
    
    $ ./otp_build configure \
         --xcomp-conf=./xcomp/erl-xcomp-arm64-ios.conf  \
         --without-ssl


To build with OpenSSL statically linked, run `configure` like this (note that 1.1.1k works, but needs a manual patch in ios cross build config to include engines):

    $ ./otp_build configure \
         --xcomp-conf=./xcomp/erl-xcomp-arm64-ios.conf  \
         --with-ssl=/path/to/libcrypto.a \
         --disable-dynamic-ssl-lib


### Compile Erlang/OTP ###

    $ ./otp_build boot

### Linking a binary with your App ###
    
To use the resulting `libbeam.a` it needs to be packaged into a Xcode `.xcframework` package together with all other required `.a` files. These include:

* `libbeam.a`
* `liberts.a`
* `libz.a`
* `libcrypto.a`

Basically all .a files that have been generated by the build.

One way of creating a combined archive out of all of these is to use `libtool`:

    $ libtool -static -o liberlang.a /Path/to/libbeam.a /Path/to/liberts.a /Path/to/....a 

This list should also include static nifs you want to include:

Finally packaging the archive into a `.xcframework` directory:

    $ xcodebuild -create-xcframework -output ./liberlang.xcframework -library liberlang.a

If you're building for multiple iOS targets (e.g. simulator and phone):

    $ xcodebuild -create-xcframework -output ./liberlang.xcframework -library /Path/to/phonelib/liberlang.a -library /Path/to/simulatorlib/liberlang.a

## Starting Erlang

To execute erlang from within an iOS project a native C/C++ wrapper is needed that can call the function:

    erl_start(int argc, char *argv[]); 
    
An example call could look like this: 

    $ const char *args[] = {
            "my_main",
            "-sbwt",
            "none",
            "--",
            "-root",
            root_dir.c_str(),
            "-progname",
            "erl",
            "--",
            "-home",
            home_dir.c_str(),
            "--",
            "-kernel",
            "shell_history",
            "enabled",
            "--",
            "-start_epmd",
            "false",
            "-elixir",
            "ansi_enabled",
            "true",
            "-noshell",
            "-s",
            "elixir",
            "start_cli",
            "-mode",
            "interactive",
            "-config",
            config_path.c_str(),
            "-boot",
            boot_path.c_str(),
            "-boot_var",
            "RELEASE_LIB",
            lib_path.c_str(),
            "--",
            "--",
            "-extra",
            "--no-halt",
    };

    erl_start(sizeof(args) / sizeof(args[0]), (char **)args);

### Reference Example

At the time of writing (October 2021) there is a full reference iOS application available at https://github.com/elixir-desktop/ios-example-app.

An implementation of the native wrapper can be viewed at https://github.com/elixir-desktop/ios-example-app/blob/main/native-lib.cpp.