Last active
August 29, 2015 14:25
-
-
Save dkubb/842da22c212c944673b1 to your computer and use it in GitHub Desktop.
Create Statically Linked Haskell Binaries
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
*.hi | |
*.o | |
*.sw[op] | |
/.shake | |
/.stack-work | |
/dist | |
/output |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# LANGUAGE OverloadedStrings #-} | |
{-# LANGUAGE RecordWildCards #-} | |
import Turtle | |
import Prelude hiding (FilePath) | |
data Options = Options | |
{ repo :: Text | |
, file :: FilePath | |
} | |
parser :: Parser Options | |
parser = Options | |
<$> (optText "repo" 'r' "The docker repo name" <|> pure "nilcons/ghc-musl") | |
<*> argPath "file" "The source file path" | |
compile :: FilePath -> Text -> IO ExitCode | |
compile inFile repo = do | |
let inDir = directory inFile | |
shell (cmd inDir inDir repo inFile) empty | |
where | |
cmd = format ("docker run --volume "%fp%":"%fp%" --rm "%s%" ghc -O2 -threaded -optl-static -optl-s "%fp) | |
compress :: FilePath -> IO ExitCode | |
compress outFile = shell (cmd outFile) empty | |
where | |
cmd = format ("upx --best --ultra-brute --lzma -- "%fp) | |
main :: IO () | |
main = do | |
-- Parse command-line options | |
Options{..} <- options "Static Haskell Binary Creation" parser | |
-- Get the full input directory | |
inFile <- realpath file | |
-- Compile and compress the static binaries | |
status <- compile inFile repo .&&. compress (dropExtension inFile) | |
case status of | |
ExitSuccess -> return () | |
ExitFailure msg -> die ("compilation failure" <> repr msg) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: minimal-docker-image | |
version: 0.0.0 | |
cabal-version: >= 1.2 | |
executable Build | |
ghc-options: -Wall | |
-Werror | |
-fwarn-incomplete-record-updates | |
-fwarn-tabs | |
-fwarn-monomorphism-restriction | |
-fwarn-unused-do-bind | |
-fwarn-implicit-prelude | |
default-language: Haskell2010 | |
main-is: Build.hs | |
build-depends: base >= 4 && < 5, | |
turtle >= 1.2.0 && < 2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
resolver: nightly-2015-07-12 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment