Installation d'une IDE OCaml sous Windows

On fait ici l'hypothèse d'une installation sous Windows 64.

L'installation se déroulera en plusieurs étapes:

À noter qu'il est aussi possible d'utiliser un top-level tout en un:

Installation de Cygwin

Si vous avez déjà une installation de Cygwin, vérifiez que cette dernière est pour une architecture 64bits. Sinon, téléchargez l'installateur pour Cygwin ici:

Lancez l'installateur (ce dernier n'étant pas signé, Windows se plaindra et vous demandera de confirmer l'exécution de l'installateur). Laissez toutes les options de l'installateur par défaut, à l'exception de la sélection du miroir (n'importe lequel conviendra). Sur la page de sélection des paquets à installer, sélectionnez wget (tapez wget dans le champ Search en haut à gauche, et positionnez le champ View sur Full. Le paquet wget dans la section Web devrait apparaître. Cliquez alors sur Skip pour le sélectionner pour installation). Finalisez l'installation en validant vos choix.

Les binaires de Cygwin doivent être dans la variable d'environnement PATH. Ouvrez le Control Panel, puis cherchez l'option Edit the system environment variables. Ceci ouvrira une boîte de dialogue. Cherchez le bouton Environment variables et cliquez dessus. Ceci ouvrir une seconde boîte de dialogue. Dans la section System variables, cherchez la variable de nom Path. Sélectionnez là et cliquez sur le bouton Edit.... De là:

Enfin, fermez toutes les boites de dialogue en validant tous vos choix.

L'installateur Cygwin aura ajouté une icône, sur le bureau, pour l'application Cygwin64. Lancez cette dernière. Vous obtiendrez alors un terminal. Exécutez les commandes suivantes:

wget -O /bin/apt-cyg https://rawgit.com/transcode-open/apt-cyg/master/apt-cyg
chmod 755 /bin/apt-cyg

De là, il vous est possible d'installer toutes les dépendances OCaml via la commande:

apt-cyg install automake diffutils libreadline make m4
apt-cyg install mingw64-x86_64-gcc-core mingw64-x86_64-gmp mingw64-x86_64-openssl
apt-cyg install mingw64-x86_64-pkg-config mingw64-x86_64-sqlite3
apt-cyg install patch rlwrap unzip wget git

Quittez le terminal. Votre installation de Cygwin est terminée.

Installation de OCaml

Vérifiez qu'il ne reste aucune trace d'une installation de OCaml. Si oui, faites le ménage --- n'oubliez pas de supprimer toutes les variables d'environnement de la forme OCAML....

Téléchargez l'installateur pour OCaml ici:

Lancez l'installateur. Laissez toutes les options de l'installateur par défaut, à l'exception de la sélection des composants où vous devez supprimer l'installation de Cygwin.

Installation de Eclipse

Téléchargez un installateur pour Eclipse ici:

Lancez l'installateur et suivez les instructions. Si vous avez une installation précédente d'Eclipse, vous pouvez la garder.

Lancez Eclipse. Pour installer le plugin OCaml, allez dans le menu Help -> Install New Software... Une boîte de dialogue apparaîtra. À partir de cette dernière, ajoutez une nouvelle source de plugin en cliquant sur le bouton Add... (dans la section Work with...). Le nom de la source (champ Name de la nouvelle boîte de dialogue) importe peu --- utilisez OCaml par example. La source (champ Location) doit être positionné sur

Validez la nouvelle source. Dans la liste sous la section Work with..., le plugin OcaIDE devrait apparaître. Sélectionnez le puis validez. Eclipse vous demandera de confirmer l'installation de OcaIDE et d'accepter sa licence. Une fois l'installation terminée, Eclipse vous proposera de redémarrer. Acceptez.

Une fois Eclipse redémarré, allez dans le menu Window -> Preferences... Allez dans la section OcaIDE -> Paths. Dans le champ OCaml Binaries Directory, entrez C:\OCaml\bin, puis cliquez sur Apply à droite. Le reste des champs devrait se remplir tout seul. Dans la section Other tools, dans le champ OCaml lib path, entrez C:\OCaml\lib. Validez le tout.

OCaml in Emacs

These instructions are not intended to be used on the computers in the salles info. They are intended for your own computers.

Summary

This page shows how to set up a development environment for OCaml inside Emacs under Linux or OS X. Specifically, these instructions will cover:

We assume that you already have a recent Emacs distribution (version 24.5 or later). You almost certainly already have Emacs installed. If not, you can install Emacs as follows.

Setting up OPAM, OCaml, and required packages

For maximum flexibility, we will be using a source distribution of OCaml, i.e., a version of OCaml that is built from scratch using the source code of OCaml. Nowadays this is best achieved using OPAM, the community standard OCaml package manager. You will be using OPAM not only to install the OCaml compiler toolchain but also certain OCaml packages that are part of the community-maintained collection of OCaml libraries and tools.

  1. Download and install OPAM You can do this easily by running the following from your shell.

    wget https://raw.github.com/ocaml/opam/master/shell/opam_installer.sh -O - | sh -s ${HOME}/bin

Make sure that ~/bin is in your $PATH, either by running:

    export PATH=${HOME}/bin:${PATH}

or modifying your ~/.bash_profile (or equivalent for your shell).

  1. Install the OCaml compiler toolchain from OPAM by running:

    opam switch 4.04.0

When that command finishes, you will need to reset your shell's environment by running:

    eval `opam config env`

We recommend that you run the following command to automatically set up OPAM for the future.

    opam config setup --all
  1. Test your installation by running the following commands. Both should succeed and the second command should print the string 4.04.0.

     opam config setup --list
     ocamlc -vnum
  2. Install Tuareg, Merlin, and ocp-indent through OPAM by running the following command.

     opam install tuareg merlin ocp-indent

Configuring Emacs

Add the following lines to your ~/.emacs or ~/.emacs.d/init.el. (If neither file exists, make the directory ~/.emacs.d and edit ~/.emacs.d/init.el.)

;;; Tuareg mode for OCaml

(defconst opam-directory
  (car (split-string (shell-command-to-string "opam config var share")))
  "The opam config variable `share'")

(defconst opam-site-lisp
  (concat opam-directory "/emacs/site-lisp")
  "Where opam stores site lisp")

(load (concat opam-site-lisp "/tuareg-site-file"))

(dolist (ext '(".cmo" ".cmx" ".cma" ".cmxa" ".cmi" ".cmxs" ".cmt"))
  (add-to-list 'completion-ignored-extensions ext))

(autoload 'merlin-mode "merlin" "Merlin mode" t)
(autoload 'ocp-setup-indent "ocp-indent" "OCP Indent" t)

(add-hook 'tuareg-mode-hook 'tuareg-make-indentation-regexps)
(add-hook 'tuareg-mode-hook 'ocp-setup-indent t)

;; configure some useful Merlin defaults

(add-hook 'tuareg-mode-hook 'merlin-mode)
(setq
  merlin-command 'opam               ; use opam inside merlin
  merlin-locate-in-new-window 'never ; merlin-locate doesn't create new windows
)

;; configure some useful auto-complete defaults

(add-hook 'tuareg-mode-hook 'auto-complete-mode)
(setq
  ac-use-menu-map t                 ; use ac-menu-map instead of global map
  ac-auto-start nil                 ; auto-complete must be explicitly invoked
)
(define-key ac-menu-map (kbd "C-n") 'ac-next)     ; C-n to next ac entry
(define-key ac-menu-map (kbd "C-p") 'ac-previous) ; C-p to previous ac entry

Configuring projects

In order to use Merlin on your own projects, you need to enable the -bin-annot flag when compiling your OCaml code. The easiest way to do this is to add the following myocamlbuild.ml file in your projects.

open Ocamlbuild_plugin ;;
let () =
  dispatch begin function
  | After_rules ->
      flag ["ocaml" ; "compile"] (A "-bin-annot")
  | _ -> ()
  end ;;

To improve Merlin's error message, we also suggest adding the following .merlin file to the project. It turns various warnings into errors and checks that you don't use string mutation with the -safe-string flag to the OCaml compiler toolchain.

FLG -short-paths
FLG -labels
FLG -principal
FLG -w @3@5@6@8..12@14@20@26@28@29
FLG -safe-string
FLG -strict-sequence

Building projects

If your main program is main.ml, then you can compile it using:

ocamlbuild main.native

This will recompile all necessary OCaml source files and produce the binary ./main.native which you can then execute.

For more information on how to use ocamlbuild, consult its official documentation.

Créer un projet sous OcaIDE

Une fois Eclipse lancé, aller dans File -> New -> Project... Sélectionnez le type de projet OCaml / OCaml Project (ocamlbuild). Cliquez sur Next, choisissez un nom de projet, puis cliquer sur Finish.

Le projet contiendra par défaut un répertoire _build. Ne rien mettre dedans ! Ce répertoire est supprimé à chaque compilation.

À la racine du projet, faites un clic droit, puis sélectionnez New -> Module... Choisissez un nom pour votre module (par exemple, TDX). Ceci créera un fichier TDX.ml à la racine de votre projet. De nouveau, à la racine de votre projet, faites un clic droit, puis sélectionnez Properties. Dans le champ Project / Targets, entrée TDX.native. (C'est-à-dire, le point d'entrée de votre programme est le module TDX, et vous voulez une compilation vers le format natif.) Quittez la boîte de dialogue en validant vos choix.

Eclipse va alors compiler votre module et un fichier TDX.native va être créé (sous Windows, ce fichier sera dans le répertoire _build seulement). Sélectionnez ce fichier, faites un clic droit et sélectionnez Run as -> OCaml Executable. Ceci lancera votre programme OCaml.

OcaIDE vient également avec un top-level OCaml. Vous pouvez le trouver dans l'onglet OCaml Toplevel (par défaut, en bas). Vous pouvez taper directement vos instructions OCaml dans ce toplevel, ou, via la fonction F6 (menu Ocaml -> Eval In Toplevel).