Skip to content

Commit 59d143d

Browse files
authored
Merge pull request #5267 from commercialhaskell/static-linux
Static linux
2 parents 556543d + 71405b1 commit 59d143d

File tree

6 files changed

+68
-33
lines changed

6 files changed

+68
-33
lines changed

.azure/azure-linux-template.yml

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,26 +10,36 @@ jobs:
1010
GHC 8.4:
1111
BUILD: stack
1212
STACK_YAML: stack-ghc-84.yaml
13+
EXTRA_SUFFIX: ""
1314
GHC 8.6:
1415
BUILD: stack
1516
STACK_YAML: stack-ghc-86.yaml
17+
EXTRA_SUFFIX: ""
1618
GHC 8.8:
1719
BUILD: stack
1820
STACK_YAML: stack-ghc-88.yaml
21+
EXTRA_SUFFIX: ""
22+
Alpine:
23+
BUILD: stack
24+
STACK_YAML: stack.yaml
25+
EXTRA_SUFFIX: "alpine"
26+
STACK_ARGS: --docker --system-ghc --no-install-ghc --flag stack:static
1927
style:
2028
BUILD: style
29+
EXTRA_SUFFIX: ""
2130
pedantic:
2231
BUILD: pedantic
2332
STACK_YAML: stack.yaml
33+
EXTRA_SUFFIX: ""
2434
steps:
2535
- script: |
2636
export STACK_ROOT="$(Build.SourcesDirectory)"/.stack-root;
2737
mkdir -p ~/.local/bin
2838
curl -f -L "https://github.com/fpco/cache-s3/releases/download/${CACHE_S3_VERSION}/cache-s3-${CACHE_S3_VERSION}-${OS_NAME}-x86_64.tar.gz" -o ~/.local/bin/cache-s3.tar.gz
2939
tar xzf ~/.local/bin/cache-s3.tar.gz -C ~/.local/bin
3040
export PATH=$HOME/.local/bin:$PATH;
31-
cache-s3 --prefix="${CACHE_S3_PREFIX}" --git-branch="$(Build.SourceBranchName)" --suffix="${OS_NAME}" restore stack --base-branch="${BASE_BRANCH}"
32-
cache-s3 --prefix="${CACHE_S3_PREFIX}" --git-branch="$(Build.SourceBranchName)" --suffix="${OS_NAME}" restore stack work --base-branch="${BASE_BRANCH}"
41+
cache-s3 --prefix="${CACHE_S3_PREFIX}" --git-branch="$(Build.SourceBranchName)" --suffix="${OS_NAME}${EXTRA_SUFFIX}" restore stack --base-branch="${BASE_BRANCH}"
42+
cache-s3 --prefix="${CACHE_S3_PREFIX}" --git-branch="$(Build.SourceBranchName)" --suffix="${OS_NAME}${EXTRA_SUFFIX}" restore stack work --base-branch="${BASE_BRANCH}"
3343
etc/scripts/ci-setup.sh
3444
case "$BUILD" in
3545
style)
@@ -38,11 +48,9 @@ jobs:
3848
;;
3949
*)
4050
export PATH=$HOME/.local/bin:$PATH
41-
stack --install-ghc $ARGS test --bench --only-dependencies
51+
stack test $STACK_ARGS --bench --only-dependencies
4252
;;
4353
esac
44-
GHC_OPTIONS="-Werror"
45-
if [ "$GHCVER" = "8.2.1" ]; then GHC_OPTIONS="$GHC_OPTIONS -Wno-missing-home-modules"; fi
4654
set -ex
4755
case "$BUILD" in
4856
style)
@@ -51,7 +59,21 @@ jobs:
5159
hlint test/ --cpp-simple
5260
;;
5361
stack)
54-
stack test --haddock --no-haddock-deps --ghc-options="$GHC_OPTIONS"
62+
stack test $STACK_ARGS --haddock --no-haddock-deps --ghc-options="-Werror" --copy-bins --local-bin-path bin
63+
64+
# Get output about whether the exe is dynamically linked
65+
if [[ "$EXTRA_SUFFIX" == "alpine" ]]
66+
then
67+
# ldd returns exit code 1 if it's static, so failure is success
68+
(ldd ./bin/stack && exit 1) || true
69+
else
70+
ldd ./bin/stack
71+
fi
72+
73+
# Make sure we can run the executable on this OS
74+
# Important to make sure the Alpine exe is properly static
75+
./bin/stack --version
76+
5577
;;
5678
pedantic)
5779
stack --system-ghc build --pedantic
@@ -94,9 +116,9 @@ jobs:
94116
export AWS_SECRET_ACCESS_KEY="$(AWS_SECRET_ACCESS_KEY)";
95117
export STACK_ROOT="$(Build.SourcesDirectory)"/.stack-root;
96118
if [ "$(Build.SourceBranchName)" = "${BASE_BRANCH}" ]; then
97-
cache-s3 --prefix="${CACHE_S3_PREFIX}" --git-branch="$(Build.SourceBranchName)" --suffix="${OS_NAME}" save stack;
119+
cache-s3 --prefix="${CACHE_S3_PREFIX}" --git-branch="$(Build.SourceBranchName)" --suffix="${OS_NAME}${EXTRA_SUFFIX}" save stack;
98120
fi;
99-
cache-s3 --prefix="${CACHE_S3_PREFIX}" --git-branch="$(Build.SourceBranchName)" --suffix="${OS_NAME}" save stack work
121+
cache-s3 --prefix="${CACHE_S3_PREFIX}" --git-branch="$(Build.SourceBranchName)" --suffix="${OS_NAME}${EXTRA_SUFFIX}" save stack work
100122
condition: and(succeeded(), in(variables['Build.Reason'], 'IndividualCI', 'BatchedCI'))
101123
env:
102124
OS_NAME: ${{ parameters.os }}

