Difference between revisions of "Building LDC from source"

From D Wiki
Jump to: navigation, search
m (Prerequisites: we require LLVM 3.5+ now)
m (Advice)
(20 intermediate revisions by 5 users not shown)
Line 1: Line 1:
This page gives an overview of what is required to build and install [[LDC]] on most Posix-like systems such as Linux or OS X. For building LDC on Windows, please see [[Building and hacking LDC on Windows using MSVC|the dedicated page]].
+
This page shows you how to build and install [[LDC]] on most Posix-like systems such as linux, macOS, BSD, or Android. For building LDC on Windows, please see [[Building and hacking LDC on Windows using MSVC|its dedicated page]].
 +
 
 +
== Advice ==
 +
It is hard for us to keep these wiki pages up-to-date. If you run into trouble, have a look at the build scripts for our [http://wiki.dlang.org/LDC_contributor%27s_guide#Continuous_Integration Continuous Integration] platforms: the files <tt>.circleci/config.yml</tt> (Ubuntu Linux and macOS) and <tt>appveyor.yml</tt> (Windows) are always up-to-date with the latest build setup.
  
 
== Prerequisites ==
 
== Prerequisites ==
Line 5: Line 8:
 
* Git (for fetching the source code, if not using a tarball)
 
* Git (for fetching the source code, if not using a tarball)
 
* a C++ toolchain (GCC, Clang, …)
 
* a C++ toolchain (GCC, Clang, …)
 +
* a D compiler (currently LDC and DMD are supported)
 +
** If you don't have a D compiler installed: LDC 0.17 is the last version that does not need a D compiler to be built. Thus for bootstrapping, you can first build 0.17, and then use that to build newer compiler versions. Our testing infrastructure explicitly tests that new LDC versions can be built with 0.17. The git branch is called <tt>ltsmaster</tt> or [https://github.com/ldc-developers/ldc/releases you can get the source for the latest 0.17 release].
 
* CMake 2.8+
 
* CMake 2.8+
* LLVM 3.5+
+
* Make or Ninja
* libconfig++ and its header files (the <tt>-devel</tt> or <tt>-dev</tt> package for some Linux distributions, on OSX: <tt>sudo port install libconfig-hr</tt>)
+
* LLVM 3.7+
* libcurl-dev for building the D2 standard library and tests (various versions available, e.g. libcurl4-gnutls-dev on Ubuntu)
+
* For 0.17 ltsmaster, you need libconfig++ and its header files
 +
** Get the <tt>libconfig-devel</tt> or <tt>libconfig-dev</tt> package for some Linux distributions. On OSX, <tt>sudo port install libconfig-hr</tt>); on Android with the Termux app, <tt>apt install libconfig-dev</tt>
 +
* libcurl-dev for building the Phobos standard library and tests (various versions available, e.g. libcurl4-gnutls-dev on Ubuntu)
 
* libedit-dev
 
* libedit-dev
 
* zlib-dev (e.g. zlib1g-dev on Ubuntu)
 
* zlib-dev (e.g. zlib1g-dev on Ubuntu)
 
