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