Compiling KiCad from Source
===========================
KiCad Documentation Team
Last revised on 31-Jan-2010 by Jerry Jacobs

Introduction
------------
This is a generic guide how to compile kicad from scratch. On windows and linux.
If you need something more distribution specific or the experimental mac-osx port
read the other documents in the Documentation/compiling folder.

Installation of Tools
---------------------
On windows:

Get msys and mingw here:
http://mingw.org/
msys sets up a development environment that allows the bash shell to run.
mingw are a set of tools that run on windows or under msys.  You will need
at least the following mingw packages: make, gcc, g++, binutils, autoconf, and
automake.

On linux:

Install "mesa".  Use your package manager to install the
development libaries.

Tools in PATH
-------------
Make sure g++, make and other tools are in your path.
If windows, then try running g++ and make from within your msys bash shell.


Install or Build wxWidgets
--------------------------

WARNING:
 see wxWidgets_patch_notes.txt for patches and issues in wxWidgets.

If on windows, download
http://sourceforge.net/projects/wxwindows/files/wxAll/2.8.10/wxWidgets-2.8.12.zip/download
or a newer version.
Start msys so you have a bash shell.  Decide where your wxWidgets build directory
will be.  It must be where you can access it from within the msys environment,
such as home/<user>.   Edit your msys/1.0/etc/fstab file if needed to provide
access to this build directory from msys.  (Note that if you want you can build
a "debug" version of the wxWidgets library at this point, instead of the release
version, or in addition to the the release version.)
Unzip the wmMWS zip file into the build directory.  Change directories into
there, and then:

    mkdir build-release
    mkdir build-debug

.Release
----
    cd build-release
    ../configure --enable-unicode --enable-monolithic --disable-shared --with-msw --with-opengl
    make
    make install
----

.Debug
----
    cd build-debug
    ../configure --enable-unicode --enable-monolithic --enable-debug --enable-debug_gdb --disable-shared --with-msw --with-opengl
    make
    make install
----

The default install path is /usr/local.  Generally speaking MinGW likes header
files in /mingw/include and library link files in /mingw/lib.  You can install
path by setting --prefix=/mingw to configure above to change where "make install"
puts everything.  We will refer to the --prefix setting as <wxInstallDir> below.

Verify that wx-config is in your path.  Modify your PATH environment variable
if need be so you can run wx-config from a command prompt.  You may have to
restart your msys shell, depending on how you modify your PATH.

If on linux, use your package manager to install shared object libraries and the
development versions of the wxWidgets packages which include the C++ headers. An
alternative is to build static libaries from source. Verify that wx-config is in
your path by running it from a command prompt. Linux users then go to next step.


Install or Build zlib
---------------------
If on windows, download http://www.zlib.net/zlib123.zip or a newer version.
Start msys so you have a bash shell. Decide where your zlib build directory
will be. It must be where you can access it from within the msys environment,
such as home/<user>. Edit your msys/1.0/etc/fstab file if needed to provide
access to this build directory from msys. Unzip the zlib123.zip file into this
build directory. Change directories into there, and then:

    ./configure
    make
    make install


If linux, use your package manager to install zlib pre-built binaries.

Note (JP Charras):
Under Linux, zlib is usually installed.
If it is not found by the wxWidget installation, wxWidgets creates an alternate zlib library.
Under Windows, if zlib is not installed, my cmake build will try to use the
wxWidgets zlib build.  So, under windows kicad build should work without
zlib install.

If you are using the wxWidget zlib, make sure that the wxWidgets/src/zlib directory is copied
to ${wxWidgets_ROOT_DIR}/src/zlib

Install CMake
-------------
If windows, download the installation binary for windows from cmake.org.
Install that and choose to add cmake to your path during installation.  You
will have to restart and command shells for the new path to take effect.
Verify that cmake is in your path by trying to run it from a command prompt.


If linux, use your package manager to install cmake. You should get cmake 2.6.1
or later. If only an older one is available in your package repository, build
2.6.1 from source. Verify that cmake is in your path by trying to run it from a
command prompt.


