Difference between revisions of "Building under Posix"

From D Wiki
Jump to: navigation, search
(Moved from Starting_as_a_Contributor)
 
(Bootstrap dmd)
 
(23 intermediate revisions by 12 users not shown)
Line 1: Line 1:
 
== Prerequisites ==
 
== Prerequisites ==
  
To build D on POSIX, you will need to have <tt>make</tt>, <tt>g++</tt>, <tt>libcurl4-openssl-dev</tt>, and <tt>git</tt> installed on your system, as well as a working GitHub account. To install the appropriate dependencies on e.g. Ubuntu:
+
To build [[DMD]] on POSIX, you will need to have <tt>make</tt>, <tt>g++</tt>, <tt>libcurl4-openssl-dev</tt>, and <tt>git</tt> 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
 
  sudo apt-get install curl git make g++ libcurl4-openssl-dev
Line 10: Line 10:
 
  $ brew install git openssl
 
  $ brew install git openssl
  
To build the 32-bit phobos on a 64-bit machine, some 32-bit packages are also needed:
+
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 libcurl4-gnutls-dev:i386
+
  sudo apt-get install gcc-multilib g++-multilib libc6-dev-i386 linux-libc-dev:i386 libcurl4-gnutls-dev:i386
  
 
Other versions and variations of <tt>libcurl</tt> may work as well.
 
Other versions and variations of <tt>libcurl</tt> may work as well.
  
== Building D ==
+
== Building DMD ==
  
==== Fetch <tt>dmd</tt> from GitHub ====
+
=== Fetch repositories from GitHub ===
  
Let's start by getting the current development (master) branch of dmd from GitHub. Assume the root directory for everything D-related is <tt>~/code</tt> (replace appropriately). This is easily done by running at a command prompt:
+
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 <tt>~/dlang</tt> (replace appropriately). This is easily done by running at a command prompt:
  
 
<syntaxhighlight lang=bash>
 
<syntaxhighlight lang=bash>
cd ~/code
+
cd ~/dlang
 
git clone https://github.com/dlang/dmd
 
git clone https://github.com/dlang/dmd
 +
git clone https://github.com/dlang/phobos
 +
git clone https://github.com/dlang/tools
 
</syntaxhighlight>
 
</syntaxhighlight>
  
After this step completes successfully, the directory <tt>~/code/dmd</tt> should be up and filled with good stuff.
+
After this step completes successfully, the <tt>~/dlang</tt> should be up and filled with good stuff.
  
==== Bootstrap <tt>dmd</tt> ====
+
=== Bootstrap <tt>dmd</tt> ===
  
