sv2v/src/Job.hs

115 lines
3.5 KiB
Haskell
Raw Normal View History

2019-02-26 21:03:49 +01:00
{-# LANGUAGE DeriveDataTypeable #-}
2020-02-08 00:57:48 +01:00
{-# LANGUAGE TemplateHaskell #-}
2019-02-26 21:03:49 +01:00
{- sv2v
- Author: Zachary Snow <zach@zachjs.com>
-
- Command line arguments.
-}
module Job where
2019-02-26 21:03:49 +01:00
2020-02-08 00:57:48 +01:00
import GitHash (giHash, tGitInfoCwd)
2019-09-15 16:31:50 +02:00
import System.IO (stderr, hPutStr)
2019-02-26 21:03:49 +01:00
import System.Console.CmdArgs
2019-09-15 16:31:50 +02:00
import System.Environment (getArgs, withArgs)
2019-02-26 21:03:49 +01:00
data Exclude
= Always
| Assert
| Interface
| Logic
| Succinct
deriving (Show, Typeable, Data, Eq)
2019-02-26 21:03:49 +01:00
data Job = Job
2019-09-15 16:31:50 +02:00
{ files :: [FilePath]
, incdir :: [FilePath]
, define :: [String]
2019-09-15 16:31:50 +02:00
, siloed :: Bool
2020-03-19 01:02:20 +01:00
, skipPreprocessor :: Bool
2019-09-15 16:31:50 +02:00
, exclude :: [Exclude]
, verbose :: Bool
2019-02-26 21:03:49 +01:00
} deriving (Show, Typeable, Data)
2020-02-08 00:57:48 +01:00
gitHash :: String
gitHash = giHash $$tGitInfoCwd
shortGitHash :: String
shortGitHash = take 7 gitHash
2019-02-26 21:03:49 +01:00
defaultJob :: Job
defaultJob = Job
2019-09-15 16:31:50 +02:00
{ files = def &= args &= typ "FILES"
, incdir = nam_ "I" &= name "incdir" &= typDir
&= help "Add directory to include search path"
&= groupname "Preprocessing"
, define = nam_ "D" &= name "define" &= typ "NAME[=VALUE]"
&= help "Define a macro for preprocessing"
, siloed = nam_ "siloed" &= help ("Lex input files separately, so"
++ " macros from earlier files are not defined in later files")
2020-03-19 01:02:20 +01:00
, skipPreprocessor = nam_ "skip-preprocessor" &= help "Disable preprocessor"
2019-09-15 16:31:50 +02:00
, exclude = nam_ "exclude" &= name "E" &= typ "CONV"
2020-03-19 01:02:20 +01:00
&= help ("Exclude a particular conversion (always, assert, interface,"
++ " or logic)")
2019-09-15 16:31:50 +02:00
&= groupname "Conversion"
, verbose = nam "verbose" &= help "Retain certain conversion artifacts"
2019-02-26 21:03:49 +01:00
}
&= program "sv2v"
2020-03-31 01:50:04 +02:00
&= summary ("sv2v v0.0.3 (" ++ shortGitHash ++ ")")
2019-02-26 21:03:49 +01:00
&= details [ "sv2v converts SystemVerilog to Verilog."
2020-02-08 00:57:48 +01:00
, "More info: https://github.com/zachjs/sv2v"
, "(C) 2019-2020 Zachary Snow, 2011-2015 Tom Hawkins" ]
2019-09-15 16:31:50 +02:00
&= helpArg [explicit, name "help", groupname "Other"]
&= versionArg [explicit, name "version"]
&= verbosityArgs [ignore] [ignore]
where
-- borrowed from: https://github.com/ndmitchell/hlint
nam xs = nam_ xs &= name [head xs]
nam_ xs = def &= name xs &= explicit
type DeprecationPhase = [String] -> IO [String]
oneunit :: DeprecationPhase
oneunit strs = do
let strs' = filter (not . isOneunitArg) strs
if strs == strs'
then return strs
else do
hPutStr stderr $ "Deprecation warning: --oneunit has been removed, "
++ "and is now on by default\n"
return strs'
where
isOneunitArg :: String -> Bool
isOneunitArg "-o" = True
isOneunitArg "--oneunit" = True
isOneunitArg _ = False
flagRename :: String -> String -> DeprecationPhase
flagRename before after strs = do
let strs' = map rename strs
if strs == strs'
then return strs
else do
hPutStr stderr $ "Deprecation warning: " ++ before ++
" has been renamed to " ++ after ++ "\n"
return strs'
where
rename :: String -> String
rename arg =
if before == take (length before) arg
then after ++ drop (length before) arg
else arg
2019-02-26 21:03:49 +01:00
readJob :: IO Job
readJob = do
2019-09-15 16:31:50 +02:00
strs <- getArgs
strs' <- oneunit strs
>>= flagRename "-i" "-I"
>>= flagRename "-d" "-D"
>>= flagRename "-e" "-E"
>>= flagRename "-V" "--version"
>>= flagRename "-?" "--help"
job <- withArgs (strs') $ cmdArgs defaultJob
return $ if verbose job
then job { exclude = Succinct : exclude job }
else job