MLton 20210117

# SelfCompiling

If you want to compile MLton, you must first get the Sources. You can compile with either MLton or SML/NJ, but we strongly recommend using MLton, since it generates a much faster and more robust executable.

## Compiling with MLton

To compile with MLton, you need the binary versions of mlton, mllex, and mlyacc that come with the MLton binary package. To be safe, you should use the same version of MLton that you are building. However, older versions may work, as long as they don’t go back too far. To build MLton, run make from within the root directory of the sources. This will build MLton first with the already installed binary version of MLton and will then rebuild MLton with itself.

First, the Makefile calls mllex and mlyacc to build the lexer and parser, and then calls mlton to compile itself. When making MLton using another version the Makefile automatically uses mlton-stubs.mlb, which will put in enough stubs to emulate the structure MLton. Once MLton is built, the Makefile will rebuild MLton with itself, this time using mlton.mlb and the real structure MLton from the Basis Library. This second round of compilation is essential in order to achieve a fast and robust MLton.

Compiling MLton requires at least 1GB of RAM for 32-bit platforms (2GB is preferable) and at least 2GB RAM for 64-bit platforms (4GB is preferable). If your machine has less RAM, self-compilation will likely fail, or at least take a very long time due to paging. Even if you have enough memory, there simply may not be enough available, due to memory consumed by other processes. In this case, you may see an Out of memory message, or self-compilation may become extremely slow. The only fix is to make sure that enough memory is available.

### Possible Errors

- The C compiler may not be able to find the GMP header file, gmp.h leading to an error like the following.

        cenv.h:49:18: fatal error: gmp.h: No such file or directory

  The solution is to install (or build) GMP on your machine. If you install it at a location not on the default seach path, then run make WITH_GMP_INC_DIR=/path/to/gmp/include WITH_GMP_LIB_DIR=/path/to/gmp/lib.

- The following errors indicates that a binary version of MLton could not be found in your path.

      /bin/sh: mlton: command not found

      make[2]: mlton: Command not found

  You need to have mlton in your path to build MLton from source.

  During the build process, there are various times that the Makefile-s look for a mlton in your path and in src/build/bin. It is OK if the latter doesn’t exist when the build starts; it is the target being built. Failure to find a mlton in your path will abort the build.

## Compiling with SML/NJ

To compile with SML/NJ, run make bootstrap-smlnj from within the root directory of the sources. You must use a recent version of SML/NJ. First, the Makefile calls ml-lex and ml-yacc to build the lexer and parser. Then, it calls SML/NJ with the appropriate sources.cm file. Once MLton is built with SML/NJ, the Makefile will rebuild MLton with this SML/NJ built MLton and then will rebuild MLton with the MLton built MLton. Building with SML/NJ takes significant time (particularly during the "parseAndElaborate" phase when the SML/NJ built MLton is compiling MLton). Unless you are doing compiler development and need rapid recompilation, we recommend compiling with MLton.