Compiling ocaml MinGW64 w64

shadinger edited this page May 31, 2012 · 45 revisions
Clone this wiki locally

Compiling ocaml 64 bits on Windows MinGW64-w64

Please note that the entire process takes ~2 hours to complete.

There is an excellent tutorial to install MinGW64, MSYS and compiling most libs here: http://www.gaia-gis.it/spatialite-3.0.0-BETA/mingw64_how_to.html

Install MinGW64-w64 and MSYS

MinGW64-w64

First download and install the pre-packaged TDM-GCC from here: http://tdm-gcc.tdragon.net/download

Select the MinGW-w64 tdm64-gcc-4.6.1 version: http://sourceforge.net/projects/tdm-gcc/files/TDM-GCC%20Installer/tdm64-gcc-4.6.1.exe/download

  • run the installer

  • unselect "Check for updated files on the TDM-GCC server"

  • click on "create"

  • leave the default installation directory: C:\MinGW64

  • unselect "Start Menu Items" and "Add to PATH" if you want to leave your windows environment clean, leave all default packages

  • install

This packaging is nice as it includes both prefixed and unprefixed version of the gcc tools (i.e. both gcc and x86_64-w64-mingw32-gcc).

MSYS

Then download MSYS from here: http://sourceforge.net/projects/mingw-w64/files/External%20binary%20packages%20%28Win64%20hosted%29/MSYS%20%2832-bit%29/

Download MSYS-2011xxxx.zip (latest).

Unzip the content in C:\msys64 (you should have c:\msys64\bin, c:\msys64\etc...)

MSYS post-install

