1 #!/usr/bin/env runhaskell
3 {-# OPTIONS -Wall -cpp #-}
6 import Distribution.PackageDescription
7 import Distribution.Simple
8 import Distribution.Simple.LocalBuildInfo
9 import Distribution.Simple.Utils
11 import System.FilePath
13 import System.Directory
14 import Control.Exception
18 let hooks = simpleUserHooks {
19 buildHook = build_primitive_sources
20 $ buildHook simpleUserHooks
22 defaultMainWithHooks hooks
25 Mostly snarfed from ghc-prim's Setup.hs.
28 type Hook a = PackageDescription -> LocalBuildInfo -> UserHooks -> a -> IO ()
31 -- Hack: If PrimEnv.hs exists *and* genprimopcode or
32 -- primops.txt doesn't exist, don't rebuild PrimEnv.hs
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",
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")
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]
59 ExitSuccess -> removeFile source
61 #if __GLASGOW_HASKELL__ >= 610
62 (try :: IO () -> IO (Either IOException ()))
67 renameFile source target