This step is interesting because in order to build <tt>dmd</tt>, <tt>dmd</tt> is necessary. Fortunately, the steps of downloading and using a preexisting <tt>dmd</tt> compiler are automated. All you need to do is run this command:
+
This step is interesting because in order to build <tt>dmd</tt>, <tt>dmd</tt> is necessary. Fortunately, the steps of downloading and using a preexisting <tt>dmd</tt> compiler are automated (only for platforms for which [https://dlang.org/download there are official builds: Windows, macOS, linux, and FreeBSD]). All you need to do is run this command:
  
 
<syntaxhighlight lang=bash>
 
<syntaxhighlight lang=bash>
cd ~/code/dmd
+
cd ~/dlang/dmd
make -f posix.mak AUTO_BOOTSTRAP=1
+
make -f posix.mak -j8 AUTO_BOOTSTRAP=1
 
</syntaxhighlight>
 
</syntaxhighlight>
  
That's going to take a while. To make it faster, passing <tt>-j8</tt> accelerates things by running eight processes in parallel. The build produces the compiler binary <tt>~/code/dmd/src/dmd</tt>.
+
That's going to take a while. The build produces the compiler binary situated in an OS-dependent directory such as <tt>~/dlang/dmd/generated/linux/release/64/dmd</tt>.
  
 
To make <tt>dmd</tt> builds faster in the future, you need to obviate the need for bootstrapping. Install <tt>dmd</tt> from the [http://dlang.org/download.html download page] or simply put the freshly built <tt>dmd</tt> binary in a place accessible through <tt>$PATH</tt> (a popular choice is <tt>~/bin</tt>).
 
To make <tt>dmd</tt> builds faster in the future, you need to obviate the need for bootstrapping. Install <tt>dmd</tt> from the [http://dlang.org/download.html download page] or simply put the freshly built <tt>dmd</tt> binary in a place accessible through <tt>$PATH</tt> (a popular choice is <tt>~/bin</tt>).
  
On Windows, you will need
+
=== Bootstrap <tt>dmd</tt> on macOS arm64 ===
  
==== Fetch and build <tt>druntime</tt> ====
+
DMD only creates x86 code, but you can still develop it on macOS arm64. Follow these steps:
 +
# Download LDC universal package. You can use the Dlang install.sh script, and pass <tt>-arch universal</tt>. You need this package to target x86_64, because DMD can only be built into an x86_64 binary (because of x86 assembly in its source code).
 +
# Go to the DMD source folder and execute: <syntaxhighlight lang=bash>HOST_DMD="<path to ldc>/bin/ldmd2" DFLAGS="-mtriple=x86_64-apple-macos11" DMD="<path to ldc>/bin/ldmd2" make </syntaxhighlight>
  
<tt>druntime</tt> is the core runtime library for D, needed for building most every D program, including the standard library itself. So it's the next step in the progression (note that it requires a properly built <tt>dmd</tt>, so make sure the above steps have completed successfully). To fetch and build druntime, issue these commands:
+
=== Build <tt>Phobos</tt> ===
 +
 
 +
Most D programs use D's standard library <tt>Phobos</tt>. To build it, run:
  
 
<syntaxhighlight lang=bash>
 
<syntaxhighlight lang=bash>
cd ~/code
+
cd ~/dlang/phobos
git clone https://github.com/dlang/druntime
+
make -f posix.mak -j8
cd druntime
 
make -f posix.mak
 
 
</syntaxhighlight>
 
</syntaxhighlight>
  
All that should go pretty fast. The somewhat anticlimactic result of the build is a library called <tt>libdruntime.a</tt> situated in an OS-dependent directory such as <tt>~/code/druntime/generated/linux/release/64/</tt>. Make sure it's there. This library is made available to your applications through Phobos, so you must follow the next step to reflect any changes to druntime in your application.
+
The build produces (with similar anticlimacticity) static and shared libraries such as <tt>~/dlang/phobos/generated/linux/release/64/libphobos2.a</tt> and <tt>~/code/phobos/generated/linux/release/64/libphobos2.so</tt>.
  
==== Fetch and build <tt>phobos</tt> ====
+
As part of the build <tt>druntime</tt> will be built automatically, e.g. the generated <tt>druntime</tt> interfaces can be found at <tt>~/dlang/druntime/import</tt>.
 +
The generated <tt>druntime</tt> libraries like <tt>~/dlang/druntime/generated/linux/release/64/libdruntime.a</tt> get bundled with built <tt>phobos</tt> libraries.
  
Most D programs use D's standard library <tt>phobos</tt>. To get and build it, make sure you first fetch and build the latest <tt>dmd</tt> and <tt>druntime</tt>. Then:
+
== Optional build configurations ==
  
 +
The following options are ''optional'' build configuration. You will very likely not need them and can skip to your [https://wiki.dlang.org/Starting_as_a_Contributor#Development development flow].
 +
 +
=== TZDatabaseDir ===
 +
 +
Phobos needs to find the tzdata dir on Posix systems. Normally it is located on standard paths and Phobos uses those. But on some systems such as NixOS for example, there is no such standard path and to build Phobos properly, it's needed to specify the absolute path to the directory.
 +
This is possible by writing the path to a file named <tt>TZDatabaseDirFile</tt> and telling the compiler to use this file as an import for the path via the <tt>DFLAGS</tt> argument listed below. The <tt>-J</tt> argument defines the path where the compiler should look for the <tt>TZDatabaseDirFile</tt>.
 
<syntaxhighlight lang=bash>
 
<syntaxhighlight lang=bash>
cd ~/code
+
cd ~/dlang/phobos
git clone https://github.com/dlang/phobos
+
make -f posix.mak -j8 DFLAGS="-version=TZDatabaseDir -J/tmp/"
cd phobos
+
</syntaxhighlight>
make -f posix.mak
+
 
 +
=== LibcurlPath ===
 +
 
 +
For the same reason as for the tzdata dir, it's possible to specify the absolute path to the libcurl library.
 +
This is possible by writing the path to a file named <tt>LibcurlPathFile</tt> and using the <tt>DFLAGS</tt> argument as in the <tt>TZDatabaseDir</tt> case.
 +
<syntaxhighlight lang=bash>
 +
cd ~/dlang/phobos
 +
make -f posix.mak -j8 DFLAGS="-version=LibcurlPath -J/tmp/"
 +
</syntaxhighlight>
 +
 
 +
== Building <tt>dmd</tt> 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 [https://wiki.winehq.org/Ubuntu here].
 +
 
 +
2. Download [http://dlang.org/download.html the Windows dmd installer]
 +
 
 +
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. Add the optional "DMC" package during the installation.
 +
 
 +
4. 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 : <b>PATH</b>, <b>TEMP</b>, <b>SYSTEM</b>. However, <b>WINEPATH</b> can be used instead of <b>PATH</b>. Assuming that D is installed in C:\, you need to set the following environment variables:
 +
 
 +
<syntaxhighlight lang=bash>
 +
export DM_HOME=C:/D
 +
export HOST_DC=dmd
 +
export WINEPATH="C:\DMC\dm\bin\;C:\D\dmd2\windows\bin"
 +
</syntaxhighlight>
 +
 
 +
5. At this point we should be good to go:
 +
 
 +
<syntaxhighlight lang=bash>
 +
cd /path/to/dmd
 +
wine make -fwin32.mak
 +
</syntaxhighlight>
 +
 
 +
The command above builds the executable <tt>/path/to/dmd/generated/windows/release/32/dmd.exe</tt>.
 +
 
 +
6. To build druntime with the newly generated compiler, run:
 +
 
 +
<syntaxhighlight lang=bash>
 +
cd /path/to/druntime
 +
wine make -fwin32.mak
 +
</syntaxhighlight>
 +
 
 +
7. Finally, to build Phobos again using the newly generated compiler, run:
 +
 
 +
<syntaxhighlight lang=bash>
 +
cd /path/to/phobos
 +
wine make -fwin32.mak
 
</syntaxhighlight>
 
</syntaxhighlight>
  
The build produces (with similar anticlimacticity) static and shared libraries such as <tt>~/code/phobos/generated/linux/release/64/libphobos2.a</tt> and <tt>~/code/phobos/generated/linux/release/64/libphobos2.so</tt>.
+
'''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| starting as a contributor]] guide.
 +
If you want to contribute to Phobos, you may also read the [[Contributing to Phobos|contributing to Phobos guide]].
  
If you want to contribute to Phobos, continue with [[Contributing to Phobos|this guide]]
+
[[Category:DMD Compiler]]

Latest revision as of 11:08, 22 June 2024

Prerequisites

To build DMD 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 g++-multilib libc6-dev-i386 linux-libc-dev:i386 libcurl4-gnutls-dev:i386

Other versions and variations of libcurl may work as well.

Building DMD

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/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).

Bootstrap dmd on macOS arm64

DMD only creates x86 code, but you can still develop it on macOS arm64. Follow these steps:

  1. Download LDC universal package. You can use the Dlang install.sh script, and pass -arch universal. You need this package to target x86_64, because DMD can only be built into an x86_64 binary (because of x86 assembly in its source code).
  2. Go to the DMD source folder and execute:
    HOST_DMD="<path to ldc>/bin/ldmd2" DFLAGS="-mtriple=x86_64-apple-macos11" DMD="<path to ldc>/bin/ldmd2" make
    

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.

Optional build configurations

The following options are optional build configuration. You will very likely not need them and can skip to your development flow.

TZDatabaseDir

Phobos needs to find the tzdata dir on Posix systems. Normally it is located on standard paths and Phobos uses those. But on some systems such as NixOS for example, there is no such standard path and to build Phobos properly, it's needed to specify the absolute path to the directory. This is possible by writing the path to a file named TZDatabaseDirFile and telling the compiler to use this file as an import for the path via the DFLAGS argument listed below. The -J argument defines the path where the compiler should look for the TZDatabaseDirFile.

cd ~/dlang/phobos
make -f posix.mak -j8 DFLAGS="-version=TZDatabaseDir -J/tmp/"

LibcurlPath

For the same reason as for the tzdata dir, it's possible to specify the absolute path to the libcurl library. This is possible by writing the path to a file named LibcurlPathFile and using the DFLAGS argument as in the TZDatabaseDir case.

cd ~/dlang/phobos
make -f posix.mak -j8 DFLAGS="-version=LibcurlPath -J/tmp/"

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

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. Add the optional "DMC" package during the installation.

4. 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. However, WINEPATH can be used instead of PATH. Assuming that D is installed in C:\, you need to set the following environment variables:

export DM_HOME=C:/D
export HOST_DC=dmd
export WINEPATH="C:\DMC\dm\bin\;C:\D\dmd2\windows\bin"

5. 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.

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

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

7. Finally, to build Phobos again using the newly generated compiler, run:

cd /path/to/phobos
wine make -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.