swift-driver

swift-5.10-RELEASE

Swift compiler driver reimplementation in Swift
apple/swift-driver

What's New

Swift 5.10 Release

2024-03-06T16:09:36Z

What's Changed

  • NFC: Fix comment header in SwiftDriverExecutor.swift by @MaxDesiatov in #1316
  • Allow clients of Driver to pass DiagnosticsHandler by @MaxDesiatov in #1317
  • [Explicit Modules] Have -clang-target on Darwin platforms set to the SDK version by-default by @artemcm in #1274
  • Update the Swift help message to include swift --version by @shahmishal in #1321
  • SwiftDriver: add support for baremetal targets (to reflect apple/swift#35970) by @kubamracek in #1313
  • Propagate sanitizer arguments to the clang-linker-driver invocations for dynamic libraries by @artemcm in #1325
  • [Dependency Scanning] Remove coarse-grained synchronization on all dependency scan queries by @artemcm in #1319
  • Find blocklist files from toolchain dir and feed them to compiler by @nkcsgexi in #1329
  • Add -gcc-toolchain flag to swift-driver by @etcwilde in #1307
  • Add generated file note to Options.swift by @etcwilde in #1330
  • Support building this repo for more platforms, by checking the build triple by @finagolfin in #1294
  • [Explicit Module Builds] Remove logic for Swift-client-specific Clang module dependency jobs by @artemcm in #1332
  • Always quote path arguments when resolving command-lines for libSwiftScan queries by @artemcm in #1335
  • Use DarwinToolchain for apple-none targets by @rauhul in #1331
  • [Macros] Set -external-plugin-path when the toolchain is not Xcode by @rintaro in #1320
  • Use system-appropriate method when quoting path arguments on command-lines by @artemcm in #1343
  • Do not attempt to create compilation jobs when no Swift source files are specified as inputs by @artemcm in #1347
  • In WMO, make sure the dummy "primary" compilation input is a Swift source file by @artemcm in #1346
  • Revert "In WMO, make sure the dummy "primary" compilation input is a Swift source file" by @DougGregor in #1348
  • Avoid quoting paths for dependencies scanning jobs by @nkcsgexi in #1351
  • In WMO, make sure the dummy "primary" compilation input is a Swift source file by @artemcm in #1350
  • [Dependency Scanning] Unify path-escaping handling using TSC's spm_shellEscaped by @artemcm in #1354
  • build-script-helper.py: remove unused distutils import by @MaxDesiatov in #1353
  • Disable adding '-external-plugin-path' in integrated driver mode by @rintaro in #1356
  • Specify encoding when running a ninja invocation by @ahoppen in #1358
  • Revert "[Macros] Set -external-plugin-path when the toolchain is not Xcode" by @rintaro in #1359
  • Add support for compatibility shims that we don't want to -force-load by @slavapestov in #1361
  • Update requirements in Package.swift to match SwiftPM by @MaxDesiatov in #1338
  • [NFC] Package.swift: fix deprecated dependency initializers by @MaxDesiatov in #1366
  • [Dependency Scanning] Add support for Swift Overlay dependencies as a separate dependency details field by @artemcm in #1365
  • Add option -explain-module-dependency to explain a given direct or transitive module dependency by @artemcm in #1363
  • Add ignorable private flag to driver to match changes in frontend by @elsh in #1344
  • Allow unicode chars in package-name in driver by @elsh in #1367
  • Parse additional armv6 and armv7 subarchs by @rauhul in #1369
  • [cxx-interop] make -emit-clang-header-path a driver flag by @hyp in #1371
  • Formatting: remove trailing whitespaces by @MaxDesiatov in #1370
  • SwiftDriver: mark bitcode interfaces as internal by @compnerd in #1376
  • Android: look in lib/linux/ for the LLVM sanitizers instead by @finagolfin in #1372
  • [Explicit Module Builds] Add support for header dependencies of binary Swift module dependencies by @artemcm in #1375
  • Add SDK and platform paths to plugin executables by @DougGregor in #1377
  • Remove unused runtime library path on non-Darwin platforms by @finagolfin in #1374
  • Triple.swift: fix typos in local and private names by @MaxDesiatov in #1384
  • Add support for Musl libc by @MaxDesiatov in #1385
  • Add repl to swift-help topics by @bitjammer in #1382
  • Tweak testWMOWithJustObjectInputs test to not count autolink-extract jobs by @artemcm in #1388
  • Fix a non-deterministic failure in testPrintingExplicitDependencyGraph by @cachemeifyoucan in #1389
  • Driver: allow static executables when using Musl by @MaxDesiatov in #1383
  • Fix Wasm capitalization in code comments by @MaxDesiatov in #1391
  • [Driver] For immediate mode prefer using the default SDK over the SDKROOT variable by @akyrtzi in #1390
  • Set Explicit Incremental Module Build tests to use own module cache by @artemcm in #1392
  • [Explicit Modules] Handle re-mapped platform versions when specifying SDK-aligned -clang-target on Darwin platforms by @artemcm in #1394
  • Downgrade priors version mismatch warning by @keith in #1396
  • [Macros on Darwin] Use device platform paths when building for the simulator by @DougGregor in #1400
  • Support Swift CompileJob Caching by @cachemeifyoucan in #1393
  • swift-help: use the executable suffix on Windows by @compnerd in #1405
  • Update Options.swift for '-load-plugin-executable' update by @rintaro in #1409
  • Teach version requests to include the version of the separately installed blocklist by @nkcsgexi in #1411
  • [Dependency Scanning] Add option to specify a separate Clang Dependency scanner module cache. by @artemcm in #1408
  • Add missing dependency by @neonichu in #1414
  • [Explicit Module Builds][Incremental Builds] Only re-build module dependencies which have changed or whose dependencies have changed. by @artemcm in #1413
  • [Explain Dependency] Fix 'testTraceDependency' to account for Swift Overlay dependencies being separate from direct import dependencies. by @artemcm in #1417
  • Deprecate -warn-on-potentially-unavailable-enum-case option by @tshortli in #1418
  • Add -experimental-lazy-typecheck option by @tshortli in #1419
  • Tests: Address warnings about unused results and variables by @tshortli in #1421
  • Pass -experimental-lazy-typecheck to -emit-module jobs by @tshortli in #1423
  • Fix comment typo in Triple+Platforms.swift by @MaxDesiatov in #1425
  • NFC: Consolidate the code for adding symbol graph related options to a job by @tshortli in #1426
  • Jobs: support multiple lookup paths by @compnerd in #1428
  • [Caching] Fix caching tests on windows hosts by @cachemeifyoucan in #1427
  • [Options] Fix makeOptions after upstream Option TableGen change by @cachemeifyoucan in #1429
  • Disable '-static' flag check on the executable against MUSL test by @artemcm in #1432
  • Forward --ld-path to linker driver by @kabiroberai in #1416
  • SwiftDriver: speculative changes to support Windows macros by @compnerd in #1433
  • Improve swift-driver test cases to avoid fatal crash by @cachemeifyoucan in #1434
  • [makeOptions] Add new enum from swift Options by @cachemeifyoucan in #1437
  • [Caching] Support pluginCAS in swiftDriver by @cachemeifyoucan in #1422
  • [ELF] Don't pass ELF shared objects to autolink-extract by @drodriguez in #1407
  • [Explicit Module Builds] Fix topological sort of inter-module dependency graph to account for Swift overlay dependencies by @artemcm in #1438
  • Update dependencies to track release/5.10 by @ahoppen in #1440
  • [5.10] Make --ld-path option consistently use single dash by @MaxDesiatov in #1442
  • [5.10 🍒][ExplicitModuleBuilds][Tests] Remove test of prior approach at handling dependency PCH by @artemcm in #1464
  • [5.10] Add -experimental-skip-non-exportable-decls to lazy typechecking emit module jobs by @tshortli in #1466
  • [5.10] Fix forwarding of -ld-path by @MaxDesiatov in #1454
  • [5.10] Remove special handling of the -experimental-lazy-typecheck flag by @tshortli in #1469
  • [5.10][Build Script Helper] Use new SwiftPM flag to remove $ORIGIN from installed ELF executable runpaths by @finagolfin in #1470
  • [5.10] Update Options.swift for '-disable-sandbox' by @rintaro in #1493
  • [5.10] Don't add an autolink-extract job unless actually linking ELF/Wasm objects, matching the original C++ driver by @finagolfin in #1480
  • For scripts, use DYLD_FRAMEWORK/LIBRARY_PATH to find frameworks / runtimes. by @lhames in #1531

New Contributors

Full Changelog: swift-5.9.2-RELEASE...swift-5.10-RELEASE

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-system
  • 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-ReleaseAssert/swift-.../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

Description

  • Swift Tools 5.7.0
View More Packages from this Author

Dependencies

Last updated: Fri May 17 2024 02:27:51 GMT-0900 (Hawaii-Aleutian Daylight Time)