on fbcd643 help sign in
Idris-dev/Setup.hs
237 lines 9.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.Environment
17import System.Exit
18import System.FilePath ((</>), splitDirectories,isAbsolute)
19import System.Directory
20import qualified System.FilePath.Posix as Px
21import System.Process
22import qualified Data.Text as T
23import qualified Data.Text.IO as TIO
25-- After Idris is built, we need to check and install the prelude and other libs
27-- -----------------------------------------------------------------------------
28-- Idris Command Path
30-- make on mingw32 exepects unix style separators
31#ifdef mingw32_HOST_OS
32(<//>) = (Px.</>)
33idrisCmd local = Px.joinPath $ splitDirectories $ ".." <//> ".." <//> buildDir local <//> "idris" <//> "idris"
34#else
35idrisCmd local = ".." </> ".." </> buildDir local </> "idris" </> "idris"
36#endif
38-- -----------------------------------------------------------------------------
39-- Make Commands
41-- use GNU make on FreeBSD
42#if defined(freebsd_HOST_OS) || defined(dragonfly_HOST_OS)
43mymake = "gmake"
44#else
45mymake = "make"
46#endif
47make verbosity =
48 P.runProgramInvocation verbosity . P.simpleProgramInvocation mymake
50-- -----------------------------------------------------------------------------
51-- Flags
53usesGMP :: S.ConfigFlags -> Bool
54usesGMP flags =
55 case lookup (FlagName "gmp") (S.configConfigurationsFlags flags) of
56 Just True -> True
57 Just False -> False
58 Nothing -> True
60isRelease :: S.ConfigFlags -> Bool
61isRelease flags =
62 case lookup (FlagName "release") (S.configConfigurationsFlags flags) of
63 Just True -> True
64 Just False -> False
65 Nothing -> False
67isFreestanding :: S.ConfigFlags -> Bool
68isFreestanding flags =
69 case lookup (FlagName "freestanding") (S.configConfigurationsFlags flags) of
70 Just True -> True
71 Just False -> False
72 Nothing -> False
73-- -----------------------------------------------------------------------------
74-- Clean
76idrisClean _ flags _ _ = do
77 cleanStdLib
78 where
79 verbosity = S.fromFlag $ S.cleanVerbosity flags
81 cleanStdLib = do
82 makeClean "libs"
84 makeClean dir = make verbosity [ "-C", dir, "clean", "IDRIS=idris" ]
87-- -----------------------------------------------------------------------------
88-- Configure
90gitHash :: IO String
91gitHash = do h <- Control.Exception.catch (readProcess "git" ["rev-parse", "--short", "HEAD"] "")
92 (\e -> let e' = (e :: SomeException) in return "PRE")
93 return $ takeWhile (/= '\n') h
95-- Put the Git hash into a module for use in the program
96-- For release builds, just put the empty string in the module
97generateVersionModule verbosity dir release = do
98 hash <- gitHash
99 let versionModulePath = dir </> "Version_idris" Px.<.> "hs"
100 putStrLn $ "Generating " ++ versionModulePath ++
101 if release then " for release" else (" for prerelease " ++ hash)
102 createDirectoryIfMissingVerbose verbosity True dir
103 rewriteFile versionModulePath (versionModuleContents hash)
105 where versionModuleContents h = "module Version_idris where\n\n" ++
106 "gitHash :: String\n" ++
107 if release
108 then "gitHash = \"\"\n"
109 else "gitHash = \"-git:" ++ h ++ "\"\n"
111-- Generate a module that contains the lib path for a freestanding Idris
112generateTargetModule verbosity dir targetDir = do
113 absPath <- return $ isAbsolute targetDir
114 let targetModulePath = dir </> "Target_idris" Px.<.> "hs"
115 putStrLn $ "Generating " ++ targetModulePath
116 createDirectoryIfMissingVerbose verbosity True dir
117 rewriteFile targetModulePath (versionModuleContents absPath targetDir)
118 where versionModuleContents absolute td = "module Target_idris where\n\n" ++
119 "import System.FilePath\n" ++
120 "import System.Environment\n" ++
121 "getDataDir :: IO String\n" ++
122 if absolute
123 then "getDataDir = return \"" ++ td ++ "\"\n"
124 else "getDataDir = do \n" ++
125 " expath <- getExecutablePath\n" ++
126 " execDir <- return $ dropFileName expath\n" ++
127 " return $ execDir ++ \"" ++ td ++ "\"\n"
128 ++ "getDataFileName :: FilePath -> IO FilePath\n"
129 ++ "getDataFileName name = do\n"
130 ++ " dir <- getDataDir\n"
131 ++ " return (dir ++ \"/\" ++ name)"
134idrisConfigure _ flags _ local = do
135 configureRTS
136 generateVersionModule verbosity (autogenModulesDir local) (isRelease (configFlags local))
137 when (isFreestanding $ configFlags local) (do
138 targetDir <- lookupEnv "IDRIS_INSTALL_DIR"
139 case targetDir of
140 Just d -> generateTargetModule verbosity (autogenModulesDir local) d
141 Nothing -> error $ "Trying to build freestanding without a target directory."
142 ++ " Set it by defining IDRIS_INSTALL_DIR.")
143 where
144 verbosity = S.fromFlag $ S.configVerbosity flags
145 version = pkgVersion . package $ localPkgDescr local
147 -- This is a hack. I don't know how to tell cabal that a data file needs
148 -- installing but shouldn't be in the distribution. And it won't make the
149 -- distribution if it's not there, so instead I just delete
150 -- the file after configure.
151 configureRTS = make verbosity ["-C", "rts", "clean"]
153idrisPreSDist args flags = do
154 let dir = S.fromFlag (S.sDistDirectory flags)
155 let verb = S.fromFlag (S.sDistVerbosity flags)
156 generateVersionModule verb ("src") True
157 generateTargetModule verb "src" "./libs"
158 preSDist simpleUserHooks args flags
160idrisPostSDist args flags desc lbi = do
161 Control.Exception.catch (do let file = "src" </> "Version_idris" Px.<.> "hs"
162 let targetFile = "src" </> "Target_idris" Px.<.> "hs"
163 putStrLn $ "Removing generated modules:\n "
164 ++ file ++ "\n" ++ targetFile
165 removeFile file
166 removeFile targetFile)
167 (\e -> let e' = (e :: SomeException) in return ())
168 postSDist simpleUserHooks args flags desc lbi
170-- -----------------------------------------------------------------------------
171-- Build
173getVersion :: Args -> S.BuildFlags -> IO HookedBuildInfo
174getVersion args flags = do
175 hash <- gitHash
176 let buildinfo = (emptyBuildInfo { cppOptions = ["-DVERSION="++hash] }) :: BuildInfo
177 return (Just buildinfo, [])
179idrisBuild _ flags _ local = do
180 buildStdLib
181 buildRTS
182 where
183 verbosity = S.fromFlag $ S.buildVerbosity flags
185 buildStdLib = do
186 putStrLn "Building libraries..."
187 makeBuild "libs"
188 where
189 makeBuild dir = make verbosity [ "-C", dir, "build" , "IDRIS=" ++ idrisCmd local]
191 buildRTS = make verbosity (["-C", "rts", "build"] ++
192 gmpflag (usesGMP (configFlags local)))
194 gmpflag False = []
195 gmpflag True = ["GMP=-DIDRIS_GMP"]
199-- -----------------------------------------------------------------------------
200-- Copy/Install
202idrisInstall verbosity copy pkg local = do
203 installStdLib
204 installRTS
205 where
206 target = datadir $ L.absoluteInstallDirs pkg local copy
208 installStdLib = do
209 putStrLn $ "Installing libraries in " ++ target
210 makeInstall "libs" target
212 installRTS = do
213 let target' = target </> "rts"
214 putStrLn $ "Installing run time system in " ++ target'
215 makeInstall "rts" target'
217 makeInstall src target =
218 make verbosity [ "-C", src, "install" , "TARGET=" ++ target, "IDRIS=" ++ idrisCmd local]
220-- -----------------------------------------------------------------------------
221-- Main
223-- Install libraries during both copy and install
224-- See http://hackage.haskell.org/trac/hackage/ticket/718
225main = defaultMainWithHooks $ simpleUserHooks
226 { postClean = idrisClean
227 , postConf = idrisConfigure
228 , postBuild = idrisBuild
229 , postCopy = \_ flags pkg local ->
230 idrisInstall (S.fromFlag $ S.copyVerbosity flags)
231 (S.fromFlag $ S.copyDest flags) pkg local
232 , postInst = \_ flags pkg local ->
233 idrisInstall (S.fromFlag $ S.installVerbosity flags)
234 NoCopyDest pkg local
235 , preSDist = idrisPreSDist --do { putStrLn (show args) ; putStrLn (show flags) ; return emptyHookedBuildInfo }
236 , postSDist = idrisPostSDist
237 }