35661d4b2a4c4e92aa23b539bd0af39326b32446
[ghc-hetmet.git] / utils / ext-core / Setup.lhs
1 #!/usr/bin/env runhaskell
2 \begin{code}
3 {-# OPTIONS -Wall -cpp #-}
4
5 import Control.Monad
6 import Distribution.PackageDescription
7 import Distribution.Simple
8 import Distribution.Simple.LocalBuildInfo
9 import Distribution.Simple.Utils
10 import System.Cmd
11 import System.FilePath
12 import System.Exit
13 import System.Directory
14 import Control.Exception
15
16 main :: IO ()
17 main = do
18    let hooks = simpleUserHooks {
19                  buildHook = build_primitive_sources 
20                            $ buildHook simpleUserHooks
21             }
22    defaultMainWithHooks hooks
23 \end{code}
24
25 Mostly snarfed from ghc-prim's Setup.hs.
26
27 \begin{code}
28 type Hook a = PackageDescription -> LocalBuildInfo -> UserHooks -> a -> IO ()
29
30
31 -- Hack: If PrimEnv.hs exists *and* genprimopcode or
32 -- primops.txt doesn't exist, don't rebuild PrimEnv.hs
33
34 build_primitive_sources :: Hook a -> Hook a
35 build_primitive_sources f pd lbi uhs x
36  = do when (compilerFlavor (compiler lbi) == GHC) $ do
37           let genprimopcode = joinPath ["..", "..", "utils",
38                                         "genprimopcode", "genprimopcode"]
39               primops = joinPath ["..", "..", "compiler", "prelude",
40                                   "primops.txt"]
41               primhs = joinPath ["Language", "Core", "PrimEnv.hs"]
42               primhs_tmp = addExtension primhs "tmp"
43           primEnvExists <- doesFileExist primhs
44           genprimopcodeExists <- doesFileExist genprimopcode
45           primopsExists <- doesFileExist primops
46           unless (primEnvExists && not genprimopcodeExists && not primopsExists) $ do
47              maybeExit $ system (genprimopcode ++ " --make-ext-core-source < "
48                            ++ primops ++ " > " ++ primhs_tmp)
49              maybeUpdateFile primhs_tmp primhs
50              maybeExit $ system ("make -C lib/GHC_ExtCore")
51       f pd lbi uhs x
52
53 -- Replace a file only if the new version is different from the old.
54 -- This prevents make from doing unnecessary work after we run 'setup makefile'
55 maybeUpdateFile :: FilePath -> FilePath -> IO ()
56 maybeUpdateFile source target = do
57   r <- rawSystem "cmp" ["-s" {-quiet-}, source, target]
58   case r of
59     ExitSuccess   -> removeFile source
60     ExitFailure _ -> do 
61 #if __GLASGOW_HASKELL__ >= 610
62       (try :: IO () -> IO (Either IOException ()))
63 #else
64       try
65 #endif 
66        (removeFile target)
67       renameFile source target
68 \end{code}