Search code examples
windowshaskelldeploymentghc-api

Simple way to have the GHC API for application deployed on Windows


I want to deploy an application on Windows that needs to access the GHC API. Using the first simple example from the Wiki:

http://www.haskell.org/haskellwiki/GHC/As_a_library

results in the following error (compiled on one machine with haskell platform and executed on another clean windows install): test.exe: can't find a package database at C:\haskell\lib\package.conf.d

I'd like to deploy my application as a simple zip file and not require the user to have anything installed. Is there a straightforward way to include the needed GHC things in that zip file so it will work?


Solution

  • This program will copy necessary files to the specified directory (will work only on windows):

    import Data.List (isSuffixOf)
    import System.Environment (getArgs)
    import GHC.Paths (libdir)
    import System.Directory
    import System.FilePath 
    import System.Cmd
    
    main = do
      [to] <- getArgs
      let libdir' = to </> "lib"
      createDirectoryIfMissing True libdir'
      copy libdir libdir'
      rawSystem "xcopy"
        [ "/e", "/q"
        , dropFileName libdir </> "mingw"
        , to </> "mingw\\"]
    
    
    -- | skip some files while copying
    uselessFile f
      = or $ map (`isSuffixOf` f)
        [ "."
        , "_debug.a"
        , "_p.a", ".p_hi"    -- libraries built with profiling
        , ".dyn_hi", ".dll"] -- dynamic libraries
    
    
    copy from to
      = getDirectoryContents from
      >>= mapM_ copy' . filter (not . uselessFile)
      where
        copy' f = do
          let (from', to') = (from </> f, to </> f)
          isDir <- doesDirectoryExist from'
          if isDir
              then createDirectory to' >> copy from' to'
              else copyFile from' to'
    

    After running it with destination directory as argument you will have a local copy of lib and mingw (about 300 Mb total).

    You can remove unused libraries from lib to save more space.