• Stars
    star
    784
  • Rank 58,032 (Top 2 %)
  • Language
    Swift
  • License
    Apache License 2.0
  • Created about 5 years ago
  • Updated 6 months 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++
66,491
star
2

ml-stable-diffusion

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

swift-evolution

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

foundationdb

FoundationDB - the open source, distributed, transactional key-value store
C++
14,444
star
5

turicreate

Turi Create simplifies the development of custom machine learning models.
C++
11,197
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

pkl

A configuration as code language with rich validation and tooling.
Java
10,223
star
8

swift-package-manager

The Package Manager for the Swift Programming Language
Swift
9,637
star
9

ml-ferret

Python
8,415
star
10

swift-nio

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

corenet

CoreNet: A library for training deep neural networks
Jupyter Notebook
6,968
star
12

swift-algorithms

Commonly used sequence and collection algorithms for Swift
Swift
5,885
star
13

swift-corelibs-foundation

The Foundation Project, providing core utilities, internationalization, and OS independence
C
5,269
star
14

swift-protobuf

Plugin and runtime library for using protobuf with Swift
Swift
4,561
star
15

coremltools

Core ML tools contain supporting tools for Core ML model conversion, editing, and validation.
Python
4,401
star
16

password-manager-resources

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

ml-mgie

Python
3,853
star
18

tensorflow_macos

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

swift-collections

Commonly used data structures for Swift
Swift
3,651
star
20

ml-depth-pro

Depth Pro: Sharp Monocular Metric Depth in Less Than a Second.
Python
3,436
star
21

swift-argument-parser

Straightforward, type-safe argument parsing for Swift
Swift
3,289
star
22

sourcekit-lsp

Language Server Protocol implementation for Swift and C-based languages
Swift
3,160
star
23

swift-syntax

A set of Swift libraries for parsing, inspecting, generating, and transforming Swift source code.
Swift
3,064
star
24

swift-log

A Logging API for Swift
Swift
2,931
star
25

swift-async-algorithms

Async Algorithms for Swift
Swift
2,895
star
26

swift-markdown

A Swift package for parsing, building, editing, and analyzing Markdown documents.
Swift
2,669
star
27

ml-ane-transformers

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

swift-corelibs-libdispatch

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

HomeKitADK

C
2,456
star
30

swift-format

Formatting technology for Swift source code
Swift
2,341
star
31

swift-foundation

The Foundation project
Swift
2,302
star
32

homebrew-apple

Ruby
2,240
star
33

cups

Apple CUPS Sources
C
1,926
star
34

axlearn

An Extensible Deep Learning Library
Python
1,840
star
35

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,826
star
36

ml-cvnets

CVNets: A library for training computer vision networks
Python
1,777
star
37

sample-food-truck

SwiftUI sample code from WWDC22
Swift
1,738
star
38

swift-numerics

Advanced mathematical types and functions for Swift
Swift
1,669
star
39

swift-book

The Swift Programming Language book
Markdown
1,666
star
40

ml-4m

4M: Massively Multimodal Masked Modeling
Python
1,590
star
41

swift-testing

A modern, expressive testing package for Swift
Swift
1,582
star
42

ml-hypersim

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

swift-crypto

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

swift-openapi-generator

Generate Swift client and server code from an OpenAPI document.
Swift
1,423
star
45

swift-docker

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

ml-neuman

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

swift-system

Low-level system calls and types for Swift
Swift
1,166
star
48

swift-docc

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

swift-corelibs-xctest

The XCTest Project, A Swift core library for providing unit test support
Swift
1,138
star
50

swift-llbuild

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

swift-atomics

Low-level atomic operations for Swift
Swift
1,050
star
52

servicetalk

A networking framework that evolves with your application
Java
910
star
53

swift-http-types

Version-independent HTTP currency types for Swift
Swift
902
star
54

swift-llvm

LLVM
813
star
55

swift-protobuf-plugin

Moved to apple/swift-protobuf
755
star
56

unityplugins

C#
721
star
57

swift-embedded-examples

A collection of example projects using Embedded Swift
Swift
713
star
58

ml-mobileone

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

ml-aim

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

swift-lldb

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

swift-clang

C++
672
star
62

ml-gaudi

611
star
63

ml-mobileclip

This repository contains the official implementation of the research paper, "MobileCLIP: Fast Image-Text Models through Multi-Modal Reinforced Training" CVPR 2024
Python
605
star
64

swift-metrics

Metrics API for Swift
Swift
602
star
65

swift-distributed-actors

Peer-to-peer cluster implementation for Swift Distributed Actors
Swift
591
star
66

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
589
star
67

device-management

Device management schema data for MDM.
580
star
68

sample-backyard-birds

Swift
544
star
69

ml-facelit

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

ccs-calendarserver

The Calendar and Contacts Server.
Python
470
star
71

swift-3-api-guidelines-review

Swift
455
star
72

swift-org-website

Swift.org website
SCSS
450
star
73

GCGC

Jupyter Notebook
438
star
74

ml-mdm

Train high-quality text-to-image diffusion models in a data & compute efficient manner
Python
433
star
75

swift-nio-http2

HTTP/2 support for SwiftNIO
Swift
405
star
76

swift-tools-support-core

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

swift-nio-ssh

SwiftNIO SSH is a programmatic implementation of SSH using SwiftNIO
Swift
389
star
78

swift-playdate-examples

An Embedded Swift game running on Playdate by Panic
Swift
386
star
79

swift-nio-ssl

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

ml-gmpi

[ECCV 2022, Oral Presentation] Official PyTorch implementation of GMPI
Python
339
star
81

example-package-dealer

Example package for use with the Swift Package Manager
Swift
335
star
82

security-pcc

Private Cloud Compute (PCC)
Swift
334
star
83

swift-collections-benchmark

A benchmarking tool for Swift Collection algorithms
Swift
333
star
84

swift-homomorphic-encryption

Homomorphic Encryption library and applications in Swift
Swift
330
star
85

example-package-playingcard

Example package for use with the Swift Package Manager
Swift
323
star
86

indexstore-db

Index database library for use with sourcekit-lsp
C++
315
star
87

swift-docc-render

Web renderer for Swift-DocC documentation.
JavaScript
307
star
88

ml-hierarchical-confusion-matrix

Neo: Hierarchical Confusion Matrix Visualization (CHI 2022)
TypeScript
302
star
89

swift-docc-plugin

Swift Package Manager command plugin for Swift-DocC
Swift
301
star
90

swift-migration-guide

Markdown
294
star
91

ml-sigma-reparam

Python
292
star
92

pfl-research

Simulation framework for accelerating research in Private Federated Learning
Jupyter Notebook
289
star
93

ml-gsn

Python
284
star
94

swift-llbuild2

A fresh take on a low-level build system API.
Swift
281
star
95

swift-source-compat-suite

The infrastructure and project index comprising the Swift source compatibility suite.
Python
280
star
96

swift-xcode-playground-support

Logging and communication to allow Swift toolchains to communicate with Xcode.
Swift
279
star
97

sample-cloudkit-sharing

Swift
275
star
98

swift-experimental-string-processing

An early experimental general-purpose pattern matching engine for Swift.
Swift
270
star
99

swift-matter-examples

An Embedded Swift Matter application running on ESP32-C6
Swift
266
star
100

pkl-go

Pkl bindings for the Go programming language
Go
263
star