• Stars
    star
    764
  • Rank 57,045 (Top 2 %)
  • Language
    Swift
  • License
    Apache License 2.0
  • Created over 4 years ago
  • Updated about 1 month ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Swift compiler driver reimplementation in Swift

Swift Compiler Driver

Swift's compiler driver is a program that coordinates the compilation of Swift source code into various compiled results: executables, libraries, object files, Swift modules and interfaces, etc. It is the program one invokes from the command line to build Swift code (i.e., swift or swiftc) and is often invoked on the developer's behalf by a build system such as the Swift Package Manager (SwiftPM) or Xcode's build system.

The swift-driver project is a new implementation of the Swift compiler driver that is intended to replace the existing driver with a more extensible, maintainable, and robust code base. The specific goals of this project include:

  • A maintainable, robust, and flexible Swift code base
  • Library-based architecture that allows better integration with build tools
  • Leverage existing Swift build technologies (SwiftPM, llbuild)
  • A platform for experimenting with more efficient build models for Swift, including compile servers and unifying build graphs across different driver invocations

Getting Started

Note: Currently, swift-driver is only compatible with trunk development snapshots from swift.org.

The preferred way to build swift-driver is to use the Swift package manager.

On most platforms you can build using:

$ swift build

However, on Windows, some additional work must be done by the developer.

Due to the default version of swift-tools-support-core that Package.resolved references, we must first update the package dependencies.

swift package update

Then, we can build the package using:

swift build -Xcc -I -Xcc "%SystemDrive%\Library\sqlite-3.38.0\usr\include" -Xlinker -L -Xlinker "%SystemDrive%\Library\sqlite-3.38.0\usr\lib" -Xlinker "%SDKROOT%\usr\lib\swift\windows\x86_64\swiftCore.lib"

Because SQLite3 is a system library dependency, and there is no singular header and library search path, the developer must specify that. The path to SQLite3 may need to be adjusted if the library is not located at the specified location. Additionally, because Swift Package Manager does not differentiate between C/C++ and Swift targets and uses the Swift driver as the linker driver we must link in the Swift runtime into all targets manually.

To use swift-driver in place of the existing Swift driver, create a symbolic link from swift and swiftc to swift-driver:

ln -s /path/to/built/swift-driver $SOME_PATH/swift
ln -s /path/to/built/swift-driver $SOME_PATH/swiftc

Swift packages can be built with the new Swift driver by overriding SWIFT_EXEC to refer to the swiftc symbolic link created above and SWIFT_DRIVER_SWIFT_FRONTEND_EXEC to refer to the original swift-frontend, e.g.,

SWIFT_EXEC=$SOME_PATH/swiftc SWIFT_DRIVER_SWIFT_FRONTEND_EXEC=$TOOLCHAIN_PATH/bin/swift-frontend swift build

Similarly, one can use the new Swift driver within Xcode by adding a custom build setting (usually at the project level) named SWIFT_EXEC that refers to $SOME_PATH/swiftc and adding -driver-use-frontend-path $TOOLCHAIN_DIR/usr/bin/swiftc to Other Swift Flags.

Building with CMake

swift-driver can also be built with CMake, which is suggested for environments where the Swift Package Manager is not yet available. Doing so requires several dependencies to be built first, all with CMake:

  • (Non-Apple platforms only) swift-corelibs-foundation
  • llbuild configure CMake with -DLLBUILD_SUPPORT_BINDINGS="Swift" and -DCMAKE_OSX_ARCHITECTURES=x86_64 (If building on Intel) when building
    cmake -B <llbuild-build-dir> -G Ninja <llbuild-source-dir> -DLLBUILD_SUPPORT_BINDINGS="Swift" -DCMAKE_OSX_ARCHITECTURES=x86_64
    
  • swift-argument-parser
  • Yams

Once those dependencies have built, build swift-driver itself:

cmake -B <swift-driver-build-dir> -G Ninja <swift-driver-source-dir> -DTSC_DIR=<swift-tools-support-core-build-dir>/cmake/modules -DLLBuild_DIR=<llbuild-build-dir>/cmake/modules -DYams_DIR=<yamls-build-dir>/cmake/modules -DArgumentParser_DIR=<swift-argument-parser-build-dir>
cmake --build <swift-driver-build-dir>

