Developing Swift language in Xcode

Apple has opened Swift sources. Everyone is invited to join and add new features to it and Apple has not put any specific contractual requirements on contributors (contribution agreements). This post is about developing Swift in XCode - setting up developer environment on OS X. The main source of truth is Swift's official web page swift.org

Installing necessary tools

  • Xcode The standard Apple's IDE can be installed through App Store.
  • Home Brew It's package manager allowing to install a lot of useful ports of Linux software. The installation simple and exact procedure can be found on Home Brew Home Page.
  • cmake, ninja those are build tools used by build system (if you are developing swift, probably you have heard about one of this). With Brew installing those tools is straight forward, just issue command
    brew install cmake ninja
  • cmark it's implementation of Common Mark, a markdown syntax (roughly it's a document renderer). It can be installed similar to cmake and ninja: brew install cmark
    If want to read more visit cmark on Github or CommonMark.
The Brew installs packages in /usr/local, so to use it add /usr/local/bin to your PATH environment variable. You can execute following line each time you open a terminal:
export PATH=$PATH:/usr/local/bin
or add it to ~/.bash_profile or ~/.zprofile to set it automatically when you open terminal session.

To test if everything is set up correctly issue cmake command in terminal window.

Obtaining source code

The source code can be cloned from Github and it requires specific organisation of source code:
 $SWIFT_SOURCE_ROOT/
  /clang
  /swift
  /lldb                      (optional)
  /llbuild                   (optional)
  /swiftpm                   (optional, requires llbuild)
  /swift-corelibs-xctest     (optional)
  /swift-corelibs-foundation (optional)
To help with this Apple comes with script which automatically clones and updates sources. It has two disadvantages:
  1. It clones from Apple's Github https://github.com/apple, so if you want to fork some sources you will have to set proper upstreams manually.
  2. Updates are made with --rebase, from default upstream, so again, if you forked repository into some shared or public server (ie. Github or Bitbucket) you should update branch tracking, and pull any updates from Apple manually. This should not be a problem as probably any changes will go to one repository only - swift.
Both of those will be covered later. Right now we focus on setting development up.
Firstly, create folder Swift somewhere in your filesystem for sources and all dependencies. Let's call it a workspace folder.

The next step is to clone a main repository - swift one - from https://github.com/apple/swift. I prefer HTTPS instead of SSH, as I don't like to manage SSH keys. In terminal, navigate to your workspace directory and issue following command

Navigate into swift subfolder created by git, and issue following command
./utils/update-checkout --clone

Or if you prefer SSH:
./utils/update-checkout --clone-wth-ssh

Importing projects into Xcode

Move into swift subdirectory of your workspace directory (you should already be there). The source code is actually without any Xcode project settings, those have to be generated. Issue command
./utils/build-script -x

The -x tells to create Xcode projects. Similar option -X set IDE only developing, but it prevent builds in IDE.
You can use -S if you want not to build sources and use hard way, but giving more view how to work with Swift and its dependencies. If you choose this read about setting Xcode workspace, as LLVM have to be build in Xcode.
The build takes a bit of time, even two bits, even more... Grab coffee.

Importing into Xcode

This is the simpler way, which should be enough. Start Xcode than choose Open project. Navigate to folder SWIFT_WORKSPACE/build/Xcode-DebugAssert/swift-macosx-x86_64 and open Swift project. When asked you automatic settings.

In the left upper corner you will see list of build schemas (targets), select swift (you can start typing to filter items). Run it by pressing Command + R. You can see debugger waiting on some instruction - press continue. In output window you should see welcome message of swift interpreter.

To stop it write :quit, or stop debugging with Command + .

Congratulations, right now you are ready to develop Swift - not with Swift.
SHARE
    Blogger Comment
    Facebook Comment

0 komentarze:

Post a Comment