{"id":2110,"date":"2015-12-12T13:13:22","date_gmt":"2015-12-12T19:13:22","guid":{"rendered":"http:\/\/dev.iachieved.it\/iachievedit\/?p=2110"},"modified":"2020-04-18T16:58:06","modified_gmt":"2020-04-18T21:58:06","slug":"keeping-up-with-open-source-swift","status":"publish","type":"post","link":"https:\/\/dev.iachieved.it\/iachievedit\/keeping-up-with-open-source-swift\/","title":{"rendered":"Building Open Source Swift"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.shields.io\/badge\/Swift-3.0-orange.svg?style=flat\" alt=\"Swift 3.0\" \/> <img decoding=\"async\" src=\"https:\/\/img.shields.io\/badge\/OS-Linux-blue.svg?style=flat\" alt=\"Linux\" \/> <img decoding=\"async\" src=\"https:\/\/img.shields.io\/badge\/Arch-X86-red.svg?style=flat\" alt=\"X86\" \/><\/p>\n<p><b>Editor&#8217;s note:<\/b>  If you&#8217;re looking for a quick way to start working with Swift 3.0 <i>now<\/i>, try out our <a href=\"http:\/\/dev.iachieved.it\/iachievedit\/introducing-swift-3-0\/\">apt-get repository<\/a> for Trusty Tahr, Wily Werewolf, and Xenial Xerus.<\/p>\n<p>It&#8217;s been a little over six months since Apple released its Swift language into the open source community.  Since then its various repositories have quickly climbed into the <a href=\"https:\/\/github.com\/trending?since=monthly\">trending<\/a> pages on Github.  The projects are moving <i>fast<\/i> with thousands of commits a week and features being implemented on a daily basis.  While one can certainly download the official <b>SNAPSHOT<\/b> release from the <a href=\"https:\/\/swift.org\/download\/\">Swift downloads<\/a> page, to stay on the bleeding edge you&#8217;ll want to learn how to compile your own release.<\/p>\n<h2>Getting Started<\/h2>\n<p>To effectively work with building Swift from the Github repositories you&#8217;re going to need a system with a decent amount of CPU and RAM.  I&#8217;m personally using an 8-core desktop with 8G of RAM.  In addition I suggest compiling against an SSD drive with an EXT4 filesystem.  While I trust my long term data storage to <a href=\"https:\/\/en.wikipedia.org\/wiki\/ZFS#Deduplication\">ZFS with deduplication<\/a>, I don&#8217;t want to compile against it.<\/p>\n<p><b>Note:<\/b>  We&#8217;re using Ubuntu 15.10, and Apple also supports 14.04 and now 16.04.  I haven&#8217;t even tried building on a pure Debian release or RedHat installation.<\/p>\n<p>First, let&#8217;s install some prerequisites:<\/p>\n<pre class=\"crayon:false\">\nsudo apt-get install git cmake ninja-build clang uuid-dev libicu-dev icu-devtools libbsd-dev libedit-dev libxml2-dev libsqlite3-dev swig libpython-dev libncurses5-dev pkg-config\n<\/pre>\n<p>If you are on Ubuntu 14.04 you will also need to upgrade to Clang 3.6 <i>and<\/i> <a href=\"https:\/\/dev.iachieved.it\/iachievedit\/upgrading-cmake-for-a-happier-swift-build\/\">upgrade CMake to 3.4.3<\/a>.  Let&#8217;s start with getting <code>clang-3.6<\/code>:<\/p>\n<pre class=\"crayon:false\">\nsudo apt-get install clang-3.6\nsudo update-alternatives --install \/usr\/bin\/clang clang \/usr\/bin\/clang-3.6 100\nsudo update-alternatives --install \/usr\/bin\/clang++ clang++ \/usr\/bin\/clang++-3.6 100\n<\/pre>\n<p>Now we need to check out (sorry, <i>clone<\/i>) all of the repositories needed to &#8220;build Swift&#8221;.  Fortunately the base <code>swift<\/code> repository contains an <code>update-checkout<\/code> script that will clone all of the repositories required:<\/p>\n<pre class=\"crayon:false\">\n# mkdir apple # You will want to do this where you have at least 4-6G of free space\n# cd apple\n# git clone https:\/\/github.com\/apple\/swift.git swift\n# .\/swift\/utils\/update-checkout --clone\n<\/pre>\n<h2>Building<\/h2>\n<p>While not quite as bad as building the Linux kernel, compiling Swift does take a while, particularly the first time.  On my system it took roughly 40 minutes to compile and package everything.  Rebuilding after updating the repository contents took around 7 minutes with no changes to the source, with most of that time spent executing the post-build testcases.<\/p>\n<p>We&#8217;re going to use the <code>buildbot_linux<\/code> preset for the <code>swift\/utils\/build-script<\/code> utility.  The preset is defined in <a href=\"https:\/\/github.com\/apple\/swift\/blob\/master\/utils\/build-presets.ini\">build-presets.ini<\/a>.  Here you can see that it goes through specific build and install stages:<\/p>\n<pre class=\"lang:ini\">\n[preset: buildbot_linux]\nmixin-preset=mixin_linux_installation\nbuild-subdir=buildbot_linux\nlldb\nrelease\ntest\nvalidation-test\nfoundation\n\ndash-dash\n\ninstall-foundation\nreconfigure\n<\/pre>\n<p>Here we go!  In your <code>~\/apple<\/code> directory, or whereever you checked out all of the repositories:<\/p>\n<pre class=\"crayon:false\">\n.\/swift\/utils\/build-script --preset=buildbot_linux_1510 install_destdir=\/tmp\/install installable_package=\/tmp\/swift.tar.gz\n<\/pre>\n<p>You&#8217;ll notice our exact preset was <code>buildbot_linux_1510<\/code>, which for the time being is an alias to <code>buildbot_linux<\/code>.  If you are building on an Ubuntu 16.04 system you can use <code>builtbot_linux_1604<\/code>.  If you prefer you can also choose a different <code>install_destdir<\/code> and <code>installable_package<\/code>.<\/p>\n<p>The output of the packaging step is a standard <code>\/usr\/<\/code> directory layout with:<\/p>\n<table>\n<tr>\n<td><code>\/usr\/bin<\/code><\/td>\n<td>The <code>swift<\/code> compiler, <code>lldb<\/code> debugger, and various other tools get put here.<br \/>\n <\/tr>\n<tr>\n<td><code>\/usr\/include<\/code><\/td>\n<td>For now this contains only <code>lldb<\/code> include headers.<br \/>\n <\/tr>\n<tr>\n<td><code>\/usr\/lib<\/code><\/td>\n<td>All of the libraries, shared and static, that make up the Swift environment are here.  For example, <code>libFoundation.so<\/code>, <code>libswiftCore.so<\/code>, etc.<br \/>\n <\/tr>\n<tr>\n<td><code>\/usr\/local<\/code><\/td>\n<td>The packaging builtbot creates this directory but nothing lies within.<br \/>\n <\/tr>\n<tr>\n<td><code>\/usr\/share<\/code><\/td>\n<td>For now a single man page is included!  Go ahead, try it out with <code>man swift<\/code>.<br \/>\n <\/tr>\n<\/table>\n<p>To test out your new Swift compiler:<\/p>\n<pre class=\"crayon:false\">\ncd \/tmp\/install\n.\/usr\/bin\/swift --version\nSwift version 3.0-dev (LLVM 7bae82deaa, Clang 4b3278bec5, Swift 0cc49187bb)\nTarget: x86_64-unknown-linux-gnu\n<\/pre>\n<p>The <code>3.0-dev<\/code> information is baked into the source code in <a href=\"https:\/\/github.com\/apple\/swift\/blob\/master\/lib\/Basic\/Version.cpp\">Version.cpp<\/a> by gleaning the information from <a href=\"https:\/\/github.com\/apple\/swift\/blob\/master\/CMakeLists.txt\">CMakeLists.txt<\/a>.  The hex strings for LLVM, Clang, and Swift are, you guessed it, the git hash that the binary was built from.<\/p>\n<figure id=\"attachment_2132\" aria-describedby=\"caption-attachment-2132\" style=\"width: 589px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/dev.iachieved.it\/iachievedit\/wp-content\/uploads\/2015\/12\/manswift.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/dev.iachieved.it\/iachievedit\/wp-content\/uploads\/2015\/12\/manswift.png\" alt=\"Swift man page\" width=\"589\" height=\"460\" class=\"size-full wp-image-2132\" \/><\/a><figcaption id=\"caption-attachment-2132\" class=\"wp-caption-text\">Swift man page<\/figcaption><\/figure>\n<h3>Updating and Rebuilding<\/h3>\n<p>To keep current with the latest submissions to the repositories you can use the <code>update-checkout<\/code> helper script:<\/p>\n<pre class=\"crayon:false\">\n.\/swift\/utils\/update-checkout --all\n<\/pre>\n<p>After updating to rebuild (without cleaning) use the previous command:<\/p>\n<pre class=\"crayon:false\">\n.\/swift\/utils\/build-script --preset=buildbot_linux_1510 install_destdir=\/tmp\/install installable_package=\/tmp\/swift.tar.gz\n<\/pre>\n<p>To run a <code>distclean<\/code> of sorts just remove the <code>build\/buildbot_linux<\/code> directory that was created in your top-level directory, or you can selectively remove subdirectories within <code>buildbot_linux<\/code> to retrigger a build of that component.  For example, if we want to clean out the artifacts for Foundation and the Swift Package Manager and then rebuild:<\/p>\n<pre class=\"crayon:false\">\nrm -rf build\/buildbot_linux\/foundation-linux-x86_64 build\/buildbot_linux\/swiftpm-linux-x86_64\n.\/swift\/utils\/build-script --preset=buildbot_linux_1404 install_destdir=\/tmp\/install installable_package=\/tmp\/swift.tar.gz\n<\/pre>\n<h2>Using<\/h2>\n<p>I am a fan of using the <code>\/opt\/<i>vendor<\/i><\/code> directory structure for vendor packages.  Once the build is complete you can use <code>rsync<\/code> to get it placed in an appropriate directory.<\/p>\n<pre class=\"crayon:false\">\nsudo rsync -a \/tmp\/install\/ \/opt\/swift\/swift-3.0\/\n<\/pre>\n<p><b>Note:<\/b>  If you aren&#8217;t familiar with rsync be aware that trailing slashes are <b>important<\/b>!<\/p>\n<p>Update your PATH to include <code>\/opt\/swift\/swift-3.0\/usr\/bin<\/code> and you should be good to go!  Try out your own personal Swift build with the <a href=\"http:\/\/dev.iachieved.it\/iachievedit\/introducing-the-swift-package-manager\/\">Swift Package Manager<\/a>.<\/p>\n<h2>Get the Code<\/h2>\n<p>We&#8217;ve included some helper scripts on Github.<\/p>\n<pre class=\"crayon:false\">\n# git clone https:\/\/github.com\/iachievedit\/package-swift\n<\/pre>\n<p>Start with running <code>get.sh<\/code> to clone all of the required Github repositories.  Then for your first build, run <code>package.sh<\/code> and go watch an episode <i>Walking Dead<\/i> or <i>Game of Thrones<\/i>.  When you get back you should have an <code>install<\/code> directory with your own version of Swift.<\/p>\n<p>To refresh your sources run <code>update.sh<\/code> and then rebuild with <code>package.sh<\/code>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Editor&#8217;s note: If you&#8217;re looking for a quick way to start working with Swift 3.0 now, try out our apt-get repository for Trusty Tahr, Wily Werewolf, and Xenial Xerus. It&#8217;s been a little over six months since Apple released its Swift language into the open source community. Since then its various repositories have quickly climbed [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":3108,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11,19,5],"tags":[],"class_list":["post-2110","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-apple","category-linux","category-swift"],"_links":{"self":[{"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/posts\/2110"}],"collection":[{"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/comments?post=2110"}],"version-history":[{"count":32,"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/posts\/2110\/revisions"}],"predecessor-version":[{"id":3973,"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/posts\/2110\/revisions\/3973"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/media\/3108"}],"wp:attachment":[{"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/media?parent=2110"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/categories?post=2110"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/tags?post=2110"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}