Building DMD

From D Wiki
Revision as of 16:58, 2 July 2014 by Vladimir Panteleev (talk | contribs) (Undo revision 4234 by Kashyap (talk) - The old path was correct, it corresponds to the structure of the ZIP file.)
Jump to: navigation, search

If you're looking for a stable version of D, you probably want to download the official releases. This page is for those who want to try out D on platforms that aren't yet officially supported, those who are adventurous and wish to try out the latest development (unstable!) version of D, and developers who wish to contribute to D development.

Existing tools

There exist tools which can do some of the below steps automatically:

  • Digger - can download and build D from any point in its recent history.
  • DVM - can build and locally install D from source code.

Getting the sources

Official releases

The official release of DMD is available from the official download page.

Latest git

This is for those who want to test or contribute to the development version of D. The latest source code for the D compiler, runtime library, and standard library are available on GitHub. To build a working D compiler toolchain, you will need to checkout at least dmd, druntime, and phobos.

Source code structure

The D source code assumes a particular directory structure, which you probably would want to adopt so that you don't have to fiddle with the Makefiles all the time.

Posix

For Posix, it is assumed that you will have a common root directory where the compiler and library sources will sit under. For example, you can choose the common root directory to be /usr/src/d, then you can checkout the sources under this directory:

mkdir /usr/src/d
cd /usr/src/d
git clone git://github.com/D-Programming-Language/dmd.git
git clone git://github.com/D-Programming-Language/druntime.git
git clone git://github.com/D-Programming-Language/phobos.git

Optionally, if you want some related tools, you can also checkout tools.git:

git clone git://github.com/D-Programming-Language/tools.git


Note: if you're planning to submit pull requests, you should replace the above URLs with the URLs for your fork of the official sources, not the official sources themselves.

You should end up with this directory structure:

/usr/src/d/
/usr/src/d/dmd
/usr/src/d/druntime
/usr/src/d/phobos
(/usr/src/d/tools)

Windows

For windows, you can follow the first posix steps regarding checking out files from github.

You can checkout the sources wherever you like. If we call %DM_HOME% the root path, the it is recommended to have this structure:

%DM_HOME%\dmd2\src
%DM_HOME%\dmd2\src\dmd
%DM_HOME%\dmd2\src\druntime
%DM_HOME%\dmd2\src\phobos

Additionally, you should extract the digital mars compiler inside %DM_HOME%, alongside dmd2. You should finally create a windows directory with a bin and lib directory inside it. Your final structure should look like this:

%DM_HOME%\dm
%DM_HOME%\dmd2\src
%DM_HOME%\dmd2\src\dmd
%DM_HOME%\dmd2\src\druntime
%DM_HOME%\dmd2\src\phobos
%DM_HOME%\dmd2\windows
%DM_HOME%\dmd2\windows\bin
%DM_HOME%\dmd2\windows\lib

Building the sources

Posix

See also Alex Rønne Petersen's blog post on building DMD (a dead link, author has been queried via e-mail; awaiting response)

Assuming your sources are checked out in /usr/src/d, you can do the following to build them:

cd /usr/src/d/dmd/src
make -f posix.mak
cd ../../druntime
make -f posix.mak DMD=../dmd/src/dmd
cd ../phobos
make -f posix.mak DMD=../dmd/src/dmd

Note that the compiler, runtime library, and standard library have to be built in that order, as each depends on the previous one. The addition of the build option DMD=../dmd/src/dmd ensures that your newly-built dmd is being used to build druntime and phobos.

If you're using a 64-bit platform, you may want to append MODEL=64 to your make commands, as the default makefiles will build for 32-bit:

cd /usr/src/d/dmd/src
make -f posix.mak MODEL=64
cd ../../druntime
make -f posix.mak MODEL=64 DMD=../dmd/src/dmd
cd ../phobos
make -f posix.mak MODEL=64 DMD=../dmd/src/dmd

Parallel make can drastically speed up compilation times. The -j<integer> option allows you to specify the number of job slots. Number_of_cores + 1 is a often a good choice E.g.:

make -f posix.mak -j5

for a machine with 4 cores.

