VTK/GitMSBuild: Difference between revisions

From KitwarePublic
< VTK
Jump to navigationJump to search
Line 65: Line 65:
You cannot have both paths in the PATH environment variable. Use either one or the other.
You cannot have both paths in the PATH environment variable. Use either one or the other.


=== Directories ===
Now create the following directories:
Now create the following directories:
{| class="wikitable" style="text-align: left; color: green;"
{| class="wikitable" style="text-align: left; color: green;"

Revision as of 03:25, 22 March 2012

Building VTK using Git and MSBuild

Preliminary steps

We are going to create a script called pull.sh that will automatically pull down onto your computer the files from the VTK and WikiExamples git repositories. This assumes that you have Git installed on your system.

Then we will create file called build.cmd that will build debug and release versions of VTK and WikiExamples. It will use MSBuild to build the software and set the number of CPU's to use to the maximum on your system. This file has the ability to perform separate configurations such as 32-bit or 64-bit. To do this we will create a link to this file called build-x64 that passes a parameter to build.cmd specifying a 64-bit build. Of course you can edit this is as you want, for example, setting a 32-bit build instead.

The key advantage of this process is that the pulling and building process is then largely automated.

By using scripting in this manner 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 as mentioned above the scripts have been written so that a 32-bit build can be done by changing just one link.

VTK and the WikiExamples will be built for both release and debug with the release and debug versions being 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. This means that when you are building your own code, using these libraries, you can easily have a debug version and a release version. This will be demonstrated in the WikiExamples build.

If you have express versions of the compiler and want to use the 64-bit version make sure you have the correct Windows SDK installed. This link may provide useful information for VS2008: [1]

Keeping in mind that we are keeping separate source and build/release trees the following directory structure will be used:

%KITWARE_PATH%\build\VTK\Release
%KITWARE_PATH%\build\VTK\Debug
%KITWARE_PATH%\build\WikiExamples\Release
%KITWARE_PATH%\build\WikiExamples\Debug
%KITWARE_PATH%\src\VTK
%KITWARE_PATH%\src\WikiExamples
%KITWARE_PATH%\src\VTKData
%KITWARE_PATH%\src\VTKLargeData

Where %KITWARE_PATH% will be the root path e.g C:\Kitware or C:\Users\<your username>\Code\Kitware, you can choose whatever you prefer here.

Before proceeding you will need to set some environment variables.

Environment variables

Set the following environment variables:

KITWARE_PATH This will be the root path e.g C:\Kitware or C:\Users\<your username>\Code\Kitware you can choose whatever you prefer here
VTK_BIN %KITWARE_PATH%\build\VTK\Release\bin
VTK_DATA_ROOT %KITWARE_PATH%\src\VTKData
VTK_ROOT %KITWARE_PATH%\src\VTK

The VTK_BIN 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.

You normally want the release variables to be available to the system, so add the following path to the PATH environment variable:

%KITWARE_PATH%\build\VTK\Release\bin\Release

However if you are interested in having debug variables available to the system then you must add the following path to the PATH environment variable:

%KITWARE_PATH%\build\VTK\Release\bin\Debug

You cannot have both paths in the PATH environment variable. Use either one or the other.

Directories

Now create the following directories:

%KITWARE_PATH%\src
%KITWARE_PATH%\build\VTK\Debug
%KITWARE_PATH%\build\VTK\Release
%KITWARE_PATH%\build\WikiExamples\Debug
%KITWARE_PATH%\build\WikiExamples\Release

Download VTKsource and VTKData (optionally VTKLargeData) via Git access into %KITWARE_PATH%\src see:

http://www.vtk.org/Wiki/VTK/Git

Download WikiExamples via Git access into %KITWARE_PATH%\src see:

http://www.vtk.org/Wiki/VTK/Examples

Then in the source directory you should have the following folders:

%KITWARE_PATH%\src\VTK
%KITWARE_PATH%\src\VTKData
%KITWARE_PATH%\src\VTKLargeData
%KITWARE_PATH%\src\WikiExamples

Now create some scripts to facilitate pulling data from the git repositories and building using MSBuild.

Scripts and Batch files

Create the following script, calling it pull.sh and putting it in %KITWARE_PATH%\src:

#!/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."

This script will pull VTK etc. into the directories of the same name. Thus automatically updating any changed files.

To use it go to %KITWARE_PATH% in explorer and right-click on src selecting Git Bash Here, when the window opens, type:

./pull.sh

This script is valid for any linux system and also for Mac OS.

Create the following batch file calling it build.cmd and putting it in %KITWARE_PATH%\build:

echo off

set SETENV_CMD="c:\Program Files\Microsoft SDKs\Windows\v7.1\Bin\setenv.cmd"
if not exist %SETENV_CMD% goto missing
call %SETENV_CMD% /%1

SET FP=%KITWARE_PATH%\build
SET RES=log
SET FPRES="%FP%\%RES%"
if not exist "%FPRES%\NUL" md "%FPRES%"
del /Q "%FPRES%\*.log"

SET VTK_DEBUG_CONFIG="VTK.sln" /property:Configuration=Debug /target:ALL_BUILD /maxcpucount:%NUMBER_OF_PROCESSORS% /DetailedSummary
SET VTK_RELEASE_CONFIG="VTK.sln" /property:Configuration=Release /target:ALL_BUILD /maxcpucount:%NUMBER_OF_PROCESSORS% /DetailedSummary
rem For some reason we need to use the project file instead of WikiExamples.sln. 
rem MSBuild is not parsing the BUILD_ALL target in WikiExamples.sln as it does in VTK.
SET WEG_DEBUG_CONFIG="ALL_BUILD.vcxproj" /property:Configuration=Debug /maxcpucount:%NUMBER_OF_PROCESSORS% /DetailedSummary
SET WEG_RELEASE_CONFIG="ALL_BUILD.vcxproj" /property:Configuration=Release /maxcpucount:%NUMBER_OF_PROCESSORS% /DetailedSummary

echo "Building VTK Debug"
cd VTK\Debug
MSBuild %VTK_DEBUG_CONFIG% > "%FPRES%\VTKDebug.log"
cd ..\..

echo "Building VTK Release"
cd VTK\Release
MSBuild %VTK_RELEASE_CONFIG% > "%FPRES%\VTKRelease.log"
cd ..\..

echo "Building WikiExamples Debug"
cd WikiExamples\Debug
MSBuild %WEG_DEBUG_CONFIG% > "%FPRES%\WikiExamplesDebug.log"
cd ..\..

echo "Building WikiExamples Release"
cd WikiExamples\Release
MSBuild %WEG_RELEASE_CONFIG% > "%FPRES%\WikiExamplesRelease.log"
cd ..\..

echo "Finished"
goto :eof

:missing
echo Missing file
echo "%SETENV_CMD%"
goto :eof

Remember to set SETENV_CMD so that setenv.cmd is found and to FP to the build root directory.

Notice

  1. That build.cmd does not specify which build. We will create a link to this file to do this. The builds available are:
    x86
    x64
    ia64
  2. The *_CONFIG variables set the solution file or the project file and the options for MSBuild. Feel free to experiment with the options!