Skip to content

Instantly share code, notes, and snippets.

@dkubb
Last active August 29, 2015 14:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dkubb/842da22c212c944673b1 to your computer and use it in GitHub Desktop.
Save dkubb/842da22c212c944673b1 to your computer and use it in GitHub Desktop.
Create Statically Linked Haskell Binaries
*.hi
*.o
*.sw[op]
/.shake
/.stack-work
/dist
/output
{-# 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)
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
resolver: nightly-2015-07-12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment