Building and hacking LDC on Windows using MSVC

From D Wiki
Revision as of 22:06, 12 December 2012 by Klickverbot (talk | contribs) (Direct copy of the LDC GitHub wiki page)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

LDC on Windows is work in progress. This page documents how to compile LDC2 on Windows 7 x64. It also provides hints on druntime and phobos.

Building LDC

Required software

  • Windows 7 x64, of course!
  • .NET Framework 4.0 (prerequisite for Windows SDK)
  • Windows 7.1 SDK
  • Python 2.7.x (I use 2.7.3)
  • git 1.8.x (I use 1.8.0.msysgit.0 installed with option "Run Git from the Windows Command Prompt")
  • CMake 2.8.x (I use 2.8.9 installed with option "Add cmake to the system PATH for all users")
  • a tool to extract .tgz files, e.g. 7-Zip

Required source downloads

Environment check

After installing the software, you can open a Windows SDK 7.1 Command Prompt. You should see that you are targeting "Windows 7 x64 Debug". If not please execute the command setenv /x64 /Debug. Running cmake --version should display the banner from cmake. Running git --version should display the banner from git. Running msbuild /version should display the banner from msbuild. Running <Python install path>\python --version should display the banner from python.

My working directory is C:\ldcenv. Every step starts in this directory. Please feel free to adjust this to your needs.

Build LLVM

To build LLVM from the command line, just execute the following steps:

  • md build-llvm-x64
  • git clone http://llvm.org/git/llvm.git llvm
  • cd llvm
  • The next two commands are only required if LLVM 3.1 is used:
    git checkout -t origin/release_31
    git apply <path to LLVM patch>\LLVMConfig.diff
  • cd ..\build-llvm-x64
  • Type the next command in one line:

    cmake -G "Visual Studio 10 Win64" -DCMAKE_INSTALL_PREFIX="C:\Program Files\LLVM-x64-31" 
          -DPYTHON_EXECUTABLE="C:\Program Files\Python27\python.exe" -DLLVM_TARGETS_TO_BUILD="X86"
          -DLLVM_INCLUDE_TESTS=OFF C:\ldcenv\llvm
  • msbuild ALL_BUILD.vcxproj

Open a Windows SDK 7.1 Command Prompt as Administrator.

  • cd build-llvm-x64
  • msbuild INSTALL.vcxproj

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 libconfig++

Simple build instructions for libconfig++ are missing. You can do the following:

  • Install Visual Studio 2010 (e.g. Visual Studio 2010 Express)
  • Open the libconfig.sln solution
  • Create a profile for Win64 Debug
  • If you like to create a static library then change the configuration type of project libconfig++ to 'Static library'.
  • Build the project libconfig++

Build LDC2

To build LDC2 from the command line, just execute the following steps:

  • md build-ldc2-x64
  • git clone git://github.com/ldc-developers/ldc.git ldc
  • cd ldc
  • git submodule update --init
  • Now apply all the patches for LDC (see section 'Required source downloads' above):
    git apply <patch name>
  • cd runtime\druntime
  • git checkout -b ldc-latest
  • cd ..\phobos
  • git checkout -b ldc-latest
  • cd ..\..\..\build-ldc2-x64
  • Type the next command in one line:

    cmake -G "Visual Studio 10 Win64" -DCMAKE_INSTALL_PREFIX="C:\Program Files\LDC"
          -DLLVM_ROOT_DIR="C:/Program Files/LLVM-x64-31"
          -DLIBCONFIG++_INCLUDE_DIR="C:/ldcenv/libconfig-1.4.9/lib"
          -DLIBCONFIG++_LIBRARY="C:/ldcenv/libconfig-1.4.9/Debug/libconfig++.lib" C:\ldcenv\ldc

    This assumes that you have a static compiled version of libconfig++.lib. If you use the DLL version then you have to add -DLIBCONFIG_DLL=ON.

  • msbuild ALL_BUILD.vcxproj

Open a Windows SDK 7.1 Command Prompt as Administrator.

  • cd build-llvm-x64
  • msbuild INSTALL.vcxproj

Check that C:\Program Files\LDC\bin is in your path and type ldc2 -version to check that you can run LDC2.

Troubleshooting

  • git 1.7.11 requires extra setup because of git-svn. Just use 1.7.10 or 1.8.0.
  • LLVM does not work with Python 3.x. Please use Python 2.7.x.
  • Using Visual Studio 2012: You do not need to install the Windows SDK. Just open the "VS2012 x64 Native Tools Command Prompt". The CMake generator for VS 2012 is named Visual Studio 11 Win64.

Known bugs

  • No code generation for Structured Exception Handling (no support from LLVM yet).
  • Execution of tests with the RUN_TESTS project does not work.

Hacking druntime and phobos

As of version 2.061 (still under development) Druntime and Phobos have support for Win64. The following issues are known:

  • ldc.eh2: Only a dummy implementation is provided because of missing LLVM support.
  • Other missing functions: Most functions related to real type (long double in C) are missing because Visual C++ has dropped support for it (e.g. strtold(), sinl, cosl, ...). Some of them are already implemented (see e.g. std.math) but please be aware that there are still fake implementations present.

If you link your modules you have to specify the libraries phobos-ldc.lib and shell32.lib. Due to a bug in LLVM you have to specifiy the flag /LARGEADDRESSAWARE:NO, too.

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 /LARGEADDRESSAWARE:NO

or simply with: ldc2 hello.d

The resulting hello.exe produces the expected output.