Developing swift-driver

The new Swift driver is a work in progress, and there are numerous places for anyone with an interest to contribute! This section covers testing, miscellaneous development tips and tricks, and a rough development plan showing what work still needs to be done.

Driver Documentation

For a conceptual overview of the driver, see The Swift Driver, Compilation Model, and Command-Line Experience. To learn more about the internals, see Driver Design & Internals and Parseable Driver Output.

Testing

Test using command-line SwiftPM or Xcode.

$ swift test --parallel

Integration tests are costly to run and are disabled by default. Enable them using SWIFT_DRIVER_ENABLE_INTEGRATION_TESTS environment variable. In Xcode, you can set this variable in the scheme's test action.

$ SWIFT_DRIVER_ENABLE_INTEGRATION_TESTS=1 swift test --parallel

Some integration tests run the lit test suites in a Swift working copy. To enable these, clone Swift and its dependencies and build them with build-script, then set both SWIFT_DRIVER_ENABLE_INTEGRATION_TESTS and SWIFT_DRIVER_LIT_DIR, either in your Xcode scheme or on the command line:

$ SWIFT_DRIVER_ENABLE_INTEGRATION_TESTS=1 \
  SWIFT_DRIVER_LIT_DIR=/path/to/build/Ninja-ReleaseAssert/swift-.../test-... \
  swift test -c release --parallel

Testing against swift compiler trunk

swift-driver Continuous Integration runs against the most recent Trunk Development snapshot published at swift.org/download.

When developing patches that have complex interactions with the underlying swift compiler frontend, it may be prudent to ensure that swift-driver tests also pass against the current tip-of-trunk swift. To do so, create an empty pull request against github.com/apple/swift and perform cross-repository testing against your swift-driver pull request #, for example:

Using:
apple/swift-driver#208
@swift-ci smoke test

@swift-ci cross-repository testing facilities are described here.

Testing in Xcode with custom toolchain

After the toolchain is installed, Xcode needs to be told to use it. This can mean two things, building the driver with the toolchain and telling the driver to use the toolchain when running.

Building with the toolchain is easy, set the toolchain in Xcode: Menu Bar > Xcode > Toolchains > select your toolchain

Running the driver requires setting the TOOLCHAINS environment variable. This tells xcrun which toolchain to use (on darwin xcrun is used to find tools). This variable is the name of the toolchain and not the path (ex: Swift Development Snapshot). Important note: xcrun lookup is lower priority than the SWIFT_EXEC_*_EXEC family of environment variables, the tools directory, and any tools in the same directory as the driver (This includes a driver installed in a toolchain). Even though TOOLCHAINS is not highest priority it's a convenient way to run the xctest suite using a custom toolchain.

Preparing a Linux docker for debug

When developing on macOS without quick access to a Linux machine, using a Linux Docker is often helpful when debugging.

To get a docker up and running to the following:

  • Install Docker for Mac.
  • Get the newest swift docker image docker pull swift.
  • Run the following command to start a docker
$ docker run -v /path/to/swift-driver:/home/swift-driver \
  --cap-add=SYS_PTRACE --security-opt seccomp=unconfined \
  --security-opt apparmor=unconfined -it swift:latest bash
  • Install dependencies by running
$ apt-get update
$ apt-get install libsqlite3-dev
$ apt-get install libncurses-dev
  • You can now go to /home/swift-driver and run swift test --parallel to run your tests.

Rebuilding Options.swift

Options.swift, which contains the complete set of options that can be parsed by the driver, is automatically generated from the option tables in the Swift compiler. If you need to regenerate Options.swift, you will need to build the Swift compiler and then build makeOptions program with a -I that allows the generated Options.inc to be found, e.g.:

$ swift build -Xcc -I/path/to/build/Ninja-Release/swift-.../include -Xcc -I/path/to/build/Ninja-Release/llvm-.../include -Xcc -I/path/to/source/llvm-project/llvm/include --product makeOptions

Then, run makeOptions and redirect the output to overwrite Options.swift:

$ .build/path/to/makeOptions > Sources/SwiftOptions/Options.swift

Development Plan

