on 3e47b49 help sign in
Idris-dev/Setup.hs
189 lines 6.4 kB history raw
1{-# LANGUAGE CPP #-}
2import Control.Monad
3import Data.IORef
4import Control.Exception (SomeException, catch)
6import Distribution.Simple
7import Distribution.Simple.BuildPaths (autogenModulesDir)
8import Distribution.Simple.InstallDirs as I
9import Distribution.Simple.LocalBuildInfo as L
10import qualified Distribution.Simple.Setup as S
11import qualified Distribution.Simple.Program as P
12import Distribution.Simple.Utils (createDirectoryIfMissingVerbose, rewriteFile)
13import Distribution.PackageDescription
14import Distribution.Text
16import System.Exit
17import System.FilePath ((</>), splitDirectories)
18import System.Directory
19import qualified System.FilePath.Posix as Px
20import System.Process
21import qualified Data.Text as T
22import qualified Data.Text.IO as TIO
24-- After Idris is built, we need to check and install the prelude and other libs
26-- -----------------------------------------------------------------------------
27-- Idris Command Path
29-- make on mingw32 exepects unix style separators
30#ifdef mingw32_HOST_OS
31(<//>) = (Px.</>)
32idrisCmd local = Px.joinPath $ splitDirectories $ ".." <//> ".." <//> buildDir local <//> "idris" <//> "idris"
33#else
34idrisCmd local = ".." </> ".." </> buildDir local </> "idris" </> "idris"
35#endif
37-- -----------------------------------------------------------------------------
38-- Make Commands
40-- use GNU make on FreeBSD
41#ifdef freebsd_HOST_OS
42mymake = "gmake"
43#else
44mymake = "make"
45#endif
46make verbosity =
47 P.runProgramInvocation verbosity . P.simpleProgramInvocation mymake
49-- -----------------------------------------------------------------------------
50-- Flags
52usesLLVM :: S.ConfigFlags -> Bool
53usesLLVM flags =
54 case lookup (FlagName "llvm") (S.configConfigurationsFlags flags) of
55 Just True -> True
56 Just False -> False
57 Nothing -> True
59usesGMP :: S.ConfigFlags -> Bool
60usesGMP flags =
61 case lookup (FlagName "gmp") (S.configConfigurationsFlags flags) of
62 Just True -> True
63 Just False -> False
64 Nothing -> True
66isRelease :: S.ConfigFlags -> Bool
67isRelease flags =
68 case lookup (FlagName "release") (S.configConfigurationsFlags flags) of
69 Just True -> True
70 Just False -> False
71 Nothing -> False
73-- -----------------------------------------------------------------------------
74-- Clean
76idrisClean _ flags _ _ = do
77 cleanStdLib
78 cleanLLVM
79 where
80 verbosity = S.fromFlag $ S.cleanVerbosity flags
82 cleanStdLib = do
83 makeClean "libs"
85 cleanLLVM = makeClean "llvm"
87 makeClean dir = make verbosity [ "-C", dir, "clean", "IDRIS=idris" ]
90-- -----------------------------------------------------------------------------
91-- Configure
93idrisConfigure _ flags _ local = do
94 configureRTS
95 generateVersionModule
96 where
97 verbosity = S.fromFlag $ S.configVerbosity flags
98 version = pkgVersion . package $ localPkgDescr local
100 -- This is a hack. I don't know how to tell cabal that a data file needs
101 -- installing but shouldn't be in the distribution. And it won't make the
102 -- distribution if it's not there, so instead I just delete
103 -- the file after configure.
104 configureRTS = make verbosity ["-C", "rts", "clean"]
106 -- Put the Git hash into a module for use in the program
107 -- For release builds, just put the empty string in the module
108 generateVersionModule = do
109 gitHash <- Control.Exception.catch (readProcess "git" ["rev-parse", "--short", "HEAD"] "")
110 (\e -> let e' = (e :: SomeException) in return "PRE")
111 let hash = takeWhile (/= '\n') gitHash
112 let versionModulePath = autogenModulesDir local </> "Version_idris" Px.<.> "hs"
113 createDirectoryIfMissingVerbose verbosity True (autogenModulesDir local)
114 rewriteFile versionModulePath (versionModuleContents hash)
116 versionModuleContents h = "module Version_idris where\n\n" ++
117 "gitHash :: String\n" ++
118 if isRelease (configFlags local)
119 then "gitHash = \"\"\n"
120 else "gitHash = \"-git:" ++ h ++ "\"\n"
123-- -----------------------------------------------------------------------------
124-- Build
126idrisBuild _ flags _ local = do
127 buildStdLib
128 buildRTS
129 when (usesLLVM $ configFlags local) buildLLVM
130 where
131 verbosity = S.fromFlag $ S.buildVerbosity flags
133 buildStdLib = do
134 putStrLn "Building libraries..."
135 makeBuild "libs"
136 where
137 makeBuild dir = make verbosity [ "-C", dir, "build" , "IDRIS=" ++ idrisCmd local]
139 buildRTS = make verbosity (["-C", "rts", "build"] ++
140 gmpflag (usesGMP (configFlags local)))
142 buildLLVM = make verbosity ["-C", "llvm", "build"]
144 gmpflag False = []
145 gmpflag True = ["GMP=-DIDRIS_GMP"]
147-- -----------------------------------------------------------------------------
148-- Copy/Install
150idrisInstall verbosity copy pkg local = do
151 installStdLib
152 installRTS
153 when (usesLLVM $ configFlags local) installLLVM
154 where
155 target = datadir $ L.absoluteInstallDirs pkg local copy
157 installStdLib = do
158 putStrLn $ "Installing libraries in " ++ target
159 makeInstall "libs" target
161 installRTS = do
162 let target' = target </> "rts"
163 putStrLn $ "Installing run time system in " ++ target'
164 makeInstall "rts" target'
166 installLLVM = do
167 let target' = target </> "llvm"
168 putStrLn $ "Installing LLVM library in " ++ target
169 makeInstall "llvm" target'
171 makeInstall src target =
172 make verbosity [ "-C", src, "install" , "TARGET=" ++ target, "IDRIS=" ++ idrisCmd local]
174-- -----------------------------------------------------------------------------
175-- Main
177-- Install libraries during both copy and install
178-- See http://hackage.haskell.org/trac/hackage/ticket/718
179main = defaultMainWithHooks $ simpleUserHooks
180 { postClean = idrisClean
181 , postConf = idrisConfigure
182 , postBuild = idrisBuild
183 , postCopy = \_ flags pkg local ->
184 idrisInstall (S.fromFlag $ S.copyVerbosity flags)
185 (S.fromFlag $ S.copyDest flags) pkg local
186 , postInst = \_ flags pkg local ->
187 idrisInstall (S.fromFlag $ S.installVerbosity flags)
188 NoCopyDest pkg local
189 }