After building, you should have a working D compiler in /usr/src/d/dmd/src/dmd. You may need to edit dmd.conf so that the compiler can find druntime and phobos. Your dmd.conf should contain:

[Environment]

DFLAGS=-I/path/to/src/phobos -I/path/to/src/druntime/import -L-L/path/to/libs -L--no-warn-search-mismatch -L--export-dynamic

Where the first two flags (-I) must be followed by the path to src/phobos et src/druntime/import. The -L-L flag must be followed by the path to libphobos.a (for example: -L-L/usr/local/lib/lib64).

You should probably also run the unittests to make sure your build is working correctly:

cd ../druntime
make -f posix.mak -j5 unittest
cd ../phobos
make -f posix.mak -j5 unittest

(Running the unittests with -j is recommended if you have a multicore CPU, as some of them may take a while to run.)

Installation

posix.mak does not come with an install option (user supplied sample install script here: Attachment:posix-make-install-dmd.sh), but you can copy files manually to an appropriate location such as /usr/local or /opt/dmd. For example:

cd /usr/src/d/dmd/src
mkdir /opt/dmd
mkdir /opt/dmd/bin
cp dmd /opt/dmd/bin

cd ../../druntime
mkdir /opt/dmd/include
mkdir /opt/dmd/include/d2
cp -r import/* /opt/dmd/include/d2

cd ../phobos
mkdir /opt/dmd/lib
cp generated/linux/release/64/libphobos2.a /opt/dmd/lib    # for 64-bit version
cp generated/linux/release/32/libphobos2.a /opt/dmd/lib    # for 32-bit version
cp -r std /opt/dmd/include/d2
cp -r etc /opt/dmd/include/d2

Then, create the following dmd.conf in the /opt/dmd/bin directory:

[Environment]
DFLAGS=-I/opt/dmd/include/d2 -L-L/opt/dmd/lib -L--no-warn-search-mismatch -L--export-dynamic

Note that you will have to add /opt/dmd/bin to your PATH to make use of your newly installed DMD.

Uninstallation is then as simple as removing the /opt/dmd directory.

Windows

The following instructions work for win32. May or may not work with win64. This scheme is a suggestion.

Assuming your sources are checked out C:\D, and that make from digital mars is in your path, you can do the following to build them:

set DM_HOME=C:\D
cd %DM_HOME%\dmd2\src\dmd\src
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:

copy *.exe %DM_HOME%\dmd2\windows\bin
set path=%path%;%DM_HOME%\dmd2\windows\bin

From there, you have to create a sc.ini in your DMD.exe directory. It is suggested to just copy paste the one provided in the packaged D2·110·0, instead of writing your own.

Now build druntime:

cd %DM_HOME%\dmd2\src\druntime
make -fwin32.mak

And phobos:

cd %DM_HOME%\dmd2\src\phobos
make -fwin32.mak

You should copy the phobos lib into your windows\lib folder:

copy phobos.lib %DM_HOME%\dmd2\windows\lib

Optionally, you can build rdmd from source if you have checked out tools in your sources:

cd %DM_HOME%\dmd2\src\tools
make -fwin32.mak rdmd.exe
copy *.exe %DM_HOME%\dmd2\windows\bin

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 D2·110·0 package (without overwriting your phobos.lib)

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

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.

Additional Tools

If you cloned D-Programming-Language/tools.git, you also have a tools folder where small helping programs live. There is no need to build them, you can just compile them using DMD:

dmd rdmd.d;
dmd ddemangle.d;
dmd dtab;
dmd tolf;

rdmd builds your D modules automatically, from the one containing main. It'll deduce dependencies and compile/link them for you. ddemangle will demangle its input, replacing all mangled D symbols with their unmangled form. dtab transforms tabs into spaces in source code. tolf replaces line endings with LF.

Using dtab and tolf is a good idea if you want to contribute to the D-Programming-Language repos.

Building the Docs

git clone https://github.com/D-Programming-Language/tools.git
cd tools
make
cd generated/linux/default
PATH=$PATH:`pwd`
export PATH
cd ../../../..  # you should be in your top D folder

git clone https://github.com/D-Programming-Language/dlang.org.git
cd dlang
make -f posix.mak DMD=../dmd/src/dmd
cd ..
ln -s dlang/web .

cd phobos
make -f posix.mak DMD=../dmd/src/dmd html
cd ..
BROWSER web/phobos-prerelease/std_YOUR_CHOICE.html

Running the test suite

Naturally if you are hacking on the dmd compiler (to fix your favorite bug, right?!), you'll want to run the test suite to ensure that you haven't broken anything.

Windows

On Windows, the test suite requires a very specific environment and careful configuration to run successfully:

  • The PATH must be set up such that link.exe will execute OPTLINK, however the test suite Makefile is written using GNU syntax.
  • The CC variable must be set correspondingly to the C compiler for your Windows version: dmc for 32-bit, cl (Microsoft Visual C++) for 64-bit.
  • To force the makefile to use the 32-bit C compiler on a 64-bit Windows machine, the OS variable must be overridden.

With that, here's how to get the test suite working on Windows:

  • Get and install Cygwin. You'll need the basic packages containing make, mkdir, etc.
  • Create the following batch file in the test directory (dmd\test\run_test_suite.bat):
@echo off
set PATH=C:\dm\bin;C:\cygwin\bin;%WINDIR%\System32;%WINDIR%
set CC=dmc
set MAKE=C:/cygwin/bin/make.exe
%MAKE% OS=win32 quick %*
If necessary, replace the paths to DMC and Cygwin appropriately. Note that the MAKE variable must use forward slashes.
  • Create an sc.ini file in the dmd source directory (dmd\src\sc.ini). This file has the same format as dmd.conf in the instructions for POSIX below. Here is an example:
[Environment]
LIB="%@P%\..\..\phobos"
DFLAGS="-I%@P%\..\..\phobos" "-I%@P%\..\..\druntime\import"
LINKCMD=C:\dm\bin\link.exe
Again, replace C:\dm as appropriate.

This should allow running the test suite on Windows. See the POSIX section below for non-Windows-specific information.

POSIX (Linux, OS X, FreeBSD)

Since it wasn't obvious how to run the test suite, or why it failed the first couple of times, I'll add notes here on how to build on Linux/x86_64. If you are not on Linux and not on 64-bit, then you'll simply have to adjust the lines below. I wanted to be specific to avoid confusion.

How to run the test suite in dmd/test

Location: If dmd/src/ is your source directory (that contains mars.c, impcnvgen.c, dmd_msc.vcproj, posix.mak, etc) then dmd/test is the test suite directory.

Before running: a freshly built dmd at dmd/src/dmd does not have a dmd.conf file. You'll need to make dmd/src/dmd.conf file in order to run the tests.

To run the tests: (this will take a while)

cd dmd/test
make MODEL=64

Common problems and their solutions

a) common error number one: lack of correct dmd.conf, and dmd.conf must point to the installed include files.

If you do not have a dmd.conf in place, you will get error:

Error: cannot find source code for runtime library file 'object.d'
       dmd might not be correctly installed. Run 'dmd -man' for installation instructions.
Specify path to file 'object.d' with -I switch

See the installation section above.

b) common error number two: If you just say 'make' without adding the MODEL designation, you will get the default 32-bit tests on your 64-bit platform. You have to say 'make MODEL=64' in the src/dmd/test dir.

If you are on 64-bit and you do not do the MODEL=64, you will get this error:

Running runnable tests
 ... runnable/A16.d                 (-inline -release -gc -O -fPIC)
Test failed.  The logged output:
../src/dmd -m32 -Irunnable   -odtest_results/runnable -oftest_results/runnable/A16_0 runnable/A16.d runnable/imports/A16a.d
/usr/bin/ld: cannot find -lphobos2
collect2: ld returned 1 exit status
--- errorlevel 1


==============================
Test failed: expected rc == 0, exited with rc == 1

Run the druntime and phobos unit tests

Next run the unit tests for the standard libraries.

cd ../../druntime
make -f posix.mak MODEL=64 unittest
cd ../phobos
make -f posix.mak MODEL=64 unittest

See also: Pull Requests.