1 #!/usr/bin/env runhaskell
7 import Distribution.PackageDescription
8 import Distribution.Simple
9 import Distribution.Simple.LocalBuildInfo
10 import Distribution.Simple.Utils
12 import System.FilePath
14 import System.Directory
15 import Control.Exception (try)
19 let hooks = defaultUserHooks {
20 buildHook = build_primitive_sources
21 $ buildHook defaultUserHooks
23 defaultMainWithHooks hooks
26 Mostly snarfed from ghc-prim's Setup.hs.
29 type Hook a = PackageDescription -> LocalBuildInfo -> UserHooks -> a -> IO ()
32 -- Hack: If PrimEnv.hs exists *and* genprimopcode or
33 -- primops.txt doesn't exist, don't rebuild PrimEnv.hs
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",
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")
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]
60 ExitSuccess -> removeFile source
61 ExitFailure _ -> do try (removeFile target); renameFile source target