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

From D Wiki
Jump to: navigation, search
m (Known bugs)
m (Running the druntime/Phobos unit tests and druntime standalone tests)
 
(118 intermediate revisions by 3 users not shown)
Line 1: Line 1:
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.
+
Windows MSVC x86/x64 are first class targets for LDC. This page documents how to build, test and hack LDC2 on Windows.
  
== Building LDC ==
+
== LDC binaries ==
  
=== Required software ===
+
If you just want to download the very latest LDC binaries, head over to the [https://github.com/ldc-developers/ldc/releases/tag/CI GitHub CI release].
  
* Windows x64, of course!
+
== Advice ==
* 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])
+
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 Azure Pipelines scripts for [https://github.com/ldc-developers/ldc/blob/master/.azure-pipelines/posix.yml Ubuntu Linux and macOS] and [https://github.com/ldc-developers/ldc/blob/master/.azure-pipelines/windows.yml Windows] are always up-to-date with the latest build setup.
* [http://python.org/download/ Python 2.7.x or Python 3.3.x] (I use 3.3.0)
 
* [http://git-scm.com/download/win git 1.8.x] (I use 1.9.4.msysgit.2 installed with option "Run Git from the Windows Command Prompt")
 
* [http://cmake.org/cmake/resources/software.html CMake 2.8.x] (I use 3.0.2 installed with option "Add cmake to the system PATH for all users")
 
* a tool to extract .tgz files, e.g. [http://7-zip.org/ 7-Zip]
 
  
=== Required source downloads ===
+
== Building LDC ==
  
* [http://hyperrealm.com/libconfig/ libconfig] (Just use latest release 1.4.9.)
+
=== Required software ===
  
=== Environment check ===
+
* Windows, of course!
 +
* [https://www.visualstudio.com/downloads/ Visual Studio or stand-alone Build Tools] ≥ 2017. 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 recent [https://git-scm.com/download/win git] installation
 +
* [https://www.python.org/downloads/ Python] (I use [https://github.com/winpython/winpython/releases Winpython])
 +
* [https://github.com/Kitware/CMake/releases CMake &ge; 3.8]
 +
* [https://github.com/ninja-build/ninja/releases Ninja], a neat little and fast build system
 +
* For running the testsuite: GNU make ([https://dl.dropboxusercontent.com/s/4y36f5ydgrk4p5g/make-4.2.1.7z?dl=0 prebuilt v4.2.1])
  
After installing the software, you can open a &quot;VS2013 x64 Native Tools Command Prompt&quot;.
+
=== Shell environment ===
* Running <tt>cmake --version</tt> should display the banner from cmake.
 
* Running <tt>git --version</tt> should display the banner from git.
 
* Running <tt>msbuild /version</tt> should display the banner from msbuild.
 
* Running <tt>&lt;Python install path&gt;\python --version</tt> should display the banner from python.
 
  
My working directory is <tt>C:\ldcenv</tt>. Every step starts in this directory. Please feel free to adjust this to your needs.
+
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>x64 Native Tools Command Prompt for VS 2019</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>).
  
=== Build LLVM ===
+
<pre>
 
+
@echo off
To build LLVM from the command line, just execute the following steps:
+
set PATH=%~dp0LDC-x64\bin;%~dp0LLVM-x64\bin;%~dp0Tools\Ninja 1.10.2;%~dp0Tools\make-4.2.1;C:\Program Files\Git\usr\bin;C:\Program Files\Git\bin;%~dp0Tools\cmake-3.19.4-win64-x64\bin;%~dp0Tools\Winpython64-3.7.4.0Zero\python-3.7.4.amd64;%PATH%
 
+
:: set D host compiler
<ul>
+
set DMD=%~dp0ldc2-1.24.0-windows-multilib\bin\ldmd2.exe
<li><tt>git clone http://llvm.org/git/llvm.git llvm</tt></li>
+
:: set a few environment variables for dmd-testsuite
<li><tt>md build-llvm-x64</tt></li>
+
set OS=windows
<li><tt>cd build-llvm-x64</tt></li>
+
set MODEL=64
<li><p>Use a command like this:</p>
+
if not exist "%TERM%" set TERM=msys
<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;
+
start /belownormal cmd.exe /k "C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\Common7\Tools\VsDevCmd.bat" -arch=x64
      -DCMAKE_CXX_FLAGS=&quot;/DWIN32 /MP /W3 /GR- /GS- /Zo&quot; -DPYTHON_EXECUTABLE=&quot;C:\Program Files\Python33\python.exe&quot;
+
</pre>
      -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>
 
 
 
Build the INSTALL project in the generated solution: <tt>msbuild INSTALL.vcxproj</tt>
 
  
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>.
+
Use <tt>-arch=x86</tt> in the last line and <tt>set MODEL=32</tt> if you want to build a 32-bit LDC.
  
=== Build libconfig ===
+
Open a shell by executing the batch file.
  
Simple build instructions for libconfig are missing. You can do the following:
+
* Running <code>cl</code> should display the banner from the MS compiler.
 +
* 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>python --version</code> should display the banner from Python.
 +
* Running <code>cmake --version</code> should display the banner from CMake.
 +
* Running <code>ninja --version</code> should display the Ninja version.
 +
* For the tests:
 +
** Running <code>make --version</code> should display the banner from GNU make.
 +
** Starting a bash shell via <code>bash</code> and then invoking <code>link /version</code> should still display the MS linker banner. If it doesn't, make sure that git's <tt>usr\bin</tt> directory is listed before its <tt>bin</tt> directory in <tt>PATH</tt>.
  
* Extract libconfig source.
+
=== Building LLVM ===
* 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 <tt>libconfig</tt> to 'Static library'.
 
* Build the project <tt>libconfig</tt>
 
  
Alternative you can use a custom <tt>CMakeLists.txt</tt>:
+
Building LLVM takes quite a while, so you may want to download a prebuilt version from [https://github.com/ldc-developers/llvm-project/releases here]. 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 errors wrt. unsupported command-line options or linking errors when building LDC, you probably use a different MSVC/clang toolchain version and will need to build LLVM yourself.
 
 
* Extract libconfig source.
 
* Download CMakeLists.txt from [https://gist.github.com/redstar/d5694a6301c40ceb0720 this gist] and place it into the root folder.
 
* Create a build directory: <tt>md build_libconfig</tt>
 
* <tt>cd build_libconfig</tt>
 
* Now create the build files:
 
* <tt>cmake ..\libconfig-1.4.9</tt>
 
* Build the library: <tt>msbuild ALL_BUILD.vcxproj</tt>
 
 
 
=== Build LDC2 ===
 
 
 
To build LDC2 from the command line, just execute the following steps:
 
  
 
<ul>
 
<ul>
<li><tt>git clone --recursive git://github.com/ldc-developers/ldc.git ldc</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-project/releases LLVM fork] with minimal modifications; [http://releases.llvm.org/download.html vanilla LLVM] can be used as well.</li>
<li><tt>md build-ldc2-x64</tt></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-11.0.1.src</tt>.</li>
<li><tt>cd build-ldc2-x64</tt></li>
+
<li><code>cd C:\LDC</code></li>
<li><p>Type the next command in one line:</p>
+
<li>Create a build directory: <code>md build-llvm-x64</code></li>
<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;
+
<li>Change into it: <code>cd build-llvm-x64</code></li>
       -DLLVM_ROOT_DIR=&quot;C:/Program Files/LLVM-x64&quot;
+
<li><p>Use a command like this (in one line) to create the Ninja build files:</p>
       -DLIBCONFIG_INCLUDE_DIR=&quot;C:/ldcenv/libconfig-1.4.9/lib&quot;
+
<pre>cmake -G Ninja ..\llvm-11.0.1.src
       -DLIBCONFIG_LIBRARY=&quot;C:/ldcenv/libconfig-1.4.9/Debug/libconfig.lib&quot; ..\ldc</pre>
+
      -DCMAKE_INSTALL_PREFIX=&quot;C:\LDC\LLVM-x64&quot;
<p>This assumes that you have a static compiled version of <tt>libconfig.lib</tt>. If you use the DLL version then you have to add <tt>-DLIBCONFIG_DLL=ON</tt>.</p></li>
+
      -DCMAKE_BUILD_TYPE=RelWithDebInfo -DLLVM_USE_CRT_RELWITHDEBINFO=MT
 +
      -DLLVM_ENABLE_ASSERTIONS=ON
 +
       -DLLVM_TARGETS_TO_BUILD=AArch64;ARM;Mips;MSP430;NVPTX;PowerPC;RISCV;WebAssembly;X86
 +
       -DLLVM_DEFAULT_TARGET_TRIPLE=x86_64-pc-windows-msvc
 +
      -DCOMPILER_RT_INCLUDE_TESTS=OFF
 +
       -DLLVM_INCLUDE_TESTS=OFF</pre>
 +
<p>Use <code>-DCMAKE_BUILD_TYPE=Debug</code> to build a debug version (and probably use <code>-DLLVM_USE_CRT_DEBUG=MTd</code> then). The [http://llvm.org/docs/CMake.html#llvm-specific-variables LLVM page on CMake] documents other variables you can change. For a 32-bit build, use <code>-DLLVM_DEFAULT_TARGET_TRIPLE=i686-pc-windows-msvc</code>.</p></li>
 +
<li>Build LLVM: <code>ninja</code></li>
 +
<li>Install it (to <tt>C:\LDC\LLVM-x64</tt>): <code>ninja install</code></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 the INSTALL project: <tt>msbuild INSTALL.vcxproj</tt>
+
=== Building LDC ===
 
 
Check that <tt>C:\Program Files\LDC\bin</tt> is in your path and type <tt>ldc2 -version</tt> to check that you can run <tt>LDC2</tt>.
 
 
 
=== Running the tests ===
 
 
 
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>
<li>Download the latest Ninja release from [https://github.com/martine/ninja/releases GitHub] (a single .exe) and put it in your PATH.</li>
+
<li><code>cd C:\LDC</code></li>
<li>Make sure you can link against the [http://wiki.dlang.org/Curl_on_Windows Curl] library (curl.lib in your LIB/LIBPATH, curl.dll in your PATH).</li>
+
<li><code>git clone --recursive https://github.com/ldc-developers/ldc.git</code></li>
<li>Run CMake just like when building LDC2, but use the <tt>Ninja</tt> generator this time:
+
<li><code>md build-ldc-x64</code></li>
  <ul>
+
<li><code>cd build-ldc-x64</code></li>
  <li><tt>md ninja-ldc2-x64</tt></li>
+
<li><p>Use a command like this (in one line) to create the Ninja build files:</p>
  <li><tt>cd ninja-ldc2-x64</tt></li>
+
<pre>cmake -G Ninja ..\ldc
  <li><p>Type the next command in one line:</p>
+
      -DCMAKE_INSTALL_PREFIX=&quot;C:\LDC\LDC-x64&quot;
  <pre>cmake -G Ninja -DCMAKE_INSTALL_PREFIX=&quot;C:\Program Files\LDC&quot; -DCMAKE_CXX_FLAGS=&quot;/DWIN32 /MP /W2 /GR- /GS- /Zo&quot;
+
      -DCMAKE_BUILD_TYPE=RelWithDebInfo
       -DLLVM_ROOT_DIR=&quot;C:/Program Files/LLVM-x64&quot;
+
       -DLLVM_ROOT_DIR=&quot;C:/LDC/LLVM-x64&quot;</pre>
      -DLIBCONFIG_INCLUDE_DIR=&quot;C:/ldcenv/libconfig-1.4.9/lib&quot;
+
<p>You can specify the D host compiler explicitly by adding <code>-DD_COMPILER=c:\path\to\ldmd2.exe</code>.</p>
      -DLIBCONFIG_LIBRARY=&quot;C:/ldcenv/libconfig-1.4.9/Debug/libconfig.lib&quot; ..\ldc</pre>
+
</li>
  </ul></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 runtimes: <tt>ninja</tt></li>
+
<li>If you want to install it (to <tt>C:\LDC\LDC-x64</tt>): <code>ninja install</code></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>
  
For troubleshooting be sure to examine the file <tt>ninja-ldc2-x64\Testing\Temporary\LastTest.log</tt>.
+
== Tests ==
  
=== Known bugs ===
+
For troubleshooting, be sure to examine the file <tt>C:\LDC\build-ldc-x64\Testing\Temporary\LastTest.log</tt> after a <tt>ctest</tt> invocation.
  
* 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. LLVM is getting [http://llvm.org/docs/ExceptionHandling.html#c-exception-handling-using-the-windows-runtime native support for MSVCRT exceptions], so we'll have to wait for that.
+
=== Running the LDC D unit tests ===
* You'll probably need to hack your <tt>*.conf.in</tt> configuration file templates and add <tt>-L/LARGEADDRESSAWARE:NO</tt> to your default LDC switches, see [https://github.com/ldc-developers/ldc/issues/442 issue #442].
 
* At the time of this writing, you'll probably also encounter issues like [https://github.com/ldc-developers/ldc/issues/930 930].
 
  
== Hacking druntime and phobos ==
+
* <code>cd C:\LDC\build-ldc-x64</code>
 +
* <code>ctest --output-on-failure -R ldc2-unittest</code>
  
* <tt>ldc.eh2</tt>: Implementation has alpha quality.
+
=== Running the LIT (LLVM Integrated Tester) tests ===
* The MSVC runtime misses support for some C99 functions. See https://github.com/ldc-developers/ldc/issues/761 for an example.
 
** VS 2015 improves Microsoft's C99 support; see [https://github.com/ldc-developers/druntime/pull/29 PR29].
 
  
If you link your modules you have to specify the libraries <tt>phobos-ldc.lib</tt> and <tt>shell32.lib</tt>.
+
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>
 +
* Install lit: <code>python -m pip install lit</code>
 +
* Make sure it works: <code>python -c "import lit.main; lit.main.main();" --version .</code>
 +
To run the tests from your build dir you can do:
 +
* <code>cd C:\LDC\build-ldc-x64</code>
 +
* <code>ctest -V -R lit-tests</code>
 +
or you can go to the tests folder inside your build dir, and run the runlit.py script:
 +
* <code>cd C:\LDC\build-ldc-x64\tests</code>
 +
* <code>python runlit.py -v .</code>
 +
The second way is convenient for running individual tests:
 +
* <code>python runlit.py -v codegen/align.d</code>
  
== Example ==
+
=== Running the druntime/Phobos unit tests and druntime integration tests ===
  
The simple D program <tt>hello.d</tt>
+
* Make sure the 64/32-bit <tt>libcurl.dll</tt> (bundled with LDC) can be found in a PATH directory. Verify by running <code>where libcurl.dll</code>.
 +
* <code>cd C:\LDC\build-ldc-x64</code>
 +
* Build the unit tests: <code>ninja all-test-runners</code>
 +
* Run the tests, excluding LDC unit tests, LIT tests and dmd-testsuite: <code>ctest --output-on-failure -E &quot;dmd-testsuite|lit-tests|ldc2-unittest&quot;</code>
  
<source lang="d">import std.stdio;
+
=== Running the dmd-testsuite tests ===
  
int main()
+
* If you haven't already, set the <tt>OS</tt> and <tt>MODEL</tt> environment variables:
{
+
** <code>set OS=windows</code> (since v1.17, previously <code>win{32,64}</code>)
    writefln("Hello LDC2");
+
** <code>set MODEL={32,64}</code>
    return 0;
+
* <code>cd C:\LDC\build-ldc-x64</code>
}</source>
+
* <code>ctest -V -R dmd-testsuite</code>
can be compiled and linked with the commands:
+
** Debug only: <code>ctest -V -R "build-run-dmd-testsuite|dmd-testsuite-debug"</code>
 +
** Release only: <code>ctest -V -R dmd-testsuite -E -debug</code>
  
<pre>
+
== Developing/debugging LDC/LLVM with Visual Studio ==
ldc2 -c hello.d
+
 
link hello2.obj phobos-ldc.lib shell32.lib
+
Be sure to use Rainer's great [http://rainers.github.io/visuald/visuald/StartPage.html Visual D] plugin for VS.
</pre>
 
  
or simply with: <tt>ldc2 hello.d</tt>
+
* <code>cd C:\LDC</code>
 +
* <code>md 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>
  
The resulting <tt>hello.exe</tt> produces the expected output.
+
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.
  
 +
I don't recommend building LDC/LLVM with VS directly; I only use VS for development/debugging and build in the shell with Ninja.
  
  

Latest revision as of 04:47, 8 February 2021

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 2019 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.10.2;%~dp0Tools\make-4.2.1;C:\Program Files\Git\usr\bin;C:\Program Files\Git\bin;%~dp0Tools\cmake-3.19.4-win64-x64\bin;%~dp0Tools\Winpython64-3.7.4.0Zero\python-3.7.4.amd64;%PATH%
:: set D host compiler
set DMD=%~dp0ldc2-1.24.0-windows-multilib\bin\ldmd2.exe
:: set a few environment variables for dmd-testsuite
set OS=windows
set MODEL=64
if not exist "%TERM%" set TERM=msys
start /belownormal cmd.exe /k "C:\Program Files (x86)\Microsoft Visual Studio\2019\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.
  • For the tests:
    • Running make --version should display the banner from GNU make.
    • Starting a bash shell via bash and then invoking link /version should still display the MS linker banner. If it doesn't, make sure that git's usr\bin directory is listed before its bin directory in PATH.

Building LLVM

Building LLVM takes quite a while, so you may want to download a prebuilt version from here. 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 errors wrt. unsupported command-line options or 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-11.0.1.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-11.0.1.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;RISCV;WebAssembly;X86
          -DLLVM_DEFAULT_TARGET_TRIPLE=x86_64-pc-windows-msvc
          -DCOMPILER_RT_INCLUDE_TESTS=OFF
          -DLLVM_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 invocation.

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.main; lit.main.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 integration tests

  • Make sure the 64/32-bit libcurl.dll (bundled with LDC) can be found in a PATH directory. Verify by running where libcurl.dll.
  • 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

  • If you haven't already, set the OS and MODEL environment variables:
    • set OS=windows (since v1.17, previously win{32,64})
    • set MODEL={32,64}
  • cd C:\LDC\build-ldc-x64
  • ctest -V -R dmd-testsuite
    • Debug only: ctest -V -R "build-run-dmd-testsuite|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