Difference between revisions of "Building under Posix"

From D Wiki
Jump to: navigation, search
(Building dmd for Windows on Linux)
(Building dmd for Windows on Linux)
Line 99: Line 99:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
The command above builds the executable <tt>/path/to/dmd/generated/Windows_NT/release/32/dmd.exe</tt>.  
+
The command above builds the executable <tt>/path/to/dmd/generated/windows/release/32/dmd.exe</tt>.  
  
 
7. To build druntime with the newly generated compiler, run:
 
7. To build druntime with the newly generated compiler, run:
Line 105: Line 105:
 
<syntaxhighlight lang=bash>
 
<syntaxhighlight lang=bash>
 
cd /path/to/druntime
 
cd /path/to/druntime
wine make DMD=/path/to/dmd/generated/Windows_NT/release/32/dmd.exe -fwin32.mak
+
wine make DMD=/path/to/dmd/generated/windows/release/32/dmd.exe -fwin32.mak
 
</syntaxhighlight>
 
</syntaxhighlight>
  
Line 112: Line 112:
 
<syntaxhighlight lang=bash>
 
<syntaxhighlight lang=bash>
 
cd /path/to/phobos
 
cd /path/to/phobos
wine make DMD=/path/to/dmd/generated/Windows_NT/release/32/dmd.exe -fwin32.mak
+
wine make DMD=/path/to/dmd/generated/windows/release/32/dmd.exe -fwin32.mak
 
</syntaxhighlight>
 
</syntaxhighlight>
  

Revision as of 10:49, 4 October 2017

Prerequisites

To build D on POSIX, you will need to have make, g++, libcurl4-openssl-dev, and git installed on your system, as well as a working GitHub account. To install the appropriate dependencies on e.g. Ubuntu:

sudo apt-get install curl git make g++ libcurl4-openssl-dev

On OS X with Homebrew:

$ xcode-select --install
$ brew install git openssl

To build the 32-bit phobos on a 64-bit machine, some 32-bit packages are also needed:

sudo apt-get install gcc-multilib libc6-dev-i386 linux-libc-dev:i386 libcurl4-gnutls-dev:i386

Other versions and variations of libcurl may work as well.

Building D

Fetch repositories from GitHub

Let's start by getting the current development (master) branch of the D repositories from GitHub. Assume the root directory for everything D-related is ~/dlang (replace appropriately). This is easily done by running at a command prompt:

cd ~/dlang
git clone https://github.com/dlang/dmd
git clone https://github.com/dlang/druntime
git clone https://github.com/dlang/phobos
git clone https://github.com/dlang/tools

After this step completes successfully, the ~/dlang should be up and filled with good stuff.

Bootstrap dmd

This step is interesting because in order to build dmd, dmd is necessary. Fortunately, the steps of downloading and using a preexisting dmd compiler are automated (only for platforms for which there are official builds: Windows, macOS, linux, and FreeBSD). All you need to do is run this command:

cd ~/dlang/dmd
make -f posix.mak -j8 AUTO_BOOTSTRAP=1

That's going to take a while. The build produces the compiler binary situated in an OS-dependent directory such as ~/dlang/dmd/generated/linux/release/64/dmd.

To make dmd builds faster in the future, you need to obviate the need for bootstrapping. Install dmd from the download page or simply put the freshly built dmd binary in a place accessible through $PATH (a popular choice is ~/bin).

Build phobos

Most D programs use D's standard library phobos. To build it, run:

cd ~/dlang/phobos
make -f posix.mak -j8

The build produces (with similar anticlimacticity) static and shared libraries such as ~/dlang/phobos/generated/linux/release/64/libphobos2.a and ~/code/phobos/generated/linux/release/64/libphobos2.so.

As part of the build druntime will be built automatically, e.g. the generated druntime interfaces can be found at ~/dlang/druntime/import. The generated druntime libraries like ~/dlang/druntime/generated/linux/release/64/libdruntime.a get bundled with built phobos libraries.

Building dmd for Windows on Linux

Most Linux developers do not have a Windows machine to test their changes on both platforms, fortunately there is a tool called Wine (Windows Emulator) which enables running Windows specific binaries on Linux. In order to be able to build and run the Windows dmd version on Linux, you must go through the following steps: (it is assumed that all the above steps have been completed)

1. Install Wine following the instructions found here.

2. Download the Windows dmd installer and the DigitalMars C++ compiler.

3. Install dmd for Windows using Wine

   wine /path/to/dmd/installer.exe

This command will start the dmd Windows installation as if you were on Windows. Wine creates a typical Windows directory structure located in ~/.wine/drive_c where drive_c is the directory which corresponds to C:\ on Windows. The default installation directory is C:\D, the rest of this document assumes that.

4. After installing dmd, you will need to unzip the C++ compiler (dmc) directly in drive_c so that it will be visible to Wine:

  unzip /path/to/dmc.zip -d ~/.wine/drive_c/

The command above installs dmc under directory ~/.wine/drive_c/dm. The C compiler binary is ~/.wine/drive_c/dm/bin/dmc.exe.

5. Now that the Windows compiler is installed, all we need to do is to properly set the environment variables needed by win32.mak. Wine forwards all the existing environment variables, except a few special ones : PATH, TEMP, SYSTEM which need to be set in a different way. Assuming that D is installed in C:\, you need to set the following environment variables:

export DM_HOME=C:/D
export HOST_DC=dmd

We still need to append to PATH the path to dmc and dmd. We can't modify it as in the case of the variables listed above, because that will alter the Unix environment settings. Instead it should be set in a registry. To set it, type wine regedit and then go to HKEY_CURRENT_USER/Environment. Now you can create or modify the values of the needed variables. In our case:

   "Path" = "C:\dm\bin\;C:\D\dmd2\windows\bin"

Right-click in the right pane of the Windows registry editor and create or modify the string value called "Path" to include the string above. (You could also define DM_HOME and HOST_DC in the same place instead of the command line.)

6. At this point we should be good to go:

cd /path/to/dmd
wine make -fwin32.mak

The command above builds the executable /path/to/dmd/generated/windows/release/32/dmd.exe.

7. To build druntime with the newly generated compiler, run:

cd /path/to/druntime
wine make DMD=/path/to/dmd/generated/windows/release/32/dmd.exe -fwin32.mak

8. Finally, to build phobos again using the newly generated compiler, run:

cd /path/to/phobos
wine make DMD=/path/to/dmd/generated/windows/release/32/dmd.exe -fwin32.mak

NOTE : building the latest development version of the compiler in linux, under wine, requires that the latest windows release version of the compiler is installed via wine.

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.