Skip to content

Commit

Permalink
Static linking and backtrace after Swift 5.9 (#77)
Browse files Browse the repository at this point in the history
* Link binary statically for drastic image size reductions

* Copy swift-backtrace binary to final image

* Set SWIFT_BACKTRACE env var automatically to enable it

* Remove support for old versions of Swift, add Swift 6.0

* Use fast symbolication
  • Loading branch information
vzsg authored Sep 21, 2024
1 parent b27549b commit 5836da7
Show file tree
Hide file tree
Showing 84 changed files with 63 additions and 2,321 deletions.
17 changes: 11 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,9 @@ $ heroku create --buildpack vapor/vapor

$ git push heroku main
remote: -----> Swift app detected
remote: -----> Using Swift 5.10.1 (default)
remote: -----> Using built-in clang (Swift 5.10.1)
remote: -----> Using Swift 6.0 (default)
remote: -----> Installing swiftenv
remote: -----> Installing Swift 5.10.1
remote: -----> Installing Swift 6.0
...
```

Expand Down Expand Up @@ -50,9 +49,9 @@ web: Run --env=production --port=$PORT

### Specify a Swift version

The buildpack defaults to Swift 5.10.1 and will be updated when new Swift versions are released.
The buildpack defaults to Swift 6.0, but is compatible with Swift 5.9 - 5.10.1.

If you need to use a specific version of the Swift toolchain, including older versions – for example Swift 4.2.x to retain compatibility with Swift 3 projects, or a previous version as you run into issues with the latest – you can pin any version number using a file called `.swift-version` in the root of the project folder, or by setting a `SWIFT_VERSION` configuration variable on Heroku, then deploying again.
If you need to use a specific older version of the Swift toolchain from this range, you can pin the version number using a file called `.swift-version` in the root of the project folder, or by setting a `SWIFT_VERSION` configuration variable on Heroku, then deploying again.

```shell
$ echo '5.9.2' > .swift-version
Expand All @@ -69,7 +68,13 @@ $ git commit -m "Pin Swift version to 5.9.2" --allow-empty
$ git push heroku main
```

The version format used file is compatible with [swiftenv](http://github.com/kylef/swiftenv).
#### Legacy versions

If your project requires an even older version of Swift, you can use a previous release of the buildpack via an old tag.

```shell
$ heroku buildpacks:set https://github.com/vapor-community/heroku-buildpack.git#5101-release
```

### Active build configuration

Expand Down
13 changes: 2 additions & 11 deletions bin/compile
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@ BUILD_DIR=$1
CACHE_DIR=$2
ENV_DIR=$3

CLANG_VERSION=7.0.1

SWIFT_VERSION="5.10.1"
SWIFT_VERSION="6.0"
SWIFT_BUILD_CONFIGURATION="release"
SWIFT_BUILD_FLAGS=""

Expand All @@ -38,15 +36,7 @@ if [ -f "$ENV_DIR/SWIFT_BUILD_FLAGS" ]; then
SWIFT_BUILD_FLAGS=`cat "$ENV_DIR/SWIFT_BUILD_FLAGS"`
fi


mkdir -p "$CACHE_DIR/$STACK"

if [[ $SWIFT_VERSION = 5* ]]; then
puts-step "Using built-in clang (Swift $SWIFT_VERSION)"
else
source "$BIN_DIR/steps/clang"
fi

source "$BIN_DIR/steps/swiftenv"

cd $BUILD_DIR
Expand All @@ -65,3 +55,4 @@ mkdir -p $BUILD_DIR/.profile.d

set-env PATH '$HOME/.swift-bin:$PATH'
set-env LD_LIBRARY_PATH '$LD_LIBRARY_PATH:$HOME/.swift-lib'
set-env SWIFT_BACKTRACE 'enable=yes,sanitize=yes,threads=all,images=all,interactive=no,output-to=stderr,symbolicate=fast,swift-backtrace=.swift-bin/swift-backtrace-static'
9 changes: 0 additions & 9 deletions bin/steps/clang

This file was deleted.

2 changes: 1 addition & 1 deletion bin/steps/swift-build
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
puts-step "Building package ($SWIFT_BUILD_CONFIGURATION configuration)"
swift build $SWIFT_BUILD_FLAGS -c "$SWIFT_BUILD_CONFIGURATION"
swift build $SWIFT_BUILD_FLAGS -c "$SWIFT_BUILD_CONFIGURATION" --static-swift-stdlib
5 changes: 2 additions & 3 deletions bin/steps/swift-install
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
DYNAMIC_LIBRARY_REGEX=".*\.so\(\.[0-9]+\)*\'"
SWIFT_PREFIX="$(swiftenv prefix)"

puts-step "Installing dynamic libraries"
mkdir -p $BUILD_DIR/.swift-lib
SWIFT_PREFIX="$(swiftenv prefix)"
find -L $SWIFT_PREFIX/usr/lib/swift/linux -regex "$DYNAMIC_LIBRARY_REGEX" -type f -exec cp -a {} $BUILD_DIR/.swift-lib \; || true 2>/dev/null
find -L .build/$SWIFT_BUILD_CONFIGURATION -regex "$DYNAMIC_LIBRARY_REGEX" -type f -exec cp -a {} $BUILD_DIR/.swift-lib \; || true 2>/dev/null
cp -a /usr/lib/x86_64-linux-gnu/libatomic* $BUILD_DIR/.swift-lib

puts-step "Installing binaries"
mkdir -p $BUILD_DIR/.swift-bin
find -L .build/$SWIFT_BUILD_CONFIGURATION ! -regex "$DYNAMIC_LIBRARY_REGEX" -type f -perm /a+x -exec cp -a {} $BUILD_DIR/.swift-bin \; || true 2>/dev/null
cp -a $SWIFT_PREFIX/usr/libexec/swift/linux/swift-backtrace-static $BUILD_DIR/.swift-bin

puts-step "Installing resources"
find -L .build/$SWIFT_BUILD_CONFIGURATION/* -regex '.*\.resources$' -exec cp -a {} $BUILD_DIR/.swift-bin \; || true 2>/dev/null
Expand Down
7 changes: 0 additions & 7 deletions swiftenv/share/swiftenv-build/2.2

This file was deleted.

7 changes: 0 additions & 7 deletions swiftenv/share/swiftenv-build/2.2-dev

This file was deleted.

7 changes: 0 additions & 7 deletions swiftenv/share/swiftenv-build/2.2.1

This file was deleted.

13 changes: 0 additions & 13 deletions swiftenv/share/swiftenv-build/3.0

This file was deleted.

13 changes: 0 additions & 13 deletions swiftenv/share/swiftenv-build/3.0-dev

This file was deleted.

13 changes: 0 additions & 13 deletions swiftenv/share/swiftenv-build/3.0.1

This file was deleted.

13 changes: 0 additions & 13 deletions swiftenv/share/swiftenv-build/3.0.2

This file was deleted.

13 changes: 0 additions & 13 deletions swiftenv/share/swiftenv-build/3.1

This file was deleted.

13 changes: 0 additions & 13 deletions swiftenv/share/swiftenv-build/3.1-dev

This file was deleted.

13 changes: 0 additions & 13 deletions swiftenv/share/swiftenv-build/3.1.1

This file was deleted.

13 changes: 0 additions & 13 deletions swiftenv/share/swiftenv-build/4.0

This file was deleted.

13 changes: 0 additions & 13 deletions swiftenv/share/swiftenv-build/4.0-dev

This file was deleted.

13 changes: 0 additions & 13 deletions swiftenv/share/swiftenv-build/4.0.2

This file was deleted.

13 changes: 0 additions & 13 deletions swiftenv/share/swiftenv-build/4.0.3

This file was deleted.

13 changes: 0 additions & 13 deletions swiftenv/share/swiftenv-build/4.1

This file was deleted.

13 changes: 0 additions & 13 deletions swiftenv/share/swiftenv-build/4.1-dev

This file was deleted.

13 changes: 0 additions & 13 deletions swiftenv/share/swiftenv-build/4.2-dev

This file was deleted.

13 changes: 0 additions & 13 deletions swiftenv/share/swiftenv-build/5.0-dev

This file was deleted.

24 changes: 0 additions & 24 deletions swiftenv/share/swiftenv-install/3.0

This file was deleted.

28 changes: 0 additions & 28 deletions swiftenv/share/swiftenv-install/3.0.1

This file was deleted.

Loading

0 comments on commit 5836da7

Please sign in to comment.