Please check the LLVM page on [http://www.llvm.org/docs/GettingStarted.html#broken-versions-of-gcc-and-other-tools broken versions of GCC and other tools] to make sure your toolchain is not known to be bad.
 
  
 
=== LLVM ===
 
=== LLVM ===
  
Many Linux distributions already provide recent binary LLVM packages, sometimes in the form of user-curated package repositories (PPA, …). If an recent LLVM pacakge is available, you might prefer to use it, as LLVM is a rather big project to build.
+
Many Linux distributions already provide recent binary LLVM packages, sometimes in the form of user-curated package repositories (PPA, …). If a recent LLVM package is available, you might prefer to use it, as LLVM is a rather big project to build.  Only the Android target requires building [https://github.com/ldc-developers/llvm our lightly tweaked version of LLVM], which is what we'll use here.
  
 
==== Building LLVM manually ====
 
==== Building LLVM manually ====
  
Download [http://llvm.org/releases/3.5.0/llvm-3.5.0.src.tar.xz LLVM 3.5.0] with <tt>$ wget http://llvm.org/releases/3.5.0/llvm-3.5.0.src.tar.xz</tt>, extract the archive, and then run:
+
We try to keep LDC up-to-date with LLVM trunk, but the latest official release is recommended for the least amount of trouble (with LLVM trunk, you will have to recompile LLVM often).  Download [https://github.com/ldc-developers/llvm/releases/tag/ldc-v5.0.1/ our lightly tweaked version of the last official release, 5.0.1], extract the archive, configure it, and build:
 +
 
 +
<syntaxhighlight lang=bash>
 +
curl -L -O https://github.com/ldc-developers/llvm/releases/download/ldc-v5.0.1/llvm-5.0.1.src.tar.xz
 +
tar xf llvm-5.0.1.src.tar.xz
 +
cd llvm-5.0.1.src/
 +
mkdir build && cd build/
 +
 
 +
cmake -GNinja .. -DCMAKE_BUILD_TYPE=Release -DLLVM_TARGETS_TO_BUILD="X86;AArch64;ARM;PowerPC;NVPTX" -DLLVM_BUILD_TOOLS=OFF -DLLVM_BUILD_UTILS=OFF # remove -GNinja to use Make instead
 +
ninja all llvm-config
 +
 
 +
cd ../../
 +
</syntaxhighlight>
 +
 
 +
If you are planning to work on LDC itself, you might want to install a debug build of LLVM instead by using <tt>-DCMAKE_BUILD_TYPE=Debug</tt>. Warning: This leads to a ''heavy'' slowdown!
  
<pre>$ ./configure --enable-optimized --disable-assertions --enable-targets=x86
+
If you are building natively in Termux for Android, you'll want to add <tt>-DLLVM_DEFAULT_TARGET_TRIPLE=armv7-none-linux-android</tt>, because CMake cannot detect the Android platform yet.
$ make  # add -j&lt;n&gt; as appropriate depending on your system
 
$ sudo make install</pre>
 
If you are planning to work on LDC itself, you might want to install a debug build of LLVM instead by using <tt>./configure --enable-assertions</tt>. Warning: This leads to a ''heavy'' slowdown!
 
  
 
== LDC ==
 
== LDC ==
  
Now, your system should be ready to build and install LDC from source.
+
Now that you're ready to build and install LDC from source, clone the [https://github.com/ldc-developers/ldc LDC GitHub repository] or get [https://github.com/ldc-developers/ldc/releases one of our official source releases]:
 
 
First, clone the [https://github.com/ldc-developers/ldc LDC GitHub repository]:
 
 
<pre>$ git clone --recursive https://github.com/ldc-developers/ldc.git</pre>
 
<pre>$ git clone --recursive https://github.com/ldc-developers/ldc.git</pre>
  
If you're behind a company firewall and cloning of the submodules fail, first configure git to use a different protocol, ex https:
+
If you're behind a company firewall and cloning of the submodules fails, first configure git to use a different protocol, ex https:
 
<pre>$ git config --global url."https://github".insteadOf git://github</pre>
 
<pre>$ git config --global url."https://github".insteadOf git://github</pre>
  
If you already have a local copy of the source tree, don’t forget to make sure your submodules are up to date by running <tt>git submodule update --init</tt>.
+
If you already have the git repo, don’t forget to make sure your submodules are up to date by running <tt>git submodule update --init</tt>.
 +
 
 +
Run the following commands to configure and build ldc and its runtime libraries (see the list of useful CMake switches below):
 +
 
 +
<syntaxhighlight lang=bash>
 +
cd ldc
 +
 
 +
# Make a working directory for the build (name/path arbitrary).
 +
mkdir build && cd build
 +
 
 +
# If host D compiler is not on path, explicitly specify dmd/ldmd2
 +
# (not required for ltsmaster/0.17.x).
 +
export DMD=/path/to/your/dmd2/bin/dmd
 +
 
 +
# Run CMake, giving path to top-level source directory. (Remove
 +
# -G Ninja to use default generator, i.e. make.)
 +
cmake -G Ninja -DLLVM_CONFIG=../../llvm-5.0.1.src/build/bin/llvm-config ..
 +
 
 +
# Build and install LDC. Use -j<n> to limit parallelism if running out of memory.
 +
ninja
 +
sudo ninja install
 +
</syntaxhighlight>
 +
The last step is optional; instead of installing it to the system, you can also choose to run LDC from the <tt>bin/</tt> directory in your CMake working tree.  If you're using an LLVM already installed in your system instead of the tweaked version we just compiled, you don't need to specify <tt>-DLLVM_CONFIG=..</tt>, because our CMake config should pick it up automatically.  If you want to target Android and are building ldc 1.4 or later, add <tt>-DLDC_TARGET_PRESET=Android-arm</tt> to the CMake config.
 +
 
 +
If cross-compiling the runtime libraries, you'll need to specify the C cross-compiler before running CMake
 +
 
 +
<pre>$ export CC=/home/david/android-ndk-r16b/toolchains/llvm/prebuilt/linux-x86_64/bin/clang</pre>
  
Then, just run the following commands as usual (see the list of useful CMake switches below):
+
and pass any C, D, or linker flags you need to CMake:
  
<pre>cd ldc
+
<pre>-DRT_CFLAGS="-target armv7-none-linux-gnueabihf -Os" -DD_FLAGS="-w;-mtriple=armv7-none-linux-gnueabihf" -DLD_FLAGS="-target armv7-none-linux-gnueabihf -fpie -pie"</pre>
mkdir build &amp;&amp; cd build  # arbitrary working directory
 
cmake ..
 
make  # -j&lt;n&gt; as appropriate
 
sudo make install</pre>
 
The last step is optional; instead of installing it to the system, you can also choose to run LDC from the <tt>bin/</tt> directory in your CMake working tree.
 
  
 
=== Useful CMake variables ===
 
=== Useful CMake variables ===
Line 53: Line 88:
 
* '''CMAKE_INSTALL_PREFIX''': The installation prefix, <tt>/usr/local</tt> by default (e.g. <tt>-DCMAKE_INSTALL_PREFIX=/opt/ldc</tt>).
 
* '''CMAKE_INSTALL_PREFIX''': The installation prefix, <tt>/usr/local</tt> by default (e.g. <tt>-DCMAKE_INSTALL_PREFIX=/opt/ldc</tt>).
 
* '''INCLUDE_INSTALL_DIR''': The location the D modules for druntime and Phobos are installed to.
 
* '''INCLUDE_INSTALL_DIR''': The location the D modules for druntime and Phobos are installed to.
* '''RUNTIME_DIR''', '''PHOBOS2_DIR''': By default, druntime and Phobos are expected in <tt>runtime/</tt> as Git submodules. Should circumstances require it, these paths can be changed by setting the variables accordingly.
+
* '''RUNTIME_DIR''', '''PHOBOS2_DIR''': By default, druntime and Phobos are expected in <tt>runtime/</tt> as git submodules. Should circumstances require it, these paths can be changed by setting the variables accordingly.
* '''LLVM_ROOT_DIR''' and '''LLVM_CONFIG''': Allows you to specify the LLVM instance to use. LLVM_CONFIG specifies the path and name of the <tt>llvm-config</tt> binary to use. By default, it is assumed to be <tt>${LLVM_ROOT_DIR}/bin/llvm-config</tt>, otherwise it is searched for on default system paths.
+
* '''LLVM_ROOT_DIR''' and '''LLVM_CONFIG''': Allows you to specify the LLVM instance to use. LLVM_CONFIG specifies the path and name of the <tt>llvm-config</tt> binary to use. By default, it is assumed to be <tt>${LLVM_ROOT_DIR}/bin/llvm-config</tt>, otherwise it is searched for on default system paths. EDIT: https://github.com/ldc-developers/ldc/issues/1928#issuecomment-268421779 suggests we should just use `ccmake -DLLVM_ROOT_DIR=$homebrew_D/ ..` on ubuntu 14.04 even if /usr/bin/llvm-config-3.8 is available
* '''LIBCONFIG_LIBRARY''' and '''LIBCONFIG_INCLUDE_DIR''': These variables can be used to specify the location of the libconfig++ library files and the path to the corresponding header files.
+
* '''LIBCONFIG_LIBRARY''' and '''LIBCONFIG_INCLUDE_DIR''': Only for 0.17 ltsmaster, these variables can be used to specify the location of the libconfig++ library files and the path to the corresponding header files. NOTE: on error Could NOT find LibConfig (missing: LIBCONFIG_INCLUDE_DIR LIBCONFIG_LIBRARY) and using brew, use for eg: CMAKE_PREFIX_PATH=`brew --prefix` cmake .. [see https://github.com/ldc-developers/ldc/issues/952] or use `sudo apt-get install libconfig++`
 +
* '''D_COMPILER''': path to prebuilt D compiler, needed for anything newer than 0.17 ltsmaster
 +
 
 +
NOTE: see https://github.com/Linuxbrew/homebrew-core/blob/master/Formula/ldc.rb for brew's install
  
 
== Tips ==
 
== Tips ==
  
The Makefiles generated by CMake respect the <tt>DESTDIR</tt> variable for the <tt>install</tt> target. It is prepended to all the file installation targets. This can be useful for building packages: <tt>$ make install DESTDIR=&lt;your root directory&gt;</tt>
+
The Makefiles generated by CMake respect the <tt>DESTDIR</tt> variable for the <tt>install</tt> target. It is prepended to all the file installation targets. This can be useful for building packages:
  
 +
<tt>$ make install DESTDIR=&lt;your root directory&gt;</tt>
  
 
[[Category:LDC]]
 
[[Category:LDC]]

Revision as of 23:02, 15 April 2018

This page shows you how to build and install LDC on most Posix-like systems such as linux, macOS, BSD, or Android. For building LDC on Windows, please see its dedicated page.

Advice

It is hard for us to keep these wiki pages up-to-date. If you run into trouble, have a look at the build scripts for our Continuous Integration platforms: the files .circleci/config.yml (Ubuntu Linux and macOS) and appveyor.yml (Windows) are always up-to-date with the latest build setup.

Prerequisites

  • Git (for fetching the source code, if not using a tarball)
  • a C++ toolchain (GCC, Clang, …)
  • a D compiler (currently LDC and DMD are supported)
    • If you don't have a D compiler installed: LDC 0.17 is the last version that does not need a D compiler to be built. Thus for bootstrapping, you can first build 0.17, and then use that to build newer compiler versions. Our testing infrastructure explicitly tests that new LDC versions can be built with 0.17. The git branch is called ltsmaster or you can get the source for the latest 0.17 release.
  • CMake 2.8+
  • Make or Ninja
  • LLVM 3.7+
  • For 0.17 ltsmaster, you need libconfig++ and its header files
    • Get the libconfig-devel or libconfig-dev package for some Linux distributions. On OSX, sudo port install libconfig-hr); on Android with the Termux app, apt install libconfig-dev
  • libcurl-dev for building the Phobos standard library and tests (various versions available, e.g. libcurl4-gnutls-dev on Ubuntu)
  • libedit-dev
  • zlib-dev (e.g. zlib1g-dev on Ubuntu)

LLVM

Many Linux distributions already provide recent binary LLVM packages, sometimes in the form of user-curated package repositories (PPA, …). If a recent LLVM package is available, you might prefer to use it, as LLVM is a rather big project to build. Only the Android target requires building our lightly tweaked version of LLVM, which is what we'll use here.

Building LLVM manually

We try to keep LDC up-to-date with LLVM trunk, but the latest official release is recommended for the least amount of trouble (with LLVM trunk, you will have to recompile LLVM often). Download our lightly tweaked version of the last official release, 5.0.1, extract the archive, configure it, and build:

curl -L -O https://github.com/ldc-developers/llvm/releases/download/ldc-v5.0.1/llvm-5.0.1.src.tar.xz
tar xf llvm-5.0.1.src.tar.xz
cd llvm-5.0.1.src/
mkdir build && cd build/

cmake -GNinja .. -DCMAKE_BUILD_TYPE=Release -DLLVM_TARGETS_TO_BUILD="X86;AArch64;ARM;PowerPC;NVPTX" -DLLVM_BUILD_TOOLS=OFF -DLLVM_BUILD_UTILS=OFF # remove -GNinja to use Make instead
ninja all llvm-config

cd ../../

If you are planning to work on LDC itself, you might want to install a debug build of LLVM instead by using -DCMAKE_BUILD_TYPE=Debug. Warning: This leads to a heavy slowdown!

If you are building natively in Termux for Android, you'll want to add -DLLVM_DEFAULT_TARGET_TRIPLE=armv7-none-linux-android, because CMake cannot detect the Android platform yet.

LDC

Now that you're ready to build and install LDC from source, clone the LDC GitHub repository or get one of our official source releases:

$ git clone --recursive https://github.com/ldc-developers/ldc.git

If you're behind a company firewall and cloning of the submodules fails, first configure git to use a different protocol, ex https:

$ git config --global url."https://github".insteadOf git://github

If you already have the git repo, don’t forget to make sure your submodules are up to date by running git submodule update --init.

Run the following commands to configure and build ldc and its runtime libraries (see the list of useful CMake switches below):

cd ldc

# Make a working directory for the build (name/path arbitrary).
mkdir build && cd build

# If host D compiler is not on path, explicitly specify dmd/ldmd2
# (not required for ltsmaster/0.17.x).
export DMD=/path/to/your/dmd2/bin/dmd

# Run CMake, giving path to top-level source directory. (Remove
# -G Ninja to use default generator, i.e. make.)
cmake -G Ninja -DLLVM_CONFIG=../../llvm-5.0.1.src/build/bin/llvm-config ..

# Build and install LDC. Use -j<n> to limit parallelism if running out of memory.
ninja
sudo ninja install

The last step is optional; instead of installing it to the system, you can also choose to run LDC from the bin/ directory in your CMake working tree. If you're using an LLVM already installed in your system instead of the tweaked version we just compiled, you don't need to specify -DLLVM_CONFIG=.., because our CMake config should pick it up automatically. If you want to target Android and are building ldc 1.4 or later, add -DLDC_TARGET_PRESET=Android-arm to the CMake config.

If cross-compiling the runtime libraries, you'll need to specify the C cross-compiler before running CMake

$ export CC=/home/david/android-ndk-r16b/toolchains/llvm/prebuilt/linux-x86_64/bin/clang

and pass any C, D, or linker flags you need to CMake:

-DRT_CFLAGS="-target armv7-none-linux-gnueabihf -Os" -DD_FLAGS="-w;-mtriple=armv7-none-linux-gnueabihf" -DLD_FLAGS="-target armv7-none-linux-gnueabihf -fpie -pie"

Useful CMake variables

  • LIB_SUFFIX: Some Linux distributions, such as Fedora, expect 64 bit libraries in /usr/lib64 instead of /usr/lib. In this case, the installation directory can be adjusted using -DLIB_SUFFIX=64.
  • CMAKE_INSTALL_PREFIX: The installation prefix, /usr/local by default (e.g. -DCMAKE_INSTALL_PREFIX=/opt/ldc).
  • INCLUDE_INSTALL_DIR: The location the D modules for druntime and Phobos are installed to.
  • RUNTIME_DIR, PHOBOS2_DIR: By default, druntime and Phobos are expected in runtime/ as git submodules. Should circumstances require it, these paths can be changed by setting the variables accordingly.
  • LLVM_ROOT_DIR and LLVM_CONFIG: Allows you to specify the LLVM instance to use. LLVM_CONFIG specifies the path and name of the llvm-config binary to use. By default, it is assumed to be ${LLVM_ROOT_DIR}/bin/llvm-config, otherwise it is searched for on default system paths. EDIT: https://github.com/ldc-developers/ldc/issues/1928#issuecomment-268421779 suggests we should just use `ccmake -DLLVM_ROOT_DIR=$homebrew_D/ ..` on ubuntu 14.04 even if /usr/bin/llvm-config-3.8 is available
  • LIBCONFIG_LIBRARY and LIBCONFIG_INCLUDE_DIR: Only for 0.17 ltsmaster, these variables can be used to specify the location of the libconfig++ library files and the path to the corresponding header files. NOTE: on error Could NOT find LibConfig (missing: LIBCONFIG_INCLUDE_DIR LIBCONFIG_LIBRARY) and using brew, use for eg: CMAKE_PREFIX_PATH=`brew --prefix` cmake .. [see https://github.com/ldc-developers/ldc/issues/952] or use `sudo apt-get install libconfig++`
  • D_COMPILER: path to prebuilt D compiler, needed for anything newer than 0.17 ltsmaster

NOTE: see https://github.com/Linuxbrew/homebrew-core/blob/master/Formula/ldc.rb for brew's install

Tips

The Makefiles generated by CMake respect the DESTDIR variable for the install target. It is prepended to all the file installation targets. This can be useful for building packages:

$ make install DESTDIR=<your root directory>