1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
|
{-
Kaya - My favourite toy language.
Copyright (C) 2004, 2005 Edwin Brady
This file is distributed under the terms of the GNU General
Public Licence. See COPYING for licence.
-}
module Options where
import System.Exit
import Lib
data Option = NoPrelude | DumpTAC | DumpTree | DumpRaw | KeepC | ShowGCC
| DumpEqns | DumpCG | DumpDeps | Libdir String | XMLDocs | HTMLDocs
| SeedKey String | ForceBuild | NoChase | Quiet
| DeprWarn | DeprFail | StaticExecutable
| DynLink String | NoOpts | Profile | NoRTChecks | FastVM
| NoEnvLibs | DumpSimpl | MakeSO | PICObj
deriving (Show, Eq)
type Options = [Option]
nolink opts = False
doprelude opts = not $ elem NoPrelude opts
dumptac opts = elem DumpTAC opts
dumptree opts = elem DumpTree opts
dumpsimpl opts = elem DumpSimpl opts
dumpraw opts = elem DumpRaw opts
dumpeqns opts = elem DumpEqns opts
dumpcg opts = elem DumpCG opts
dumpdeps opts = elem DumpDeps opts
static opts = elem StaticExecutable opts || elem Profile opts
keepc opts = elem KeepC opts
mkso opts = elem MakeSO opts
picobj opts = elem PICObj opts
showgcc opts = elem ShowGCC opts
xmldocs opts = elem XMLDocs opts
htmldocs opts = elem HTMLDocs opts
deprwarn opts = elem DeprWarn opts
deprfail opts = elem DeprFail opts
forcebuild opts = elem ForceBuild opts || elem NoChase opts
nochase opts = elem NoChase opts
quiet opts = elem Quiet opts || elem NoChase opts
noopts opts = elem NoOpts opts
profile opts = elem Profile opts
nortchecks opts = elem NoRTChecks opts
fastvm opts = elem FastVM opts
noenvlibs opts = elem NoEnvLibs opts
getlibdir [] acc = acc
getlibdir (Libdir d:xs) acc = getlibdir xs ((stripSlash d++"/"):acc)
getlibdir (_:xs) acc = getlibdir xs acc
getdynlinks [] acc = ("kayastd":"kayaweb":"kayare":acc) -- Standard library
getdynlinks (DynLink d:xs) acc = getdynlinks xs (d:acc)
getdynlinks (_:xs) acc = getdynlinks xs acc
getseed [] = Nothing
getseed ((SeedKey x):xs) = Just x
getseed (x:xs) = getseed xs
usage :: [String] -> IO (Maybe String,[String],[Option])
usage xs = do (f,ext, opts) <- parseargs xs
return (f, ext, opts)
parseargs [] = do putStrLn $ "Kaya version " ++ Lib.version
putStrLn "Usage: \n\t kayac <filename> [options]"
exitWith (ExitFailure 1)
parseargs xs = do (opts,rest) <- getopts xs
case rest of
(x:xs) -> return (Just x, xs, opts)
_ -> return (Nothing, rest, opts) -- no input file
getopts [] = return ([],[])
getopts ("-v":[]) = do putStrLn versionInfo
exitWith (ExitSuccess)
getopts ("-version":[]) = do putStrLn versionInfo
exitWith (ExitSuccess)
getopts ("-h":[]) = do putStrLn helptext
exitWith (ExitSuccess)
getopts ("-?":[]) = do putStrLn helptext
exitWith (ExitSuccess)
getopts ("-help":[]) = do putStrLn helptext
exitWith (ExitSuccess)
getopts ("-installdir":[]) = do putStrLn installdir
exitWith (ExitSuccess)
getopts ("-noprelude":xs) = do (opts,rest) <- getopts xs
return (NoPrelude:opts,rest)
getopts ("-dumptac":xs) = do (opts,rest) <- getopts xs
return (DumpTAC:opts,rest)
getopts ("-dumptree":xs) = do (opts,rest) <- getopts xs
return (DumpTree:opts,rest)
getopts ("-dumpsimpl":xs) = do (opts,rest) <- getopts xs
return (DumpSimpl:opts,rest)
getopts ("-dumpraw":xs) = do (opts,rest) <- getopts xs
return (DumpRaw:opts,rest)
getopts ("-dumpeqns":xs) = do (opts,rest) <- getopts xs
return (DumpEqns:opts,rest)
getopts ("-dumpcg":xs) = do (opts,rest) <- getopts xs
return (DumpCG:opts,rest)
getopts ("-dumpdeps":xs) = do (opts,rest) <- getopts xs
return (DumpDeps:opts,rest)
getopts ("-static":xs) = do (opts,rest) <- getopts xs
return (StaticExecutable:opts,rest)
getopts ("-keepc":xs) = do (opts,rest) <- getopts xs
return (KeepC:opts,rest)
getopts ("-repl":xs) = do (opts,rest) <- getopts xs
return (PICObj:MakeSO:opts,rest)
--getopts ("-pic":xs) = do (opts,rest) <- getopts xs
-- return (PICObj:opts,rest)
getopts ("-showgcc":xs) = do (opts,rest) <- getopts xs
return (ShowGCC:opts,rest)
getopts ("-L":dir:xs) = do (opts,rest) <- getopts xs
return (Libdir dir:opts,rest)
getopts ("-d":dyn:xs) = do (opts,rest) <- getopts xs
return (DynLink dyn:opts,rest)
getopts ("-libdir":dir:xs) = do (opts,rest) <- getopts xs
return (Libdir dir:opts,rest)
getopts ("-seedkey":val:xs) = do (opts,rest) <- getopts xs
return(SeedKey val:opts,rest)
getopts ("-xmldocs":xs) = do (opts,rest) <- getopts xs
return (XMLDocs:opts,rest)
getopts ("-htmldocs":xs) = do (opts,rest) <- getopts xs
return (HTMLDocs:opts,rest)
getopts ("-deprwarn":xs) = do (opts,rest) <- getopts xs
return (DeprWarn:opts,rest)
getopts ("-deprfail":xs) = do (opts,rest) <- getopts xs
return (DeprFail:opts,rest)
getopts ("-force":xs) = do (opts,rest) <- getopts xs
return (ForceBuild:opts,rest)
getopts ("-nochase":xs) = do (opts,rest) <- getopts xs
return (NoChase:opts,rest)
getopts ("-q":xs) = do (opts,rest) <- getopts xs
return (Quiet:opts,rest)
getopts ("-noopts":xs) = do (opts,rest) <- getopts xs
return (NoOpts:opts,rest)
getopts ("-profile":xs) = do (opts,rest) <- getopts xs
return (Profile:opts,rest)
getopts ("-noenvlibs":xs) = do (opts,rest) <- getopts xs
return (NoEnvLibs:opts,rest)
getopts ("-nortchecks":xs) = do (opts,rest) <- getopts xs
return (NoRTChecks:opts,rest)
getopts ("-fastvm":xs) = do (opts,rest) <- getopts xs
return (FastVM:opts,rest)
getopts (x:xs) = do (opts,rest) <- getopts xs
return (opts,x:rest)
stripSlash :: String -> String
stripSlash xs | (h:hs) <- reverse xs = if h =='/'
then reverse hs
else xs
| otherwise = xs
helptext = "Usage:\n\tkayac sourcefile.k [opts]\n" ++
"\tkayac -help\n"++
"\tkayac -version\n"++
"Major options:\n"++
" -force : force recompilation of all modules\n"++
" -libdir DIR : add DIR to the library search path\n"++
" -nochase : don't chase and recompile modules\n"++
" -nortchecks : disable run-time checking in compiled program\n"++
" -fastvm : optimise for speed instead of memory usage\n"++
" -profile : enable profiling of compiled program\n"++
" -q : quiet: produce far less output\n"++
" -static : link final executable statically\n"++
" -xmldocs : generate API documentation in XML format\n"++
"For fuller documentation, see the man page kayac(1)"
versionInfo = "Kaya version " ++ Lib.version ++ "\n" ++
kernel ++ " " ++ machine
|