on a5e76fc help sign in
Idris-dev/Setup.hs
244 lines 9.7 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
60execOnly :: S.ConfigFlags -> Bool
61execOnly flags =
62 case lookup (FlagName "execonly") (S.configConfigurationsFlags flags) of
63 Just True -> True
64 Just False -> False
65 Nothing -> True
67isRelease :: S.ConfigFlags -> Bool
68isRelease flags =
69 case lookup (FlagName "release") (S.configConfigurationsFlags flags) of
70 Just True -> True
71 Just False -> False
72 Nothing -> False
74isFreestanding :: S.ConfigFlags -> Bool
75isFreestanding flags =
76 case lookup (FlagName "freestanding") (S.configConfigurationsFlags flags) of
77 Just True -> True
78 Just False -> False
79 Nothing -> False
80-- -----------------------------------------------------------------------------
81-- Clean
83idrisClean _ flags _ _ = do
84 cleanStdLib
85 where
86 verbosity = S.fromFlag $ S.cleanVerbosity flags
88 cleanStdLib = do
89 makeClean "libs"
91 makeClean dir = make verbosity [ "-C", dir, "clean", "IDRIS=idris" ]
94-- -----------------------------------------------------------------------------
95-- Configure
97gitHash :: IO String
98gitHash = do h <- Control.Exception.catch (readProcess "git" ["rev-parse", "--short", "HEAD"] "")
99 (\e -> let e' = (e :: SomeException) in return "PRE")
100 return $ takeWhile (/= '\n') h
102-- Put the Git hash into a module for use in the program
103-- For release builds, just put the empty string in the module
104generateVersionModule verbosity dir release = do
105 hash <- gitHash
106 let versionModulePath = dir </> "Version_idris" Px.<.> "hs"
107 putStrLn $ "Generating " ++ versionModulePath ++
108 if release then " for release" else (" for prerelease " ++ hash)
109 createDirectoryIfMissingVerbose verbosity True dir
110 rewriteFile versionModulePath (versionModuleContents hash)
112 where versionModuleContents h = "module Version_idris where\n\n" ++
113 "gitHash :: String\n" ++
114 if release
115 then "gitHash = \"\"\n"
116 else "gitHash = \"git:" ++ h ++ "\"\n"
118-- Generate a module that contains the lib path for a freestanding Idris
119generateTargetModule verbosity dir targetDir = do
120 absPath <- return $ isAbsolute targetDir
121 let targetModulePath = dir </> "Target_idris" Px.<.> "hs"
122 putStrLn $ "Generating " ++ targetModulePath
123 createDirectoryIfMissingVerbose verbosity True dir
124 rewriteFile targetModulePath (versionModuleContents absPath targetDir)
125 where versionModuleContents absolute td = "module Target_idris where\n\n" ++
126 "import System.FilePath\n" ++
127 "import System.Environment\n" ++
128 "getDataDir :: IO String\n" ++
129 if absolute
130 then "getDataDir = return \"" ++ td ++ "\"\n"
131 else "getDataDir = do \n" ++
132 " expath <- getExecutablePath\n" ++
133 " execDir <- return $ dropFileName expath\n" ++
134 " return $ execDir ++ \"" ++ td ++ "\"\n"
135 ++ "getDataFileName :: FilePath -> IO FilePath\n"
136 ++ "getDataFileName name = do\n"
137 ++ " dir <- getDataDir\n"
138 ++ " return (dir ++ \"/\" ++ name)"
141idrisConfigure _ flags _ local = do
142 configureRTS
143 generateVersionModule verbosity (autogenModulesDir local) (isRelease (configFlags local))
144 when (isFreestanding $ configFlags local) (do
145 targetDir <- lookupEnv "IDRIS_INSTALL_DIR"
146 case targetDir of
147 Just d -> generateTargetModule verbosity (autogenModulesDir local) d
148 Nothing -> error $ "Trying to build freestanding without a target directory."
149 ++ " Set it by defining IDRIS_INSTALL_DIR.")
150 where
151 verbosity = S.fromFlag $ S.configVerbosity flags
152 version = pkgVersion . package $ localPkgDescr local
154 -- This is a hack. I don't know how to tell cabal that a data file needs
155 -- installing but shouldn't be in the distribution. And it won't make the
156 -- distribution if it's not there, so instead I just delete
157 -- the file after configure.
158 configureRTS = make verbosity ["-C", "rts", "clean"]
160idrisPreSDist args flags = do
161 let dir = S.fromFlag (S.sDistDirectory flags)
162 let verb = S.fromFlag (S.sDistVerbosity flags)
163 generateVersionModule verb ("src") True
164 generateTargetModule verb "src" "./libs"
165 preSDist simpleUserHooks args flags
167idrisPostSDist args flags desc lbi = do
168 Control.Exception.catch (do let file = "src" </> "Version_idris" Px.<.> "hs"
169 let targetFile = "src" </> "Target_idris" Px.<.> "hs"
170 putStrLn $ "Removing generated modules:\n "
171 ++ file ++ "\n" ++ targetFile
172 removeFile file
173 removeFile targetFile)
174 (\e -> let e' = (e :: SomeException) in return ())
175 postSDist simpleUserHooks args flags desc lbi
177-- -----------------------------------------------------------------------------
178-- Build
180getVersion :: Args -> S.BuildFlags -> IO HookedBuildInfo
181getVersion args flags = do
182 hash <- gitHash
183 let buildinfo = (emptyBuildInfo { cppOptions = ["-DVERSION="++hash] }) :: BuildInfo
184 return (Just buildinfo, [])
186idrisBuild _ flags _ local = unless (execOnly (configFlags local)) $ do
187 buildStdLib
188 buildRTS
189 where
190 verbosity = S.fromFlag $ S.buildVerbosity flags
192 buildStdLib = do
193 putStrLn "Building libraries..."
194 makeBuild "libs"
195 where
196 makeBuild dir = make verbosity [ "-C", dir, "build" , "IDRIS=" ++ idrisCmd local]
198 buildRTS = make verbosity (["-C", "rts", "build"] ++
199 gmpflag (usesGMP (configFlags local)))
201 gmpflag False = []
202 gmpflag True = ["GMP=-DIDRIS_GMP"]
206-- -----------------------------------------------------------------------------
207-- Copy/Install
209idrisInstall verbosity copy pkg local = unless (execOnly (configFlags local)) $ do
210 installStdLib
211 installRTS
212 where
213 target = datadir $ L.absoluteInstallDirs pkg local copy
215 installStdLib = do
216 putStrLn $ "Installing libraries in " ++ target
217 makeInstall "libs" target
219 installRTS = do
220 let target' = target </> "rts"
221 putStrLn $ "Installing run time system in " ++ target'
222 makeInstall "rts" target'
224 makeInstall src target =
225 make verbosity [ "-C", src, "install" , "TARGET=" ++ target, "IDRIS=" ++ idrisCmd local]
227-- -----------------------------------------------------------------------------
228-- Main
230-- Install libraries during both copy and install
231-- See http://hackage.haskell.org/trac/hackage/ticket/718
232main = defaultMainWithHooks $ simpleUserHooks
233 { postClean = idrisClean
234 , postConf = idrisConfigure
235 , postBuild = idrisBuild
236 , postCopy = \_ flags pkg local ->
237 idrisInstall (S.fromFlag $ S.copyVerbosity flags)
238 (S.fromFlag $ S.copyDest flags) pkg local
239 , postInst = \_ flags pkg local ->
240 idrisInstall (S.fromFlag $ S.installVerbosity flags)
241 NoCopyDest pkg local
242 , preSDist = idrisPreSDist --do { putStrLn (show args) ; putStrLn (show flags) ; return emptyHookedBuildInfo }
243 , postSDist = idrisPostSDist
244 }