The goal of the new Swift driver is to provide a drop-in replacement for the existing driver, which means that there is a fixed initial feature set to implement before the existing Swift driver can be deprecated and removed. The development plan below covers that feature set, as well as describing a number of tasks that can improve the Swift driver---from code cleanups, to improving testing, implementing missing features, and integrating with existing systems.

  • Code and documentation quality
    • Search for FIXME: or TODO:: there are lots of little things to improve!
    • Improve documentation of how to incorporate the driver into your own builds
    • Add useful descriptions to any Error thrown within the library
  • Option parsing
    • Look for complete "coverage" of the options in Options.swift. Is every option there checked somewhere in the driver?
    • Find a better way to describe aliases for options. Can they be of some other type OptionAlias so we can't make the mistake of (e.g.) asking for an alias option when we're translating options?
    • Diagnose unused options on the command line
    • Typo correction for misspelled option names
    • Find a better way than makeOptions.cpp to translate the command-line options from Swift's repository into Options.swift.
  • Platform support
    • Teach the DarwinToolchain to also handle iOS, tvOS, watchOS
    • Fill out the GenericUnixToolchain toolchain to get it working
    • Implement a WindowsToolchain
    • Implement proper tokenization for response files
  • Compilation modes
    • Batch mode
    • Whole-module-optimization mode
    • REPL mode
    • Immediate mode
  • Features
    • Precompiled bridging headers
    • Support embedding of bitcode
    • Incremental compilation
    • Parseable output, as used by SwiftPM
    • Response files
    • Input and primary input file lists
    • Complete OutputFileMap implementation to handle all file types uniformly
  • Testing
    • Build stuff with SwiftPM or Xcode or your favorite build system, using swift-driver. Were the results identical? What changed?
    • Shim in swift-driver so it can run the Swift repository's driver test suite.
    • Investigate differences in the test results for the Swift repository's driver test suite (above) between the existing and new driver.
    • Port interesting tests from the Swift repository's driver test suite over to XCTest
    • Fuzz the command-line options to try to crash the Swift driver itself
  • Integration
    • Teach the Swift compiler's build-script to build swift-driver.
    • Building on the above, teach the Swift compiler's build-toolchain to install swift-driver as the primary driver so we can test full toolchains with the new driver

Build all Swift interfaces from an SDK

Based on libSwiftDriver, swift-build-sdk-interfaces is a tool to batch build all Swift textual interfaces (.swiftinterface) from an SDK into binary modules (.swiftmodule). As an example, the following command finds all Swift textual interface from the MacOSX SDK, builds all of them into binary modules, and outputs module-specific error logs into the given directory.

$SDKROOT=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk SWIFT_EXEC=/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swiftc swift-build-sdk-interfaces -o /tmp/outputs -v -log-path /tmp/logs

  • SDKROOT: an env var to specify the SDK to work on
  • SWIFT_EXEC: teach swift-build-sdk-interfaces about where to find the Swift compiler to use
  • -O: the output directory for all binary modules built from textual interfaces
  • -log-path: where to dump log files when fatal error happens

More Repositories

1

swift

The Swift Programming Language
C++
65,899
star
2

ml-stable-diffusion

Stable Diffusion with Core ML on Apple Silicon
Python
16,002
star
3

swift-evolution

This maintains proposals for changes and user-visible enhancements to the Swift Programming Language.
Markdown
15,013
star
4

foundationdb

FoundationDB - the open source, distributed, transactional key-value store
C++
13,947
star
5

turicreate

Turi Create simplifies the development of custom machine learning models.
C++
11,153
star
6

darwin-xnu

The Darwin Kernel (mirror). This repository is a pure mirror and contributions are currently not accepted via pull-requests, please submit your contributions via https://developer.apple.com/bug-reporting/
C
10,558
star
7

swift-package-manager

The Package Manager for the Swift Programming Language
Swift
9,592
star
8

ml-ferret

Python
7,576
star
9

swift-nio

Event-driven network application framework for high performance protocol servers & clients, non-blocking.
Swift
7,274
star
10

swift-algorithms

Commonly used sequence and collection algorithms for Swift
Swift
5,622
star
11

swift-corelibs-foundation

The Foundation Project, providing core utilities, internationalization, and OS independence
Swift
5,189
star
12

