Skip to content
This repository was archived by the owner on Nov 1, 2018. It is now read-only.

Commit c669451

Browse files
committed
Import from darcs
0 parents  commit c669451

File tree

1,470 files changed

+272809
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

1,470 files changed

+272809
-0
lines changed

Main.hs

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
module Main where
3+
4+
import qualified Make.Main
5+
6+
main = Make.Main.main

Make/CMake.hs

+126
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
2+
module Make.CMake(cmake) where
3+
4+
import Make.Useful
5+
import qualified Data.Map as Map
6+
import System.Time
7+
8+
9+
type Files = Map.Map String (CalendarTime, [FilePath])
10+
type Dates = Map.Map String CalendarTime
11+
12+
13+
cmake :: String -> FilePath -> FilePath -> IO ()
14+
cmake name dir output = do
15+
ghc <- ask "ghc"
16+
(_, allfiles) <- getContentsRec dir
17+
cfiles <- return $ filter (not . isPrefixOf "hat/") $ filter (".c" `isSuffixOf`) allfiles
18+
19+
-- ensure that each file is in the list
20+
files <- loadFiles name
21+
files <- removeFiles dir files
22+
files <- addFiles dir files cfiles
23+
saveFiles name files
24+
let dates = dateMap files
25+
26+
ensureDirectory $ "obj" </> name
27+
obj <- mapM (buildFile dir ("obj" </> name) dates) cfiles
28+
systemSuccess ("ERROR: " ++ output ++ " failed to link") $
29+
ghc ++ " " ++ unwords obj ++ " -o " ++ output
30+
return ()
31+
32+
33+
buildFile :: String -> FilePath -> Dates -> FilePath -> IO FilePath
34+
buildFile dir output dates file = do
35+
ghc <- ask "ghc"
36+
let out = output </> takeFileName file ++ ".o"
37+
done <- doesFileExist out
38+
done <- if not done then return False else do
39+
let lastTime = Map.lookup file dates
40+
time <- getModificationTime out
41+
time <- toCalendarTime time
42+
return $ isJust lastTime && time > fromJust (lastTime)
43+
44+
when (not done) $ do
45+
putStrLn $ "Compiling " ++ file
46+
systemSuccess ("ERROR: " ++ file ++ " failed to compile") $
47+
ghc ++ " " ++ (dir </> file) ++ " -Isrc/runtime/BCKernel -Isrc/runtime/BCKernel/msvc/gmp" ++
48+
" -Idepends/ctypes/libffi_msvc -c -o " ++ out
49+
return out
50+
51+
52+
53+
-- for each file give the newest file it depends on
54+
-- be stupid, just find a fixed point
55+
-- also handles circular dependancies correctly
56+
dateMap :: Files -> Map.Map String CalendarTime
57+
dateMap files = fix (Map.map fst files)
58+
where
59+
fix dates = if dates == dates2 then dates else fix dates2
60+
where dates2 = Map.mapWithKey (calc dates) dates
61+
62+
calc dates file time = maximum (time : map f deps)
63+
where
64+
f dep = Map.findWithDefault time dep dates
65+
deps = snd $ Map.findWithDefault (time,[]) file files
66+
67+
68+
removeFiles :: String -> Files -> IO Files
69+
removeFiles dir files = filterM check (Map.toAscList files) >>= return . Map.fromAscList
70+
where
71+
check (file, (modify,_)) = do
72+
exists <- doesFileExist (dir </> file)
73+
if not exists then return False else do
74+
time <- getModificationTime (dir </> file)
75+
time <- toCalendarTime time
76+
return $ time == modify
77+
78+
79+
80+
addFiles :: String -> Files -> [FilePath] -> IO Files
81+
addFiles dir files [] = return files
82+
addFiles dir files (t:odo)
83+
| t `Map.member` files = addFiles dir files odo
84+
| otherwise = do
85+
time <- getModificationTime (dir </> t)
86+
time <- toCalendarTime time
87+
incs <- readIncludes dir t
88+
addFiles dir (Map.insert t (time,incs) files) (incs++odo)
89+
90+
91+
-- from a file, what does it include
92+
readIncludes :: FilePath -> FilePath -> IO [FilePath]
93+
readIncludes dir file = readFile (dir </> file) >>= return . map relative . concatMap (f 0) . lines
94+
where
95+
f 0 x | "#" `isPrefixOf` x2 = f 1 $ trim $ tail x2
96+
where x2 = ltrim x
97+
98+
f 1 x | "include " `isPrefixOf` x = f 2 $ drop 8 x
99+
100+
f 2 x | n >= 3 && head x == '\"' && last x == '\"' = [take (n-2) $ tail x]
101+
where n = length x
102+
103+
f _ _ = []
104+
105+
relative inc = useSlash $ unwords $ collapse $ words $ useSpace $ dropFileName file </> inc
106+
where
107+
useSlash = replace ' ' '/'
108+
useSpace = replace '/' ' '
109+
110+
collapse (x:"..":xs) = collapse xs
111+
collapse (x:xs) = x : collapse xs
112+
collapse [] = []
113+
114+
115+
116+
loadFiles :: String -> IO Files
117+
loadFiles name = do
118+
let file = "obj/cmake_cache_" ++ name ++ ".txt"
119+
b <- doesFileExist file
120+
if b then readFile file >>= return . read else return Map.empty
121+
122+
123+
saveFiles :: String -> Files -> IO ()
124+
saveFiles name files = do
125+
let file = "obj/cmake_cache_" ++ name ++ ".txt"
126+
writeFile file (show files)

