PREREQUISITES

CHECKOUT slicer source files

Check out the code using git:

While it is not enforced, we strongly recommend you to AVOID the use of SPACES for both the source directory and the build directory. We mean it.

Due to maximum path length limitations during build the build process, source and build folders must be located in a folder with very short total path length. This is expecially critical on Windows and MacOS. For example, C:\D\S4 for source and C:\D\S4R for release-mode build folder works on Windows; and /sq5 has been confirmed to work on MacOS.

On Windows enter the commands above in to a bash shell, such as Git Bash (part of msysgit) or use a Git client with a graphical user interface.

CONFIGURE and generate Slicer solution files

There are many ways of customizing Slicer's user interface or feature set. This section describes how a custom Slicer build can be configured at build-time. See an overview of various other options in this presentation.

You can configure and generate Slicer solution files using either ccmake or cmake-gui. To streamline this process, you can also use the SlicerCustomAppTemplate project maintained by Kitware.

Customizing application build

Customized editions of Slicer can be generated without changing Slicer source code, just by modifying CMake variables:

SlicerApp_APPLICATION_NAME: Custom application name to be used, instead of default "Slicer". The name is used in installation package name, window title bar, etc.

Slicer_DISCLAIMER_AT_STARTUP: String that is displayed to the user after first startup of Slicer after installation (disclaimer, welcome message, etc).

Slicer_DEFAULT_HOME_MODULE: Module name that is activated automatically on application start.

Slicer_DEFAULT_FAVORITE_MODULES: Modules that will be added to the toolbar by default for easy access. List contains module names, separated by space character.

Slicer_CLIMODULES_DISABLED: Built-in CLI modules that will be removed from the application. List contains module names, separated by semicolon character.

Slicer_QTLOADABLEMODULES_DISABLED: Built-in Qt loadable modules that will be removed from the application. List contains module names, separated by semicolon character.

Slicer_QTSCRIPTEDMODULES_DISABLED: Built-in scripted loadable modules that will be removed from the application. List contains module names, separated by semicolon character.

Slicer_USE_PYTHONQT_WITH_OPENSSL: enable/disable building the application with SSL support (ON/OFF)

Slicer_EXTENSION_SOURCE_DIRS: Defines additional extensions that will be included in the application package as built-in modules. Full paths of extension source directories has to be specified, separated by semicolons.

Per-platform instructions

Windows

Recommended: run CMake (cmake-gui) from the Windows Start menu

Set the build directory. Use a very short path, for example: C:\D\S4 for source code and for C:\D\S4D build directory are known to work.

You cannot use the same build tree for both release or debug mode builds. If both build types are needed, then the same source directory can be used, but a separate build directory should be created and configured for each build type.

Select your compiler: Visual Studio 16 2019 Win64

Do not configure yet.

Add Qt5_DIR variable pointing to Qt5 folder such as C:\Qt\5.15.0\msvc2019_64\lib\cmake\Qt5: click Add entry button, set Name to Qt5_DIR, Type to PATH, and Value to the Qt5 folder.

Configure

Click generate then close cmake-gui.

If building in release mode:

Open the top-level Slicer.sln file in the build directory in Visual Studio

Set active configuration to Release. Visual Studio will select Debug build configuration by default when you first open the solution in the Visual Studio GUI. If you build Slicer in release mode and accidentally forget to switch the build configuration to Release then the build will fail. Note: you can avoid this manual configuration mode selection by setting CMAKE_CONFIGURATION_TYPES to Release in cmake-gui.

Alternative option: Configure and build using command-line or batch file

Instead of using CMake (cmake-gui), it is also possible to configure by creating a .bat file that contains command-line instructions and run this .bat file each time a Slicer needs to be re-built. The examples below assume Slicer source code is in C:\D\S4 folder and Qt-5.10 is installed in default location.

General information

One of them is in the top-level bin directory Slicer-SuperBuild and the other one is in the subdirectory Slicer-build:

Slicer-SuperBuild/Slicer-build

The first project in Slicer-SuperBuild manages all the external dependencies of Slicer (VTK, ITK, Python, ...). To build Slicer for the first time, run make (or build the solution file in Visual Studio) in Slicer-SuperBuild, which will update and build the external libraries and if successful will then build the subproject Slicer-build.

The second project in Slicer-SuperBuild/Slicer-build is the "traditional" build directory of Slicer. After local changes in Slicer (or after an svn update on the source directory of Slicer), only running make (or building the solution file in Visual Studio) in Slicer-SuperBuild/Slicer-build is necessary (the external libraries are considered built and up to date).

Warning: An significant amount of disk space is required to compile Slicer in Debug (>10GB on Windows)

Workaround for if the firewall is blocking git protocol

Some firewalls will block the git protocol. A possible workaround is to configure Slicer by disabling the option Slicer_USE_GIT_PROTOCOL. Then the http protocol will be used instead. Consider also reading https://github.com/commontk/CTK/issues/33

BUILD Slicer

After configuration, start the build process in the Slicer-SuperBuild directory

Linux or MacOSX (Makefile)

Windows (Visual Studio)

Start a terminal.

$ cd ~/Projects/Slicer-SuperBuild
$ make -j<NUMBEROFCORES>

In case of file download hash mismatch error, you need to acquire the latest wget, and build cmake with OpenSSL turned on. For more information, see here and here

When using the -j option, the build will continue past the source of the first error. If the build fails and you don't see what failed, rebuild without the -j option. Or, to speed up this process build first with the -j and -k options and then run plain make. The -k option will make the build keep going so that any code that can be compiled independent of the error will be completed and the second make will reach the error condition more efficiently.

If you make local changes to Slicer, open the solution file located in the directory Slicer-SuperBuild/Slicer-build instead. You should then be able to either build all projects or just a specific one.

Windows

Unix-like

NA

Common errors

A tool returned an error code from "Generating vtksysProcessFwd9xEnc.c"

The application has failed to start because its side-by-side configuration is incorrect.
Please see the application event log or use the command-line sxstrace.exe tool for more detail.
Project : error PRJ0019: A tool returned an error code from "Generating
vtksysProcessFwd9xEnc.c"

Platform Notes

Windows: If build was OK, but it Slicer doesn't start (gives the error: [bin/Release/SlicerQT-real.exe] exit abnormally - Report the problem.) then one possible root cause is that you have a copy of Python26.dll in your windows system directory (e.g., c:\Windows\System32\python26.dll). The solution is to rename or remove the python dll in the system directory. See more details here: http://www.na-mic.org/Bug/view.php?id=1180