WTSC 2: Setup playground

156 阅读2分钟

Before we get into the core process of compilation of Swift Compiler, we need some setups for our playground which facilitate our experiments with Swift Compiler.

Here is the link to the Swift Opensource Project. Inside that project, there is a detailed guide to show us how to build the swift compiler, therefore, I don't want to copy those to here, but show how to build swift on ubuntu as an example. If your system is not ubuntu or linux, you can check out the build guide from Swift Opensource Project.

The following is a list of instructions to build swift on Ubuntu. For my convenience, I will create a directory on root, /wtsc , to shorten the path to swift compiler.

0. System Requirements

sudo apt-get install    \
  clang                 \
  cmake                 \
  git                   \
  icu-devtools          \
  libcurl4-openssl-dev  \
  libedit-dev           \
  libicu-dev            \
  libncurses5-dev       \
  libpython3-dev        \
  libsqlite3-dev        \
  libxml2-dev           \
  ninja-build           \
  pkg-config            \
  python                \
  python-six            \
  rsync                 \
  swig                  \
  systemtap-sdt-dev     \
  tzdata                \
  uuid-dev
sudo snap install sccache --candidate --classic

sccache --start-server

1. clone swift open source project

mkdir /wtsc && cd /wtsc
git clone https://github.com/apple/swift.git

2. build swift compiler

/wtsc/swift/utils/build-script \
  --skip-build-benchmarks \
  --cmake-c-launcher="$(which sccache)" \
  --cmake-cxx-launcher="$(which sccache)" \
  --release \
  --debug-swift \
  --llvm-targets-to-build="X86" \
  --install-destdir=/wtsc \
  --install-all

3. after build success, we can see what have been built.

cd /wtsc/build/Ninja-ReleaseAssert+swift-DebugAssert
ls
cmark-linux-x86_64  llvm-linux-x86_64  swift-linux-x86_64

we can see that there are three projects have been built, not only swift project, but also cmark, and llvm. Because swift compiler is based on llvm infrastructure, therefore it needs llvm project to built. And swift language support its markup language in comment to enrich its source code documentary, so it is no wonder swift needs cmark and is built with it.

After we get into 

/wtsc/usr/bin

 we can see a list of executables have been built. One of them is swift compiler, swift-frontend, and some symbolic links to it, like swift, swiftc, swift-ident, etc.

ls -alh | grep swift-frontend
lrwxrwxrwx  1 k k   14 Oct  7 19:04 swift -> swift-frontend
lrwxrwxrwx  1 k k   14 Oct  7 19:04 swift-autolink-extract -> swift-frontend
lrwxrwxrwx  1 k k   14 Oct  7 19:04 swiftc -> swift-frontend
-rwxr-xr-x  1 k k 881M Oct  7 19:04 swift-frontend
lrwxrwxrwx  1 k k   14 Oct  7 19:04 swift-indent -> swift-frontend
lrwxrwxrwx  1 k k   14 Oct  7 19:04 swift-symbolgraph-extract -> swift-frontend

Because swift compiler, swift-frontend, is just an ordinary linux program which can be invoke as a command line on shell. when it is invoked by different alias (symbolic link), in its main function, the first argument it gets would be the first part of the command line, the name of the invoked program not the target program. Eventhough they are linked to swift-frontend.

swiftc --help # the first argument is swiftc

swift --help # the first arugment is swift

4. check if compiler works

swift--version

From now on, we have a workable swift compiler. And We can write a hello world program in swift.

/* /wtsc/helloworld/helloworld.swift */
print("Hello world!\n")

/wtsc/helloworld $:
swiftc helloworld.swiftc -o helloworld

/wtsc/helloworld $:
./helloworld
Hello world!

In later posts we will explore its inside work to see how it transforms source code to executable.