1Windows native port
2===================
3
4The previous section explained how to cross-build OpenTURNS from Linux
5to target Windows. This section gives hints in order to compile
6OpenTURNS natively with Microsoft Windows tools. This port has started
7since OpenTURNS 1.4 and is still in an early stage, any help from
8developers who are familiar with Windows environment will be greatly
9appreciated.
10
11These instructions have been tested with Visual Studio 2010 on Windows
1232 and 64 bits, but they should work with more recent versions too.
13Visual Studio Express Edition does not ship 64 bits compilers, but they
14can be installed from Windows SDKs, instructions can be easily found
15online. No Fortran compiler is required.
16
17Automatic compilation
18---------------------
19
20First of all, CMake must be installed, as well as C/C++ compilers. We
21will describe below how to use CMake with Visual Studio and Microsoft
22compilers, but CMake can also be used with other build systems (like
23NMake, for instance) and other compilers, see CMake documentation for
24further details.
25
26The following programs are required in order to build on Windows:
27
28-  `OpenBLAS <https://github.com/xianyi/OpenBLAS/>`_ (or any other BLAS
29   implementation)
30
31The following programs are optional, and we will show how to embed them:
32
33-  `Boost <https://www.boost.org/>`_
34
35-  `LibXML2 <http://www.xmlsoft.org/>`_
36
37-  `TBB <https://www.threadingbuildingblocks.org/>`_
38
39-  `hmat-oss <https://github.com/jeromerobert/hmat-oss/>`_
40
41If you want to recompile them from sources, you may also have to install
42
43-  `Git <http://git-scm.com/>`_
44
45Installation layout
46~~~~~~~~~~~~~~~~~~~
47
48In this tutorial, dependencies are installed by following the
49layout shown in figure [fig:win-inst-layout]. Below the top-level
50directory are four configurations (Debug and Release for Windows 32 bits
51or 64 bits). Of course, if you are only interested by a single
52configuration, there is no need to create others. Each configuration
53contains subdirectories for Boost, hmat-oss, LibXML2, OpenBLAS
54and TBB programs. Each project contains one or several directories:
55``bin`` for DLLs, ``include`` for header files, and ``lib`` for static
56libraries.
57
58.. figure:: Figures/win_native/win-inst-layout.png
59   :alt: Windows installation layout
60
61   Windows installation layout
62
63For convenience, all libraries will be compiled as static libraries,
64except OpenBLAS.
65
66Build and install OpenBLAS and TBB
67~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
68
69For different reasons, OpenBLAS and TBB cannot be compiled along with
70other dependencies. As explained on their site, OpenBLAS is currently
71only supported on Windows with Mingw compiler. But binaries can be used
72with Visual Studio, this is what we will do. Thus go to
73http://sourceforge.net/projects/openblas/files/ and download Windows
74binaries, for instance
75``OpenBLAS-v0.2.13-Win32.zip`` for Windows 32 bits, or
76``OpenBLAS-v0.2.13-Win64-int32.zip`` for Windows 64 bits.
77
78Unzip these archives, and copy files to our installation folder:
79
80-  | ``OpenBLAS-x.y-Win32\bin\libopenblas.dll`` into
81     ``Win32-Release\openblas\bin\`` and
82   | ``Win32-Debug\openblas\bin\``
83
84-  | ``OpenBLAS-x.y-Win32\lib\libopenblas.dll.a`` into
85     ``Win32-Release\openblas\lib\`` and
86   | ``Win32-Debug\openblas\lib\``
87
88-  | ``OpenBLAS-x.y-Win32\include`` into ``Win32-Release\openblas\`` and
89   | ``Win32-Debug\openblas\``
90
91-  | ``OpenBLAS-x.y-Win64-int32\bin\libopenblas.dll`` into
92     ``Win64-Release\openblas\bin\`` and
93   | ``Win64-Debug\openblas\bin\``
94
95-  | ``OpenBLAS-x.y-Win64-int32\lib\libopenblas.dll.a`` into
96     ``Win64-Release\openblas\lib\`` and
97   | ``Win64-Debug\openblas\lib\``
98
99-  | ``OpenBLAS-x.y-Win64-int32\include`` into
100     ``Win64-Release\openblas\`` and
101   | ``Win64-Debug\openblas\``
102
103Note that DLLs have been compiled with Mingw, and require some Mingw
104runtime libraries. They can be found in
105http://sourceforge.net/projects/openblas/files/v0.2.12/mingw32_dll.zip
106and
107http://sourceforge.net/projects/openblas/files/v0.2.12/mingw64_dll.zip.
108They are:
109
110-  ``libgcc_s_sjlj-1.dll``, ``libgfortran-3.dll`` and
111   ``libquadmath-0.dll`` for Win32
112
113-  ``libgcc_s_seh-1.dll``, ``libgfortran-3.dll`` and
114   ``libquadmath-0.dll`` for Win64
115
116TBB comes with its own Visual Studio 2010 configuration file, but we did
117not find how to integrate it into the build system described below. Thus
118the easiest solution is to:
119
120#. Download TBB sources from
121   https://www.threadingbuildingblocks.org/download
122
123#. Unpack it.
124
125#. Launch ``build\vs2010\makefile.sln``
126
127#. Select ``Win32`` or ``x64`` architecture, and ``Release`` or
128   ``Debug`` configuration (not ``Release-MT`` or ``Debug-MT``, unless
129   you know what you are doing).
130
131#. If you want to build a static library, edit Properties, tab
132   Configuration Properties, General, Configuration Type, as shown in
133   figure [fig:vs-tbb-static]
134
135#. Build project.
136
137#. Copy resulting libraries into installation folder:
138
139   -  ``build\vs2010\ia32\Debug\tbb_debug.lib`` into
140      ``Win32-Debug\tbb\lib\``
141
142   -  ``build\vs2010\ia32\Release\tbb.lib`` into
143      ``Win32-Release\tbb\lib\``
144
145   -  ``build\vs2010\intel64\Debug\tbb_debug.lib`` into
146      ``Win64-Debug\tbb\lib\``
147
148   -  ``build\vs2010\intel64\Release\tbb.lib`` into
149      ``Win64-Release\tbb\lib\``
150
151#. | Copy ``include\tbb`` folder into installation folders:
152     ``Win32-Debug\tbb\include``,
153   | ``Win32-Release\tbb\include``, ``Win64-Debug\tbb\include`` and
154     ``Win64-Release\tbb\include``.
155
156.. figure:: Figures/win_native/vs-tbb-static.png
157   :alt: Visual Studio settings to build tbb as a static library
158
159   Visual Studio settings to build tbb as a static library
160
161Build and install
162~~~~~~~~~~~~~~~~~
163
164OpenBLAS and TBB are low level libraries. Other libraries use STL, and
165care must be taken to avoid mismatch between runtime libraries. To this
166end, we decided to use a so called *SuperBuild* approach with CMake. We
167defined a metaproject which drives compilation of those dependencies,
168and also of OpenTURNS itself. Clone `ot-superbuild git repository
169<https://bitbucket.org/dbarbier/ot-superbuild>`_ (or download an archive
170from this URL), launch ``cmake-gui`` program, and follow the following
171steps:
172
173#. Launch ``cmake-gui``, and select source and build directories
174
175.. figure:: Figures/win_native/cmake-gui-start.png
176
177#. Click on button. Select a generator (either Visual Studio 10 or
178   Visual Studio 10 Win64) and compiler
179
180.. figure:: Figures/win_native/cmake-gui-compiler.png
181
182#. For Win64, CMake may give an error about missing 64-bit tools, as in
183   snapshot below. Visual Studio Express Edition does not embed 64-bit
184   compilers, and CMake thus checks whether we are using Express Edition
185   or not.
186
187.. figure:: Figures/win_native/cmake-gui-error.png
188
189   It seems that this detection is sometimes buggy; if you know that
190   64-bit compilers are available, you can workaround this automatic
191   detection by clicking on button, adding a ``CMAKE_GENERATOR_TOOLSET``
192   variable, of type ``STRING``, and value ``v100``.
193
194.. figure:: Figures/win_native/cmake-gui-toolset.png
195
196#. Click on button again, everything should work fine now, and output
197   window should display ``Configuring done``.
198
199#. Now that CMake has checked that our compiler is working fine, we can
200   tell it where to find OpenBLAS and TBB. Set ``OPENBLAS_INCLUDE_DIR``,
201   ``OPENBLAS_LIBRARY``, ``TBB_INCLUDE_DIR`` and ``TBB_LIBRARY``
202   variables, as shown below:
203
204.. figure:: Figures/win_native/cmake-gui-superbuild.png
205
206   and click on button.
207
208#. If everything went fine, click on button. This generates Visual
209   Studio solution files in the specified build directory, and you can
210   now close ``cmake-gui`` window.
211
212#. Launch ``openturns-superbuild`` solution file.
213
214.. figure:: Figures/win_native/vs-superbuild.png
215
216   Select ``Release`` or ``Debug`` configuration (it must match TBB
217   configuration), and build solution file. This will download sources
218   (a working Internet connection is thus required), unpack and build
219   them. It can take a long time on a slow machine, or with a slow
220   Internet connection, since some downloaded sources are large.
221
222#. Copy ``build64\ExternalProjects\Install\*`` directories into
223   installation prefix (``OT1.5\Win64-Release\``, or ``Win32-Release``,
224   etc)
225
226Manual compilation
227------------------
228
229If you want to modify settings, the simplest solution is to proceed as
230in previous section, and modify Visual Studio settings afterwards.
231Dependencies are downloaded, built and installed into an
232``ExternalProjects`` subdirectory of build directory, ie
233``build64\ExternalProjects`` in our example. This directory contains the
234following folders:
235
236-  ``Build``: contains generated Visual Studio projects, and files
237   generated during builds
238
239-  ``Download``: contains project archives
240
241-  ``Install``: after build, each project installs resulting files
242   (header files and libraries) there
243
244-  ``Source``: unpacked source files
245
246-  ``Stamp``: keeps track of already processed steps
247
248-  ``tmp``
249
250Each directory in turn contains one directory per project. Thus if one
251wants to modify some settings when compiling OpenTURNS, one has to go to
252``build64\ExternalProjects\Build\openturns\`` directory and launch the
253Visual Studio solution file found there, in this case ``OpenTURNS.sln``.
254For instance, one can build OpenTURNS tests from this solution file.
255Beware to always check that active configuration is the desired one.
256
257Troubleshooting
258---------------
259
260-  It is possible to build multiple configurations with Visual Studio
261   solution files, but this is currently not supported by our
262   ``CMakeLists.txt`` files; thus one must launch ``cmake-gui``, adapt
263   variables (for instance paths to OpenBLAS and TBB libraries must be
264   modified for each configuration) and press and buttons.
265
266-  No OpenBLAS library in ``Debug`` mode is provided, but the one from
267   ``Release`` mode works also in ``Debug`` mode. On the other hand,
268   OpenTURNS and TBB configurations must match, it is not possible to
269   link OpenTURNS in ``Debug`` mode against TBB in ``Release`` mode, or
270   vice-versa.
271
272-  Boost contains files with very long filenames, which causes trouble
273   on NTFS. If you have already built Boost and want to build it again,
274   Visual Studio may complain that it encountered an error when building
275   it again. In that case, launch file explorer and remove Boost
276   directory, then press again button of CMake (because some of its
277   generated files had been removed too), it should now build fine.
278
279