After experimenting with Apple-provided Swift binaries on Ubuntu, I decided to use the instructions at https://github.com/apple/swift/blob/master/README.md to try and build Swift on a Linux distro where pre-build binaries are not available. As of this writing they are only provided for Ubuntu 14.04 and 15.10. I picked CentOS 7.1 in an Azure virtual machine (VM). The VM instance was a standard DS3 (4 cores, 14 GB memory).
My attempts have been only partially successful. Please see the summary.
utils/build-script -t
and ran out of memory when an executable was buing built. Reading the help dumped by the script when given the -h option, I figured that the memory footprint could be reduced by doing a release build; the default is debug. So, after adding the -R option, I saw the build process complete without starving the box out of memory.
mkdir swift_2.2
cd swift_2.2
Then I obtained the sources from GitHub:
git clone --branch swift-2.2-SNAPSHOT-2015-12-01-b https://github.com/apple/swift.git swift
git clone --branch swift-2.2-SNAPSHOT-2015-12-01-b https://github.com/apple/swift-llvm.git llvm
git clone --branch swift-2.2-SNAPSHOT-2015-12-01-b https://github.com/apple/swift-clang.git clang
git clone --branch swift-2.2-SNAPSHOT-2015-12-01-b https://github.com/apple/swift-lldb.git lldb
git clone https://github.com/apple/swift-cmark.git cmark
git clone --branch swift-2.2-SNAPSHOT-2015-12-01-b https://github.com/apple/swift-llbuild.git llbuild
git clone --branch swift-2.2-SNAPSHOT-2015-12-01-b https://github.com/apple/swift-package-manager.git swiftpm
git clone --branch swift-2.2-SNAPSHOT-2015-12-01-b https://github.com/apple/swift-corelibs-xctest.git
git clone --branch swift-2.2-SNAPSHOT-2015-12-01-b https://github.com/apple/swift-corelibs-foundation.git
git clone --branch release https://github.com/ninja-build/ninja.git
A few things to note:
You can do
utils/build-script -h
to find out about the various options the build script supports.
cd swift
utils/build-script -t -R
This will build LLVM, Clang, and Swift. The -t option will cause tests to be built and run. Unless your box has a lot of memory, make sure you use the -R flag to do a release build. Otherwise debug will be built (the default), and the build may run out of memory. I was unable to do this even on a box with 14 GB. Watching the memory with the -R flag showed that at least 6 GB of RAM is needed for a release build.
At one point the build failed, complaining about the missing /usr/include/x86_64-linux-gnu/sys/ioctl.h
header. This was fixed by
ln -s /usr/include /usr/include/x86_64-linux-gnu
and the build completed successfully.
# This is from the swift_2.2 directory
cd swift
utils/build-script -l -b -p --foundation -R
Note that tests are not built here. You can add -t to build and run them if desired.
After awhile the build stops with this error:
This happens while swiftpm is being built. So, we'll go straight to building Foundation:
utils/build-script --foundation -R
The Foundation build completes, but I can't see how to use it yet.
However, swift REPL does not work:
[root@centos71 ~]# swift
LLVM ERROR: Compiler-internal integrated REPL unimplemented for this platform
Trying to use the Swift build system the way it is described in the swift.org instructions mentioned earlier gives:
[root@centos71 Hello]# swift build
error: unable to invoke subcommand: /root/swift_2.2/build/Ninja-ReleaseAssert/swift-linux-x86_64/bin/swift-build (No such file or directory)
Indeed, the file swift-build does not exist in that directory. This works fine on Ubuntu with the Apple-provided binaries.
Now try to use Foundation. Suppose the file junk.swift contains:
import Foundation
print ("Hey swift on Centos7.1!")
Trying to run it:
[root@centos71 ~]# swift junk.swift
junk.swift:1:8: error: no such module 'Foundation'
import Foundation
^
[root@centos71 ~]# swift --help
OVERVIEW: Swift compiler
USAGE: swift [options]
So, let's try a few things:
[root@centos71 ~]# swift -I /root/swift_2.2/build/Ninja-ReleaseAssert/foundation-linux-x86_64/Foundation junk.swift
junk.swift:1:8: error: missing required module 'CoreFoundation'
import Foundation
^
[root@centos71 ~]# swift -I /root/swift_2.2/build/Ninja-ReleaseAssert/foundation-linux-x86_64/Foundation -F /root/swift_2.2/build/Ninja-ReleaseAssert/foundation-linux-x86_64/Foundation -framework CoreFoundation junk.swift
junk.swift:1:8: error: missing required module 'CoreFoundation'
import Foundation
^
[root@centos71 ~]# swift -I /root/swift_2.2/build/Ninja-ReleaseAssert/foundation-linux-x86_64/Foundation -F /root/swift_2.2/build/Ninja-ReleaseAssert/foundation-linux-x86_64/Foundation/CoreFoundation -framework CoreFoundation junk.swift
junk.swift:1:8: error: missing required module 'CoreFoundation'
import Foundation
^
As one can see, using the -I flag with the location of the Foundation framework lets swift find Foundation, but I still need to figure out how to make it see CoreFoundation. More documentation reading is needed... If you have any ideas, please let me know.
In addition to building the source tagged with swift-2.2-SNAPSHOT-2015-12-01-b, I also tried the current head of the repository, i.e. cloning without a --branch option, as well as the most recent tag, which is swift-2.2-SNAPSHOT-2015-12-10-a as of this writing. The results were almost the same, except that when the untagged code or the code with the most recent tag was used, things were slightly different:
ln -s /usr/include /usr/include/x86_64-linux-gnu
LLVM ERROR: Compiler-internal integrated REPL unimplemented for this platform; use the LLDB-enhanced REPL instead.
Thus it looks like the newly-open-sourced Swift is in its infancy, and there is a lot of homework to be done, but its a great baby and will hopefully grow into a mature open source language!
Please feel free to contact me with questions or comments.