Run C:\msys64\msys.bat (you need to run this as an Administrator for the postinstall to succeed, then you don't need anymore admin privileges)

Launch the post-install:

sh /postinstall/pi.sh

This is a post install process that will try to normalize between
your MinGW install if any as well as your previous MSYS installs
if any.  I don't have any traps as aborts will not hurt anything.
Do you wish to continue with the post install? [yn ] y

Do you have MinGW installed? [yn ] y

Please answer the following in the form of c:/foo/bar.
Where is your MinGW installation? c:/mingw64

Creating /etc/fstab with mingw mount bindings.
        Normalizing your MSYS environment.

You have script /bin/cmd
You have both /bin/ftp.exe and /bin/ftp.
Removing /bin/ftp.

Oh joy, you do not have c:/mingw64/bin/make.exe. Keep it that way.

If everythig is okay you should be able to do the following without errors:

ls /mingw
gcc -v

Now create some prefixed version for some popular tools:

cd /mingw/bin
cp ar.exe x86_64-w64-mingw32-ar.exe
cp ranlib.exe x86_64-w64-mingw32-ranlib.exe
cp windres.exe x86_64-w64-mingw32-windres.exe
cp as.exe x86_64-w64-mingw32-as.exe
cp ld.exe x86_64-w64-mingw32-ld.exe
cp strip.exe x86_64-w64-mingw32-strip.exe
cp objdump.exe x86_64-w64-mingw32-objdump.exe
cd

Install git

Hmmm, no easy way to have git under MSYS...

git config --global user.name "your_username"
git config --global user.email "your@email"

Prepare your environment

Create a .profile as follows (in your C:/msys64/home/[user]/.profile):

#!/bin/sh.exe
#
# Get the aliases and functions
#
if [ -f ${HOME}/.bashrc ]
then
  . ${HOME}/.bashrc
fi
export CVS_RSH="ssh"
export EDITOR="/usr/bin/vim"
export HISTSIZE="500"
export PATH="${HOME}/bin:${PATH}"
export FLEXLINKFLAGS="-nocygpath -L C:\MinGW64\x86_64-w64-mingw32\lib"
export OCAMLLIB="C:/ocamlmgw64/lib"
export PATH="${PATH}:/c/flexdll:/c/ocamlmgw64/bin"

Create a .bashrc file as follows (in your C:/msys64/home/[user]/.bashrc):

# .bashrc
# Source global definitions
if [ -f /etc/bashrc ]
then
  . /etc/bashrc
fi
# User specific aliases and functions
#
alias ll="ls -l"
alias vi="vim"

Install flexdll

flexdll compilation is a bit of a nightmare due to bootstrapping issues: flexdll needs ocaml to compile, and ocaml needs flexdll to compile.

As a convenience, a native 64 bits flexdll package is located here: https://github.com/shadinger/flexdll-win64/blob/master/_bin/flexdll.rar

Download the rar file (click on "raw" button) and unrar the content in C:\flexdll directory.

Install findutils

Mingw64/MSYS is lacking the findutils and namely the "find" command. So we need to get it from MSYS32.

Dowload the precompiled findutils from MSYS32: http://sourceforge.net/projects/mingw/files/MSYS/Base/findutils/findutils-4.4.2-2/findutils-4.4.2-2-msys-1.0.13-bin.tar.lzma/download

tar lxvf findutils-4.4.2-2-msys-1.0.13-bin.tar.lzma
cp bin/find.exe /bin
cp bin/oldfind.exe /bin
cp bin/xargs.exe /bin
rm -rf bin

Compile git for MingW64

Compile ocaml

git clone https://github.com/shadinger/ocaml-win64.git

Make sure to temporarily reinit some environment variables:

export OCAMLLIB=

Compile

cd ocaml-win64
cp config/m-nt.h config/m.h
cp config/s-nt.h config/s.h
cp config/Makefile.mingw64 config/Makefile
make -f Makefile.nt world
make -f Makefile.nt opt
make -f Makefile.nt opt.opt
make -f Makefile.nt install

This will install all ocamlfiles into C:\ocamlmgw64

Compile additional libs

Compile zlib

Download zlib-1.2.7 here: http://zlib.net/zlib-1.2.7.tar.gz

tar zxvf zlib-1.2.7.tar.gz
cd zlib-1.2.7
make -f win32/Makefile.gcc
cp zlib.h /mingw/include/
cp zconf.h /mingw/include/
cp libz.a /mingw/x86_64-w64-mingw32/lib/
cp libz.dll.a /mingw/x86_64-w64-mingw32/lib/
cp zlib1.dll /mingw/bin/
cd ..

compile openssl

Download openssl here: http://www.openssl.org/source/openssl-1.0.0i.tar.gz

tar zxvf openssl-1.0.0i.tar.gz
cd openssl-1.0.0i
./Configure --openssldir=/mingw/x86_64-w64-mingw32 mingw64 no-asm
make
make install_sw  (software only, we don't need documentation)
cd ..

Note: it is currenlty important not to compile in 'shared' mode to force ocaml to statically link with openssl. I didn't find a suitable way to force static linking in opa or ocaml...

compile ocaml modules

compile findlib

You will need a patched version of Findlib 1.2.7. Original Findlib is hosted here

git clone https://github.com/shadinger/findlib-win64.git
cd findlib-win64
./configure
make all
make opt
make install
cd ..

compile ulex

Ulex is hosted here Download version 1.1 here

gunzip -c ulex-1.1.tar.gz | tar xvf -
cd ulex-1.1
make all
make all.opt
make install
cd ..

compile ocamlgraph

Ocamlgraph is hosted here Download version 1.8.1 here

tar zxvf ocamlgraph-1.8.1.tar.gz
cd ocamlgraph-1.8.1
./configure  _(ignore warning about lablgnomecanvas not found)_
make
make install
make install-findlib
cd ..

compile camlzip

Camlzip is hosted here Download version 1.04 here

tar zxvf camlzip-1.04.tar.gz
cd camlzip-1.04
make all
make allopt
make install
make installopt
cp zlib.o zlib.cmi zlib.cmx zlib.mli `ocamlc -g -where`/zip
cd ..

Please note that you need to manually copy some zlib.* libraries.

compile cryptokit

Cryptokit is hosted here Download version 1.5 here

gunzip -c cryptokit-1.5.tar.gz | tar xvf -
cd cryptokit-1.5
ocaml setup.ml -configure
ocaml setup.ml -build
ocaml setup.ml -install
cd ..

There is somewhat an inconsistency somewhere in the build process while cryptokit is looked for in lib/ocaml or in lib/ocaml/site-lib.

 cp -r `ocamlc -g -where`/site-lib/cryptokit `ocamlc -g -where`/

compile ocamlssl

Ocamlssl is hosted here Download version 0.4.6 here

git clone https://github.com/shadinger/ocaml-ssl.git
cd ocaml-ssl
export STATIC=1
./configure
make
make install
unset STATIC
cd ..

Compile opalang

From the MSYSGIT window:

git clone https://github.com/shadinger/opalang.git
cd opalang
git remote add upstream https://github.com/MLstate/opalang.git
git checkout -b win-merge origin/win-merge

From the MSYS window:

cd opalang

Since MSYSGIT doesn't support symlinks on Windows, we need to manually copy the symlinked files:

rm doc/book/hello_web_services/resources
cp -r doc/book/hello_chat/resources doc/book/hello_web_services/
cp libtrx/default.trx libbase/
cp libtrx/default.trx libnet/
cp libtrx/default.trx libirc/
cp libtrx/default.trx ocamllang/
cp libtrx/default.trx protocols/
cp libtrx/default.trx teerex/
cp libbase.mllib ocamlbase.mltop
cp opalang/classic_syntax/parser_path.trx opalang/js_syntax/parser_path.trx
## BROKEN cp packages/experimental/OPAges/Makefile.common packages/experimental/OPAges/opace/
cp ocamllang/ocaml_keywords.trx protocols/
cp ocamllang/ocaml_parser.trx protocols/
cp ocamllang/ocaml_types.trx protocols/
cp opalang/classic_syntax/xml.trx opalang/js_syntax/

Then launch the compilation

./configure
make
make install