swift-protobuf

Plugin and runtime library for using protobuf with Swift
Swift
4,446
star
13

password-manager-resources

A place for creators and users of password managers to collaborate on resources to make password management better.
JavaScript
4,010
star
14

coremltools

Core ML tools contain supporting tools for Core ML model conversion, editing, and validation.
Python
3,974
star
15

ml-mgie

Python
3,682
star
16

tensorflow_macos

TensorFlow for macOS 11.0+ accelerated using Apple's ML Compute framework.
Shell
3,643
star
17

swift-collections

Commonly used data structures for Swift
Swift
3,434
star
18

pkl

A configuration as code language with rich validation and tooling.
Java
3,360
star
19

swift-argument-parser

Straightforward, type-safe argument parsing for Swift
Swift
3,163
star
20

sourcekit-lsp

Language Server Protocol implementation for Swift and C-based languages
Swift
3,110
star
21

swift-log

A Logging API for Swift
Swift
2,931
star
22

swift-syntax

A set of Swift libraries for parsing, inspecting, generating, and transforming Swift source code.
Swift
2,887
star
23

swift-async-algorithms

Async Algorithms for Swift
Swift
2,695
star
24

swift-markdown

A Swift package for parsing, building, editing, and analyzing Markdown documents.
Swift
2,586
star
25

HomeKitADK

C
2,456
star
26

ml-ane-transformers

Reference implementation of the Transformer architecture optimized for Apple Neural Engine (ANE)
Python
2,431
star
27

swift-corelibs-libdispatch

The libdispatch Project, (a.k.a. Grand Central Dispatch), for concurrency on multicore hardware
C
2,420
star
28

swift-format

Formatting technology for Swift source code
Swift
2,261
star
29

homebrew-apple

Ruby
2,227
star
30

swift-foundation

The Foundation project
Swift
2,088
star
31

cups

Apple CUPS Sources
C
1,828
star
32

sample-food-truck

SwiftUI sample code from WWDC22
Swift
1,695
star
33

ml-fastvit

This repository contains the official implementation of the research paper, "FastViT: A Fast Hybrid Vision Transformer using Structural Reparameterization" ICCV 2023
Python
1,693
star
34

ml-cvnets

CVNets: A library for training computer vision networks
Python
1,664
star
35

swift-book

The Swift Programming Language book
Markdown
1,616
star
36

swift-numerics

Advanced mathematical types and functions for Swift
Swift
1,602
star
37

ml-hypersim

Hypersim: A Photorealistic Synthetic Dataset for Holistic Indoor Scene Understanding
Python
1,495
star
38

swift-crypto

Open-source implementation of a substantial portion of the API of Apple CryptoKit suitable for use on Linux platforms.
C
1,400
star
39

swift-docker

Docker Official Image packaging for Swift
Dockerfile
1,331
star
40

ml-neuman

Official repository of NeuMan: Neural Human Radiance Field from a Single Video (ECCV 2022)
Python
1,233
star
41

swift-openapi-generator

Generate Swift client and server code from an OpenAPI document.
Swift
1,142
star
42

swift-system

Low-level system calls and types for Swift
Swift
1,137
star
43

swift-corelibs-xctest

The XCTest Project, A Swift core library for providing unit test support
Swift
1,120
star
44

swift-docc

Documentation compiler that produces rich API reference documentation and interactive tutorials for your Swift framework or package.
Swift
1,093
star
45

swift-llbuild

A low-level build system, used by Xcode and the Swift Package Manager
C++
1,067
star
46

swift-atomics

Low-level atomic operations for Swift
Swift
1,004
star
47

swift-testing

Swift
981
star
48

servicetalk

A networking framework that evolves with your application
Java
881
star
49

swift-http-types

Version-independent HTTP currency types for Swift
Swift
815
star
50

swift-llvm

LLVM
815
star
51

swift-protobuf-plugin

Moved to apple/swift-protobuf
757
star
52

swift-lldb

This is the version of LLDB that supports the Swift programming language & REPL.
C++
673
star
53

swift-clang

C++
673
star
54

unityplugins

C#
645
star
55

ml-mobileone

