Friday, January 7, 2011

Installing ECL on Windows XP with MSVC++ 2010 Express, SLIME, and Emacs


In this tutorial, we'll build Embeddable Common Lisp (ECL) from source and set it up for Windows XP. Later on, we'll get ECL to work on Emacs + SLIME. We'll also build a simple "Hello world!" standalone executable using MSVC.

For further reference, you might find the ECL manual entry on installation and this ECL wiki page page helpful.

Set up Directories

This idea is from Peter Christensen about SBCL on Windows XP. You might want to read it if you plan on installing SBCL on XP with SLIME.

The first step is to create the directory structure for your ECL/EMACS/SLIME system. Create a root folder with two subfolders: 'bin' and a user folder. In this tutorial, the root directory is named 'lisp' and the user> directory is named 'me'. You can name them whatever you want, but avoid spaces since they may cause problems later on. For this tutorial, we will use this directory structure:
  • C:\lisp
  • C:\lisp\bin
  • C:\lisp\me


Download Required Packages


Download the following:
  1. Visual C++ 2010 Express
  2. The ECL sources (tested with ecl-10.4.1)
  3. Pre-compiled Emacs binaries (tested with emacs-22.3-bin-i386)
  4. SLIME (tested with slime-2010-04-16)
Run the Visual C++ installer. It will download and install MSVC to 'C:\Program Files\Microsoft Visual Studio 10.0' and MS SDKs in 'C:\Program Files\Microsoft SDKs'. This tutorial assumes that you installed to the default directories.

The installer may ask you to reboot up to two times. Installation will pick up where it left off upon reboot.

Extract the ECL tarball and Emacs binaries to the bin folder. Remove the version numbers to make typing and upgrading easier. Extract the SLIME tarball to the 'site-lisp' folder in emacs. We now have this directory structure:
  • C:\lisp\bin\ecl
  • C:\lisp\bin\emacs
  • C:\lisp\bin\emacs\site-lisp\slime


Build ECL


To build ECL, use the Visual Studio Command Prompt (2010) which you can access from the Start menu. Using the Visual Studio prompt, change directory to the 'msvc' folder inside ECL, then run 'nmake':

>cd C:\lisp\bin\ecl\msvc
>nmake

Make sure you enter correct path names. You can also check the ECL Makefile for additional nmake flags.

If you encounter problems with mspdb100.dll, you can copy it from 'C:\Program Files\Microsoft Visual Studio 10.0\Common7\IDE' to 'C:\Program Files\Microsoft Visual Studio 10.0\VC\bin'.

Your ECL system will be built inside "C:\dev\bin\ecl\msvc". Check your build by running ecl2.exe. In the same prompt:

>ecl2
ECL (Embeddable Common-Lisp) 10.4.1
Copyright (C) 1984 Taiichi Yuasa and Masami Hagiya
Copyright (C) 1993 Giuseppe Attardi
Copyright (C) 2000 Juan J. Garcia-Ripoll
        ECL is free software, and you are welcome to redistribute it
under certain conditions; see file 'Copyright' for details.
Type :h for Help.  Top level.
>

After making sure that it works, you may close the Visual Studio prompt.


Set up Emacs


Now you need to set Emacs to recognize the user home directory. This part is, again, from Peter Christensen.


Create the file "site-start.el" in the emacs site-lisp directory so we have "C:\lisp\bin\emacs\site-lisp\site-start.el"


We want Emacs to use the user directory as its home. In this tutorial, the user directory is named 'me', so we add this line in site-start.el:


(setenv "HOME" "C:/lisp/me/")

Note the use of forward slashes (/), and the trailing slash.


Set up SLIME


