{"id":2324,"date":"2015-12-27T14:35:22","date_gmt":"2015-12-27T20:35:22","guid":{"rendered":"http:\/\/dev.iachieved.it\/iachievedit\/?p=2324"},"modified":"2016-06-17T20:34:23","modified_gmt":"2016-06-18T02:34:23","slug":"open-source-swift-on-raspberry-pi-2","status":"publish","type":"post","link":"https:\/\/dev.iachieved.it\/iachievedit\/open-source-swift-on-raspberry-pi-2\/","title":{"rendered":"Swift for the Raspberry Pi"},"content":{"rendered":"<p><b>Editor&#8217;s Note:<\/b>  Swift 3.0 for Raspberry Pi 2 and Pi 3 is now available!  Head on over to our <a href=\"http:\/\/dev.iachieved.it\/iachievedit\/swift-3-0-on-raspberry-pi-2-and-3\/\">Swift 3.0 for Raspberry Pi<\/a> post for instructions on how to download and install it.<\/p>\n<p>The following is similar instructions to that provided for <a href=\"http:\/\/dev.iachieved.it\/iachievedit\/debian-packages-for-swift-on-arm\/\">other ARM devices<\/a> like BeagleBoneBlack and BeagleBoard X15.  There are some slight differences to get <code>clang-3.6<\/code> properly installed.<\/p>\n<p>If you&#8217;ve already installed the <code>swift-2.2<\/code> package with the instructions below and just want to upgrade to the latest version, use <code>sudo apt-get update &amp;&amp; sudo apt-get upgrade<\/code>.<\/p>\n<p><b><font color=\"red\">Disclaimer!<\/font><\/b>  Swift on ARM systems is very much at an <b>alpha<\/b> stage at the moment!  The package below does not contain a working<\/p>\n<ul>\n<li><a href=\"https:\/\/github.com\/apple\/swift-package-manager\">Swift package manager<\/a>\n<li><strike><a href=\"https:\/\/github.com\/apple\/swift-corelibs-foundation\">Foundation<\/a> implementation<\/strike>\n<li><a href=\"https:\/\/en.wikipedia.org\/wiki\/Read%E2%80%93eval%E2%80%93print_loop\">REPL<\/a>\n<\/ul>\n<p><b>12\/27\/15 Update:<\/b>  The REPL is now somewhat functional on BeagleBone and BeagleBoard, but there is a memory corruption error on the Raspberry Pi 2.<\/code><br \/>\n<b>1\/4\/16 Update:<\/b>  Foundation is now working!  Try some of the examples from <a href=\"http:\/\/dev.iachieved.it\/iachievedit\/foundation-on-linux\/\">this post<\/a>.<\/p>\n<p>You can track the progress of the port of Swift to ARM platforms at <a href=\"https:\/\/bugs.swift.org\">bugs.swift.org<\/a>, and follow @iachievedit on Twitter for updates when new packages are released.  To help out, create an account at <code>bugs.swift.org<\/code> and when you find broken stuff, file bugs and tag them with &#8216;arm&#8217;.<\/p>\n<h3>Prerequisites<\/h3>\n<p>Both the <code>clang-3.6<\/code> and <code>libicu-dev<\/code> packages need to be installed.  In addition, you should use <code>update-alternatives<\/code> to provide <code>\/usr\/bin<\/code> links for <code>clang<\/code> and <code>clang++<\/code>.  To get <code>clang-3.6<\/code> on a Raspberry Pi 2 you can use repositories provided by <a href=\"https:\/\/plus.google.com\/u\/0\/106813818225399872098\/posts\">Robert C. Nelson<\/a>.<\/p>\n<p>Run the following commands to add the <code>rcn-ee<\/code> repository:<\/p>\n<pre class=\"crayon:false\">\nwget http:\/\/repos.rcn-ee.com\/debian\/pool\/main\/r\/rcn-ee-archive-keyring\/rcn-ee-archive-keyring_2015.10.22~bpo90+20151022+1_all.deb\nsudo dpkg -i rcn-ee-archive-keyring_2015.10.22~bpo90+20151022+1_all.deb\necho \"deb [arch=armhf] http:\/\/repos.rcn-ee.com\/debian\/ jessie main\" | sudo tee --append \/etc\/apt\/sources.list\nsudo apt-get update\n<\/pre>\n<p>Then, install the dependencies <code>libicu-dev<\/code> and <code>clang-3.6<\/code>:<\/p>\n<pre class=\"crayon:false\">\nsudo apt-get install libicu-dev\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><b>Note:<\/b>  You <i>must<\/i> use <code>update-alternatives<\/code> to link <code>\/usr\/bin\/clang<\/code> and <code>clang++<\/code>.<\/p>\n<h3>Installation<\/h3>\n<p>At the moment there are only Raspberry Pi 2 packages for Debian Jessie.  I have tested the basic installation on a Raspberry Pi 2.<\/p>\n<p><b>1.  Add the repository key<\/b><\/p>\n<pre class=\"crayon:false\">\nwget -qO- http:\/\/dev.iachieved.it\/iachievedit.gpg.key | sudo apt-key add -\n<\/pre>\n<p><b>2.  Add the repository to <code>sources.list<\/code><\/b><\/p>\n<pre class=\"crayon:false\">\necho \"deb [arch=armhf] http:\/\/iachievedit-repos.s3.amazonaws.com\/ jessie main\" | sudo tee --append \/etc\/apt\/sources.list\n<\/pre>\n<p><b>3.  Run <code>apt-get update<\/code><\/b><\/p>\n<pre class=\"crayon:false\">\nsudo apt-get update\n<\/pre>\n<p><b>4.  Install swift-2.2!<\/b><\/p>\n<pre class=\"crayon:false\">\nsudo apt-get install swift-2.2\n<\/pre>\n<h3>Trying it Out<\/h3>\n<p>Again, a warning!  There is no Swift package manager in this build yet and don&#8217;t expect to be able to use <code>swift<\/code> to run it in REPL mode.  You can however compile basic applications, even those using <code>Glibc<\/code> and <code>Foundation<\/code>.<\/p>\n<pre class=\"crayon:false\">\nroot@beaglebone:~# more helloWorld.swift \nprint(\"Hello, world!\")\n\nfor i in 1...10 {\n  print(i)\n}\nroot@beaglebone:~# swiftc helloWorld.swift \nroot@beaglebone:~# .\/helloWorld \nHello, world!\n1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n<\/pre>\n<p>or, try out <code>swiftcat.swift<\/code> which uses <code>Glibc<\/code>.<\/p>\n<pre class=\"lang:swift\">\nimport Glibc\n\nguard Process.arguments.count == 2 else {\n  print(\"Usage:  swiftcat FILENAME\")\n  exit(-1)\n}\n\nlet filename = Process.arguments[1]\n\nlet BUFSIZE = 1024\nlet pp      = popen(\"cat \" + filename, \"r\")\nvar buf     = [CChar](count:BUFSIZE, repeatedValue:CChar(0))\n\nwhile fgets(&buf, Int32(BUFSIZE), pp) != nil {\n  print(String.fromCString(buf)!, terminator:\"\")\n}\n\nexit(0)\n<\/pre>\n<p>With the latest <code>0ubuntu11<\/code> release, <code>Foundation<\/code> is supported too!<br \/>\n<code>countdown.swift<\/code><\/p>\n<pre class=\"lang:swift\">         \nimport Foundation        \nimport Glibc         \nlet thread = NSThread(){         \n  print(\"Entering thread\")       \n  for i in (1...10).reverse() {      \n   print(\"\\(i)...\", terminator:\"\")       \n   fflush(stdout)        \n   sleep(1)      \n  }      \n  print(\"\\nExiting thread\")          \n  print(\"Done\")      \n  exit(0)        \n}        \nthread.start()       \nselect(0, nil, nil, nil, nil)        \n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Editor&#8217;s Note: Swift 3.0 for Raspberry Pi 2 and Pi 3 is now available! Head on over to our Swift 3.0 for Raspberry Pi post for instructions on how to download and install it. The following is similar instructions to that provided for other ARM devices like BeagleBoneBlack and BeagleBoard X15. There are some slight [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":3035,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[19,13,5],"tags":[34],"class_list":["post-2324","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-linux","category-raspberry-pi","category-swift","tag-swift-raspberry-pi"],"_links":{"self":[{"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/posts\/2324"}],"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=2324"}],"version-history":[{"count":21,"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/posts\/2324\/revisions"}],"predecessor-version":[{"id":3045,"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/posts\/2324\/revisions\/3045"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/media\/3035"}],"wp:attachment":[{"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/media?parent=2324"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/categories?post=2324"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/tags?post=2324"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}