.azure/azure-nightly-template-linux.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ jobs:
4040
nix-channel --update # Get GHC 8.2.2
4141
export STACK_ROOT="$(Build.SourcesDirectory)"/.stack-root;
4242
export PATH=$HOME/.local/bin:$PATH;
43-
stack etc/scripts/release.hs check
43+
stack etc/scripts/release.hs check --alpine
4444
4545
set +ex
4646
displayName: Integration Test
@@ -49,7 +49,7 @@ jobs:
4949
5050
export STACK_ROOT="$(Build.SourcesDirectory)"/.stack-root;
5151
export PATH=$HOME/.local/bin:$PATH;
52-
stack etc/scripts/release.hs build
52+
stack etc/scripts/release.hs build --alpine
5353
cp _release/stack-* $(Build.ArtifactStagingDirectory)
5454
5555
set +ex

etc/scripts/release.hs

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,12 @@ options =
139139
"Label to give the uploaded release asset"
140140
, Option "" [noTestHaddocksOptName] (NoArg $ Right $ \g -> g{gTestHaddocks = False})
141141
"Disable testing building haddocks."
142-
, Option "" [staticOptName] (NoArg $ Right $ \g -> g{gBuildArgs = gBuildArgs g ++ ["--flag=stack:static"]})
143-
"Build a static binary."
142+
, Option "" [alpineOptName]
143+
(NoArg $ Right $ \g ->
144+
g{gBuildArgs =
145+
gBuildArgs g ++
146+
["--flag=stack:static", "--docker", "--system-ghc", "--no-install-ghc"]})
147+
"Build a static binary using Alpine Docker image."
144148
, Option "" [buildArgsOptName]
145149
(ReqArg
146150
(\v -> Right $ \g -> g{gBuildArgs = gBuildArgs g ++ words v})
@@ -196,18 +200,19 @@ rules global@Global{..} args = do
196200
Stdout dirty <- cmd "git status --porcelain"
197201
when (not gAllowDirty && not (null (trim dirty))) $
198202
error ("Working tree is dirty. Use --" ++ allowDirtyOptName ++ " option to continue anyway.")
199-
withTempDir $ \tmpDir -> do
200-
let cmd0 c = cmd [gProjectRoot </> releaseBinDir </> binaryName </> stackExeFileName]
201-
(stackArgs global)
202-
["--local-bin-path=" ++ tmpDir]
203-
c
204-
() <- cmd0 "install" gBuildArgs integrationTestFlagArgs $ concat $ concat
205-
[["--pedantic --no-haddock-deps "]
206-
,[" --haddock" | gTestHaddocks]
207-
,[" stack"]]
208-
let cmd' c = cmd (AddPath [tmpDir] []) stackProgName (stackArgs global) c
209-
() <- cmd' "test" gBuildArgs integrationTestFlagArgs "--pedantic --exec stack-integration-test stack"
210-
return ()
203+
() <- cmd
204+
[gProjectRoot </> releaseBinDir </> binaryName </> stackExeFileName]
205+
(stackArgs global)
206+
["build"]
207+
gBuildArgs
208+
integrationTestFlagArgs
209+
["--pedantic", "--no-haddock-deps", "--test"]
210+
["--haddock" | gTestHaddocks]
211+
["stack"]
212+
() <- cmd
213+
[gProjectRoot </> releaseBinDir </> binaryName </> stackExeFileName]
214+
["exec"]
215+
[gProjectRoot </> releaseBinDir </> binaryName </> "stack-integration-test"]
211216
copyFileChanged (releaseBinDir </> binaryName </> stackExeFileName) out
212217

213218
unless gUploadOnly $ releaseDir </> binaryPkgZipFileName %> \out -> do
@@ -242,7 +247,7 @@ rules global@Global{..} args = do
242247
unless gUploadOnly $ releaseDir </> binaryExeFileName %> \out -> do
243248
need [releaseBinDir </> binaryName </> stackExeFileName]
244249
(Stdout versionOut) <- cmd (releaseBinDir </> binaryName </> stackExeFileName) "--version"
245-
-- () <- cmd "git diff"
250+
() <- cmd "git diff"
246251
when (not gAllowDirty && "dirty" `isInfixOf` lower versionOut) $
247252
error ("Refusing continue because 'stack --version' reports dirty. Use --" ++
248253
allowDirtyOptName ++ " option to continue anyway.")
@@ -265,8 +270,8 @@ rules global@Global{..} args = do
265270
,out])
266271
(removeFile out)
267272
Linux ->
268-
cmd "strip -p --strip-unneeded --remove-section=.comment -o"
269-
[out, releaseBinDir </> binaryName </> stackExeFileName]
273+
-- Using Ubuntu's strip to strip an Alpine exe doesn't work, so just copy
274+
liftIO $ copyFile (releaseBinDir </> binaryName </> stackExeFileName) out
270275
_ ->
271276
cmd "strip -o"
272277
[out, releaseBinDir </> binaryName </> stackExeFileName]
@@ -534,9 +539,9 @@ noTestHaddocksOptName = "no-test-haddocks"
534539
buildArgsOptName :: String
535540
buildArgsOptName = "build-args"
536541

