HOME -> Swift on Linux

Building Swift on CentOS 6.7 from Source

December 26, 2015

Having tried building Swift from source on CentOS 7.1 (see http://www.swiftprogrammer.info/swift_centos_1.html) and Ubuntu 14.04 (http://www.swiftprogrammer.info/swift_ubuntu_2.html), I decided to try this on an older CentOS version, 6.7. I was inspired by this quesion on StackOverflow: http://stackoverflow.com/questions/34234250/building-swift-on-centos/34280446#34280446. This was not as easy as installing on recent Linux versions because

The build was done on a Microsoft Azure standard D2 instance with 2 cores and 7 GB of RAM.

Prerequisites

This tutorial assumes good working knowledge of the Linux command line, including how to install software from the repository or from source code. The first step was to

Build GCC 4.8.2 from Source

This is well described at https://gcc.gnu.org/wiki/InstallingGCC">https://gcc.gnu.org/wiki/InstallingGCC. GCC 4.8.2 was installed in /opt/gcc-4.8.2/. If you install it elsewhere, the paths in this tutorial will need to be modified accordingly.

Once GCC 4.8.2 was installed, the old version was removed using yum. To enable Clang to use the headers and libraries that came with the new GCC, the following variables were set: export CPLUS_INCLUDE_PATH=/opt/gcc-4.8.2/include/c++/4.8.2:/opt/gcc-4.8.2/include/c++/4.8.2/x86_64-unknown-linux-gnu export LIBRARY_PATH=/opt/gcc-4.8.2/lib64:/opt/gcc-4.8.2/lib/gcc/x86_64-unknown-linux-gnu/4.8.2 export LD_LIBRARY_PATH=/usr/local/lib:/opt/gcc-4.8.2/lib64 For the same reason, namely to allow Clang to work properly, the following files were symlinked from /opt/gcc-4.8.2/lib/gcc/x86_64-unknown-linux-gnu/4.8.2/ to /usr/lib64: crtbegin.o crtbeginS.o crtbeginT.o crtend.o crtendS.o crtfastmath.o crtprec32.o crtprec64.o crtprec80.o /usr/local/lib was added to LD_LIBRARY_PATH to facilitate some dependencies, built from source, that installed their libraries into /usr/local/lib.

And, of course, the location of the new GCC binaries, /opt/gcc-4.8.2/bin/, had to be added to the path. The next step was to

Build CMake from Source

CMake available from the repository is too old, so a newer version needs to be installed. To avoid confusion, it is a good idea to remove, using yum, the older version if it is installed. CMake installation is straightforward. The source is available from https://cmake.org. Please make sure the cmake binary is in your PATH. Building CMake is a good test to see if your newly-installed GCC is functional. Now it's time to

Build Clang from Source

This is well described here: http://www.llvm.org/docs/GettingStarted.html. Please note that you only need LLVM and Clang in this case. Also, you don't have to check the code out of the repository, but instead get the sources from http://www.llvm.org/releases/download.html.

You might also find some useful information at http://www.omniprog.info/cpp.html. Most articles found there deal with more than just building Clang, but nonetheless some information may be helpful.

Make sure the PATH is set up such that your newly-built Clang is used.

Get Dependencies from the Repository

The following dependencies were good enough as installed from the repository using yum. These I had to install: These were already installed, but if your system does not have them, you can install them using yum: I was able to get away without installing swig, even though it is listed as a dependency on the swift.org site.

Install Dependencies from Source

The following had to be built from source and installed:

Building Swift

Procedure for downloading and building Swift is the same as described in the pages mentioned earlier for Ubuntu 14.04 and CentOS 7.1. The result is basically the same as well: The git tag of the Swift source code I used was swift-2.2-SNAPSHOT-2015-12-10-a.

Please feel free to contact me with questions or comments.

© 2015 swiftprogrammer.infoAnatoli Peredera