Create a ".emacs" file in the user directory. We'll have something like "C:\lisp\me\.emacs". Put the following lines inside the .emacs file:


     (setq inferior-lisp-program "C:/lisp/bin/ecl/msvc/ecl2.exe")
     (add-to-list 'load-path "C:/lisp/bin/emacs/site-lisp/slime/")
     (require 'slime)
     (slime-setup '(slime-repl))


Create Scripts to Set the ECL Environment


ECL looks for certain directories in three environment variables: PATH, LIB and INCLUDE:


PATH:

  • C:\lisp\bin\ecl\msvc (path to ecl2.exe and ecl.dll)
  • C:\Program Files\Microsoft SDKs\Windows\v7.0A\bin (Microsoft SDK binaries)
  • C:\Program Files\Microsoft Visual Studio 10.0\VC\bin (path to cl.exe)


LIB:

  • C:\lisp\bin\ecl\msvc (path to ecl.lib)
  • C:\Program Files\Microsoft SDKs\Windows\v7.0A\Lib (Microsoft SDK lib)
  • C:\Program Files\Microsoft Visual Studio 10.0\VC\lib (MSVC lib)


INCLUDE:

  •  C:\Program Files\Microsoft SDKs\Windows\v7.0A\Include (Microsoft SDK include)
  •  C:\Program Files\Microsoft Visual Studio 10.0\VC\include (MSVC include)


I have all these variables set in scripts as ecl-slime.bat and ecl-prompt.bat in my root directory. I also find it convenient to work right away in my user directory, so I change directory to it.


C:\lisp\ecl-slime.bat gives me a SLIME REPL prompt in Emacs:


set PATH=%PATH%;C:\lisp\bin\ecl\msvc;C:\Program Files\Microsoft SDKs\Windows\v7.0A\bin;C:\Program Files\Microsoft Visual Studio 10.0\VC\bin
set LIB=C:\lisp\bin\ecl\msvc;C:\Program Files\Microsoft SDKs\Windows\v7.0A\Lib;C:\Program Files\Microsoft Visual Studio 10.0\VC\lib
set INCLUDE=C:\Program Files\Microsoft SDKs\Windows\v7.0A\Include;C:\Program Files\Microsoft Visual Studio 10.0\VC\include
cd me
C:\lisp\bin\emacs\bin\runemacs.exe -f slime

C:\lisp\ecl-cmd.bat produces a cmd prompt set with the ECL environment variables. It only differs from ecl-slime.bat in the last line:


set PATH=%PATH%;C:\lisp\bin\ecl\msvc;C:\Program Files\Microsoft SDKs\Windows\v7.0A\bin;C:\Program Files\Microsoft Visual Studio 10.0\VC\bin
set LIB=C:\lisp\bin\ecl\msvc;C:\Program Files\Microsoft SDKs\Windows\v7.0A\Lib;C:\Program Files\Microsoft Visual Studio 10.0\VC\lib
set INCLUDE=C:\Program Files\Microsoft SDKs\Windows\v7.0A\Include;C:\Program Files\Microsoft Visual Studio 10.0\VC\include
cd me
cmd

Build a Standalone "Hello World!" Program


Run ecl-slime.bat, then press M-x, type in 'slime', and press enter. After ECL finishes compiling swank, C-x C-f to make a new file, hello.lisp and enter these lines:


(format t "Hello world!")
(quit)

Save hello.lisp and compile it in REPL:


(compile-file "hello.lisp" :system-p t)

:system-p t tells compile-file to create an object file needed to build the standalone exe. In this case, it's hello.obj. To build the standalone executable, in REPL:


(c:build-program "hello" :lisp-files '("hello.obj"))

The compiler will look for the src directory where you extracted ECL earlier. This should create hello.exe, which looks for ecl.dll when run. It looks for ecl.dll in at least three places: in the present working directory, in the same directory as hello.exe, or in a directory included in the PATH variable.


Now let's test our program! Run ecl-cmd.bat so that hello.exe could find ecl.dll via the PATH variable. Run hello.exe to get the Hello world output:


>hello.exe
Hello world!

Congratulations! You now have a fully functional ECL build complete with EMACS and SLIME.