Building and hacking LDC on Windows using MSVC
LDC on Windows is work in progress. This page documents how to build, test and hack LDC2 on Windows x64.
Contents
Building LDC
Required software
- Windows x64, of course!
- Visual Studio (e.g. VS Community 2015)
- git 1.8+ (I use PortableGit)
- Python 2.7.x or Python 3.3.x (I use Portable Python)
- CMake 2.8+
- Ninja, a neat little and fast build system
- Curl library (just use a precompiled one)
- Put 64-bit curl.lib in a LIB/LIBPATH directory (e.g., C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\lib\amd64)...
- ... and 64-bit libcurl.dll in a PATH directory (e.g., C:\LDC\LDC-x64\bin).
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\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% if not exist "%TERM%" set TERM=msys start /belownormal %comspec% /k "C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\vcvarsall.bat" amd64
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
- 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\Portable Python 2.7.5.1\App\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
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
- cd C:\LDC
- git clone https://github.com/hyperrealm/libconfig.git libconfig
- cd libconfig
- 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
Use a command like this (in one line):
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 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 the dmd-testsuite ones: ctest -E dmd-testsuite
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 is shipped with git. Additionally, we need GNU make and reasonably recent GNU diffutils and coreutils:
- Download GNU make for Windows: binary + dependencies
- Download GNU diffutils for Windows: binaries (same dependencies as make)
- Download GNU coreutils for Windows: binaries (same dependencies as make)
- Extract all 4 ZIP archives into a common root folder, e.g., C:\LDC\Tools\make
- Edit your shell batch script and add the bin subdirectory to your PATH, e.g., set PATH=%~dp0Tools\make\bin;%PATH%
- Make sure that directory preceeds the git bin directory in the PATH environment variable, so that the more recent executables in C:\LDC\Tools\make\bin are preferred over the (in my case, ancient) versions shipped with git.
- 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. I prefer to run them manually, outside of ctest.
- cd C:\LDC\build-ldc-x64
- If you want to reset the results, delete the folder: rmdir /S dmd-testsuite(-debug)
- Get the command line from C:\LDC\build-ldc-x64\tests\d2\CTestTestfile.cmake (e.g., "make" "-k" "-C" "C:/LDC/ldc/tests/d2/dmd-testsuite" "RESULTS_DIR=C:/LDC/build-ldc-x64/dmd-testsuite-debug" ... "quick")
- You may want to redirect stdout and stderr to a log file by appending something like > dmd-testsuite.log 2>&1
- Start the tests.
- Run the tests a second time without resetting the results to get a list of the failures and disabled tests.
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 solution C:\LDC\vs-ldc-x64\ldc.sln.
A Visual Studio solution for LLVM can be created the same way.
Known bugs/limitations
- Structured Exception Handling is only available for LLVM 3.7 head. Support in ldc.eh2 has still alpha quality and is known to crash your application under certain circumstances. LLVM is getting native support for MSVCRT exceptions, so we'll have to wait for that.
- At the time of this writing, LDC master is based on D 2.066, and branch merge-2.067 provides important fixes for MSVC.
- Even more cutting edge, right now anyway, is my win64 branch, providing further important fixes (not just for LDC, but all submodules too).
- You'll most likely notice issue 988. :P
- You'll probably also encounter issues like #930.
- Visual Studio 2015 is greatly encouraged due to improved C99 support; see PR29.
- State as of August 9th 2015: 2 failing unittests (core.thread and std.uni-debug), 11 dmd-testsuite-debug failures, 10 dmd-testsuite failures
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.