Obtain Sources
--------------
You can use the Launchpad repository or a tar file for this.  See the wiki.

Launchpad repository handle 2 branches:
- a testing branch (used by developers)
- a stable branch (a copy of the testing branch, when this testing branch is near a stable state))


Create Makefiles with CMake
---------------------------
If windows, go into your msys shell.  Linux and windows users both then make
two "out of source" build directories:
    cd <kicadSource>
    mkdir -p build/release
    mkdir build/debug
    cd build/release

On either cmake command line shown below, you can optionally include
    -DCMAKE_INSTALL_PREFIX=<finallInstallDir>

If windows, run the following command:
    cmake -G "MSYS Makefiles" -DCMAKE_BUILD_TYPE=Release -DwxWidgets_ROOT_DIR=<wxInstallDir> ../../

If linux, run instead the following command:
    cmake -DCMAKE_BUILD_TYPE=Release ../../

Take a look at CMakeCache.txt, and in particular CMAKE_INSTALL_PREFIX, which
gives the final install directory. If not what you want, edit it with a text
editor and re-run the same cmake command again, but with no
-DCMAKE_INSTALL_PREFIX given on the command line.

Compiling with Make
------------------~
You compile Kicad here.  You will only need to do step 8) again when one of the
CMakeLists.txt files change.  If windows, you must be in your msys shell.
On either platform then:

    cd <kicadSource>/build/release
    make
    [sudo] make install

You are now done unless you want to make a Debug build.


Compiling a Debug version
-------------------------
    cd <kicadSource>/build/debug

On either cmake command line shown below, you can optionally include
-DCMAKE_INSTALL_PREFIX=<finallInstallDir> before the final ../../ argument.
Although normally you do not install the Debug binaries, you can debug them
where they were built.

If windows, run the following command:
    cmake -G "MSYS Makefiles" -DCMAKE_BUILD_TYPE=Debug -DwxWidgets_USE_DEBUG=ON -DwxWidgets_ROOT_DIR=<wxInstallDir> ../../

If linux, run instead the following command:
    cmake -DCMAKE_BUILD_TYPE=Debug -DwxWidgets_USE_DEBUG=ON ../../

Make the Debug binaries:
    make


Fine-tune Build Process
-----------------------
These should be set from command line:

One of these 2 option *must* be set to ON:
  KICAD_STABLE_VERSION or KICAD_TESTING_VERSION
    ** KICAD_STABLE_VERSION:
    set this option to ON to build the stable version of KICAD. mainly used to set version ID
    Usually set for kicad version downloaded from stable branch

    ** KICAD_TESTING_VERSION
    set this option to ON to build the stable version of KICAD. mainly used to set version ID
    Usually set for kicad version downloaded from testing branch

 CMAKE_BUILD_TYPE Release/Debug (REQUIRED)
   Choose build type: Release/Debug.

 wxWidgets_USE_DEBUG ON/OFF (REQUIRED)
   Should be set to ON only when CMAKE_BUILD_TYPE=Debug.

 wxWidgets_USE_STATIC ON/OFF (OPTIONAL)

 CMAKE_VERBOSE_MAKEFILE ON/OFF (OPTIONAL)
   Turns ON/OFF verbose build messages.
   You can also pass VERBOSE=1 to make for the same effect.

 CMAKE_INSTALL_PREFIX (OPTIONAL)

 USE_WX_GRAPHICS_CONTEXT ON/OFF (OPTIONAL)
   *Experimental* advanced drawing library code using wxGraphicsContext (for tests only).
   Under Windows, a very recent version of mingw is needed.
   It requires wxWidgets to be built with the --enable-graphics_ctx switch.
   See building wxWidgets above.

 USE_IMAGES_IN_MENUS ON/OFF (OPTIONAL)
   Force building Kicad with or without images in menu items.  If this is not defined on
   when CMake is used to create the build files, images will be included in menu items
   on all platforms except OSX.

Note: that it is easy to build only a specific binary such as pcbnew alone:
    make pcbnew
