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 16, 2019
1 parent ed750cb commit 4126e44
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 10 deletions.
35 changes: 31 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,19 @@ build-static:
nix-build nix/release-static.nix

define nix-shell
nix-shell nix/shell.nix $(1)
$(2) nix-shell nix/shell.nix $(1)
endef

define nix-shell-pure
$(call nix-shell,--pure $(1))
$(call nix-shell,--pure $(1),$(2))
endef

define nix-shell-run
$(call nix-shell,--run "$(1)")
$(call nix-shell,--run "$(1)",$(2))
endef

define nix-shell-pure-run
$(call nix-shell-pure,--run "$(1)")
$(call nix-shell-pure,--run "$(1)",$(2))
endef

.PHONY: cabal2nix
Expand All @@ -36,6 +36,33 @@ cabal2nix:
cd nix && \
cabal2nix --no-haddock .. > default.nix)

.PHONY: container-client
container-client:
$(call nix-shell-pure-run,\
export NAME=performabot-work &&\
buildah rm \$$NAME || true &&\
buildah from --name \$$NAME scratch &&\
buildah copy \$$NAME result/bin/client / &&\
buildah config --cmd /client \$$NAME &&\
buildah commit \$$NAME performabot-client &&\
buildah rm \$$NAME \
,sudo)

.PHONY: container-server
container-server:
$(call nix-shell-pure-run,\
export NAME=performabot-work &&\
buildah rm \$$NAME || true &&\
buildah from --pull-always --name \$$NAME \
docker.io/library/alpine &&\
buildah run -v \$$(pwd):/build \$$NAME sh -c '\
nix-build build/nix/release.nix &&\
cp result/bin/server /usr/local/bin \
' &&\
buildah commit \$$NAME performabot-server &&\
buildah rm \$$NAME \
,sudo)

.PHONY: clean
clean:
$(call nix-shell-pure-run,git clean -fdx)
Expand Down
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"
];
})
2 changes: 2 additions & 0 deletions nix/shell.nix
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ let
in
pkgs.stdenv.mkDerivation {
buildInputs = with pkgs; project.env.nativeBuildInputs ++ [
buildah
cabal-install
cabal2nix
expect
Expand All @@ -24,6 +25,7 @@ pkgs.stdenv.mkDerivation {
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 4126e44

Please sign in to comment.