Skip to content

Commit d73a906

Browse files
committed
Add option to revert to dynamic stdlib
1 parent ef4620d commit d73a906

File tree

4 files changed

+48
-5
lines changed

4 files changed

+48
-5
lines changed

README.md

+28-1
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,37 @@ $ heroku config:set SWIFT_BUILD_CONFIGURATION=debug
8585
$ git commit -m "Change to debug configuration on Heroku" --allow-empty
8686
$ git push heroku master
8787
...
88-
remote: -----> Building package (debug configuration)
88+
remote: -----> Building package (debug configuration, static stdlib)
8989
...
9090
```
9191

92+
### Statically linked standard library
93+
94+
Swift supports static linkage for its standard library and Foundation, which has two advantages:
95+
96+
1. The resulting binary is more self-contained, and can run on other machines running the same version of the same distribution _without having to install the Swift toolchain._
97+
1. As only the relevant parts of the standard library/Foundation are copied into the binary, the resulting image will be smaller. A smaller image deploys faster too.
98+
99+
The buildpack uses static linkage by default.
100+
101+
However, if your project does not build, throwing errors about missing symbols for example, you can opt-in for the previous, dynamic linkage.
102+
103+
Define a configuration variable called `SWIFT_DYNAMIC_STDLIB` with any non-empty value, then deploy again.
104+
105+
```shell
106+
$ heroku config:set SWIFT_DYNAMIC_STDLIB=true
107+
$ git commit -m "Change to dynamic stdlib on Heroku" --allow-empty
108+
$ git push heroku master
109+
```
110+
111+
To use static linkage again, _unset_ the configuration variable and deploy again.
112+
113+
```shell
114+
$ heroku config:unset SWIFT_DYNAMIC_STDLIB
115+
$ git commit -m "Change to dynamic stdlib on Heroku" --allow-empty
116+
$ git push heroku master
117+
```
118+
92119
### Other build arguments
93120

94121
If you want to pass extra flags to the `swift build` command, you can do so by setting the `SWIFT_BUILD_FLAGS` config variable.

bin/compile

+8-1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,13 @@ if [ -f "$ENV_DIR/SWIFT_BUILD_FLAGS" ]; then
3636
SWIFT_BUILD_FLAGS=`cat "$ENV_DIR/SWIFT_BUILD_FLAGS"`
3737
fi
3838

39+
if [ -f "$ENV_DIR/SWIFT_DYNAMIC_STDLIB" ]; then
40+
SWIFT_DYNAMIC_STDLIB=`cat "$ENV_DIR/SWIFT_DYNAMIC_STDLIB" | tr -d '[[:space:]]'`
41+
SWIFT_BACKTRACE_EXECUTABLE="swift-backtrace"
42+
else
43+
SWIFT_BACKTRACE_EXECUTABLE="swift-backtrace-static"
44+
fi
45+
3946
mkdir -p "$CACHE_DIR/$STACK"
4047
source "$BIN_DIR/steps/swiftenv"
4148

@@ -55,4 +62,4 @@ mkdir -p $BUILD_DIR/.profile.d
5562

5663
set-env PATH '$HOME/.swift-bin:$PATH'
5764
set-env LD_LIBRARY_PATH '$LD_LIBRARY_PATH:$HOME/.swift-lib'
58-
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'
65+
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_EXECUTABLE"

bin/steps/swift-build

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,7 @@
1-
puts-step "Building package ($SWIFT_BUILD_CONFIGURATION configuration)"
2-
swift build $SWIFT_BUILD_FLAGS -c "$SWIFT_BUILD_CONFIGURATION" --static-swift-stdlib
1+
if [ ! -z "$SWIFT_DYNAMIC_STDLIB" ]; then
2+
puts-step "Building package ($SWIFT_BUILD_CONFIGURATION configuration, dynamic stdlib)"
3+
swift build $SWIFT_BUILD_FLAGS -c "$SWIFT_BUILD_CONFIGURATION"
4+
else
5+
puts-step "Building package ($SWIFT_BUILD_CONFIGURATION configuration, static stdlib)"
6+
swift build $SWIFT_BUILD_FLAGS -c "$SWIFT_BUILD_CONFIGURATION" --static-swift-stdlib
7+
fi

bin/steps/swift-install

+5-1
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,14 @@ puts-step "Installing dynamic libraries"
55
mkdir -p $BUILD_DIR/.swift-lib
66
find -L .build/$SWIFT_BUILD_CONFIGURATION -regex "$DYNAMIC_LIBRARY_REGEX" -type f -exec cp -a {} $BUILD_DIR/.swift-lib \; || true 2>/dev/null
77

8+
if [ ! -z "$SWIFT_DYNAMIC_STDLIB" ]; then
9+
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
10+
fi
11+
812
puts-step "Installing binaries"
913
mkdir -p $BUILD_DIR/.swift-bin
1014
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
11-
cp -a $SWIFT_PREFIX/usr/libexec/swift/linux/swift-backtrace-static $BUILD_DIR/.swift-bin
15+
cp -a $SWIFT_PREFIX/usr/libexec/swift/linux/$SWIFT_BACKTRACE_EXECUTABLE $BUILD_DIR/.swift-bin
1216

1317
puts-step "Installing resources"
1418
find -L .build/$SWIFT_BUILD_CONFIGURATION/* -regex '.*\.resources$' -exec cp -a {} $BUILD_DIR/.swift-bin \; || true 2>/dev/null

0 commit comments

Comments
 (0)