PIL: How to build

Tools required:
* Visual Studio 2008
* Python 2.7.x (2.7.8 was used), must be in PATH
* Unpacker (7-zip for example)

1. Get and compile all required libs:
   * Tk/Tcl (Python built-in) only C headers are required:
     * Get Tk and Tcl sources from http://sourceforge.net/projects/tcl/files/Tcl/, version should match version 
       used in your Python (for Python 2.7.8 you need Tk/Tcl 8.5.15), unpack
     * Copy to new directory (for example "C:\Python27\Tcl\include") tcl.h, tclDecls.h and tclPlatDecls.h from Tcl
       sources and tk.h, tkDecls.h and tkIntXlibDecls.h for Tk sources, copy X11 subdirectory form "xlib/X11" in Tk 
       sources to the X11 subdirectory in same include directory
     OR
     * If your Python is 2.7.8: unpack provided tk-tcl-headers.7z into your Python directory (like "C:\Python27")
   * JPEG lib:
     * Get latest version 6 of jpeg lib at http://www.ijg.org/files/ (jpegsr6b.zip for example), unpack
     * Run "Visual Studio 2008 Command Prompt" and change current directory to JPEG source directory
     * Compile:
       * Run in JPEG sources root:
         copy /b jconfig.vc jconfig.h
         nmake -f makefile.vc libjpeg.lib cvars="-D_CRT_SECURE_NO_WARNINGS=1 -wd4996 -MD -D_MD -Ob2 -Oi -Ot -arch:SSE2" nodebug=1
       OR
       * Run provided jpeg-vc_make.cmd in JPEG sources root
   * zlib:
     * Get latest zlib from http://www.zlib.net (zlib128.zip for example), unpack
     * Run "Visual Studio 2008 Command Prompt" and change current directory to zlib source directory
     * Compile:
       * Run in zlib sources root:
         nmake -f win32/Makefile.msc zlib.lib LOC="-DASMV -DASMINF" OBJA="inffas32.obj match686.obj" CFLAGS="-nologo -MD -Oy- -Zi -Fd\"zlib\" -DASMV -DASMINF -Ox -Ob2 -Oi -Ot -arch:SSE2"
       OR
       * Run provided zlib-vc_make.cmd in zlib sources root
   * Freetype2:
     * Get latest freetype from http://download.savannah.gnu.org/releases/freetype/ (freetype-2.5.3.tag.bz2 for
       example), unpack
     * Run "Visual Studio 2008 Command Prompt" and change current directory to Freetype2 source directory
     * Compile:
       * Run in Freetype2 sources root:
         MSBuild builds\windows\vc2008\freetype.sln /m /t:Build /p:Configuration="LIB Release"
         mkdir "lib"
         copy /b objs\win32\vc2008\*.lib lib\freetype.lib
         mkdir "include\freetype"
         copy /b "include\*.h" "include\freetype"
       OR
       * Run provided freetype2-vc_make.cmd in Freetype2 sources root
   * lcms:
     * Get latest little cms version 1 from http://sourceforge.net/projects/lcms/files/lcms/ (lcms-1.19.zip for
       example), unpack
     * Compile:
       * Open Projects\VC2008\lcms.sln with Visual Studio 2008
       * Change build type to "Release"
       * In properties of "lcms" project change "Runtime Library" to "Multi-threaded DLL"
       * Right click "lcms" project and choose "Build"
       * Copy file Lib\MS\lcms.lib to Lib\lcms.lib
       OR
       * Run "Visual Studio 2008 Command Prompt" and change current directory to lcms source directory
       * Run provided lcms-vc_make.cmd in lcms sources root

2. Download PIL sources from http://effbot.org/downloads/Imaging-1.1.7.tar.gz, unpack

3. Edit setup.py with any text editor:
  * Change TCL_ROOT, JPEG_ROOT, ZLIB_ROOT, FREETYPE_ROOT and LCMS_ROOT so they point to files (see instructions in
    setup.py), for example:
    TCL_ROOT = "C:/Python27/Tcl", "C:/Python27/Tcl/include"
    JPEG_ROOT = "C:/Devl/libs/jpeg/jpeg-6b2"
    ZLIB_ROOT = "C:/Devl/libs/zlib/zlib-1.2.8"
    TIFF_ROOT = None
    FREETYPE_ROOT = libinclude("C:/Devl/libs/freetype/freetype-2.5.3")
    LCMS_ROOT = libinclude("C:/Devl/libs/LCMS/lcms-1.19")

4. Patch PIL sources:
  * Edit libImaging/Access.c:
    * Insert at line 214, just before "#define ADD..":
      int i;
      for (i = 0; i < ACCESS_TABLE_SIZE; i++)
          access_table[i].mode = NULL;
  OR
  * Apply more advanced supplied patch PIL-001-patch-init.patch
    
5. Run "Visual Studio 2008 Command Prompt" and change current directory to PIL source directory

6. Compile:
  * Run in PIL sources root:
    python setup.py build_ext -i
    python selftest.py
  * Check that all libs are marked as "support available", if not - correct paths in setup.py changed at step 3,
    run "python setup.py clean" and delete all .pyd files in PIL, then retry compilation
  * Run in PIL sources root:
    del PIL\*.pyc
    python -O -m compileall PIL\
  OR
  * Run provided PIL-vc_make.cmd in PIL sources root
  * Check that all libs are marked as "support available", if not - correct paths in setup.py changed at step 3
    and rerun PIL-vc_make.cmd

7. Collect files in "PIL\" subdirectory in PIL sources root