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

From D Wiki
Jump to: navigation, search
(Building LLVM)
m (Update build instructions)
Line 13: Line 13:
  
 
* Windows, of course!
 
* Windows, of course!
* [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.
+
* [https://www.visualstudio.com/downloads/ Visual Studio or stand-alone Build Tools] ≥ 2015. Make sure to install the Visual C++ toolchain.
 
* A [https://github.com/ldc-developers/ldc/releases/ D compiler] (the <tt>ltsmaster</tt> git branch does not need a D compiler to build). DMD as host compiler isn't supported on Windows, make sure to use LDC.
 
* A [https://github.com/ldc-developers/ldc/releases/ D compiler] (the <tt>ltsmaster</tt> git branch does not need a D compiler to build). DMD as host compiler isn't supported on Windows, make sure to use LDC.
 
* [https://git-scm.com/download/win git &ge; 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] (I use [https://github.com/winpython/winpython/releases Winpython])
+
* [https://www.python.org/downloads/ Python] (I use [https://github.com/winpython/winpython/releases Winpython])
* [http://www.cmake.org/download/ CMake &ge; 3.4.3]
+
* [https://github.com/Kitware/CMake/releases CMake &ge; 3.4.3]
 
* [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], but note that versions > 7.48 seem not to work)
+
* cURL DLL (just use a [http://downloads.dlang.org/other/ prebuilt one] &ge; 7.65.3-2)
 
** Put the 64/32-bit <tt>libcurl.dll</tt> in a PATH directory (e.g., <tt>C:\LDC\LDC-x64\bin</tt>).
 
** Put the 64/32-bit <tt>libcurl.dll</tt> in a PATH directory (e.g., <tt>C:\LDC\LDC-x64\bin</tt>).
 +
* For running the testsuite:
 +
** GNU make ([https://dl.dropboxusercontent.com/s/4y36f5ydgrk4p5g/make-4.2.1.7z?dl=0 prebuilt v4.2.1])
 +
** If you get line-ending related grep errors during the DMD tests: a working GNU grep ([https://dl.dropboxusercontent.com/s/vwlfgz8wv1fmqnn/grep-3.0.7z?dl=0 prebuilt v3.0])
  
 
=== Shell environment ===
 
=== Shell environment ===
Line 30: Line 33:
 
<pre>
 
<pre>
 
@echo off
 
@echo off
set PATH=%~dp0LDC-x64\bin;%~dp0LLVM-x64\bin;%~dp0Tools\Ninja 1.8.2;%~dp0Tools\PortableGit-2.17.0-64-bit\usr\bin;%~dp0Tools\PortableGit-2.17.0-64-bit\bin;%~dp0Tools\make-4.2.1;%~dp0Tools\cmake-3.12.3-win64-x64\bin;%~dp0Tools\WinPython-64bit-2.7.13.1Zero\python-2.7.13.amd64;%~dp0Tools\WinPython-64bit-2.7.13.1Zero\python-2.7.13.amd64\Scripts;%PATH%
+
set PATH=%~dp0LDC-x64\bin;%~dp0LLVM-x64\bin;%~dp0Tools\Ninja 1.9.0;%~dp0Tools\make-4.2.1;%~dp0Tools\grep-3.0;C:\Program Files\Git\usr\bin;C:\Program Files\Git\bin;%~dp0Tools\cmake-3.15.2-win64-x64\bin;%~dp0Tools\Winpython64-3.7.4.0Zero\python-3.7.4.amd64;%PATH%
 
:: set D host compiler
 
:: set D host compiler
set DMD=%~dp0ldc2-1.13.0-windows-multilib\bin\ldmd2.exe
+
set DMD=%~dp0ldc2-1.17.0-windows-multilib\bin\ldmd2.exe
 
:: set a few environment variables for dmd-testsuite
 
:: set a few environment variables for dmd-testsuite
set OS=win64
+
set OS=windows
 
set MODEL=64
 
set MODEL=64
 
set DMD_TESTSUITE_MAKE_ARGS=-j4
 
set DMD_TESTSUITE_MAKE_ARGS=-j4
Line 41: Line 44:
 
</pre>
 
</pre>
  
Use <tt>-arch=x86</tt> in the last line and <tt>set OS=win32</tt> as well as <tt>set MODEL=32</tt> if you want to build a 32-bit LDC.
+
Use <tt>-arch=x86</tt> in the last line and <tt>set MODEL=32</tt> if you want to build a 32-bit LDC.
  
 
Open a shell by executing the batch file.
 
Open a shell by executing the batch file.
Line 48: Line 51:
 
* Running <code>link /version</code> should display the banner from the MS linker.
 
* Running <code>link /version</code> should display the banner from the MS linker.
 
* Running <code>git --version</code> should display the banner from git.
 
* Running <code>git --version</code> should display the banner from git.
* Running <code>python --version</code> should display the banner from python.
+
* Running <code>python --version</code> should display the banner from Python.
* Running <code>cmake --version</code> should display the banner from cmake.
+
* Running <code>cmake --version</code> should display the banner from CMake.
* Running <code>ninja --version</code> should display the ninja version.
+
* Running <code>ninja --version</code> should display the Ninja version.
 +
* Running <code>where libcurl.dll</code> should find a file.
 +
* Running <code>make --version</code> should display the banner from GNU make (only required for the tests).
  
 
=== Building LLVM ===
 
=== Building LLVM ===
  
Building LLVM takes quite a while. You may want to download a prebuilt version from [https://github.com/ldc-developers/llvm/releases here]; note that the C++ compiler has changed from MS to clang since v7.0.0-2, so use an older version. We use these prebuilt packages for CI and the official release packages. Just extract the archive into <tt>C:\LDC\LLVM-x64</tt> and then skip this section. In case you encounter linking errors when building LDC, you probably use a different MSVC toolchain version and will need to build LLVM yourself.
+
Building LLVM takes quite a while. You may want to download a prebuilt version from [https://github.com/ldc-developers/llvm/releases here]; note that the C++ compiler has changed from MS to clang since v7.0.0-2, so either use an older LLVM version or use clang for building LDC (see CI scripts). We use these prebuilt packages for CI and the official release packages. Just extract the archive into <tt>C:\LDC\LLVM-x64</tt> and then skip this section. In case you encounter linking errors when building LDC, you probably use a different MSVC/clang toolchain version and will need to build LLVM yourself.
  
 
<ul>
 
<ul>
Line 85: Line 90:
 
<li><code>cd build-ldc-x64</code></li>
 
<li><code>cd build-ldc-x64</code></li>
 
<li><p>Use a command like this (in one line) to create the Ninja build files:</p>
 
<li><p>Use a command like this (in one line) to create the Ninja build files:</p>
<pre>cmake -G Ninja -DCMAKE_INSTALL_PREFIX=&quot;C:\LDC\LDC-x64&quot; -DCMAKE_BUILD_TYPE=RelWithDebInfo
+
<pre>cmake -G Ninja ..\ldc
       -DLLVM_ROOT_DIR=&quot;C:/LDC/LLVM-x64&quot; ..\ldc</pre>
+
      -DCMAKE_INSTALL_PREFIX=&quot;C:\LDC\LDC-x64&quot;
<p>You can specify the D host compiler explicitly by adding <code>-DD_COMPILER=c:\path\to\{dmd,ldmd2}.exe</code>.</p>
+
      -DCMAKE_BUILD_TYPE=RelWithDebInfo
 +
       -DLLVM_ROOT_DIR=&quot;C:/LDC/LLVM-x64&quot;</pre>
 +
<p>You can specify the D host compiler explicitly by adding <code>-DD_COMPILER=c:\path\to\ldmd2.exe</code>.</p>
 
</li>
 
</li>
 
<li>Build LDC and the default libraries: <code>ninja</code>. The binaries end up in <tt>C:\LDC\build-ldc-x64\bin</tt>.</li>
 
<li>Build LDC and the default libraries: <code>ninja</code>. The binaries end up in <tt>C:\LDC\build-ldc-x64\bin</tt>.</li>
Line 103: Line 110:
  
 
=== Running the LIT (LLVM Integrated Tester) tests ===
 
=== Running the LIT (LLVM Integrated Tester) tests ===
 +
 
You'll need to have [https://pypi.python.org/pypi/lit lit] installed for Python:
 
You'll need to have [https://pypi.python.org/pypi/lit lit] installed for Python:
 
* Upgrade [https://pip.pypa.io/ pip]: <code>python -m pip install -U pip</code>
 
* Upgrade [https://pip.pypa.io/ pip]: <code>python -m pip install -U pip</code>
Line 124: Line 132:
 
=== Running the dmd-testsuite tests ===
 
=== 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 [https://www.gnu.org/software/make/ GNU make].
 
 
* Either build GNU make from source yourself (they ship with a Visual Studio solution) or download [https://dl.dropboxusercontent.com/s/4y36f5ydgrk4p5g/make-4.2.1.7z?dl=0 this pre-built one (v4.2.1)].
 
* Edit your shell batch script and add the directory containing your <tt>make.exe</tt> to your PATH, e.g., <code>set PATH=%~dp0Tools\make;%PATH%</code>
 
* Spawn a new shell and make sure <code>make --version</code> prints its banner.
 
 
Now that we have extended our LDC build environment, we're able to run the dmd-testsuite tests:
 
 
* <code>cd C:\LDC\build-ldc-x64</code>
 
 
* Set some environment variables for dmd-testsuite's Makefile:
 
* Set some environment variables for dmd-testsuite's Makefile:
 
** <tt>OS</tt> and <tt>MODEL</tt>
 
** <tt>OS</tt> and <tt>MODEL</tt>
*** <code>set OS=win{32,64}</code>
+
*** <code>set OS=windows</code> (since v1.17, previously <code>win{32,64}</code>)
 
*** <code>set MODEL={32,64}</code>
 
*** <code>set MODEL={32,64}</code>
 
** <tt>DMD_TESTSUITE_MAKE_ARGS</tt> enables parallel execution, e.g.
 
** <tt>DMD_TESTSUITE_MAKE_ARGS</tt> enables parallel execution, e.g.
 
*** <code>set DMD_TESTSUITE_MAKE_ARGS=-j4</code>
 
*** <code>set DMD_TESTSUITE_MAKE_ARGS=-j4</code>
 +
* <code>cd C:\LDC\build-ldc-x64</code>
 
* <code>ctest -V -R dmd-testsuite</code>
 
* <code>ctest -V -R dmd-testsuite</code>
 
** Debug only: <code>ctest -V -R dmd-testsuite-debug</code>
 
** Debug only: <code>ctest -V -R dmd-testsuite-debug</code>
Line 150: Line 150:
 
* <code>md vs-ldc-x64</code>
 
* <code>md vs-ldc-x64</code>
 
* <code>cd vs-ldc-x64</code>
 
* <code>cd vs-ldc-x64</code>
* Use the cmake command from the Build LDC section, but use the VS generator instead of Ninja this time: <code>cmake -G "Visual Studio 15 Win64" ...</code>
+
* Use the CMake command from the Build LDC section, but use the VS generator instead of Ninja this time: <code>cmake -G "Visual Studio 15 Win64" ...</code>
  
 
This creates the VS 2017 solution <tt>C:\LDC\vs-ldc-x64\ldc.sln</tt>.
 
This creates the VS 2017 solution <tt>C:\LDC\vs-ldc-x64\ldc.sln</tt>.

Revision as of 19:45, 25 August 2019

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 the GitHub CI release.

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 Azure Pipelines scripts for Ubuntu Linux and macOS and 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 x64 Native Tools Command Prompt for VS 2017 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.9.0;%~dp0Tools\make-4.2.1;%~dp0Tools\grep-3.0;C:\Program Files\Git\usr\bin;C:\Program Files\Git\bin;%~dp0Tools\cmake-3.15.2-win64-x64\bin;%~dp0Tools\Winpython64-3.7.4.0Zero\python-3.7.4.amd64;%PATH%
:: set D host compiler
set DMD=%~dp0ldc2-1.17.0-windows-multilib\bin\ldmd2.exe
:: set a few environment variables for dmd-testsuite
set OS=windows
set MODEL=64
set DMD_TESTSUITE_MAKE_ARGS=-j4
if not exist "%TERM%" set TERM=msys
start /belownormal %comspec% /k "C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\Common7\Tools\VsDevCmd.bat" -arch=x64

Use -arch=x86 in the last line and set MODEL=32 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 link /version should display the banner from the MS linker.
  • 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.
  • Running where libcurl.dll should find a file.
  • Running make --version should display the banner from GNU make (only required for the tests).

Building LLVM

Building LLVM takes quite a while. You may want to download a prebuilt version from here; note that the C++ compiler has changed from MS to clang since v7.0.0-2, so either use an older LLVM version or use clang for building LDC (see CI scripts). We use these prebuilt packages for CI and the official release packages. Just extract the archive into C:\LDC\LLVM-x64 and then skip this section. In case you encounter linking errors when building LDC, you probably use a different MSVC/clang toolchain version and will need to build LLVM yourself.

  • Download an LLVM source tarball (llvm-x.y.z.src.tar.xz). We maintain an LLVM fork with minimal modifications; vanilla LLVM can be used as well.
  • Extract it into C:\LDC (e.g., with 7-Zip), so that you end up with the source tree in e.g. C:\LDC\llvm-8.0.0.src.
  • cd C:\LDC
  • 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 ..\llvm-8.0.0.src
          -DCMAKE_INSTALL_PREFIX="C:\LDC\LLVM-x64"
          -DCMAKE_BUILD_TYPE=RelWithDebInfo -DLLVM_USE_CRT_RELWITHDEBINFO=MT
          -DLLVM_ENABLE_ASSERTIONS=ON
          -DLLVM_TARGETS_TO_BUILD=AArch64;ARM;Mips;MSP430;NVPTX;PowerPC;X86
          -DLLVM_EXPERIMENTAL_TARGETS_TO_BUILD=RISCV;WebAssembly
          -DLLVM_DEFAULT_TARGET_TRIPLE=x86_64-pc-windows-msvc
          -DCOMPILER_RT_INCLUDE_TESTS=OFF

    Use -DCMAKE_BUILD_TYPE=Debug to build a debug version (and probably use -DLLVM_USE_CRT_DEBUG=MTd then). The LLVM page on CMake documents other variables you can change. For a 32-bit build, use -DLLVM_DEFAULT_TARGET_TRIPLE=i686-pc-windows-msvc.

  • Build LLVM: ninja
  • Install it (to C:\LDC\LLVM-x64): ninja install
  • Note that you'll need to keep the C:\LDC\build-llvm-x64 directory in order to keep the LLVM debuginfos (.pdb files) available.

Building LDC

  • cd C:\LDC
  • git clone --recursive https://github.com/ldc-developers/ldc.git
  • md build-ldc-x64
  • cd build-ldc-x64
  • Use a command like this (in one line) to create the Ninja build files:

    cmake -G Ninja ..\ldc
          -DCMAKE_INSTALL_PREFIX="C:\LDC\LDC-x64"
          -DCMAKE_BUILD_TYPE=RelWithDebInfo
          -DLLVM_ROOT_DIR="C:/LDC/LLVM-x64"

    You can specify the D host compiler explicitly by adding -DD_COMPILER=c:\path\to\ldmd2.exe.

  • Build LDC and the default libraries: ninja. The binaries end up in C:\LDC\build-ldc-x64\bin.
  • If you want to install it (to C:\LDC\LDC-x64): ninja install

Tests

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

Running the LDC D unit tests

  • cd C:\LDC\build-ldc-x64
  • ctest --output-on-failure -R ldc2-unittest

Running the LIT (LLVM Integrated Tester) tests

You'll need to have lit installed for Python:

  • Upgrade pip: python -m pip install -U pip
  • Install lit: python -m pip install lit
  • Make sure it works: python -c "import lit; lit.main();" --version

To run the tests from your build dir you can do:

  • cd C:\LDC\build-ldc-x64
  • ctest -V -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 druntime/Phobos unit tests and druntime standalone tests

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

Running the dmd-testsuite tests

  • Set some environment variables for dmd-testsuite's Makefile:
    • OS and MODEL
      • set OS=windows (since v1.17, previously win{32,64})
      • set MODEL={32,64}
    • DMD_TESTSUITE_MAKE_ARGS enables parallel execution, e.g.
      • set DMD_TESTSUITE_MAKE_ARGS=-j4
  • cd C:\LDC\build-ldc-x64
  • 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

Be sure to use Rainer's great Visual D plugin for VS.

  • 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 15 Win64" ...

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

I don't recommend building LDC/LLVM with VS directly; I only use VS for development/debugging and build in the shell with Ninja.



Windows MSVC