Difference between revisions of "Building and hacking LDC on Windows using MSVC"

From D Wiki
Jump to: navigation, search
(Formatting)
(Revise compiler switches, LLVM build type and instructions on how to run the tests.)
Line 1: Line 1:
LDC on Windows is work in progress. This page documents how to compile LDC2 on Windows 7 x64. It also provides hints on druntime and phobos.
+
LDC on Windows is work in progress. This page documents how to compile LDC2 on Windows x64. It also provides hints on druntime and phobos.
  
 
== Building LDC ==
 
== Building LDC ==
Line 5: Line 5:
 
=== Required software ===
 
=== Required software ===
  
* Windows 7 x64, of course!
+
* Windows x64, of course!
 
* Install Visual Studio (e.g. [http://www.visualstudio.com/en-us/downloads/download-visual-studio-vs#DownloadFamilies_2 Visual Studio 2013 Express for Windows Desktop])
 
* Install Visual Studio (e.g. [http://www.visualstudio.com/en-us/downloads/download-visual-studio-vs#DownloadFamilies_2 Visual Studio 2013 Express for Windows Desktop])
 
* [http://python.org/download/ Python 2.7.x or Python 3.3.x] (I use 3.3.0)
 
* [http://python.org/download/ Python 2.7.x or Python 3.3.x] (I use 3.3.0)
Line 35: Line 35:
 
<li><tt>cd build-llvm-x64</tt></li>
 
<li><tt>cd build-llvm-x64</tt></li>
 
<li><p>Use a command like this:</p>
 
<li><p>Use a command like this:</p>
<pre>cmake -G &quot;Visual Studio 12 Win64&quot; -DCMAKE_INSTALL_PREFIX=&quot;C:\Program Files\LLVM-x64&quot; -DCMAKE_CXX_FLAGS="/DWIN32 /D_HAS_EXCEPTIONS=0 /MP /W3 /GR- /GS- /d2Zi+"
+
<pre>cmake -G &quot;Visual Studio 12 Win64&quot; -DCMAKE_INSTALL_PREFIX=&quot;C:\Program Files\LLVM-x64&quot; -DCMAKE_BUILD_TYPE=&quot;RelWithDebInfo&quot;
      -DPYTHON_EXECUTABLE=&quot;C:\Program Files\Python33\python.exe&quot; -DLLVM_TARGETS_TO_BUILD=&quot;X86&quot;
+
      -DCMAKE_CXX_FLAGS=&quot;/DWIN32 /MP /W3 /GR- /GS- /Zo&quot; -DPYTHON_EXECUTABLE=&quot;C:\Program Files\Python33\python.exe&quot;
      -DLLVM_INCLUDE_TESTS=OFF -DLLVM_INCLUDE_EXAMPLES=OFF -DLLVM_ENABLE_ASSERTIONS=ON -DLLVM_APPEND_VC_REV=ON ..\llvm</pre></li>
+
      -DLLVM_TARGETS_TO_BUILD=&quot;X86&quot; -DLLVM_INCLUDE_TESTS=OFF -DLLVM_INCLUDE_EXAMPLES=OFF -DLLVM_ENABLE_ASSERTIONS=ON -DLLVM_APPEND_VC_REV=ON ..\llvm</pre></li>
 
</ul>
 
</ul>
  
Line 73: Line 73:
 
<li><tt>cd build-ldc2-x64</tt></li>
 
<li><tt>cd build-ldc2-x64</tt></li>
 
<li><p>Type the next command in one line:</p>
 
<li><p>Type the next command in one line:</p>
<pre>cmake -G &quot;Visual Studio 12 Win64&quot; -DCMAKE_INSTALL_PREFIX=&quot;C:\Program Files\LDC&quot; -DCMAKE_CXX_FLAGS="/DWIN32 /D_HAS_EXCEPTIONS=0 /MP /W2 /GR- /GS- /d2Zi+"
+
<pre>cmake -G &quot;Visual Studio 12 Win64&quot; -DCMAKE_INSTALL_PREFIX=&quot;C:\Program Files\LDC&quot; -DCMAKE_CXX_FLAGS=&quot;/DWIN32 /MP /W2 /GR- /GS- /Zo&quot;
 
       -DLLVM_ROOT_DIR=&quot;C:/Program Files/LLVM-x64&quot;
 
       -DLLVM_ROOT_DIR=&quot;C:/Program Files/LLVM-x64&quot;
 
       -DLIBCONFIG_INCLUDE_DIR=&quot;C:/ldcenv/libconfig-1.4.9/lib&quot;
 
       -DLIBCONFIG_INCLUDE_DIR=&quot;C:/ldcenv/libconfig-1.4.9/lib&quot;
Line 87: Line 87:
  
 
The tests can currently NOT be run via <tt>msbuild RUN_TESTS.vcxproj</tt>. So we'll use a neat little build system called [http://martine.github.io/ninja/ Ninja] for this.
 
The tests can currently NOT be run via <tt>msbuild RUN_TESTS.vcxproj</tt>. So we'll use a neat little build system called [http://martine.github.io/ninja/ Ninja] for this.
 +
Note that the dmd-testsuite tests require the <tt>make</tt> tool.
  
 
<ul>
 
<ul>
Line 96: Line 97:
 
   <li><tt>cd ninja-ldc2-x64</tt></li>
 
   <li><tt>cd ninja-ldc2-x64</tt></li>
 
   <li><p>Type the next command in one line:</p>
 
   <li><p>Type the next command in one line:</p>
   <pre>cmake -G Ninja -DCMAKE_INSTALL_PREFIX=&quot;C:\Program Files\LDC&quot; -DCMAKE_CXX_FLAGS="/DWIN32 /D_HAS_EXCEPTIONS=0 /MP /W2 /GR- /GS- /d2Zi+"
+
   <pre>cmake -G Ninja -DCMAKE_INSTALL_PREFIX=&quot;C:\Program Files\LDC&quot; -DCMAKE_CXX_FLAGS=&quot;/DWIN32 /MP /W2 /GR- /GS- /Zo&quot;
 
       -DLLVM_ROOT_DIR=&quot;C:/Program Files/LLVM-x64&quot;
 
       -DLLVM_ROOT_DIR=&quot;C:/Program Files/LLVM-x64&quot;
 
       -DLIBCONFIG_INCLUDE_DIR=&quot;C:/ldcenv/libconfig-1.4.9/lib&quot;
 
       -DLIBCONFIG_INCLUDE_DIR=&quot;C:/ldcenv/libconfig-1.4.9/lib&quot;
 
       -DLIBCONFIG_LIBRARY=&quot;C:/ldcenv/libconfig-1.4.9/Debug/libconfig.lib&quot; ..\ldc</pre>
 
       -DLIBCONFIG_LIBRARY=&quot;C:/ldcenv/libconfig-1.4.9/Debug/libconfig.lib&quot; ..\ldc</pre>
 
   </ul></li>
 
   </ul></li>
<li>Build and run the tests: <tt>ctest --force-new-ctest-process</tt>. Be patient (~50 mins on Ivy Bridge @ 4x4 GHz). ;)
+
<li>Build LDC and the runtimes: <tt>ninja</tt></li>
</li>
+
<li>Build the unit tests: <tt>ninja druntime-ldc-unittest druntime-ldc-unittest-debug phobos2-ldc-unittest phobos2-ldc-unittest-debug</tt></li>
 +
<li>Run the tests: <tt>ctest</tt></li>
 
</ul>
 
</ul>
  
Line 109: Line 111:
 
=== Known bugs ===
 
=== Known bugs ===
  
* Structured Exception Handling is only available for LLVM 3.6 head. Support has still alpha quality and is known to crash your application under certain circumstances.
+
* Structured Exception Handling is only available for LLVM 3.7 head. Support has still alpha quality and is known to crash your application under certain circumstances.
  
 
== Hacking druntime and phobos ==
 
== Hacking druntime and phobos ==
  
As of version 2.061 Druntime and Phobos have support for Win64. The following issues are known:
+
* <tt>ldc.eh2</tt>: Implementation has alpha quality.
 
 
* <tt>ldc.eh2</tt>: Implementation has alpha quality. Only used together with LLVM 3.6.
 
 
* The MSVC runtime misses support for some functions. See https://github.com/ldc-developers/ldc/issues/761 for an example.
 
* The MSVC runtime misses support for some functions. See https://github.com/ldc-developers/ldc/issues/761 for an example.
  

Revision as of 16:24, 21 June 2015

LDC on Windows is work in progress. This page documents how to compile LDC2 on Windows x64. It also provides hints on druntime and phobos.

Building LDC

Required software

Required source downloads

Environment check

After installing the software, you can open a "VS2013 x64 Native Tools Command Prompt".

  • Running cmake --version should display the banner from cmake.
  • Running git --version should display the banner from git.
  • Running msbuild /version should display the banner from msbuild.
  • Running <Python install path>\python --version should display the banner from python.

My working directory is C:\ldcenv. Every step starts in this directory. Please feel free to adjust this to your needs.

Build LLVM

To build LLVM from the command line, just execute the following steps:

  • git clone http://llvm.org/git/llvm.git llvm
  • md build-llvm-x64
  • cd build-llvm-x64
  • Use a command like this:

    cmake -G "Visual Studio 12 Win64" -DCMAKE_INSTALL_PREFIX="C:\Program Files\LLVM-x64" -DCMAKE_BUILD_TYPE="RelWithDebInfo"
          -DCMAKE_CXX_FLAGS="/DWIN32 /MP /W3 /GR- /GS- /Zo" -DPYTHON_EXECUTABLE="C:\Program Files\Python33\python.exe"
          -DLLVM_TARGETS_TO_BUILD="X86" -DLLVM_INCLUDE_TESTS=OFF -DLLVM_INCLUDE_EXAMPLES=OFF -DLLVM_ENABLE_ASSERTIONS=ON -DLLVM_APPEND_VC_REV=ON ..\llvm

Build the INSTALL project in the generated solution: msbuild INSTALL.vcxproj

The LLVM page on CMake documents other variables you can change. The most common is to add more targets. E.g. to build a target for ARM you change the targets to build to -DLLVM_TARGETS_TO_BUILD="X86;ARM".

Build libconfig

Simple build instructions for libconfig are missing. You can do the following:

  • Extract libconfig source.
  • Open the libconfig.sln solution
  • Create a configuration for Win64 Debug
  • If you like to create a static library then change the configuration type of project libconfig to 'Static library'.
  • Build the project libconfig

Alternative you can use a custom CMakeLists.txt:

  • Extract libconfig source.
  • Download CMakeLists.txt from this gist and place it into the root folder.
  • Create a build directory: md build_libconfig
  • cd build_libconfig
  • Now create the build files:
  • cmake ..\libconfig-1.4.9
  • Build the library: msbuild ALL_BUILD.vcxproj

Build LDC2

To build LDC2 from the command line, just execute the following steps:

  • git clone --recursive git://github.com/ldc-developers/ldc.git ldc
  • md build-ldc2-x64
  • cd build-ldc2-x64
  • Type the next command in one line:

    cmake -G "Visual Studio 12 Win64" -DCMAKE_INSTALL_PREFIX="C:\Program Files\LDC" -DCMAKE_CXX_FLAGS="/DWIN32 /MP /W2 /GR- /GS- /Zo"
          -DLLVM_ROOT_DIR="C:/Program Files/LLVM-x64"
          -DLIBCONFIG_INCLUDE_DIR="C:/ldcenv/libconfig-1.4.9/lib"
          -DLIBCONFIG_LIBRARY="C:/ldcenv/libconfig-1.4.9/Debug/libconfig.lib" ..\ldc

    This assumes that you have a static compiled version of libconfig.lib. If you use the DLL version then you have to add -DLIBCONFIG_DLL=ON.

Build the INSTALL project: msbuild INSTALL.vcxproj

Check that C:\Program Files\LDC\bin is in your path and type ldc2 -version to check that you can run LDC2.

Running the tests

The tests can currently NOT be run via msbuild RUN_TESTS.vcxproj. So we'll use a neat little build system called Ninja for this. Note that the dmd-testsuite tests require the make tool.

  • Download the latest Ninja release from GitHub (a single .exe) and put it in your PATH.
  • Make sure you can link against the Curl library (curl.lib in your LIB/LIBPATH, curl.dll in your PATH).
  • Run CMake just like when building LDC2, but use the Ninja generator this time:
    • md ninja-ldc2-x64
    • cd ninja-ldc2-x64
    • Type the next command in one line:

      cmake -G Ninja -DCMAKE_INSTALL_PREFIX="C:\Program Files\LDC" -DCMAKE_CXX_FLAGS="/DWIN32 /MP /W2 /GR- /GS- /Zo"
            -DLLVM_ROOT_DIR="C:/Program Files/LLVM-x64"
            -DLIBCONFIG_INCLUDE_DIR="C:/ldcenv/libconfig-1.4.9/lib"
            -DLIBCONFIG_LIBRARY="C:/ldcenv/libconfig-1.4.9/Debug/libconfig.lib" ..\ldc
  • Build LDC and the runtimes: ninja
  • Build the unit tests: ninja druntime-ldc-unittest druntime-ldc-unittest-debug phobos2-ldc-unittest phobos2-ldc-unittest-debug
  • Run the tests: ctest

For troubleshooting be sure to examine the file ninja-ldc2-x64\Testing\Temporary\LastTest.log.

Known bugs

  • Structured Exception Handling is only available for LLVM 3.7 head. Support has still alpha quality and is known to crash your application under certain circumstances.

Hacking druntime and phobos

If you link your modules you have to specify the libraries phobos-ldc.lib and shell32.lib.

Example

The simple D program hello.d

import std.stdio;

int main()
{
    writefln("Hello LDC2");
    return 0;
}

can be compiled and linked with the commands:

ldc2 -c hello.d
link hello2.obj phobos-ldc.lib shell32.lib

or simply with: ldc2 hello.d

The resulting hello.exe produces the expected output.



Windows MSVC