Getting started with Haxe targetting HashLink

John Gabriele

2019-07

Haxe can compile to many different target platforms. This doc is about installing Haxe and the HashLink VM onto Debian GNU/Linux, compiling your Haxe code to HashLink bytecode, and then running it on the HashLink VM (aka “using HL/JIT”).

Note: The Haxe HashLink target can also produce C code which you’d then compile with a C compiler like GCC. This use of HashLink is called “HL/C”, in contrast to the above “HL/JIT”, which we’re using in this doc.

Prerequisites

Haxe 4

We want to install the newest binary release of Haxe 4 into ~/opt and ~/bin for use with HashLink. Go to the Haxe Download page and click the “(View all versions)” link at the top. Then go to newest/topmost “4.0.0-rc.X” and download the Linux 64-bit binaries. Install the binaries like so:

mv path/to/haxe-4.0.0-rc.X-linux64.tar.gz ~/opt
cd ~/opt
tar xzf haxe-4.0.0-rc.X-linux64.tar.gz
# Note, that archive unpacks into a directly named like
# haxe_YYYYMMDDhhmmss_xxxxxxx. Maybe rename it to something
# more human-readable:
mv haxe_201xxxxxxxxxxx_xxxxxxx haxe-4.0.0-rc.X
ln -s haxe-4.0.0-rc.X haxe
cd ~/bin
ln -s ~/opt/haxe/haxe .
ln -s ~/opt/haxe/haxelib .

Then, so haxe can find its std library, into your ~/.bashrc add:

export HAXE_STD_PATH="$HOME/opt/haxe/std"

Finally, set up haxelib:

haxelib setup

accepting the default ~/haxelib directory name (where haxelib will store installed library files).

If you don’t already have it installed, you’ll need to apt install neko, as haxelib requires it.

Upgrading

To upgrade to a newer preview release:

  1. As described above: download and unpack the newer Haxe release into ~/opt, and rename the resulting directory to a more human-readable name (ex., to “haxe-4.0.0-rc.X”).
  2. rm the “haxe” symlink in ~/opt and make a new one pointing to the newly unpacked & renamed haxe-4.0.0-rc.X directory.

Try it out

$ which haxe haxelib
/home/YOU/bin/haxe
/home/YOU/bin/haxelib

$ haxe --version
4.0.0-rc.X+xxxxxxx

$ haxelib version
3.4.0

Other Prereqs via apt

We’ll build the latest HashLink release from source and install into /usr/local. But before that, per the HashLink README, make sure you have a handful of recommended libraries installed:

# apt install libpng-dev libturbojpeg0-dev libvorbis-dev
# apt install libopenal-dev libsdl2-dev
# apt install libmbedtls-dev libuv1-dev

Install HashLink

No longer as root (from the above use of apt), download the latest binary release of HashLink (approx 4 MB) and build it like so:

mv hashlink-1.10.tar.gz ~/opt
cd ~/opt
tar xzf hashlink-1.10.tar.gz
cd hashlink-1.10
make

If upgrading: first, from your previous hashlink version’s directory (in ~/opt/hashlink-1.X), as root, run make uninstall there.

As root, install HashLink:

# make install

HL will be installed into /usr/local. Then run

# ldconfig

so the system can properly find the libraries just copied to /usr/local/lib (ldconfig searches the library directories and updates /etc/ld.so.cache).

If you want to use HL/C you’ll need to install via haxelib (not as root) the “hashlink” library:

$ haxelib install hashlink

This provides the build tools to make HL/C work, analogous to how the hxcpp and hxjava libraries provide the build tools for the C++ and Java targets, respectively. See the currently-installed version:

$ haxelib list hashlink

Try it out

You should now have hl installed:

$ which hl
/usr/local/bin/hl

$ hl
HL/JIT 1.10.0 (c)2015-2019 Haxe Foundation
  Usage : hl [--debug <port>] [--debug-wait] <file>

Try it out on some code. cd path/to/my-proj. Create a src/Main.hx file containing:

class Main {
    public static function main() {
        trace("Hello, World!");
    }
}

And an hl.hxml file containing:

-p src
-m Main
--hl out.hl

Then (instead of having to type out haxe -p src -main Main --hl out.hl (and then run via hl out.hl)), you can build and run in one shot from your project’s top-level directory:

haxe hl.hxml && hl out.hl

getting this output:

Main.hx:3: Hello, World!