#!/usr/bin/env runhaskell

import Distribution.Simple
import Distribution.Simple.Setup
import Distribution.PackageDescription
import System.Directory (createDirectoryIfMissing)
import System.Process (readProcess)

-- Adapted from
-- http://www.hyperedsoftware.com/blog/entries/build-info-gen.html by
-- Võ Minh Thu.

main :: IO ()
main = defaultMainWithHooks myHooks
  where myHooks = simpleUserHooks { preBuild = myPreBuild }

myPreBuild :: Args -> BuildFlags -> IO HookedBuildInfo
myPreBuild _ _ = do
  putStrLn "Generating dist/build/autogen/Build_futhark..."
  createDirectoryIfMissing True "dist/build/autogen/"

  desc <- readProcess "git" ["describe", "--dirty=-modified", "--always"] ""

  writeFile "dist/build/autogen/Build_futhark.hs" $ unlines
    [ "module Build_futhark where"
    , "gitCommit :: String"
    , "gitCommit = " ++ show (init desc)
    ]
  return emptyHookedBuildInfo

#!/usr/bin/env runhaskell

import Distribution.Simple
import Distribution.Simple.Setup
import Distribution.PackageDescription
import Distribution.Simple.BuildPaths
import Distribution.Simple.LocalBuildInfo
import System.Directory (createDirectoryIfMissing)
import System.Process (readProcess)

-- Adapted from
-- http://www.hyperedsoftware.com/blog/entries/build-info-gen.html by
-- Võ Minh Thu.

main :: IO ()
main = defaultMainWithHooks myHooks
  where myHooks = simpleUserHooks { postConf = generateBuildInfoModule }

generateBuildInfoModule :: Args -> ConfigFlags -> PackageDescription -> LocalBuildInfo -> IO ()
generateBuildInfoModule _ _ _ buildinfo = do
  let build_futhark_filename = autogenModulesDir buildinfo ++ "/Build_futhark.hs"
  putStrLn $ "Generating " ++ build_futhark_filename
  createDirectoryIfMissing True "dist/build/autogen/"

  desc <- readProcess "git" ["describe", "--dirty=-modified", "--always"] ""

  writeFile build_futhark_filename $ unlines
    [ "module Build_futhark where"
    , "gitCommit :: String"
    , "gitCommit = " ++ show (init desc)
    ]

Fix generation of the Futhark_build module.
Fix generation of the Futhark_build module.

This is now done as a post-configuration rather than as a pre-build
step.  The main improvement is that we can now build in a Cabal
sandbox (I hope).
