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

From D Wiki
Jump to: navigation, search
m (Build LLVM)
Line 1: Line 1:
Windows MSVC x86/x64 are now first class targets for LDC. This page documents how to build, test and hack LDC2 on Windows.
+
Windows MSVC x86/x64 are first class targets for LDC. This page documents how to build, test and hack LDC2 on Windows.
  
 
== LDC binaries ==
 
== LDC binaries ==
Line 13: Line 13:
  
 
* Windows, of course!
 
* Windows, of course!
* Visual Studio ≥ 2013, e.g. [https://www.visualstudio.com/en-us/downloads/download-visual-studio-vs.aspx VS Community 2015]. VS 2015 is greatly encouraged due to improved C99 support. Make sure to install the C++ toolchain.
+
* [https://www.visualstudio.com/downloads/ Visual Studio] or stand-alone [http://landinghub.visualstudio.com/visual-cpp-build-tools/ Visual C++ Build Tools] ≥ 2015. Make sure to install the C++ toolchain.
 
* A D compiler (the <tt>ltsmaster</tt> branch does not need a D compiler to build).
 
* A D compiler (the <tt>ltsmaster</tt> branch does not need a D compiler to build).
* [https://git-scm.com/download/win git 2.0+] (I use PortableGit)
+
* [https://git-scm.com/download/win git &ge; 2.0] (I use PortableGit)
 
* [http://python.org/download/ Python 2.7.x or Python 3.3.x] (I use [https://github.com/winpython/winpython/releases Winpython])
 
* [http://python.org/download/ Python 2.7.x or Python 3.3.x] (I use [https://github.com/winpython/winpython/releases Winpython])
* [http://www.cmake.org/download/ CMake 2.8+]
+
* [http://www.cmake.org/download/ CMake &ge; 2.8.9]
 
* [https://github.com/ninja-build/ninja/releases Ninja], a neat little and fast build system
 
* [https://github.com/ninja-build/ninja/releases Ninja], a neat little and fast build system
 
* [http://wiki.dlang.org/Curl_on_Windows Curl library] (just use a [http://downloads.dlang.org/other/ precompiled one])
 
* [http://wiki.dlang.org/Curl_on_Windows Curl library] (just use a [http://downloads.dlang.org/other/ precompiled one])
Line 52: Line 52:
 
<ul>
 
<ul>
 
<li>Get the source: <tt>git clone http://llvm.org/git/llvm.git llvm</tt></li>
 
<li>Get the source: <tt>git clone http://llvm.org/git/llvm.git llvm</tt></li>
<li>A post-3.8 version is currently required, so branch <tt>release_39</tt> (<tt>git checkout release_39</tt>) is currently recommended.</li>
+
<li>Branch <tt>release_39</tt> (<tt>git checkout release_39</tt>) is currently recommended (but may not support Visual C++ 2017, try branch <tt>release_40</tt> in that case).</li>
 
<li>Create a build directory: <tt>md build-llvm-x64</tt></li>
 
<li>Create a build directory: <tt>md build-llvm-x64</tt></li>
 
<li>Change into it: <tt>cd build-llvm-x64</tt></li>
 
<li>Change into it: <tt>cd build-llvm-x64</tt></li>
Line 65: Line 65:
 
</ul>
 
</ul>
  
=== Build libconfig ===
+
=== Build libconfig (only required for LDC &lt; 1.3) ===
  
Latest confirmed working revision: <tt>7585cf6</tt>
+
Latest confirmed working revision: <tt>7585cf6</tt>. Feel free to try the latest version, if it fails you can fall back on this one.
Feel free to try the latest version, if it fails you can fall back on this one.
 
  
 
* <tt>cd C:\LDC</tt>
 
* <tt>cd C:\LDC</tt>
Line 85: Line 84:
 
<li><tt>cd build-ldc-x64</tt></li>
 
<li><tt>cd build-ldc-x64</tt></li>
 
<li><p>Set environment variable to which D compiler should be used to build LDC:</p> <tt>set DMD=c:\path\to\dmd\bin\dmd.exe</tt></li>
 
<li><p>Set environment variable to which D compiler should be used to build LDC:</p> <tt>set DMD=c:\path\to\dmd\bin\dmd.exe</tt></li>
<li><p>Use a command like this (in one line):</p>
+
<li><p>Use a command like this (in one line), omitting the variables starting with <tt>LIBCONFIG</tt> when building LDC &ge; 1.3:</p>
 
<pre>cmake -G Ninja -DCMAKE_INSTALL_PREFIX=&quot;C:\LDC\LDC-x64&quot; -DCMAKE_BUILD_TYPE=RelWithDebInfo
 
<pre>cmake -G Ninja -DCMAKE_INSTALL_PREFIX=&quot;C:\LDC\LDC-x64&quot; -DCMAKE_BUILD_TYPE=RelWithDebInfo
 
       -DLLVM_ROOT_DIR=&quot;C:/LDC/LLVM-x64&quot; -DLIBCONFIG_INCLUDE_DIR=&quot;C:/LDC/libconfig/lib&quot;
 
       -DLLVM_ROOT_DIR=&quot;C:/LDC/LLVM-x64&quot; -DLIBCONFIG_INCLUDE_DIR=&quot;C:/LDC/libconfig/lib&quot;
Line 130: Line 129:
 
** <tt>DMD_TESTSUITE_MAKE_ARGS</tt> enables parallel execution, e.g.
 
** <tt>DMD_TESTSUITE_MAKE_ARGS</tt> enables parallel execution, e.g.
 
*** <tt>set DMD_TESTSUITE_MAKE_ARGS=-j4</tt>
 
*** <tt>set DMD_TESTSUITE_MAKE_ARGS=-j4</tt>
* <tt>ctest --verbose -R dmd-testsuite</tt>
+
* <tt>ctest -V -R dmd-testsuite</tt>
** Debug only: <tt>ctest --verbose -R dmd-testsuite-debug</tt>
+
** Debug only: <tt>ctest -V -R dmd-testsuite-debug</tt>
** Release only: <tt>ctest --verbose -R dmd-testsuite -E -debug</tt>
+
** Release only: <tt>ctest -V -R dmd-testsuite -E -debug</tt>
  
 
== Developing/debugging LDC/LLVM with Visual Studio ==
 
== Developing/debugging LDC/LLVM with Visual Studio ==

Revision as of 22:56, 29 April 2017

Windows MSVC x86/x64 are first class targets for LDC. This page documents how to build, test and hack LDC2 on Windows.

LDC binaries

If you just want to download the very latest LDC binaries, head over to Latest LDC binaries for Windows.

Advice

It is hard for us to keep these wiki pages up-to-date. If you run into trouble, have a look at the build scripts for our Continuous Integration platforms: the files .travis.yml (Ubuntu Linux and OSX) and appveyor.yml (Windows) are always up-to-date with the latest build setup.

Building LDC

Required software

Shell environment

I use a little batch file to set up my LDC build environment. It's located in the root of my LDC environment: C:\LDC\shell.cmd. It sets up the PATH environment variable (I've installed the portable tools into C:\LDC\Tools) and then spawns a new VS 2015 x64 Native Tools Command Prompt with a below-normal process priority, so that my system stays responsive while building. I use a shortcut on my desktop to this batch file. Please adjust it to your needs (and note that %~dp0 is the directory containing the script, i.e., C:\LDC\).

@echo off
set PATH=%~dp0LDC-x64\bin;%~dp0LLVM-x64\bin;%~dp0Tools\Ninja 1.6.0;%~dp0Tools\PortableGit-2.9.3.2-64-bit\usr\bin;%~dp0Tools\PortableGit-2.9.3.2-64-bit\bin;%~dp0Tools\make-4.2.1;%~dp0Tools\cmake-3.3.0-win32-x86\bin;%~dp0Tools\WinPython-64bit-2.7.13.1Zero\python-2.7.13.amd64;%PATH%
set DMD=%~dp0dmd2\windows\bin\dmd.exe
if not exist "%TERM%" set TERM=msys
start /belownormal %comspec% /k "C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\vcvarsall.bat" amd64

Use x86 instead of amd64 as argument to vcvarsall.bat if you want to build a 32-bit LDC.

Open a shell by executing the batch file.

  • Running cl should display the banner from the MS compiler.
  • Running git --version should display the banner from git.
  • Running python --version should display the banner from python.
  • Running cmake --version should display the banner from cmake.
  • Running ninja --version should display the ninja version.

Build LLVM

To build LLVM from the command line, just execute the following steps (from C:\LDC):

  • Get the source: git clone http://llvm.org/git/llvm.git llvm
  • Branch release_39 (git checkout release_39) is currently recommended (but may not support Visual C++ 2017, try branch release_40 in that case).
  • Create a build directory: md build-llvm-x64
  • Change into it: cd build-llvm-x64
  • Use a command like this (in one line) to create the Ninja build files:

    cmake -G Ninja -DCMAKE_INSTALL_PREFIX="C:\LDC\LLVM-x64" -DCMAKE_BUILD_TYPE=RelWithDebInfo
          -DPYTHON_EXECUTABLE="C:\LDC\Tools\WinPython-64bit-2.7.13.1Zero\python-2.7.13.amd64\python.exe"
          -DLLVM_TARGETS_TO_BUILD=X86 -DLLVM_INCLUDE_TESTS=OFF -DLLVM_INCLUDE_EXAMPLES=OFF
          -DLLVM_ENABLE_ASSERTIONS=ON -DLLVM_APPEND_VC_REV=ON -DLLVM_INSTALL_UTILS=ON ..\llvm

    Omit the CMAKE_BUILD_TYPE definition to build a debug version. 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 LLVM: ninja
  • Install it: ninja install

Build libconfig (only required for LDC < 1.3)

Latest confirmed working revision: 7585cf6. Feel free to try the latest version, if it fails you can fall back on this one.

  • cd C:\LDC
  • git clone https://github.com/hyperrealm/libconfig.git libconfig
  • cd libconfig
  • git checkout 7585cf6
  • Build the static C library: msbuild lib\libconfig.vcxproj /t:Rebuild /p:Configuration=ReleaseStatic /p:Platform=x64
    • Use configuration DebugStatic for debug LDC builds to ensure we link against the same runtime library

Build LDC

  • cd C:\LDC
  • git clone --recursive git://github.com/ldc-developers/ldc.git ldc
  • md build-ldc-x64
  • cd build-ldc-x64
  • Set environment variable to which D compiler should be used to build LDC:

    set DMD=c:\path\to\dmd\bin\dmd.exe
  • Use a command like this (in one line), omitting the variables starting with LIBCONFIG when building LDC ≥ 1.3:

    cmake -G Ninja -DCMAKE_INSTALL_PREFIX="C:\LDC\LDC-x64" -DCMAKE_BUILD_TYPE=RelWithDebInfo
          -DLLVM_ROOT_DIR="C:/LDC/LLVM-x64" -DLIBCONFIG_INCLUDE_DIR="C:/LDC/libconfig/lib"
          -DLIBCONFIG_LIBRARY="C:/LDC/libconfig/lib/x64/ReleaseStatic/libconfig.lib" ..\ldc
  • Build LDC and the runtimes: ninja
  • If you want to install it: ninja install

Tests

Running the LIT-based tests

You'll need to have lit installed for Python. To run the tests from your build dir you can do:

  • cd C:\LDC\build-ldc-x64
  • ctest --output-on-failure -R lit-tests

or you can go to the tests folder inside your build dir, and run the runlit.py script.

  • cd C:\LDC\build-ldc-x64\tests
  • python runlit.py -v .

The second way is convenient for running individual tests:

  • python runlit.py -v codegen/align.d

Running the runtime unit tests

  • cd C:\LDC\build-ldc-x64
  • Build the unit tests: ninja druntime-ldc-unittest druntime-ldc-unittest-debug phobos2-ldc-unittest phobos2-ldc-unittest-debug
  • Run the tests, excluding dmd-testsuite and the LIT tests: ctest --output-on-failure -E "dmd-testsuite|lit-tests"

For troubleshooting be sure to examine the file C:\LDC\build-ldc-x64\Testing\Temporary\LastTest.log.

Running the dmd-testsuite tests

dmd-testsuite requires a minimalistic GNU environment. bash and a lot of GNU utilities are shipped with git. Additionally, we need GNU make.

  • Either build GNU make from source yourself (they ship with a Visual Studio solution) or download this pre-built one (v4.2.1).
  • Edit your shell batch script and add the directory containing your make.exe to your PATH, e.g., set PATH=%~dp0Tools\make;%PATH%
  • Spawn a new shell and make sure make --version prints its banner.

Now that we have extended our LDC build environment, we're able to run the dmd-testsuite tests:

  • cd C:\LDC\build-ldc-x64
  • Set some environment variables for dmd-testsuite's Makefile:
    • OS
      • 32-bit: set OS=Win_32
      • 64-bit: set OS=Win_64
    • DMD_TESTSUITE_MAKE_ARGS enables parallel execution, e.g.
      • set DMD_TESTSUITE_MAKE_ARGS=-j4
  • ctest -V -R dmd-testsuite
    • Debug only: ctest -V -R dmd-testsuite-debug
    • Release only: ctest -V -R dmd-testsuite -E -debug

Developing/debugging LDC/LLVM with Visual Studio

  • cd C:\LDC
  • md vs-ldc-x64
  • cd vs-ldc-x64
  • Use the cmake command from the Build LDC section, but use the VS generator instead of Ninja this time: cmake -G "Visual Studio 14 Win64" ...

This creates the VS 2015 solution C:\LDC\vs-ldc-x64\ldc.sln. A Visual Studio solution for LLVM can be created the same way.

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
ldc2 hello.obj

or simply with: ldc2 hello.d



Windows MSVC