Make/Compile.hs

+90
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
2+
module Make.Compile(
3+
ensureYhc, ensureYhi, ensureLibraries,
4+
yhc, yhi, libraries
5+
) where
6+
7+
import Make.Useful
8+
import Make.CMake
9+
10+
11+
12+
ensureYhc = do
13+
b <- doesFileExist (exe_ "inst/bin/yhc")
14+
when (not b) yhc
15+
16+
ensureYhi = do
17+
b <- doesFileExist (exe_ "inst/bin/yhi")
18+
when (not b) yhi
19+
20+
ensureLibraries = do
21+
b <- doesFileExist "inst/lib/list.txt"
22+
when (not b) libraries
23+
24+
25+
yhc = do
26+
rel <- liftM (== "release") $ ask "type"
27+
(ghc:_) <- demand ["ghc","filepath","mtl","cpphs","uniplate"]
28+
demandExtra "compiler" "http://darcs.haskell.org/york-compiler98/"
29+
30+
let obj = "obj/yhc/" ++ if rel then "release" else "normal"
31+
ensureDirectory "obj/yhc" >> ensureDirectory obj
32+
33+
systemSuccess "ERROR: yhc failed to compile" $
34+
"ghc extra/compiler/MainYhc.hs --make " ++
35+
"-iextra/compiler -isrc/libraries/core " ++
36+
"-odir " ++ obj ++ " -hidir " ++ obj ++ " -o " ++ exe_ "inst/bin/yhc" ++
37+
if rel then " -O" else ""
38+
39+
40+
yhi = do
41+
ensureInst "bin"
42+
createConfigure
43+
cmake "yhi" "src/runtime/BCKernel" "inst/bin/yhi"
44+
45+
46+
createConfigure = do
47+
let dest = "src/runtime/BCKernel/config.h"
48+
src = "Make/src/config.h"
49+
rebuild <- shouldBuild src dest
50+
same <- if rebuild then return False else do
51+
txt <- readFile dest
52+
let (line0:line1:_) = lines txt
53+
(line2,_) <- replaceVars line0
54+
return $ line1 == line2
55+
56+
when (not same) $ do
57+
txt <- readFile src
58+
(txt2, used) <- replaceVars txt
59+
let used2 = snub used
60+
line0 = unwords $ "/*" : map (\(a,b) -> a ++ "=" ++ "%(" ++ a ++ ")") used2 ++ ["*/"]
61+
line1 = unwords $ "/*" : map (\(a,b) -> a ++ "=" ++ b) used2 ++ ["*/"]
62+
writeFile dest (line0 ++ "\n" ++ line1 ++ "\n" ++ txt2)
63+
64+
65+
replaceVars :: String -> IO (String, [(String,String)])
66+
replaceVars str = do
67+
let (skip,deal) = break (== '%') str
68+
if null deal then return (skip,[]) else do
69+
let (a,_:b) = break (== ')') $ drop 2 deal
70+
val <- ask a
71+
val <- return $ if null val then "0" else val
72+
(str,bind) <- replaceVars b
73+
return (skip++val++str, (a,val):bind)
74+
75+
76+
libraries = ensureInst "lib"
77+
78+
79+
80+
ensureInst x = ensureDirectory "inst" >> ensureDirectory ("inst/" ++ x)
81+
82+
83+
demandExtra folder repo = do
84+
let out = "extra" </> folder
85+
b <- doesDirectoryExist out
86+
when (not b) $ do
87+
ensureDirectory "extra"
88+
[darcs] <- demand ["darcs"]
89+
systemSuccess "failed to download extra" $
90+
darcs ++ " get --partial --repo-name=extra/" ++ folder ++ " " ++ repo

Make/Configure.hs

+147
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
2+
module Make.Configure(configure, ensureConfigure) where
3+
4+
import Make.Profile
5+
import Make.Useful
6+
import Make.Depends
7+
import System.Info
8+
9+
10+
ensureConfigure :: IO ()
11+
ensureConfigure = do
12+
valid <- ask "valid"
13+
when (null valid) configure
14+
15+
16+
typeSizes = ["char","short","int","long","long long","float","double","void*"]
17+
18+
19+
configure :: IO ()
20+
configure = do
21+
conf "os" getOS
22+
conf "arch" getArch
23+
24+
confProg "ghc" $ version [6,6]
25+
confProg "ghc-pkg" $ version [6,4,1]
26+
confProg "haddock" $ version [0,8]
27+
confProg "darcs" $ version [1,0,4]
28+
confProg "hscolour" $ version [1,0,4]
29+
30+
conf "bigendian" getEndian
31+
mapM (\x -> conf (filter isAlpha x) (getTypeSize x)) typeSizes
32+
33+
confHsPackage "filepath" [1,0]
34+
confHsPackage "cpphs" [1,3]
35+
confHsPackage "mtl" [1,0]
36+
confHsPackage "uniplate" [1,0]
37+
38+
setProfile "valid" "1"
39+
saveProfile
40+
where
41+
conf name val = do
42+
ans <- ask ('!':name)
43+
res <- if null ans then val else return ans
44+
putStrLn $ "set " ++ name ++ "=" ++
45+
(if null res then " (blank)" else res) ++
46+
(if null ans then "" else " (user-supplied)")
47+
setProfile name res
48+
49+
confProg name test = do
50+
ans <- ask ('!':name)
51+
let res = if null ans then name else ans
52+
b <- test name res
53+
conf name (return $ if b then res else "")
54+
55+
confHsPackage name test = do
56+
b <- checkHsPackage name test
57+
b <- if b then return b else do
58+
b2 <- dependsHsPackage name
59+
if b2 then checkHsPackage name test else return False
60+
conf name (return $ if b then "1" else "")
61+
62+
63+
64+
getOS :: IO String
65+
getOS = return $ if os == "mingw32" then "win" else os
66+
67+
getArch :: IO String
68+
getArch = return arch
69+
70+
71+
72+
-- give an error message if the program given has a lower version
73+
-- or doesn't run
74+
version :: [Int] -> String -> String -> IO Bool
75+
version ver name exe = do
76+
-- can't use System.Process because WinHugs 2006
77+
let output = "obj/configure_" ++ name ++ ".txt"
78+
systemPipeCont (exe ++ " --version") output
79+
(do putStrLn $ "WARNING: " ++ name ++ " failed to run: " ++ exe ++ " --version"
80+
return False)
81+
$ \s -> do
82+
let real = parseVersion s
83+
success = real >= ver
84+
when (not success) $ putStrLn $
85+
"WARNING: " ++ name ++ " too old, expected " ++ showVersion ver ++
86+
", found " ++ showVersion real ++ ": " ++ exe
87+
return success
88+
89+
90+
showVersion :: [Int] -> String
91+
showVersion = concat . intersperse "." . map show
92+
93+
parseVersion :: String -> [Int]
94+
parseVersion = map read . words . map (\x -> if x == '.' then ' ' else x) .
95+
takeWhile (\x -> isDigit x || x == '.') . dropWhile (not . isDigit)
96+
97+
98+
getEndian :: IO String
99+
getEndian = runC "endian" "Make/src/endian.c" []
100+
101+
102+
getTypeSize :: String -> IO String
103+
getTypeSize name = runC nam "Make/src/typesize.c" [("TYPE",name)]
104+
where nam = filter isAlpha name
105+
106+
107+
108+
109+
-- run a C program, return the first line
110+
runC :: String -> FilePath -> [(String,String)] -> IO String
111+
runC name file defines = do
112+
let nam = "obj/configure_" ++ name
113+
defs = unwords ["-optc \"-D" ++ a ++ ['='|not (null b)] ++ b ++ "\"" | (a,b) <- defines]
114+
ghc <- ask "ghc"
115+
if null ghc
116+
then do putStrLn $ "WARNING: " ++ name ++ " skipping, no ghc found"
117+
return []
118+
else systemSuccessCont
119+
(ghc ++ " " ++ defs ++ " " ++ file ++ " -o " ++ nam ++ " -odir obj")
120+
(do putStrLn $ "WARNING: " ++ name ++ " failed to compile test" ; return "") $ do
121+
systemPipeCont (slashes nam) (nam ++ ".txt")
122+
(do putStrLn $ "WARNING: " ++ name ++ " failed to run test" ; return "") return
123+
124+
125+
loadPackageList :: String -> IO [(String,[Int])]
126+
loadPackageList name = do
127+
ghc_pkg <- ask "ghc-pkg"
128+
systemPipeCont (ghc_pkg ++ " list --simple-output") ("obj/package_" ++ name ++ ".txt")
129+
(do putStrLn $ "WARNING: ghc-pkg could not be queried: " ++ ghc_pkg; return [])
130+
(return . map f . filter ('-' `elem`) . words)
131+
where
132+
f x = (reverse b, parseVersion $ reverse a)
133+
where (a,_:b) = break (== '-') $ reverse x
134+
135+
136+
checkHsPackage :: String -> [Int] -> IO Bool
137+
checkHsPackage name ver = do
138+
pkg <- loadPackageList name
139+
let real = maximum $ [] : [b | (a,b) <- pkg, a == name]
140+
if null real then do
141+
when (not $ null pkg) $ putStrLn $ "WARNING: package " ++ name ++ " not found"
142+
return False
143+
else if real < ver then do
144+
putStrLn $ "WARNING: package " ++ name ++ " is too old, expected " ++ showVersion ver ++ ", found " ++ showVersion real
145+
return False
146+
else
147+
return True

0 commit comments

Comments
 (0)