537-
-- | @--static@ command-line option name.
538-
staticOptName :: String
539-
staticOptName = "static"
542+
-- | @--alpine@ command-line option name.
543+
alpineOptName :: String
544+
alpineOptName = "alpine"
540545

541546
-- | @--certificate-name@ command-line option name.
542547
certificateNameOptName :: String
@@ -548,7 +553,7 @@ uploadOnlyOptName = "upload-only"
548553

549554
-- | Arguments to pass to all 'stack' invocations.
550555
stackArgs :: Global -> [String]
551-
stackArgs Global{..} = ["--install-ghc", "--arch=" ++ display gArch, "--interleaved-output"]
556+
stackArgs Global{..} = ["--arch=" ++ display gArch, "--interleaved-output"]
552557

553558
-- | Name of the 'stack' program.
554559
stackProgName :: FilePath

package.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,10 @@ executables:
302302
when:
303303
- condition: ! '!(flag(integration-tests))'
304304
buildable: false
305+
- condition: flag(static)
306+
ld-options:
307+
- -static
308+
- -pthread
305309
tests:
306310
stack-test:
307311
main: Spec.hs

stack.cabal

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ cabal-version: 2.0
44
--
55
-- see: https://github.com/sol/hpack
66
--
7-
-- hash: f003cdea0be4961a87f3e62f3f9e5fee3e29924f746a6154a15ac18985deb871
7+
-- hash: 04f6dd3ff3b1b0f7db0a0ad6192866ad73926008fd5df9215d6abae839da8874
88

99
name: stack
1010
version: 2.3.0.2
@@ -553,6 +553,8 @@ executable stack-integration-test
553553
unix
554554
if !(flag(integration-tests))
555555
buildable: False
556+
if flag(static)
557+
ld-options: -static -pthread
556558
default-language: Haskell2010
557559

558560
test-suite stack-test

stack.yaml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ packages:
55

66
docker:
77
enable: false
8-
repo: fpco/stack-build:lts-14.27
8+
9+
#image: fpco/alpine-haskell-stack:8.6.5
10+
image: fpco/alpine-haskell-stack@sha256:49e7e15f3b1d3f882ba5bb701463b1d508fbf40e5aafce6ea31acd210da570ba
911

1012
nix:
1113
# --nix on the command-line to enable.

0 commit comments

Comments
 (0)