<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
  <title>Building VTK using devenv</title>
  <meta name="generator" content="Bluefish 2.0.2" >
  <meta name="author" content="amaclean" >
  <meta name="date" content="2011-09-20T09:01:52+1000" >
  <meta name="copyright" content="">
  <meta name="keywords" content="">
  <meta name="description"
 content="Building VTK using devenv">
  <meta name="ROBOTS" content="NOINDEX, NOFOLLOW">
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <meta http-equiv="content-style-type" content="text/css">
  <meta http-equiv="expires" content="0">
  <meta>
  <style type="text/css">
        h1 {border-width: 1; border: solid; text-align: center; background-color: #FFFFF0;}
        code {font-family: courier, fixed, monospace;}

        table.general {
                <!-- background-color: #39152E; -->
                border-color: black;
                border-style: solid;
                border-width: 1px;
                border-spacing: 0px;
                border-collapse: separate;
                <!-- font-family: courier, fixed, monospace; -->
        }               
        table.general td {
                border-width: 1px;
                padding: 10px;
                border-style: solid;
                <!-- border-color: gray; -->
                <!-- color: white; -->
                <!-- font-weight: bold; -->
                <!-- font-family: courier, fixed, monospace; -->
        }       
        table.terminal {
                background-color: #39152E;
                border-color: black;
                border-style: solid;
                border-width: 2px;
                border-spacing: 0px;
                border-collapse: separate;
                font-family: courier, fixed, monospace;
        }               
        table.terminal td {
                border-width: 2px;
                padding: 10px;
                border-style: solid;
                border-color: gray;
                color: white;
                <!-- font-weight: bold; -->
                <!-- font-family: courier, fixed, monospace; -->
        }
        table.tty {
                background-color: #2E3436;
                border-color: black;
                border-style: solid;
                border-width: 2px;
                border-spacing: 0px;
                border-collapse: separate;
                font-family: courier, fixed, monospace;
        }               
        table.tty td {
                border-width: 2px;
                padding: 10px;
                border-style: solid;
                border-color: gray;
                color: #BDC5BC;
                <!-- font-weight: bold; -->
                <!-- font-family: courier, fixed, monospace; -->
        }       
        
        table.code {
                background-color: #FFFFF0;
                border-color: #90EE90;
                border-spacing: 0px;
                border-style: solid;
                border-width: 2px;
                font-family: courier, fixed, monospace;
        }
        table.code td {
                border-width: 2px;
                padding: 10px;
                border-style: solid;
                border-color: #90EE90;
                color: black;
                <!-- font-weight: bold; -->
                <!-- font-family: courier, fixed, monospace; -->
        }

        pre {
           background-color: #F9F9F9;
                border-width: 1px; 
                border-style: dashed;
                border-color: blue;
                padding: 10px;
        }
        
   td.green_off {
        color: #FFFFFF;
        background-color: #000000;
        }
   td.green_on {
        color: #000000;
        background-color: #99ff00;
        }
   td.amber_on {
        color: #000000;
        background-color: #ff9900;
        }
   td.no_led {
        color: #000000;
        background-color: #cccccc;
        }
  </style>
</head>
<body>
<h1>Building VTK using devenv</h1>
<h2>Preliminary steps.</h2>
We are going to build VTK and WikiExamples using devenv called from a script. The advantage here is that we run a script to update VTK and WikiExamples from the git repositories and then run a second script to do the builds. Thus the processes are largely automated.<br>
<br>By using scripting we can easily change or add more builds and change build types. In the following discussion we will be doing a 64-bit build. However the scripts have been written so that a 32-bit build can be done by changing just one link.<br>
<br>VTK and the WikiExamples will be built for both release and debug and the release and debug versions will be in separate trees. Thus there will be a different build directory for each build configuration. The advantage of this is that there is one tree for each build and we have a clean separation of the build types. You can also extend the scripting to deal with both 32-bit ad 64-bit builds on the same machine. <br>
<br>If you have express versions of the compiler and want to use the 64-bit version then the link may provide useful information: 
<blockquote>
<p><a href="http://blog.victorjabur.com/2011/06/05/compiling-python-2-7-modules-on-windows-32-and-64-using-msvc-2008-express/">Compiling Python 2.7 Modules on Windows 32 and 64 using MSVC++ 2008 Express</a></p>
</blockquote><br>In the following discussion you should change <code>C:\Kitware_Path\</code> to something appropriate for your system.<br>
For example you may like to change it to something like <code>C:\Users\username\Code\Kitware\</code> where <code>username</code> could be your own username on the Windows system.<br>
Set up the following directories:<br>
<table class="code">
<tr><td>C:\Kitware_Path\src</td></tr>
<tr><td>C:\Kitware_Path\build\VTK\Debug</td></tr>
<tr><td>C:\Kitware_Path\build\VTK\Release</td></tr>
<tr><td>C:\Kitware_Path\build\WikiExamples\Debug</td></tr>
<tr><td>C:\Kitware_Path\build\WikiExamples\Release</td></tr>
</table>
<p>Download VTKsource via Git access into <code>C:\Kitware_Path\src</code> see: </p>
<blockquote>
<p><a href="http://www.vtk.org/Wiki/VTK/Git">http://www.vtk.org/Wiki/VTK/Git</a></p>
</blockquote>
<p>Download WikiExamples via Git access into <code>C:\Kitware_Path\src</code> see: </p>
<blockquote>
<p><a href="http://www.vtk.org/Wiki/VTK/Examples">http://www.vtk.org/Wiki/VTK/Examples</a></p>
</blockquote>
Then in the source directory you should have the following folders:<br>
<table class="code">
<tr><td>C:\Kitware_Path\src\VTK</td></tr>
<tr><td>C:\Kitware_Path\src\VTKData</td></tr>
<tr><td>C:\Kitware_Path\src\VTKLargeData</td></tr>
<tr><td>C:\Kitware_Path\src\WikiExamples</td></tr>
</table>
Now create some scripts to facilitate pulling data from the git repositories and building using nmake.
<h2>Scripts and Batch files</h2>
Create the following script, calling it <code>pull.sh</code> and putting it in <code>C:\Kitware_Path\src</code>:
<pre>
#!/bin/bash
# Pull all git repositories in the directory this is running in and 
# any specified repositories in subdirectories.

# Author: Andrew Maclean
# Date: 2010-04-30
# Last Modified: 2010-12-17
# Reason: Handles System V version of ls which generates 9 fields
#         and the Berkley version of ls which generates 8 fields. 
# Last Modified: 2011-03-18
# Reason: Made pulling a function so that subdirectories can be accessed.
#         A log directory will be created in each directory. 

pull()
{
        # Runs a git checkout into each of the subdirectories.
        # We are assuming that each of the subdirectories is
        # legitimate and that git has been set up for each directory.
        outDir="$PWD/log"
        [ -d "$outDir" ] || mkdir $outDir

        for fn in $dirs
        do
        (
            output="$outDir/${fn}.log"
            # remove old log files
            rm -f $output 2>/dev/null

            cd $fn || exit 1
            [ -d .git ] || {
                echo "Error: no .git directory for $fn"
                exit 1
            }

            echo "Pulling  $fn"
            echo "Started:  `date`" >> $output
            git pull --rebase 2>&1  >> $output
            # update submodules (if they exist)
            mods=$(git submodule | wc -l)
            [ "$mods" != 0 ] && git submodule update 2>&1 >> $output

            # update hooks (if they exist)
            hooks=$(git branch -a -l | grep hooks | wc -l)
            if [ "$hooks" != 0 ]
            then
                ( cd .git/hooks && git pull .. remotes/origin/hooks 2>&1 >> $output )
            fi

            echo "Finished: `date`" >> $output
        )
        done
}

pull_data()
{
        # Runs a git checkout into each of the subdirectories.
        # We are assuming that each of the subdirectories is
        # legitimate and that git has been set up for each directory.
        outDir="$PWD/log"
        [ -d "$outDir" ] || mkdir $outDir

        for fn in $dirs
        do
        (
            output="$outDir/${fn}.log"
            # remove old log files
            rm -f $output 2>/dev/null

            cd $fn || exit 1
            [ -d .git ] || {
                echo "Error: no .git directory for $fn"
                exit 1
            }

            echo "Pulling  $fn"
            echo "Started:  `date`" >> $output
            git pull --rebase 2>&1  >> $output

            echo "Finished: `date`" >> $output
        )
        done
}


#---------------- main -------------------
main()
{
        # We need CMake VTK ParaView ITK
        # dirs=$(ls -l |awk '/^d/ {print $NF}' | awk '$0 ~ /(^CMake$|^VTK$|^ParaView$|^ITK$)/ {print $0}')
        dirs=$(ls -l |awk '/^d/ {print $NF}' | awk '$0 ~ /(^VTK$)/ {print $0}')
        pull $dirs
        # We need VTKData ParaViewData wikiexamples ITKApps
        # dirs=$(ls -l |awk '/^d/ {print $NF}' | awk '$0 ~ /(^VTKData$|^VTKLargeData$|^ParaViewData$|^wikiexamples$|^ITKApps$)/ {print $0}')
        dirs=$(ls -l |awk '/^d/ {print $NF}' | awk '$0 ~ /(^VTKData$|^VTKLargeData$|^WikiExamples$)/ {print $0}')
        pull_data $dirs
}

main
echo "Finished pulling."
</pre>
This script will pull VTK etc. into the directories of the same name. Thus automatically updating any changed files.<br>
To use it go to <code>C:\Kitware_Path</code> in explorer and right-click on <code>src</code> selecting <code>Git Bash Here</code>, when the window opens, type:<br>
<pre>./pull.sh</pre>
This script is valid for any linux system and also for Mac OS.<br>

Create the following batch file calling it <code>devenv.cmd</code> and putting it in <code>C:\Kitware_Path\build</code>:<br>
<pre>
echo off

rem Based on: http://www.cmake.org/Wiki/CMake_Generator_Specific_Information
set VC_PATH="C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC"
if not exist %VC_PATH%\vcvarsall.bat goto missing
echo call "%VC_PATH%\vcvarsall.bat"
call %VC_PATH%\vcvarsall.bat %1

SET FP=C:\Kitware_Path\build
SET RES=log
SET FPRES="%FP%\%RES%"
rem SET DEVENV="C:/Program Files/Microsoft Visual Studio 10.0/Common7/IDE/devenv.com"

if not exist "%FPRES%\NUL" md "%FPRES%"
del /Q "%FPRES%\*.log"

echo "Building VTK Debug"
cd VTK\Debug
devenv "VTK.sln" /build Debug /project ALL_BUILD > "%FPRES%\VTKDebug.log"
cd ..\..

echo "Building VTK Release"
cd VTK\Release
devenv "VTK.sln" /build Release /project ALL_BUILD > "%FPRES%\VTKRelease.log"
cd ..\..

echo "Building WikiExamples Debug"
cd WikiExamples\Debug
devenv "WikiExamples.sln" /build Debug /project ALL_BUILD > "%FPRES%\WikiExamplesDebug.log"
cd ..\..

echo "Building WikiExamples Release"
cd WikiExamples\Release
devenv "WikiExamples.sln" /build Release /project ALL_BUILD > "%FPRES%\WikiExamplesRelease.log"
cd ..\..


echo "Finished"


goto :eof

:missing
echo Missing file
echo "%VC_PATH%\vcvarsall.bat"
goto :eof
</pre>
Remember to set <code>VC_PATH</code> so that <code>vcvarsall.bat</code> is found and <code>FP</code> to the build root directory.<br>
Notice that <code>devenv.cmd</code> does not specify which build. The builds available are:
<ul>
<li>x86</li>
<li>amd64</li>
<li>x64</li>
<li>ia64</li>
<li>x86_amd64</li>
<li>x86_ia64</li>
</ul>
Note: x64 is the same as amd64.<br>
We are going to do a 64-bit build so we will create a shortcut to <code>devenv.cmd</code> and pass it the <code>amd64</code> parameter.
Now create a shortcut to <code>devenv.cmd</code> in <code>C:\Kitware_Path</code>.<br> For the following we are assuming that we will be building 64-bit code.<br>
Rename the shortcut to <code>devenv_amd64</code> right-click on it, selecting <code>Properties</code> and edit the target line to read:<br>
<pre>%comspec% /k ""C:\Kitware_Path\build\devenv.cmd""  amd64</pre><br>
Edit the start in line to read:<br>
<pre>C:\Kitware_Path\build</pre><br>
At this point we have everything necessary to invoke 64-bit builds. However we must first set the necessary environment variables and run CMake in each of the Debug and Release directories.
<h2>Environment variables</h2>
Set the following environment variables:
<table class="code">
<tr><td>VTK_BIN</td><td>C:\Kitware_Path\build\VTK\Release\bin</td></tr>
<tr><td>VTK_DATA_ROOT</td><td>C:\Kitware_Path\src\VTKData</td></tr>
<tr><td>VTK_ROOT</td><td>C:\Kitware_Path\src\VTK</td></tr>
</table>
The <code>VTK_BIN</code> environment variable will be the default one for all your builds. If you want a debug build then you will change it as outlined below.<br>
<br>You normally want the release variables to be available to the system, so add the following path to the PATH environmnet variable:
<pre>
C:\Kitware_Path\build\VTK\Release\bin\Release
</pre>
<h2>Running CMake</h2>
We are using a 64-bit build so we need to run <pre>Visual Studio x64 Win64 Command Prompt (2010)</pre> when this opens, cd to <code>C:\Kitware_Path\build\VTK\Debug</code>.
Then find where CMake is on your system, for example, <code>C:\Program Files (x86)\CMake 2.8\bin\cmake-gui.exe</code>, and enter the following line into the command window:
<pre>"C:\Program Files (x86)\CMake 2.8\bin\cmake-gui.exe"</pre>
You should see something like this:
<table class="tty">
<tr>
<td>
Setting environment for using Microsoft Visual Studio 2010 x64 tools.<br>
<br>
C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC> cd C:\Kitware_Path\build\VTK\Debug<br>
<br>
C:\Kitware_Path\build\VTK\Debug>"C:\Program Files (x86)\CMake 2.8\bin\cmake-gui.exe"<br>
<br>
C:\Kitware_Path\build\VTK\Debug><br>
<br>               
</td>
</tr>
</table>
The CMake GUI will open.<br>
Select <code>C:/Kitware_Path/src/VTK</code> as the source code and <code>C:/Kitware_Path/build/VTK/Debug</code> as where to build the binaries.<br>
Select <b>Visual Studio 10 Win64</b> and then the other options as required, then configure and generate.<br>
Note that we selected <b>Visual Studio 10 Win64</b> because we are using Visual Studio 10 x64 tools.<br>
Proceed in a similar fashion for the release build of VTK, changing the where to build directory to <code>C:/Kitware_Path/build/VTK/Release</code> first.<br>
<b>Inportant:</b>
<ul>
<li>In the case of a debug build, set:
        <ul>
        <li><b>VTK_DIR</b> to <code>C:/Users/username/Code/Kitware/build/VTK/Debug</code></li>
        <li><b>CMAKE_BUILD_TYPE</b> to be <b>Debug</b>.</li>
        </ul>
<li>In the case of a release build, set:
        <ul>
        <li><b>VTK_DIR</b> to <code>C:/Users/username/Code/Kitware/build/VTK/Release</code></li>
        <li><b>CMAKE_BUILD_TYPE</b> to be <b>Release</b>.</li>
        </ul>
</ul>
The rationale here is that we only have a debug build in the debug tree and a release build in the release tree.<br>
Do the same for WikiExamples remembering to change paths and setting <b>VTK_DIR</b> and <b>CMAKE_BUILD_TYPE</b> as appropriate.
<h2>Checkout and build</h2>
<ol>
<li>Go to <code>C:\Kitware_Path</code> in explorer and right-click on <code>src</code> selecting <code>Git Bash Here</code>, when the window opens, type:</li>
<pre>./pull.sh</pre>

<li>Go to <code>C:\Kitware_Path</code> in explorer and double-click on the link <code>devenv-amd64</code></li>
</ol>
You should see something like this:
<table class="tty">
<tr>
<td>
<br>
C:\Kitware_Path\build>echo off<br>
call ""C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC"\vcvarsall.bat"<br>
Setting environment for using Microsoft Visual Studio 2010 x64 tools.<br>
A subdirectory or file C:\Kitware_Path\build\log already exists.<br>
"Building VTK Debug"<br>
"Building VTK Release"<br>
"Building WikiExamples Debug"<br>
"Building WikiExamples Release"<br>
"Finished"<br>
<br>
C:\Kitware_Path\build><br>
</td>
</tr>
</table>
Log files will be found in:
<table class="code">
<tr><td>C:\Kitware_Path\src\log</td></tr>
<tr><td>C:\Kitware_Path\build\log</td></tr>
</table>
<h2>Building your own code.</h2>
Assuming the environment variables have been set up as above. When you run Cmake make sure that <b>Visual Studio 10 Win64</b> is selected and that:
<li>In the case of a debug build, set:
        <ul>
        <li><b>VTK_DIR</b> to <code>C:/Users/username/Code/Kitware/build/VTK/Debug</code></li>
        </ul>
<li>In the case of a release build, set:
        <ul>
        <li><b>VTK_DIR</b> to <code>C:/Users/username/Code/Kitware/build/VTK/Release</code></li>
        </ul>
</ul>
If you are debugging then you will have to change the path from the release dlls to the debug dlls in the PATH environment variable to:
<br>You normally want the release variables to be available to the system, so add the following path to the PATH environmnet variable:
<pre>
C:\Kitware_Path\build\VTK\Debug\bin\Debug
</pre>
Or alternatively copy the VTK debug versions of the dlls into the same directory as the debug executable. This can be automatically done using CMake.
</html>