Skip to content

Commit

Permalink
Fix static build
Browse files Browse the repository at this point in the history
Signed-off-by: Sascha Grunert <mail@saschagrunert.de>
  • Loading branch information
saschagrunert committed Jun 18, 2019
1 parent ed750cb commit b585ed6
Show file tree
Hide file tree
Showing 10 changed files with 117 additions and 16 deletions.
19 changes: 19 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,16 @@ executors:
container:
docker:
- image: nixos/nix
performabot-build:
docker:
- image: saschagrunert/performabot-build

workflows:
version: 2
pipeline:
jobs:
- build
- build-static
- doc
# - doc-publish:
# requires:
Expand Down Expand Up @@ -49,6 +53,21 @@ jobs:
path: result/lib
destination: lib

build-static:
executor: performabot-build
steps:
- <<: *prepare-env
- checkout
- run:
name: build
command: make build-static
- store_artifacts:
path: result/bin
destination: bin
- store_artifacts:
path: result/lib
destination: lib

doc:
executor: container
steps:
Expand Down
5 changes: 5 additions & 0 deletions Dockerfile-build
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# vim: set syntax=dockerfile:
FROM nixos/nix
COPY . /build
RUN nix-build --no-out-link build/nix/release-static.nix
RUN rm -rf build
4 changes: 4 additions & 0 deletions Dockerfile-client
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# vim: set syntax=dockerfile:
FROM scratch
COPY result/bin/client /
ENTRYPOINT ["/client"]
4 changes: 4 additions & 0 deletions Dockerfile-server
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# vim: set syntax=dockerfile:
FROM scratch
COPY result/bin/server /
ENTRYPOINT ["/server"]
49 changes: 39 additions & 10 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,7 @@ GLOB_SCSS := config/bulma.scss
BULMA_DIR := $(BUILD_DIR)/bulma
BULMA_TAG := 0.7.5
BULMA_URL := https://github.com/jgthms/bulma

all: cabal2nix build

.PHONY: build
build:
nix-build nix/release.nix

.PHONY: build-static
build-static:
nix-build nix/release-static.nix
CONTAINER_RUNTIME := sudo podman

define nix-shell
nix-shell nix/shell.nix $(1)
Expand All @@ -30,6 +21,30 @@ define nix-shell-pure-run
$(call nix-shell-pure,--run "$(1)")
endef

define image-build
$(CONTAINER_RUNTIME) build --pull -f Dockerfile-$(1) -t performabot-$(1) .
endef


all: cabal2nix build

.PHONY: build
build:
nix-build nix/release.nix

.PHONY: build-static
build-static:
nix-build nix/release-static.nix

.PHONY: build-static-with-image
build-static-with-image:
$(CONTAINER_RUNTIME) run --rm -it -v $(shell pwd):/build \
saschagrunert/performabot-build sh -c "\
export BUILD_DIR=build/build/bin &&\
mkdir -p \$$BUILD_DIR &&\
cp result/bin/* \$$BUILD_DIR &&\
chown -R $(shell id -u):$(shell id -g) \$$BUILD_DIR"

.PHONY: cabal2nix
cabal2nix:
$(call nix-shell-pure-run,\
Expand Down Expand Up @@ -61,6 +76,20 @@ floskell:
hlint:
$(call nix-shell-pure-run,hlint -g)

.PHONY: image-build
image-build:
$(call image-build,build)

.PHONY: image-client
image-client:
$(nix-shell-pure-run,hack/is-static result/bin/client)
$(call image-build,client)

.PHONY: image-server
image-server:
$(nix-shell-pure-run,hack/is-static result/bin/server)
$(call image-build,server)

.PHONY: lint
lint: bulma cabal2nix floskell hlint
$(call nix-shell-pure-run,git diff --exit-code)
Expand Down
11 changes: 11 additions & 0 deletions hack/is-static
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/usr/bin/env bash
if [ ! -f $1 ]; then
echo "File '$1' does not exist"
exit 1
fi

file $1 | grep "statically linked" | grep -q stripped
if [ $? -ne 0 ]; then
echo "Binary does not seem to be statically linked"
exit 1
fi
20 changes: 15 additions & 5 deletions nix/release-static.nix
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,25 @@ let
pkgs = (import ./nixpkgs.nix { }).pkgsMusl;
in
(pkgs.haskellPackages.callPackage ./default.nix { }).overrideAttrs(old: {
doCheck = false;
doHaddock = false;
appendConfigureFlags = ["-f static"];
enableSharedExecutables = false;
enableSharedLibraries = true;
configureFlags = [
"--ghc-option=-optl=-static"
"--ghc-option=-fPIC"
"-f static"
"--extra-lib-dirs=${pkgs.gmp6.override { withStatic = true; }}/lib"
"--extra-lib-dirs=${pkgs.sqlite.overrideAttrs (old: {
configureFlags = old.configureFlags
++ [ "--disable-shared" "--enable-static" ];
postInstall = old.postInstall + ''
mkdir -p $bin/lib
cp $out/lib/libsqlite3.a $bin/lib
'';
})}/lib"
"--extra-lib-dirs=${pkgs.zlib.static}/lib"
"--extra-lib-dirs=${pkgs.libffi.overrideAttrs
(old: { dontDisableStatic = true; })}/lib"
"--disable-executable-stripping"
"--extra-lib-dirs=${pkgs.libffi.overrideAttrs (old: {
dontDisableStatic = true;
})}/lib"
];
})
4 changes: 4 additions & 0 deletions nix/shell.nix
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,20 @@ let
in
pkgs.stdenv.mkDerivation {
buildInputs = with pkgs; project.env.nativeBuildInputs ++ [
bash
buildah
cabal-install
cabal2nix
expect
file
git
glibcLocales
haskellPackages.floskell
haskellPackages.hpc-coveralls
haskellPackages.yesod-bin
hlint
nix-prefetch-git
runc
sass
sqlite
wget
Expand Down
7 changes: 7 additions & 0 deletions package.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ flags:
default: false
description: Turn on development settings, like auto-reload templates.
manual: false
static:
default: false
description: Build static binaries
manual: false

ghc-options:
- -Werror
Expand Down Expand Up @@ -138,6 +142,9 @@ library:
ghc-options:
- -fwarn-tabs
- -O2
- condition: flag(static)
cc-options: -static -fPIC
ld-options: -static -fPIC

tests:
performabot-test:
Expand Down
10 changes: 9 additions & 1 deletion performabot.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ cabal-version: 1.12
--
-- see: https://github.com/sol/hpack
--
-- hash: 17163459e38977ea3fe426d1f52b7d7e0a9773c789d2f5626ca94a22c2c5b58d
-- hash: 38eac125c0d21f366d8c3dabe54aaaee9b298efc0badf2725f54a765d580e700

name: performabot
version: 0.1.0
Expand Down Expand Up @@ -33,6 +33,11 @@ flag library-only
manual: False
default: False

flag static
description: Build static binaries
manual: False
default: False

library
exposed-modules:
Env
Expand Down Expand Up @@ -108,6 +113,9 @@ library
cpp-options: -DDEVELOPMENT
else
ghc-options: -fwarn-tabs -O2
if flag(static)
cc-options: -static -fPIC
ld-options: -static -fPIC
default-language: Haskell2010

executable client
Expand Down

0 comments on commit b585ed6

Please sign in to comment.