Building under Windows
Contents
Automation
Digger can checkout and build any D version automatically for you. Only a D compiler is required. Digger will download all required dependencies by itself.
dub fetch digger
dub run digger
Among many other features, Digger can build older compiler versions or versions with a specific pull request added:
# build a specific D version
$ dub run digger -- build v2.064.2
# build for x86-64
$ dub run digger -- build --model=64 v2.064.2
# build commit from a point in time
$ dub run digger -- build "@ 3 weeks ago"
# build specified branch from a point in time
$ dub run digger -- build "2.065 @ 3 weeks ago"
# build with an added pull request
$ dub run digger -- build "master + dmd#123"
Building with Visual Studio
Assuming that you already have installed Visual Studio, make sure that you have installed some Windows SDK. On Visual Studio 2017, the easiest way is to open the Visual Studio Installer, go to 'Modify' on the little drop-down, open the 'Desktop Development with C++', and check any of the 'Windows 10 SDK' (it just has different versions) or 'Windows 8.1 SDK and UCRT SDK'. On other VS versions it should be similar. Now, you should have either a '8.1' or '10' folder in C:\Program Files (x86)\Windows Kits.
Next, download and install a release version of dmd (which you might also see referred as the standard installation, or the host compiler). I will assume that it is installed in C:\D\dmd2, so if you installed it somewhere else, you have to adjust paths in the following steps.
Now, you need to install the visual D plugin, so that you can build and debug D programs in visual studio.
You can go here and download the latest version: http://www.dsource.org/projects/visuald/browser/downloads
You can read installation info here: http://www.dsource.org/projects/visuald/wiki/Installation
When setting up visual D, at some point it will ask you to point to the standard dmd installation,
so just point to the folder you installed it.
Before proceeding, be sure that visual D, host dmd and VS work together. Try to compile a couple of D programs in VS.
Next step is to download the 4 necessary source repos, dmd, druntime, phobos, and tools. I recommend not putting them under your user folder because for me, it might have been a problem that my username was in Greek and cmd could not handle it properly. That's probably not true, but until it is cross-checked, you might want to put them for example in C:\Source\D (again, if you don't put them there, you just have to adjust paths).
Building DMD
You should be able to build DMD using the visual studio solution found in: C:\Source\D\dmd\src\vcbuild
For the next steps, I will assume that you are compiling the 64-bit, debug version. That means on the 2 drop-downs on the top,
you have selected 'Debug' instead of 'Release' and 'x64' instead of 'Win32'. For this guide, not having selected those
just means that you will have to use different paths. If you have 32-bit machine, select 'Win32' and 'Debug' and adjust
paths accordingly. So, hit 'Local Windows Debugger' to compile it (and run it).
Doing that, in the folder: C:\Source\D\dmd\generated\Windows\Debug\x64
there should be the 'dmd.exe' executable.
Now, we have to build the druntime and phobos. Again, in all the instructions below, if you want the 32-bit counterparts, you will have to adjust accordinly. One note is that instead of the win64.mak files, you want to use the win32.mak.
Building DRuntime
First of all, go to: C:\Source\D\druntime. Note: For all the path changes to follow, note that I have the Enterprise version of VS, 14.16.27023. If any of those 2 don't match yours, use the paths specific to your case.
There, there should be a win64.mak file. Open it and adjust the following variables in the following way:
VCDIR=C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\VC\Tools\MSVC\14.16.27023
SDKDIR=C:\Program Files (x86)\Windows Kits\10 (you want 8.1 if you installed 8.1 SDK)
DMD=C:\D\dmd2\windows\bin\dmd (you want to use the host compiler to build druntime, not the just generated one).
CC=$(VCDIR)\bin\Hostx64\x64\cl.exe
LD=$(VCDIR)\bin\Hostx64\x64\link.exe
AR=$(VCDIR)\bin\Hostx64\x64\lib.exe
Finally, if you don't have 'make' installed, we will use the one coming with the host compiler:
MAKE=C:\D\dmd2\windows\bin\make
Also, when I'm writing make
on cmd, you want to use the path above.
Having done all that, just do on cmd: make -f win64.mak
and you should (hopefully) build without errors.
Building Phobos
Then, go to: C:\Source\D\phobos Do the same changes in its own .mak file and build in the same way as above.
Final steps
Now, having done all that, you want to go to: C:\D\dmd2\windows\bin (where the host dmd executable files are located) and copy anything that is not in C:\Source\D\dmd\generated\Windows\Debug\x64, in that same folder (the easiest way to do that is by copying and pasting all of them and hitting 'skip all files' for the duplicates).
Now, then, you want to copy all the other folders (not the 'bin') from: C:\D\dmd2\windows to one level above where the generated dmd.exe is, so in: C:\Source\D\dmd\generated\Windows\Debug
As a final step, go to C:\Source\D\dmd\generated\Windows\Debug\x64\sc.ini (which should be the sc.ini copied from host dmd) and change [Environment] paths. For me, it was:
DFLAGS="-I%@P%\..\..\..\..\..\phobos" "-I%@P%\..\..\..\..\..\druntime\import"
Notes
This is not at all an exhaustive guide, but I hope it helps any newcomers to use VS. I want to first of all thanks rainers for its help and also stress that unfortunately, some things may have to be different for you. For example, rainers' paths look something like this:
CC=c:\Progra~2\Micros~1\2019\Preview\VC\Tools\MSVC\14.20.27607/bin/Hostx64/x64/cl.exe
AR=c:\Progra~2\Micros~1\2019\Preview\VC\Tools\MSVC\14.20.27607/bin/Hostx64/x64/lib.exe
VCDIR=c:\Progra~2\Micros~1\2019\Preview\VC\Tools\MSVC\14.20.27607
Prerequisites
On Windows, you will need Git for Windows, D compiler, the DigitalMars C++ compiler, and, for 64-bit or 32-bit COFF builds, the Microsoft Windows SDK.
Assuming that make, dmc and dmd from digital mars are in your path and your sources are checked out into C:\D\dmd2\src which has the following structure:
C:\D\dmd2\src\dmd
C:\D\dmd2\src\druntime
C:\D\dmd2\src\phobos
C:\D\dmd2\src\tools
git Configuration
When cloning from the GitHub repositories you may encounter problems if your git configuration on Windows replaces line endings with the Windows variant (i.e. \r\n) instead of leaving it as \n, which is what D repositories require.
To check your settings, run git config core.autocrlf. If it prints true, set it to false with git config --global core.autocrlf false.
Building D
The following instructions work for win32. May or may not work with win64. This scheme is a suggestion. These instructions should work when building from a clean repository, however, this repository contains autogenerated code that may be left behind after switching branches so running a git clean after switching branches is a good idea:
git clean -xfd
Building DMD
To build DMD compiler you should run the following commands (win32.mak file expects that HOST_DC variable is set to dmd):
set DM_HOME=C:\D
cd %DM_HOME%\dmd2\src\dmd\src
set HOST_DC=dmd
make -fwin32.mak release
From there, it is suggested to move the built binaries into your %DM_HOME%\windows\bin directory, and add that to your path:
mkdir %DM_HOME%\dmd2\windows\bin
copy *.exe %DM_HOME%\dmd2\windows\bin
set path=%DM_HOME%\dmd2\windows\bin;%path%
From there, you have to create a sc.ini in your %DM_HOME%\dmd2\windows\bin directory (where dmd.exe is). It is suggested to just copy paste the one provided in the packaged 2.110.0, instead of writing your own, like:
copy "C:\Program Files\D\dmd2\windows\bin\sc.ini" %DM_HOME%\dmd2\windows\bin
Building D runtime
Make sure that you are using dmd compiled in previous step because older compiler might not compile the latest druntime code.
To build D runtime you should run the following commands:
cd %DM_HOME%\dmd2\src\druntime
make -fwin32.mak
Building Phobos
Make sure that you are using dmd compiled in previous step because older compiler might not compile the latest phobos code.
To build Phobos you should run the following commands:
cd %DM_HOME%\dmd2\src\phobos
make -fwin32.mak
You should copy phobos lib into your %DM_HOME%\windows\lib folder:
mkdir %DM_HOME%\dmd2\windows\lib
copy phobos.lib %DM_HOME%\dmd2\windows\lib
Building rdmd
Optionally, you can build rdmd from source if you have checked out tools in your sources. Some additional libs might be required for build and can simply be copy pasted from the 2.110.0 package without overwriting your phobos.lib.
To build rdmd you should run the following commands:
cd %DM_HOME%\dmd2\src\tools
make -fwin32.mak rdmd
You should copy rdmd into your %DM_HOME%\windows\bin folder:
mkdir %DM_HOME%\dmd2\windows\lib
copy generated\windows\32\*.exe %DM_HOME%\dmd2\windows\bin
Thirdparty libraries
The last step is getting the additional libs. curl for D2 can be found at the bottom of the download section of dlang.org: [download].
Additional libs that are necessary can simply be copy pasted from the 2.110.0 package (without overwriting your phobos.lib).
Verifying
The very last step is to verify that everything works by unittesting phobos:
cd %DM_HOME%\dmd2\src\phobos
make -fwin32.mak unittest
Common Windows issues
Missing MASM386
If when building druntime you get errors about missing MASM386, it's due to a required assembling of a file called minit.asm. However the druntime repository includes a prebuilt minit.obj file so you shouldn't need to assemble it again. As a workaround for the make error create an empty masm386.bat file and put it in a directory that's in your PATH.
It's also recommended that you use the cmd.exe terminal. Others, like PowerShell, are known to experience issues with legacy tools.
Intermediate files lead to several errors
The three main components (dmd, druntime, phobos) should always be built together with matching versions. The intermediate files generated by a previous build can lead to a failure so it's advisable to run
make -fwin32.mak clean
on each component before starting the process.
Using DigitalMars make
for running the test suite
DMD's testsuite can't be run with DigitalMars make
- use GNUMake.
Refer to the instructions in Running the test suite on Windows.
Where to go from here
If you want to contribute to a D project, please continue with the starting as a contributor guide. If you want to contribute to Phobos, you may also read the contributing to Phobos guide.