|
| 1 | +# Documentation |
| 2 | + |
| 3 | +## Usage |
| 4 | + |
| 5 | +### Declare SwiftPM dependency with nightly build |
| 6 | + |
| 7 | +1. Download and install the latest Trunk Development [toolchain](https://swift.org/download/#snapshots). |
| 8 | + |
| 9 | +2. Define the `TOOLCHAINS` environment variable as below to have the `swift` command point inside the toolchain: |
| 10 | + ```bash |
| 11 | + $ export TOOLCHAINS=swift |
| 12 | + ``` |
| 13 | + |
| 14 | +3. To make sure everything is set up correctly, check the result of `xcrun --find swift`. It should point inside the OSS toolchain. |
| 15 | + |
| 16 | +4. Add this entry to the `Package.swift` manifest of your project: |
| 17 | + |
| 18 | + ```swift |
| 19 | + // swift-tools-version:5.3 |
| 20 | + import PackageDescription |
| 21 | +
|
| 22 | + let package = Package( |
| 23 | + name: "MyTool", |
| 24 | + dependencies: [ |
| 25 | + .package(url: "https://github.com/apple/swift-syntax.git", .revision("swift-DEVELOPMENT-SNAPSHOT-2019-02-26")), |
| 26 | + ], |
| 27 | + targets: [ |
| 28 | + .target(name: "MyTool", dependencies: ["SwiftSyntax"]), |
| 29 | + ] |
| 30 | + ) |
| 31 | + ``` |
| 32 | + |
| 33 | +Tags will be created for every nightly build in the form of `swift-DEVELOPMENT-SNAPSHOT-<DATE>`. The revision field |
| 34 | +should be specified with the intended tag. |
| 35 | + |
| 36 | +Different from building SwiftSyntax from source, declaring SwiftSyntax as a SwiftPM dependency doesn't require |
| 37 | +the Swift compiler source because we always push gyb-generated files to a tag. |
| 38 | +
|
| 39 | +### Embedding SwiftSyntax in an Application |
| 40 | +
|
| 41 | +SwiftSyntax depends on the `lib_InternalSwiftSyntaxParser.dylib/.so` library which provides a C interface to the underlying Swift C++ parser. When you do `swift build` SwiftSyntax links and uses the library included in the Swift toolchain. If you are building an application make sure to embed `_InternalSwiftSyntaxParser` as part of your application's libraries. |
| 42 | + |
| 43 | +You can either copy `lib_InternalSwiftSyntaxParser.dylib/.so` directly from the toolchain or even build it yourself from the [Swift repository](https://github.com/apple/swift), as long as you are matching the same tags or branches in both the SwiftSyntax and Swift repositories. To build it for the host os (macOS/linux) use the following steps: |
| 44 | + |
| 45 | +```bash |
| 46 | +git clone https://github.com/apple/swift.git |
| 47 | +./swift/utils/update-checkout --clone |
| 48 | +./swift/utils/build-parser-lib --release --no-assertions --build-dir /tmp/parser-lib-build |
| 49 | +``` |
| 50 | + |
| 51 | +### Embedding in an iOS Application |
| 52 | + |
| 53 | +You need to build `lib_InternalSwiftSyntaxParser.dylib` yourself, you cannot copy it from the toolchain. Follow the instructions above and change the invocation of `build-parser-lib` accordingly: |
| 54 | + |
| 55 | +```bash |
| 56 | +./swift/utils/build-parser-lib --release --no-assertions --build-dir /tmp/parser-lib-build-iossim --host iphonesimulator --architectures x86_64 |
| 57 | +./swift/utils/build-parser-lib --release --no-assertions --build-dir /tmp/parser-lib-build-ios --host iphoneos --architectures arm64 |
| 58 | +``` |
| 59 | + |
| 60 | +## Contributing |
| 61 | + |
| 62 | +### Building SwiftSyntax from `main` |
| 63 | + |
| 64 | +Since SwiftSyntax relies on definitions in the main Swift repository to generate the layout of the syntax tree using `gyb`, a checkout of [apple/swift](https://github.com/apple/swift) is still required to build the latest development snapshot of SwiftSyntax. |
| 65 | + |
| 66 | +To build the `main` branch of SwiftSyntax, follow the following instructions: |
| 67 | + |
| 68 | +1. Check `swift-syntax` and `swift` out side by side: |
| 69 | + ``` |
| 70 | + - (enclosing directory) |
| 71 | + - swift |
| 72 | + - swift-syntax |
| 73 | + ``` |
| 74 | + |
| 75 | +2. Make sure you have a recent [Trunk Swift Toolchain](https://swift.org/download/#snapshots) installed. |
| 76 | +3. Define the `TOOLCHAINS` environment variable as below to have the `swift` command point inside the toolchain: |
| 77 | + |
| 78 | + ```bash |
| 79 | + $ export TOOLCHAINS=swift |
| 80 | + ``` |
| 81 | + |
| 82 | +4. To make sure everything is set up correctly, check the return statement of `xcrun --find swift`. It should point inside the latest installed trunk development toolchain. If it points inside an Xcode toolchain, check that you exported the `TOOLCHAINS` environment variable correctly. If it points inside a version-specific toolchain (like Swift 5.0-dev), you'll need to remove that toolchain. |
| 83 | +5. Run `swift-syntax/build-script.py`. |
| 84 | + If despite following those instructions, you get compiler errors, the Swift toolchain might be too old to contain recent changes in Swift's SwiftSyntaxParser C library. In that case, you'll have to build the compiler and SwiftSyntax together with the following command: |
| 85 | +
|
| 86 | + ```bash |
| 87 | + $ swift/utils/build-script --swiftsyntax --swiftpm --llbuild |
| 88 | + ``` |
| 89 | +
|
| 90 | +Swift-CI will automatically run the code generation step whenever a new toolchain (development snapshot or release) is published. It should thus almost never be necessary to perform the above build yourself. |
| 91 | +
|
| 92 | +Afterward, SwiftPM can also generate an Xcode project to develop SwiftSyntax by running `swift package generate-xcodeproj`. |
| 93 | +
|
| 94 | +If you also want to run tests locally, read the section below as testing has additional requirements. |
| 95 | +
|
| 96 | +### Local Testing |
| 97 | +
|
| 98 | +SwiftSyntax uses some test utilities that need to be built as part of the Swift compiler project. To build the most recent version of SwiftSyntax and test it, follow the steps in [swift/README.md](https://github.com/apple/swift/blob/main/README.md) and pass `--llbuild --swiftpm --swiftsyntax` to the build script invocation to build SwiftSyntax and all its dependencies using the current trunk (`main`) compiler. |
| 99 | +
|
| 100 | +SwiftSyntax can then be tested using the build script in `apple/swift` by running |
| 101 | +
|
| 102 | +```bash |
| 103 | +swift/utils/build-script --swiftsyntax --swiftpm --llbuild -t --skip-test-cmark --skip-test-swift --skip-test-llbuild --skip-test-swiftpm |
| 104 | +``` |
| 105 | +
|
| 106 | +This command will build SwiftSyntax and all its dependencies, tell the build script to run tests, but skip all tests but the SwiftSyntax tests. |
| 107 | +
|
| 108 | +Note that it is not currently supported by SwiftSyntax while building the Swift compiler using Xcode. |
| 109 | +
|
| 110 | +### CI Testing |
| 111 | +
|
| 112 | +Running `@swift-ci Please test` on the main Swift repository will also test the most recent version of SwiftSyntax. |
| 113 | +
|
| 114 | +Testing SwiftSyntax from its own repository is now available by commenting `@swift-ci Please test macOS platform`. |
0 commit comments