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

From D Wiki
Jump to: navigation, search
(Running the LIT-based tests)
m (Advice)
(33 intermediate revisions by 2 users not shown)
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 6: Line 6:
  
 
== Advice ==
 
== 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 [http://wiki.dlang.org/LDC_contributor%27s_guide#Continuous_Integration Continuous Integration] platforms: the files <tt>.travis.yml</tt> (Ubuntu Linux and OSX) and <tt>appveyor.yml</tt> (Windows) are always up-to-date with the latest build setup.
+
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 [http://wiki.dlang.org/LDC_contributor%27s_guide#Continuous_Integration Continuous Integration] platforms: the files <tt>.circleci/config.yml</tt> (Ubuntu Linux and macOS) and <tt>appveyor.yml</tt> (Windows) are always up-to-date with the latest build setup.
  
 
== Building LDC ==
 
== Building LDC ==
Line 13: Line 13:
  
 
* Windows, of course!
 
* Windows, of course!
* Visual Studio &ge; 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] &ge; 2015. Make sure to install the C++ toolchain.
* A D compiler (currently only [http://dlang.org/download.html#dmd DMD] works to build LDC on Windows. LDC 0.17 is the last version that does not need a D compiler to build.)
+
* A D compiler (the <tt>ltsmaster</tt> git branch does not need a D compiler to build).
* [http://git-scm.com/download/win git 1.8+] (I use [https://code.google.com/r/chen2008cj-gitclone/source/browse/PortableGit-1.8.1.2-preview20130201.7z 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 [http://portablepython.com/wiki/Download/ Portable Python])
+
* [http://python.org/download/ Python 2.7.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; 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 precompiled one)
+
* [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)
 
** 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>).
  
Line 25: Line 25:
  
 
I use a little batch file to set up my LDC build environment. It's located in the root of my LDC environment: <tt>C:\LDC\shell.cmd</tt>.
 
I use a little batch file to set up my LDC build environment. It's located in the root of my LDC environment: <tt>C:\LDC\shell.cmd</tt>.
It sets up the <tt>PATH</tt> environment variable (I've installed the portable tools into <tt>C:\LDC\Tools</tt>) and then spawns a new <tt>VS 2015 x64 Native Tools Command Prompt</tt> with a below-normal process priority, so that my system stays responsive while building.
+
It sets up the <tt>PATH</tt> environment variable (I've installed the portable tools into <tt>C:\LDC\Tools</tt>) and then spawns a new <tt>x64 Native Tools Command Prompt for VS 2017</tt> 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 <tt>%~dp0</tt> is the directory containing the script, i.e., <tt>C:\LDC\</tt>).
 
I use a shortcut on my desktop to this batch file. Please adjust it to your needs (and note that <tt>%~dp0</tt> is the directory containing the script, i.e., <tt>C:\LDC\</tt>).
  
 
<pre>
 
<pre>
 
@echo off
 
@echo off
set PATH=%~dp0LDC-x64\bin;%~dp0LLVM-x64\bin;%~dp0Tools\Ninja 1.6.0;%~dp0Tools\make-3.81\bin;%~dp0Tools\PortableGit-1.8.1.2-preview20130201\bin;%~dp0Tools\cmake-3.3.0-win32-x86\bin;%~dp0Tools\Portable Python 2.7.5.1\App;%PATH%
+
set PATH=%~dp0LDC-x64\bin;%~dp0LLVM-x64\bin;%~dp0Tools\Ninja 1.8.2;%~dp0Tools\PortableGit-2.14.1-64-bit\usr\bin;%~dp0Tools\PortableGit-2.14.1-64-bit\bin;%~dp0Tools\make-4.2.1;%~dp0Tools\cmake-3.9.1-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 DMD=%~dp0dmd2\windows\bin\dmd.exe
+
:: set D host compiler
 +
set DMD=%~dp0ldc2-1.7.0-windows-multilib\bin\ldmd2.exe
 +
:: set a few environment variables for dmd-testsuite
 +
set OS=Win_64
 +
set DMD_TESTSUITE_MAKE_ARGS=-j4
 
if not exist "%TERM%" set TERM=msys
 
if not exist "%TERM%" set TERM=msys
start /belownormal %comspec% /k "C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\vcvarsall.bat" amd64
+
start /belownormal %comspec% /k "C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\Common7\Tools\VsDevCmd.bat" -arch=x64
 
</pre>
 
</pre>
  
Use <tt>x86</tt> instead of <tt>amd64</tt> as argument to <tt>vcvarsall.bat</tt> if you want to build a 32-bit LDC.
+
Use <tt>-arch=x86</tt> in the last line and <tt>set OS=Win_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.
  
 
* Running <tt>cl</tt> should display the banner from the MS compiler.
 
* Running <tt>cl</tt> should display the banner from the MS compiler.
 +
* Running <tt>link /version</tt> should display the banner from the MS linker.
 
* Running <tt>git --version</tt> should display the banner from git.
 
* Running <tt>git --version</tt> should display the banner from git.
 
* Running <tt>python --version</tt> should display the banner from python.
 
* Running <tt>python --version</tt> should display the banner from python.
Line 48: Line 53:
 
=== Build LLVM ===
 
=== Build LLVM ===
  
To build LLVM from the command line, just execute the following steps (from <tt>C:\LDC</tt>):
+
Building LLVM takes quite a while. You may want to download a prebuilt version from [https://github.com/ldc-developers/llvm/releases here]; we use these prebuilt ones 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.
  
 
<ul>
 
<ul>
<li>Get the source: <tt>git clone http://llvm.org/git/llvm.git llvm</tt></li>
+
<li>Download an LLVM source tarball (<tt>llvm-x.y.z.src.tar.xz</tt>). We maintain an [https://github.com/ldc-developers/llvm/releases LLVM fork] with minimal modifications; [http://releases.llvm.org/download.html vanilla LLVM] can be used as well.</li>
<li>A post-3.8 version is currently required, so you may as well just use the <tt>master</tt> branch and hope that LDC is still compatible. ;) Commit <tt>fa4edb6</tt> is guaranteed to work.</li>
+
<li>Extract it into <tt>C:\LDC</tt> (e.g., with [http://www.7-zip.org/ 7-Zip]), so that you end up with the source tree in e.g. <tt>C:\LDC\llvm-6.0.0.src</tt>.</li>
 +
<li><tt>cd C:\LDC</tt></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>
 
<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\LLVM-x64&quot; -DCMAKE_BUILD_TYPE=RelWithDebInfo
+
<pre>cmake -G Ninja ..\llvm-6.0.0.src
      -DPYTHON_EXECUTABLE=&quot;C:\LDC\Tools\Portable Python 2.7.5.1\App\python.exe&quot;
+
      -DCMAKE_INSTALL_PREFIX=&quot;C:\LDC\LLVM-x64&quot;
       -DLLVM_TARGETS_TO_BUILD=X86 -DLLVM_INCLUDE_TESTS=OFF -DLLVM_INCLUDE_EXAMPLES=OFF
+
      -DCMAKE_BUILD_TYPE=RelWithDebInfo -DLLVM_USE_CRT_RELWITHDEBINFO=MT
       -DLLVM_ENABLE_ASSERTIONS=ON -DLLVM_APPEND_VC_REV=ON -DLLVM_INSTALL_UTILS=ON ..\llvm</pre>
+
       -DLLVM_TARGETS_TO_BUILD=X86 -DLLVM_ENABLE_ASSERTIONS=ON
<p>Omit the <tt>CMAKE_BUILD_TYPE</tt> definition to build a debug version. The [http://llvm.org/docs/CMake.html#id10 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 <tt>-DLLVM_TARGETS_TO_BUILD=&quot;X86;ARM&quot;</tt>.</p></li>
+
       -DCOMPILER_RT_INCLUDE_TESTS=OFF</pre>
 +
<p>Use <tt>-DCMAKE_BUILD_TYPE=Debug</tt> to build a debug version (and probably use <tt>-DLLVM_USE_CRT_DEBUG=MTd</tt> then). The [http://llvm.org/docs/CMake.html#llvm-specific-variables LLVM page on CMake] documents other variables you can change. The most common is to add more targets, e.g., to enable ARM codegen you can use <tt>-DLLVM_TARGETS_TO_BUILD=X86;ARM</tt>.</p></li>
 
<li>Build LLVM: <tt>ninja</tt></li>
 
<li>Build LLVM: <tt>ninja</tt></li>
<li>Install it: <tt>ninja install</tt></li>
+
<li>Install it (to <tt>C:\LDC\LLVM-x64</tt>): <tt>ninja install</tt></li>
 +
<li>Note that you'll need to keep the <tt>C:\LDC\build-llvm-x64</tt> directory in order to keep the LLVM debuginfos (.pdb files) available.</li>
 
</ul>
 
</ul>
  
=== Build libconfig ===
+
=== Build libconfig (only required for LDC &lt; 1.3) ===
 +
 
 +
Latest confirmed working revision: <tt>7585cf6</tt>. 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>
 
* <tt>git clone https://github.com/hyperrealm/libconfig.git libconfig</tt>
 
* <tt>git clone https://github.com/hyperrealm/libconfig.git libconfig</tt>
 
* <tt>cd libconfig</tt>
 
* <tt>cd libconfig</tt>
 +
* <tt>git checkout 7585cf6</tt>
 
* Build the static C library: <tt>msbuild lib\libconfig.vcxproj /t:Rebuild /p:Configuration=ReleaseStatic /p:Platform=x64</tt>
 
* Build the static C library: <tt>msbuild lib\libconfig.vcxproj /t:Rebuild /p:Configuration=ReleaseStatic /p:Platform=x64</tt>
 
** Use configuration <tt>DebugStatic</tt> for debug LDC builds to ensure we link against the same runtime library
 
** Use configuration <tt>DebugStatic</tt> for debug LDC builds to ensure we link against the same runtime library
Line 77: Line 88:
 
<ul>
 
<ul>
 
<li><tt>cd C:\LDC</tt></li>
 
<li><tt>cd C:\LDC</tt></li>
<li><tt>git clone --recursive git://github.com/ldc-developers/ldc.git ldc</tt></li>
+
<li><tt>git clone --recursive https://github.com/ldc-developers/ldc.git</tt></li>
 
<li><tt>md build-ldc-x64</tt></li>
 
<li><tt>md build-ldc-x64</tt></li>
 
<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>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):</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; ..\ldc</pre>
      -DLIBCONFIG_LIBRARY=&quot;C:/LDC/libconfig/lib/x64/ReleaseStatic/libconfig.lib&quot; ..\ldc</pre>
+
<p>You can specify the D host compiler explicitly by adding <tt>-DD_COMPILER=c:\path\to\{dmd,ldmd2}.exe</tt>.</p>
<li>Build LDC and the runtimes: <tt>ninja</tt></li>
+
<p>For LDC &lt; 1.3, add the paths to libconfig like this: <tt>-DLIBCONFIG_INCLUDE_DIR=&quot;C:/LDC/libconfig/lib&quot; -DLIBCONFIG_LIBRARY=&quot;C:/LDC/libconfig/lib/x64/ReleaseStatic/libconfig.lib&quot;</tt></p></li>
<li>If you want to install it: <tt>ninja install</tt></li>
+
<li>Build LDC and the default libraries: <tt>ninja</tt>. The binaries end up in <tt>C:\LDC\build-ldc-x64\bin</tt>.</li>
 +
<li>If you want to install it (to <tt>C:\LDC\LDC-x64</tt>): <tt>ninja install</tt></li>
 
</ul>
 
</ul>
  
 
== Tests ==
 
== Tests ==
  
=== Running the LIT-based tests ===
+
For troubleshooting, be sure to examine the file <tt>C:\LDC\build-ldc-x64\Testing\Temporary\LastTest.log</tt> after a <tt>ctest</tt> invokation.
You'll need to have [https://pypi.python.org/pypi/lit lit] installed for Python. To run the tests from your build dir you can do:
+
 
 +
=== Running the LDC D unit tests ===
 +
 
 
* <tt>cd C:\LDC\build-ldc-x64</tt>
 
* <tt>cd C:\LDC\build-ldc-x64</tt>
* <tt>ctest --output-on-failure -R lit-tests</tt>
+
* <tt>ctest --output-on-failure -R ldc2-unittest</tt>
or you can go to the tests folder inside your build dir, and run the runlit.py script.
+
 
 +
=== Running the LIT (LLVM Integrated Tester) tests ===
 +
You'll need to have [https://pypi.python.org/pypi/lit lit] installed for Python:
 +
* Upgrade [https://pip.pypa.io/ pip]: <tt>python -m pip install -U pip</tt>
 +
* Install lit: <tt>python -m pip install lit</tt>
 +
* Make sure it works: <tt>python -c "import lit; lit.main();" --version</tt>
 +
To run the tests from your build dir you can do:
 +
* <tt>cd C:\LDC\build-ldc-x64</tt>
 +
* <tt>ctest -V -R lit-tests</tt>
 +
or you can go to the tests folder inside your build dir, and run the runlit.py script:
 
* <tt>cd C:\LDC\build-ldc-x64\tests</tt>
 
* <tt>cd C:\LDC\build-ldc-x64\tests</tt>
 
* <tt>python runlit.py -v .</tt>
 
* <tt>python runlit.py -v .</tt>
Line 101: Line 123:
 
* <tt>python runlit.py -v codegen/align.d</tt>
 
* <tt>python runlit.py -v codegen/align.d</tt>
  
=== Running the runtime unit tests ===
+
=== Running the druntime/Phobos unit tests ===
  
 
* <tt>cd C:\LDC\build-ldc-x64</tt>
 
* <tt>cd C:\LDC\build-ldc-x64</tt>
* Build the unit tests: <tt>ninja druntime-ldc-unittest druntime-ldc-unittest-debug phobos2-ldc-unittest phobos2-ldc-unittest-debug</tt>
+
* Build the unit tests: <tt>ninja all-test-runners</tt>
* Run the tests, excluding dmd-testsuite and the LIT tests: <tt>ctest --output-on-failure -E &quot;dmd-testsuite|lit-tests&quot;</tt>
+
* Run the tests, excluding LDC unit tests, LIT tests and dmd-testsuite: <tt>ctest --output-on-failure -E &quot;dmd-testsuite|lit-tests|ldc2-unittest&quot;</tt>
 
 
For troubleshooting be sure to examine the file <tt>C:\LDC\build-ldc-x64\Testing\Temporary\LastTest.log</tt>.
 
  
 
=== Running the dmd-testsuite tests ===
 
=== Running the dmd-testsuite tests ===
  
dmd-testsuite requires a minimalistic GNU environment. bash is shipped with git. Additionally, we need GNU make and reasonably recent GNU diffutils and coreutils:
+
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].
  
* Download [http://gnuwin32.sourceforge.net/packages/make.htm GNU make for Windows]: [http://gnuwin32.sourceforge.net/downlinks/make-bin-zip.php binary] + [http://gnuwin32.sourceforge.net/downlinks/make-dep-zip.php dependencies]
+
* 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)].
* Download [http://gnuwin32.sourceforge.net/packages/diffutils.htm GNU diffutils for Windows]: [http://gnuwin32.sourceforge.net/downlinks/diffutils-bin-zip.php binaries] (same dependencies as make)
+
* Edit your shell batch script and add the directory containing your <tt>make.exe</tt> to your PATH, e.g., <tt>set PATH=%~dp0Tools\make;%PATH%</tt>
* Download [http://gnuwin32.sourceforge.net/packages/coreutils.htm GNU coreutils for Windows]: [http://gnuwin32.sourceforge.net/downlinks/coreutils-bin-zip.php binaries] (same dependencies as make)
 
* Extract all 4 ZIP archives into a common root folder, e.g., <tt>C:\LDC\Tools\make</tt>
 
* Edit your shell batch script and add the bin subdirectory to your PATH, e.g., <tt>set PATH=%~dp0Tools\make\bin;%PATH%</tt>
 
** Make sure that directory preceeds the git bin directory in the PATH environment variable, so that the more recent executables in <tt>C:\LDC\Tools\make\bin</tt> are preferred over the (in my case, ancient) versions shipped with git.
 
 
* Spawn a new shell and make sure <tt>make --version</tt> prints its banner.
 
* Spawn a new shell and make sure <tt>make --version</tt> prints its banner.
  
Line 124: Line 140:
  
 
* <tt>cd C:\LDC\build-ldc-x64</tt>
 
* <tt>cd C:\LDC\build-ldc-x64</tt>
* Set your OS environment variable for dmd-testsuite's Makefile:
+
* Set some environment variables for dmd-testsuite's Makefile:
** 32-bit: <tt>set OS=Win_32</tt>
+
** <tt>OS</tt>
** 64-bit: <tt>set OS=Win_64</tt>
+
*** 32-bit LDC: <tt>set OS=Win_32</tt>
* <tt>ctest --verbose -R dmd-testsuite</tt>
+
*** 64-bit LDC: <tt>set OS=Win_64</tt>
** Debug only: <tt>ctest --verbose -R dmd-testsuite-debug</tt>
+
** <tt>DMD_TESTSUITE_MAKE_ARGS</tt> enables parallel execution, e.g.
** Release only: <tt>ctest --verbose -R dmd-testsuite -E -debug</tt>
+
*** <tt>set DMD_TESTSUITE_MAKE_ARGS=-j4</tt>
 +
* <tt>ctest -V -R dmd-testsuite</tt>
 +
** Debug only: <tt>ctest -V -R dmd-testsuite-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 ==
 +
 +
Be sure to use Rainer's great [http://rainers.github.io/visuald/visuald/StartPage.html Visual D] plugin for VS.
  
 
* <tt>cd C:\LDC</tt>
 
* <tt>cd C:\LDC</tt>
 
* <tt>md vs-ldc-x64</tt>
 
* <tt>md vs-ldc-x64</tt>
 
* <tt>cd vs-ldc-x64</tt>
 
* <tt>cd vs-ldc-x64</tt>
* Use the cmake command from the Build LDC section, but use the VS generator instead of Ninja this time: <tt>cmake -G "Visual Studio 14 Win64" ...</tt>
+
* Use the cmake command from the Build LDC section, but use the VS generator instead of Ninja this time: <tt>cmake -G "Visual Studio 15 Win64" ...</tt>
  
This creates the VS 2015 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>.
 
A Visual Studio solution for LLVM can be created the same way.
 
A Visual Studio solution for LLVM can be created the same way.
  
== Example ==
+
I don't recommend building LDC/LLVM with VS directly; I only use VS for development/debugging and build in the shell with Ninja.
 
 
The simple D program <tt>hello.d</tt>
 
 
 
<source lang="d">import std.stdio;
 
 
 
int main()
 
{
 
    writefln("Hello LDC2");
 
    return 0;
 
}</source>
 
can be compiled and linked with the commands:
 
 
 
<pre>
 
ldc2 -c hello.d
 
ldc2 hello.obj
 
</pre>
 
 
 
or simply with: <tt>ldc2 hello.d</tt>
 
 
 
  
  

Revision as of 23:00, 15 April 2018

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 .circleci/config.yml (Ubuntu Linux and macOS) 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 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.8.2;%~dp0Tools\PortableGit-2.14.1-64-bit\usr\bin;%~dp0Tools\PortableGit-2.14.1-64-bit\bin;%~dp0Tools\make-4.2.1;%~dp0Tools\cmake-3.9.1-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 D host compiler
set DMD=%~dp0ldc2-1.7.0-windows-multilib\bin\ldmd2.exe
:: set a few environment variables for dmd-testsuite
set OS=Win_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 OS=Win_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.

Build LLVM

Building LLVM takes quite a while. You may want to download a prebuilt version from here; we use these prebuilt ones 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 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-6.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-6.0.0.src
          -DCMAKE_INSTALL_PREFIX="C:\LDC\LLVM-x64"
          -DCMAKE_BUILD_TYPE=RelWithDebInfo -DLLVM_USE_CRT_RELWITHDEBINFO=MT
          -DLLVM_TARGETS_TO_BUILD=X86 -DLLVM_ENABLE_ASSERTIONS=ON
          -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. The most common is to add more targets, e.g., to enable ARM codegen you can use -DLLVM_TARGETS_TO_BUILD=X86;ARM.

  • 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.

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 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 -DCMAKE_INSTALL_PREFIX="C:\LDC\LDC-x64" -DCMAKE_BUILD_TYPE=RelWithDebInfo
          -DLLVM_ROOT_DIR="C:/LDC/LLVM-x64" ..\ldc

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

    For LDC < 1.3, add the paths to libconfig like this: -DLIBCONFIG_INCLUDE_DIR="C:/LDC/libconfig/lib" -DLIBCONFIG_LIBRARY="C:/LDC/libconfig/lib/x64/ReleaseStatic/libconfig.lib"

  • 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

  • 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

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 LDC: set OS=Win_32
      • 64-bit LDC: 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

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