Bootstrapping arm64-darwin mlton:
We want to bootstrap an mlton compiler that runs natively on a Apple Silicon (aka M1, arm64) and generates native arm64 executables. Thanks to Rosetta2, this is possible starting from an amd64 mlton compiler. There are many recipes for this; I used the following:
Ingredients:
- An Apple Silicon machine (the "M1").
- An amd64-darwin mlton compiler statically linked against gmp, and statically linking
its generated executables against gmp.
If you have access to a amd64-darwin machine, you can build one as follows:
- download an amd64-darwin, gmp-static variant, mlton compiler, at mlton.org: mlton-20210117-1.amd64-darwin-19.6.gmp-static.tgz; unpack it then install on the M1 as /opt/mlton-20210117-amd64-darwin;
- from an installation of gmp on a amd64-darwin machine, get files libgmp.a and gmp.h;
- then update /opt/mlton-20210117-amd64-darwin as follows:
- cp libgmp.a /opt/mlton-20210117-amd64-darwin/lib/mlton/targets/self
- cp gmp.h /opt/mlton-20210117-amd64-darwin/lib/mlton/targets/self/include
Recipe:
- add /opt/mlton-20210117-amd64-darwin/bin to your PATH:
- PATH=/opt/mlton-20210117-amd64-darwin/bin:$PATH
- We proceed by bootstrapping a native arm64 mlton compiler using it, as follows:
- download an mlton source distribution at mlton.org and unpack it, say at mlton-20210117
- install gmp if not done yet. In the sequel, I assume that gmp was installed
by homebrew:
- brew install gmp
- As for the above amd64 compiler, the process will be simpler if we build an arm64 compiler statically linked against gmp and statically linking the executables it generates against it. For this we are going to "pre-install" the static libgmp.a library and gmp.h into the hierarchy in which the arm64 mlton compiler will be stored. If gmp was installed by homebrew, this amounts to the following (please adapt otherwise), enter:
- Now build the compiler by:
- make binary-release
- tar xzf mlton-20210117-1.arm64-darwin.tgz
- mv mlton-20210117-1.arm64-darwin /opt
- PATH=/opt/mlton-20210117-1.arm64-darwin/bin:$PATH
- As desired, that compiler is statically linked against gmp and statically
linking the executables it generates against it.
If you prefer the executables dynamically linked against gmp, just remove libgmp.a
and gmp.h from the hierarchy at /opt/mlton-20210117-1.arm64-darwin.
Since gmp dynamic libraries are not installed by homebrew on M1 at a standard place,
you will need to set variables GMP_INC_DIR and GMP_LIB_DIR in script
/opt/mlton-20210117-1.arm64-darwin/bin/mlton so the C compiler can find libgmp.
This can be done using the Makefile provided with binary distributions by running
- make WITH_GMP_DIR=/opt/homebrew/opt/gmp update
in folder /opt/mlton-20210117-1.arm64-darwin.
- If, in addition, you prefer an mlton itself dynamically linked against gmp, then proceed
by bootstrapping it using the mlton built (with libgmp.a and gmp.h), as follows:
- cd mlton-20210117
- rm -f *.tgz
- make clean
- rm -rf build
- make WITH_GMP_DIR=/opt/homebrew/opt/gmp binary-release
- cd mlton-20210117
- make clean (just in case)
- rm -rf build (just in case)
- make dirs
- cp /opt/homebrew/lib/libgmp.a build/lib/mlton/targets/self
- cp /opt/homebrew/include/gmp.h build/lib/mlton/targets/self/include
- cp /opt/homebrew/include/gmp.h runtime