1 #!/usr/bin/env runhaskell
7 import Distribution.PackageDescription
8 import Distribution.Simple
9 import Distribution.Simple.LocalBuildInfo
10 import Distribution.Simple.Utils
12 import System.FilePath
14 import System.Directory
15 import Control.Exception (try)
19 let hooks = defaultUserHooks {
20 buildHook = build_primitive_sources
21 $ buildHook defaultUserHooks
23 defaultMainWithHooks hooks
26 Mostly snarfed from ghc-prim's Setup.hs.
29 type Hook a = PackageDescription -> LocalBuildInfo -> UserHooks -> a -> IO ()
31 build_primitive_sources :: Hook a -> Hook a
32 build_primitive_sources f pd lbi uhs x
33 = do when (compilerFlavor (compiler lbi) == GHC) $ do
34 let genprimopcode = joinPath ["..", "..", "utils",
35 "genprimopcode", "genprimopcode"]
36 primops = joinPath ["..", "..", "compiler", "prelude",
38 primhs = joinPath ["Language", "Core", "PrimEnv.hs"]
39 primhs_tmp = addExtension primhs "tmp"
40 maybeExit $ system (genprimopcode ++ " --make-ext-core-source < "
41 ++ primops ++ " > " ++ primhs_tmp)
42 maybeUpdateFile primhs_tmp primhs
43 maybeExit $ system ("make -C lib/GHC_ExtCore")
46 -- Replace a file only if the new version is different from the old.
47 -- This prevents make from doing unnecessary work after we run 'setup makefile'
48 maybeUpdateFile :: FilePath -> FilePath -> IO ()
49 maybeUpdateFile source target = do
50 r <- rawSystem "cmp" ["-s" {-quiet-}, source, target]
52 ExitSuccess -> removeFile source
53 ExitFailure _ -> do try (removeFile target); renameFile source target