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 c07e452
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 7 deletions.
23 changes: 19 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,21 @@ cabal2nix:
cd nix && \
cabal2nix --no-haddock .. > default.nix)

.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/nixos/nix &&\
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
14 changes: 12 additions & 2 deletions nix/release-static.nix
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,22 @@ 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"
Expand Down
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
13 changes: 13 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 @@ -66,6 +70,9 @@ executables:
when:
- condition: flag(library-only)
buildable: false
- condition: flag(static)
cc-options: -static -fPIC
ld-options: -static -fPIC
server:
main: Main.hs
source-dirs: src/cmd/server
Expand All @@ -78,6 +85,9 @@ executables:
when:
- condition: flag(library-only)
buildable: false
- condition: flag(static)
cc-options: -static -fPIC
ld-options: -static -fPIC

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

tests:
performabot-test:
Expand Down
18 changes: 17 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: 55ae048beeae3bd31764ba2dd9cdd871a760148f1c1ff32eab03b1c7efb8f969

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 @@ -106,8 +111,13 @@ library
if (flag(dev)) || (flag(library-only))
ghc-options: -fwarn-tabs -O0
cpp-options: -DDEVELOPMENT
cc-options: -static -fPIC
ld-options: -static -fPIC
else
ghc-options: -fwarn-tabs -O2
if flag(static)
cc-options: -static -fPIC
ld-options: -static -fPIC
default-language: Haskell2010

executable client
Expand All @@ -126,6 +136,9 @@ executable client
, performabot
if flag(library-only)
buildable: False
if flag(static)
cc-options: -static -fPIC
ld-options: -static -fPIC
default-language: Haskell2010

executable server
Expand All @@ -141,6 +154,9 @@ executable server
, performabot
if flag(library-only)
buildable: False
if flag(static)
cc-options: -static -fPIC
ld-options: -static -fPIC
default-language: Haskell2010

test-suite performabot-test
Expand Down

0 comments on commit c07e452

Please sign in to comment.