Cabalize ext-core tools
[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 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",
37                                   "primops.txt"]
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")
44       f pd lbi uhs x
45
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]
51   case r of
52     ExitSuccess   -> removeFile source
53     ExitFailure _ -> do try (removeFile target); renameFile source target
54
55
56 \end{code}