This repository contains the official implementation of the research paper, "An Improved One millisecond Mobile Backbone".
Swift
641
star
56

ml-gaudi

602
star
57

ml-aim

This repository provides the code and model checkpoints of the research paper: Scalable Pre-training of Large Autoregressive Image Models
Python
602
star
58

swift-metrics

Metrics API for Swift
Swift
602
star
59

axlearn

Python
564
star
60

swift-distributed-actors

Peer-to-peer cluster implementation for Swift Distributed Actors
Swift
562
star
61

ARKitScenes

This repo accompanies the research paper, ARKitScenes - A Diverse Real-World Dataset for 3D Indoor Scene Understanding Using Mobile RGB-D Data and contains the data, scripts to visualize and process assets, and training code described in our paper.
Python
552
star
62

sample-backyard-birds

Swift
506
star
63

device-management

Device management schema data for MDM.
506
star
64

ccs-calendarserver

The Calendar and Contacts Server.
Python
470
star
65

ml-facelit

Official repository of FaceLit: Neural 3D Relightable Faces (CVPR 2023)
Python
457
star
66

swift-3-api-guidelines-review

Swift
452
star
67

swift-org-website

Swift.org website
SCSS
438
star
68

GCGC

Jupyter Notebook
436
star
69

swift-nio-http2

HTTP/2 support for SwiftNIO
Swift
405
star
70

swift-tools-support-core

Contains common infrastructural code for both SwiftPM and llbuild.
Swift
390
star
71

swift-nio-ssh

SwiftNIO SSH is a programmatic implementation of SSH using SwiftNIO
Swift
364
star
72

swift-nio-ssl

TLS Support for SwiftNIO, based on BoringSSL.
C
345
star
73

ml-gmpi

Official PyTorch implementation of GMPI (ECCV 2022, Oral Presentation)
Python
329
star
74

example-package-dealer

Example package for use with the Swift Package Manager
Swift
319
star
75

swift-collections-benchmark

A benchmarking tool for Swift Collection algorithms
Swift
316
star
76

example-package-playingcard

Example package for use with the Swift Package Manager
Swift
308
star
77

swift-docc-render

Web renderer for Swift-DocC documentation.
JavaScript
300
star
78

indexstore-db

Index database library for use with sourcekit-lsp
C++
299
star
79

swift-playdate-examples

A technical demonstration of Embedded Swift running on Playdate by Panic
Swift
295
star
80

swift-docc-plugin

Swift Package Manager command plugin for Swift-DocC
Swift
295
star
81

ml-hierarchical-confusion-matrix

Neo: Hierarchical Confusion Matrix Visualization (CHI 2022)
TypeScript
292
star
82

ml-gsn

Python
284
star
83

swift-llbuild2

A fresh take on a low-level build system API.
Swift
280
star
84

swift-source-compat-suite

The infrastructure and project index comprising the Swift source compatibility suite.
Python
278
star
85

sample-cloudkit-sharing

Swift
275
star
86

swift-xcode-playground-support

Logging and communication to allow Swift toolchains to communicate with Xcode.
Swift
270
star
87

swift-experimental-string-processing

An early experimental general-purpose pattern matching engine for Swift.
Swift
263
star
88

ml-sigma-reparam

Python
255
star
89

swift-standard-library-preview

Swift
253
star
90

swift-nio-transport-services

Extensions for SwiftNIO to support Apple platforms as first-class citizens.
Swift
252
star
91

swift-stress-tester

Stress testing utilities for Swift's tooling
Swift
207
star
92

swift-service-discovery

A service discovery API for Swift.
Swift
203
star
93

swift-certificates

An implementation of X.509 for Swift
Swift
195
star
94

swift-nio-examples

examples of how to use swift-nio
Swift
195
star
95

swift-cluster-membership

Distributed Membership Protocol implementations in Swift
Swift
191
star
96

swift-aoc-starter-example

Swift starter project for solving Advent of Code challenges.
Swift
189
star
97

sample-cloudkit-coredatasync

Swift
187
star
98

swift-distributed-tracing

Instrumentation library for Swift server applications
Swift
186
star
99

pfl-research

Simulation framework for accelerating research in Private Federated Learning
Python
186
star
100

swift-internals

HTML
182
star