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

From D Wiki
Jump to: navigation, search
m (Update build instructions for LDC on Win64.)
Line 15: Line 15:
  
 
* [http://hyperrealm.com/libconfig/ libconfig++] (Just use latest release 1.4.9.)
 
* [http://hyperrealm.com/libconfig/ libconfig++] (Just use latest release 1.4.9.)
* [http://reviews.llvm.org/file/data/u47n3egee45qnw7hzi4m/PHID-FILE-admxb6oahb4n2hymrduj/pdata_patch.patch Patch fixing creation of .pdata segments] (Patch by Twobit, see [http://reviews.llvm.org/D3418 LLVM review])
+
 
* Patches for LDC: (currently none)
 
 
=== Environment check ===
 
=== Environment check ===
  
Line 29: Line 28:
 
<ul>
 
<ul>
 
<li><tt>md build-llvm-x64</tt></li>
 
<li><tt>md build-llvm-x64</tt></li>
 +
<li><tt>cd build-llvm-x64</tt></li>
 
<li><tt>git clone http://llvm.org/git/llvm.git llvm</tt></li>
 
<li><tt>git clone http://llvm.org/git/llvm.git llvm</tt></li>
<li><tt>cd llvm</tt></li>
 
<li>Apply fix for LLVM 3.5 head:<br />
 
<tt>git apply &lt;path to LLVM patch&gt;\pdata_patch.patch</tt></li>
 
<li><tt>cd ..</tt></li>
 
 
<li><p>Use a command like this:</p>
 
<li><p>Use a command like this:</p>
 
<pre>cmake -G &quot;Visual Studio 12 Win64&quot; -DCMAKE_INSTALL_PREFIX=&quot;C:\Program Files\LLVM-x64&quot; -DCMAKE_CXX_FLAGS="/DWIN32 /D_HAS_EXCEPTIONS=0 /MP /W3 /GR- /GS- /d2Zi+"  
 
<pre>cmake -G &quot;Visual Studio 12 Win64&quot; -DCMAKE_INSTALL_PREFIX=&quot;C:\Program Files\LLVM-x64&quot; -DCMAKE_CXX_FLAGS="/DWIN32 /D_HAS_EXCEPTIONS=0 /MP /W3 /GR- /GS- /d2Zi+"  
Line 40: Line 36:
 
</ul>
 
</ul>
  
Build the INSTALL project in the generated solution.
+
Build the INSTALL project in the generated solution, e.g., via <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>.
 
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>.
Line 69: Line 65:
 
<ul>
 
<ul>
 
<li><tt>md build-ldc2-x64</tt></li>
 
<li><tt>md build-ldc2-x64</tt></li>
 +
<li><tt>cd build-ldc2-x64</tt></li>
 
<li><tt>git clone --recursive git://github.com/ldc-developers/ldc.git ldc</tt></li>
 
<li><tt>git clone --recursive git://github.com/ldc-developers/ldc.git ldc</tt></li>
<li><tt>cd ldc</tt></li>
+
<li>Due to [http://llvm.org/bugs/show_bug.cgi?id=21016 LLVM bug #21016], you currently need to add <tt>x86utils</tt> to the list in file <tt>ldc\CMakeLists.txt</tt>, line 19 (before the closing parenthesis).</li>
<li>Now apply all the patches for LDC (see section 'Required source downloads' above):<br />
 
<tt>git apply &lt;patch name&gt;</tt></li>
 
<li><tt>cd ..</tt></li>
 
 
<li><p>Type the next command in one line:</p>
 
<li><p>Type the next command in one line:</p>
 
<pre>cmake -G &quot;Visual Studio 12 Win64&quot; -DCMAKE_INSTALL_PREFIX=&quot;C:\Program Files\LDC&quot; -DCMAKE_CXX_FLAGS="/DWIN32 /D_HAS_EXCEPTIONS=0 /MP /W2 /GR- /GS- /d2Zi+"
 
<pre>cmake -G &quot;Visual Studio 12 Win64&quot; -DCMAKE_INSTALL_PREFIX=&quot;C:\Program Files\LDC&quot; -DCMAKE_CXX_FLAGS="/DWIN32 /D_HAS_EXCEPTIONS=0 /MP /W2 /GR- /GS- /d2Zi+"
Line 82: Line 76:
 
</ul>
 
</ul>
  
Build the INSTALL project.
+
Build the INSTALL project, e.g., via <tt>msbuild INSTALL.vcxproj</tt>.
  
 
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>.
 
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>.
Line 92: Line 86:
 
=== Known bugs ===
 
=== Known bugs ===
  
* Structured Exception Handling is only available for LLVM 3.5 head with the patch above applied. Support has still alpha quality and is known to crash your application under certain circumstances.
+
* Structured Exception Handling is only available for LLVM 3.6 head. Support has still alpha quality and is known to crash your application under certain circumstances.
 
* Execution of tests with the <tt>RUN_TESTS</tt> project does not work.
 
* Execution of tests with the <tt>RUN_TESTS</tt> project does not work.
  

Revision as of 19:14, 26 September 2014

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

Required source downloads

Environment check

After installing the software, you can open a "VS2013 x64 Native Tools Command Prompt". 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
  • cd build-llvm-x64
  • git clone http://llvm.org/git/llvm.git llvm
  • Use a command like this:

    cmake -G "Visual Studio 12 Win64" -DCMAKE_INSTALL_PREFIX="C:\Program Files\LLVM-x64" -DCMAKE_CXX_FLAGS="/DWIN32 /D_HAS_EXCEPTIONS=0 /MP /W3 /GR- /GS- /d2Zi+" 
          -DPYTHON_EXECUTABLE="C:\Program Files\Python33\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

Build the INSTALL project in the generated solution, e.g., via 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:

  • 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 libconfig++ to 'Static library'.
  • Build the project libconfig++

Alternative you can use a custom CMakeLists.txt:

  • Extract libconfig++ file.
  • Download CMakeLists.txt from this gist and place it into the root folder.
  • Create a build directory: md build_libconfig
  • cd build_libconfig
  • Now create the build files:
  • cmake ..\libconfig-1.4.9
  • Build the library: msbuild ALL_BUILD.vcxproj

Build LDC2

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

  • md build-ldc2-x64
  • cd build-ldc2-x64
  • git clone --recursive git://github.com/ldc-developers/ldc.git ldc
  • Due to LLVM bug #21016, you currently need to add x86utils to the list in file ldc\CMakeLists.txt, line 19 (before the closing parenthesis).
  • Type the next command in one line:

    cmake -G "Visual Studio 12 Win64" -DCMAKE_INSTALL_PREFIX="C:\Program Files\LDC" -DCMAKE_CXX_FLAGS="/DWIN32 /D_HAS_EXCEPTIONS=0 /MP /W2 /GR- /GS- /d2Zi+"
          -DLLVM_ROOT_DIR="C:/Program Files/LLVM-x64"
          -DLIBCONFIG++_INCLUDE_DIR="C:/ldcenv/libconfig-1.4.9/lib"
          -DLIBCONFIG++_LIBRARY="C:/ldcenv/libconfig-1.4.9/Debug/libconfig++.lib" 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.

Build the INSTALL project, e.g., via 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.

Known bugs

  • Structured Exception Handling is only available for LLVM 3.6 head. Support has still alpha quality and is known to crash your application under certain circumstances.
  • 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: Implementation has alpha quality. Only used together with LLVM 3.4.
  • 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.



Windows MSVC