2 We need to do some ugly hacks here as base mix of portable and
3 unportable stuff, as well as home to some GHC magic.
6 module Main (main) where
10 import Distribution.PackageDescription
11 import Distribution.Simple
12 import Distribution.Simple.LocalBuildInfo
13 import Distribution.Simple.Utils
15 import System.FilePath
17 import System.Directory
18 import Control.Exception (try)
21 main = do let hooks = defaultUserHooks {
22 buildHook = build_primitive_sources
23 $ buildHook defaultUserHooks,
24 makefileHook = build_primitive_sources
25 $ makefileHook defaultUserHooks,
26 haddockHook = build_primitive_sources
27 $ haddockHook defaultUserHooks }
28 defaultMainWithHooks hooks
30 type Hook a = PackageDescription -> LocalBuildInfo -> UserHooks -> a -> IO ()
32 build_primitive_sources :: Hook a -> Hook a
33 build_primitive_sources f pd lbi uhs x
34 = do when (compilerFlavor (compiler lbi) == GHC) $ do
35 let genprimopcode = joinPath ["..", "..", "utils",
36 "genprimopcode", "genprimopcode"]
37 primops = joinPath ["..", "..", "compiler", "prelude",
39 primhs = joinPath ["GHC", "Prim.hs"]
40 primopwrappers = joinPath ["GHC", "PrimopWrappers.hs"]
41 primhs_tmp = addExtension primhs "tmp"
42 primopwrappers_tmp = addExtension primopwrappers "tmp"
43 maybeExit $ system (genprimopcode ++ " --make-haskell-source < "
44 ++ primops ++ " > " ++ primhs_tmp)
45 maybeUpdateFile primhs_tmp primhs
46 maybeExit $ system (genprimopcode ++ " --make-haskell-wrappers < "
47 ++ primops ++ " > " ++ primopwrappers_tmp)
48 maybeUpdateFile primopwrappers_tmp primopwrappers
51 -- Replace a file only if the new version is different from the old.
52 -- This prevents make from doing unnecessary work after we run 'setup makefile'
53 maybeUpdateFile :: FilePath -> FilePath -> IO ()
54 maybeUpdateFile source target = do
55 r <- rawSystem "cmp" ["-s" {-quiet-}, source, target]
57 ExitSuccess -> removeFile source
58 ExitFailure _ -> do try (removeFile target); renameFile source target