| 1 |
{-# LANGUAGE CPP #-} |
| 2 |
{-# LANGUAGE NondecreasingIndentation #-} |
| 3 |
|
| 4 |
{-# OPTIONS -fno-warn-incomplete-patterns -optc-DNON_POSIX_SOURCE #-} |
| 5 |
|
| 6 |
----------------------------------------------------------------------------- |
| 7 |
-- |
| 8 |
-- GHC Driver program |
| 9 |
-- |
| 10 |
-- (c) The University of Glasgow 2005 |
| 11 |
-- |
| 12 |
----------------------------------------------------------------------------- |
| 13 |
|
| 14 |
module Clash.Main (defaultMain, defaultMainWithAction) where |
| 15 |
|
| 16 |
-- The official GHC API |
| 17 |
import qualified GHC |
| 18 |
import GHC ( -- DynFlags(..), HscTarget(..), |
| 19 |
-- GhcMode(..), GhcLink(..), |
| 20 |
Ghc, GhcMonad(..), |
| 21 |
LoadHowMuch(..) ) |
| 22 |
import CmdLineParser |
| 23 |
|
| 24 |
-- Implementations of the various modes (--show-iface, mkdependHS. etc.) |
| 25 |
import LoadIface ( showIface ) |
| 26 |
import HscMain ( newHscEnv ) |
| 27 |
import DriverPipeline ( oneShot, compileFile ) |
| 28 |
import DriverMkDepend ( doMkDependHS ) |
| 29 |
import DriverBkp ( doBackpack ) |
| 30 |
#if defined(GHCI) |
| 31 |
import Clash.GHCi.UI ( interactiveUI, ghciWelcomeMsg, defaultGhciSettings ) |
| 32 |
#endif |
| 33 |
|
| 34 |
-- Frontend plugins |
| 35 |
#if defined(GHCI) |
| 36 |
import DynamicLoading ( loadFrontendPlugin ) |
| 37 |
import Plugins |
| 38 |
#else |
| 39 |
import DynamicLoading ( pluginError ) |
| 40 |
#endif |
| 41 |
import Module ( ModuleName ) |
| 42 |
|
| 43 |
|
| 44 |
-- Various other random stuff that we need |
| 45 |
import GHC.HandleEncoding |
| 46 |
import Config |
| 47 |
import Constants |
| 48 |
import HscTypes |
| 49 |
import Packages ( pprPackages, pprPackagesSimple ) |
| 50 |
import DriverPhases |
| 51 |
import BasicTypes ( failed ) |
| 52 |
import DynFlags hiding (WarnReason(..)) |
| 53 |
import EnumSet as EnumSet |
| 54 |
import ErrUtils |
| 55 |
import FastString |
| 56 |
import Outputable |
| 57 |
import SrcLoc |
| 58 |
import Util |
| 59 |
import Panic |
| 60 |
import UniqSupply |
| 61 |
import MonadUtils ( liftIO ) |
| 62 |
import DynamicLoading ( initializePlugins ) |
| 63 |
|
| 64 |
-- Imports for --abi-hash |
| 65 |
import LoadIface ( loadUserInterface ) |
| 66 |
import Module ( mkModuleName ) |
| 67 |
import Finder ( findImportedModule, cannotFindModule ) |
| 68 |
import TcRnMonad ( initIfaceCheck ) |
| 69 |
import Binary ( openBinMem, put_ ) |
| 70 |
import BinFingerprint ( fingerprintBinMem ) |
| 71 |
|
| 72 |
-- Standard Haskell libraries |
| 73 |
import System.IO |
| 74 |
import System.Environment |
| 75 |
import System.Exit |
| 76 |
import System.FilePath |
| 77 |
import Control.Monad |
| 78 |
import Data.Char |
| 79 |
import Data.List |
| 80 |
import Data.Proxy |
| 81 |
import Data.Maybe |
| 82 |
|
| 83 |
-- clash additions |
| 84 |
import Paths_clash_ghc |
| 85 |
import Clash.GHCi.UI (makeHDL) |
| 86 |
import Exception (gcatch) |
| 87 |
import Data.IORef (IORef, newIORef, readIORef, modifyIORef') |
| 88 |
import qualified Data.Version (showVersion) |
| 89 |
|
| 90 |
import Clash.Backend (Backend) |
| 91 |
import Clash.Backend.SystemVerilog (SystemVerilogState) |
| 92 |
import Clash.Backend.VHDL (VHDLState) |
| 93 |
import Clash.Backend.Verilog (VerilogState) |
| 94 |
import Clash.Driver.Types |
| 95 |
(ClashOpts (..), defClashOpts) |
| 96 |
import Clash.GHC.ClashFlags |
| 97 |
import Clash.Util (clashLibVersion) |
| 98 |
import Clash.GHC.LoadModules (ghcLibDir, setWantedLanguageExtensions) |
| 99 |
import Clash.GHC.Util (handleClashException) |
| 100 |
|
| 101 |
----------------------------------------------------------------------------- |
| 102 |
-- ToDo: |
| 103 |
|
| 104 |
-- time commands when run with -v |
| 105 |
-- user ways |
| 106 |
-- Win32 support: proper signal handling |
| 107 |
-- reading the package configuration file is too slow |
| 108 |
-- -K<size> |
| 109 |
|
| 110 |
----------------------------------------------------------------------------- |
| 111 |
-- GHC's command-line interface |
| 112 |
|
| 113 |
defaultMain :: [String] -> IO () |
| 114 |
defaultMain = defaultMainWithAction (return ()) |
| 115 |
|
| 116 |
defaultMainWithAction :: Ghc () -> [String] -> IO () |
| 117 |
defaultMainWithAction startAction = flip withArgs $ do |
| 118 |
initGCStatistics -- See Note [-Bsymbolic and hooks] |
| 119 |
hSetBuffering stdout LineBuffering |
| 120 |
hSetBuffering stderr LineBuffering |
| 121 |
|
| 122 |
configureHandleEncoding |
| 123 |
GHC.defaultErrorHandler defaultFatalMessager defaultFlushOut $ do |
| 124 |
-- 1. extract the -B flag from the args |
| 125 |
argv0 <- getArgs |
| 126 |
|
| 127 |
-- let (minusB_args, argv1) = partition ("-B" `isPrefixOf`) argv0 |
| 128 |
-- mbMinusB | null minusB_args = Nothing |
| 129 |
-- | otherwise = Just (drop 2 (last minusB_args)) |
| 130 |
|
| 131 |
let argv1 = map (mkGeneralLocated "on the commandline") argv0 |
| 132 |
libDir <- ghcLibDir |
| 133 |
|
| 134 |
r <- newIORef defClashOpts |
| 135 |
(argv2, clashFlagWarnings) <- parseClashFlags r argv1 |
| 136 |
|
| 137 |
-- 2. Parse the "mode" flags (--make, --interactive etc.) |
| 138 |
(mode, argv3, modeFlagWarnings) <- parseModeFlags argv2 |
| 139 |
let flagWarnings = modeFlagWarnings ++ clashFlagWarnings |
| 140 |
|
| 141 |
-- If all we want to do is something like showing the version number |
| 142 |
-- then do it now, before we start a GHC session etc. This makes |
| 143 |
-- getting basic information much more resilient. |
| 144 |
|
| 145 |
-- In particular, if we wait until later before giving the version |
| 146 |
-- number then bootstrapping gets confused, as it tries to find out |
| 147 |
-- what version of GHC it's using before package.conf exists, so |
| 148 |
-- starting the session fails. |
| 149 |
case mode of |
| 150 |
Left preStartupMode -> |
| 151 |
do case preStartupMode of |
| 152 |
ShowSupportedExtensions -> showSupportedExtensions |
| 153 |
ShowVersion -> showVersion |
| 154 |
ShowNumVersion -> putStrLn cProjectVersion |
| 155 |
ShowOptions isInteractive -> showOptions isInteractive r |
| 156 |
Right postStartupMode -> |
| 157 |
-- start our GHC session |
| 158 |
GHC.runGhc (Just libDir) $ do |
| 159 |
|
| 160 |
dflags <- GHC.getSessionDynFlags |
| 161 |
let dflagsExtra = setWantedLanguageExtensions dflags |
| 162 |
|
| 163 |
ghcTyLitNormPlugin = GHC.mkModuleName "GHC.TypeLits.Normalise" |
| 164 |
ghcTyLitExtrPlugin = GHC.mkModuleName "GHC.TypeLits.Extra.Solver" |
| 165 |
ghcTyLitKNPlugin = GHC.mkModuleName "GHC.TypeLits.KnownNat.Solver" |
| 166 |
dflagsExtra1 = dflagsExtra |
| 167 |
{ DynFlags.pluginModNames = nub $ |
| 168 |
ghcTyLitNormPlugin : ghcTyLitExtrPlugin : |
| 169 |
ghcTyLitKNPlugin : |
| 170 |
DynFlags.pluginModNames dflagsExtra |
| 171 |
} |
| 172 |
|
| 173 |
case postStartupMode of |
| 174 |
Left preLoadMode -> |
| 175 |
liftIO $ do |
| 176 |
case preLoadMode of |
| 177 |
ShowInfo -> showInfo dflagsExtra1 |
| 178 |
ShowGhcUsage -> showGhcUsage dflagsExtra1 |
| 179 |
ShowGhciUsage -> showGhciUsage dflagsExtra1 |
| 180 |
PrintWithDynFlags f -> putStrLn (f dflagsExtra1) |
| 181 |
Right postLoadMode -> |
| 182 |
main' postLoadMode dflagsExtra1 argv3 flagWarnings startAction r |
| 183 |
|
| 184 |
main' :: PostLoadMode -> DynFlags -> [Located String] -> [Warn] |
| 185 |
-> Ghc () -> IORef ClashOpts |
| 186 |
-> Ghc () |
| 187 |
main' postLoadMode dflags0 args flagWarnings startAction clashOpts = do |
| 188 |
-- set the default GhcMode, HscTarget and GhcLink. The HscTarget |
| 189 |
-- can be further adjusted on a module by module basis, using only |
| 190 |
-- the -fvia-C and -fasm flags. If the default HscTarget is not |
| 191 |
-- HscC or HscAsm, -fvia-C and -fasm have no effect. |
| 192 |
let dflt_target = hscTarget dflags0 |
| 193 |
(mode, lang, link) |
| 194 |
= case postLoadMode of |
| 195 |
DoInteractive -> (CompManager, HscInterpreted, LinkInMemory) |
| 196 |
DoEval _ -> (CompManager, HscInterpreted, LinkInMemory) |
| 197 |
DoMake -> (CompManager, dflt_target, LinkBinary) |
| 198 |
DoBackpack -> (CompManager, dflt_target, LinkBinary) |
| 199 |
DoMkDependHS -> (MkDepend, dflt_target, LinkBinary) |
| 200 |
DoAbiHash -> (OneShot, dflt_target, LinkBinary) |
| 201 |
DoVHDL -> (CompManager, HscNothing, NoLink) |
| 202 |
DoVerilog -> (CompManager, HscNothing, NoLink) |
| 203 |
DoSystemVerilog -> (CompManager, HscNothing, NoLink) |
| 204 |
_ -> (OneShot, dflt_target, LinkBinary) |
| 205 |
|
| 206 |
let dflags1 = dflags0{ ghcMode = mode, |
| 207 |
hscTarget = lang, |
| 208 |
ghcLink = link, |
| 209 |
verbosity = case postLoadMode of |
| 210 |
DoEval _ -> 0 |
| 211 |
_other -> 1 |
| 212 |
} |
| 213 |
|
| 214 |
-- turn on -fimplicit-import-qualified for GHCi now, so that it |
| 215 |
-- can be overriden from the command-line |
| 216 |
-- XXX: this should really be in the interactive DynFlags, but |
| 217 |
-- we don't set that until later in interactiveUI |
| 218 |
-- We also set -fignore-optim-changes and -fignore-hpc-changes, |
| 219 |
-- which are program-level options. Again, this doesn't really |
| 220 |
-- feel like the right place to handle this, but we don't have |
| 221 |
-- a great story for the moment. |
| 222 |
dflags2 | DoInteractive <- postLoadMode = def_ghci_flags |
| 223 |
| DoEval _ <- postLoadMode = def_ghci_flags |
| 224 |
| otherwise = dflags1 |
| 225 |
where def_ghci_flags = dflags1 `gopt_set` Opt_ImplicitImportQualified |
| 226 |
`gopt_set` Opt_IgnoreOptimChanges |
| 227 |
`gopt_set` Opt_IgnoreHpcChanges |
| 228 |
|
| 229 |
-- The rest of the arguments are "dynamic" |
| 230 |
-- Leftover ones are presumably files |
| 231 |
(dflags3, fileish_args, dynamicFlagWarnings) <- |
| 232 |
GHC.parseDynamicFlags dflags2 args |
| 233 |
|
| 234 |
-- Propagate -Werror to Clash |
| 235 |
liftIO . modifyIORef' clashOpts $ \opts -> |
| 236 |
opts { opt_werror = EnumSet.member Opt_WarnIsError (generalFlags dflags3) } |
| 237 |
|
| 238 |
let dflags4 = case lang of |
| 239 |
HscInterpreted | not (gopt Opt_ExternalInterpreter dflags3) -> |
| 240 |
let platform = targetPlatform dflags3 |
| 241 |
dflags3a = updateWays $ dflags3 { ways = interpWays } |
| 242 |
dflags3b = foldl gopt_set dflags3a |
| 243 |
$ concatMap (wayGeneralFlags platform) |
| 244 |
interpWays |
| 245 |
dflags3c = foldl gopt_unset dflags3b |
| 246 |
$ concatMap (wayUnsetGeneralFlags platform) |
| 247 |
interpWays |
| 248 |
in dflags3c |
| 249 |
_ -> |
| 250 |
dflags3 |
| 251 |
|
| 252 |
GHC.prettyPrintGhcErrors dflags4 $ do |
| 253 |
|
| 254 |
let flagWarnings' = flagWarnings ++ dynamicFlagWarnings |
| 255 |
|
| 256 |
handleSourceError (\e -> do |
| 257 |
GHC.printException e |
| 258 |
liftIO $ exitWith (ExitFailure 1)) $ do |
| 259 |
liftIO $ handleFlagWarnings dflags4 flagWarnings' |
| 260 |
|
| 261 |
liftIO $ showBanner postLoadMode dflags4 |
| 262 |
|
| 263 |
let |
| 264 |
-- To simplify the handling of filepaths, we normalise all filepaths right |
| 265 |
-- away. Note the asymmetry of FilePath.normalise: |
| 266 |
-- Linux: p/q -> p/q; p\q -> p\q |
| 267 |
-- Windows: p/q -> p\q; p\q -> p\q |
| 268 |
-- #12674: Filenames starting with a hypen get normalised from ./-foo.hs |
| 269 |
-- to -foo.hs. We have to re-prepend the current directory. |
| 270 |
normalise_hyp fp |
| 271 |
| strt_dot_sl && "-" `isPrefixOf` nfp = cur_dir ++ nfp |
| 272 |
| otherwise = nfp |
| 273 |
where |
| 274 |
#if defined(mingw32_HOST_OS) |
| 275 |
strt_dot_sl = "./" `isPrefixOf` fp || ".\\" `isPrefixOf` fp |
| 276 |
#else |
| 277 |
strt_dot_sl = "./" `isPrefixOf` fp |
| 278 |
#endif |
| 279 |
cur_dir = '.' : [pathSeparator] |
| 280 |
nfp = normalise fp |
| 281 |
normal_fileish_paths = map (normalise_hyp . unLoc) fileish_args |
| 282 |
(srcs, objs) = partition_args normal_fileish_paths [] [] |
| 283 |
|
| 284 |
dflags5 = dflags4 { ldInputs = map (FileOption "") objs |
| 285 |
++ ldInputs dflags4 } |
| 286 |
|
| 287 |
-- we've finished manipulating the DynFlags, update the session |
| 288 |
_ <- GHC.setSessionDynFlags dflags5 |
| 289 |
dflags6 <- GHC.getSessionDynFlags |
| 290 |
hsc_env <- GHC.getSession |
| 291 |
|
| 292 |
---------------- Display configuration ----------- |
| 293 |
case verbosity dflags6 of |
| 294 |
v | v == 4 -> liftIO $ dumpPackagesSimple dflags6 |
| 295 |
| v >= 5 -> liftIO $ dumpPackages dflags6 |
| 296 |
| otherwise -> return () |
| 297 |
|
| 298 |
liftIO $ initUniqSupply (initialUnique dflags6) (uniqueIncrement dflags6) |
| 299 |
---------------- Final sanity checking ----------- |
| 300 |
liftIO $ checkOptions postLoadMode dflags6 srcs objs |
| 301 |
|
| 302 |
---------------- Do the business ----------- |
| 303 |
handleSourceError (\e -> do |
| 304 |
GHC.printException e |
| 305 |
liftIO $ exitWith (ExitFailure 1)) $ do |
| 306 |
clashOpts' <- liftIO (readIORef clashOpts) |
| 307 |
let clash fun = gcatch (fun startAction clashOpts srcs) (handleClashException dflags6 clashOpts') |
| 308 |
case postLoadMode of |
| 309 |
ShowInterface f -> liftIO $ doShowIface dflags6 f |
| 310 |
DoMake -> doMake srcs |
| 311 |
DoMkDependHS -> doMkDependHS (map fst srcs) |
| 312 |
StopBefore p -> liftIO (oneShot hsc_env p srcs) |
| 313 |
DoInteractive -> ghciUI clashOpts hsc_env dflags6 srcs Nothing |
| 314 |
DoEval exprs -> ghciUI clashOpts hsc_env dflags6 srcs $ Just $ |
| 315 |
reverse exprs |
| 316 |
DoAbiHash -> abiHash (map fst srcs) |
| 317 |
ShowPackages -> liftIO $ showPackages dflags6 |
| 318 |
DoFrontend f -> doFrontend f srcs |
| 319 |
DoBackpack -> doBackpack (map fst srcs) |
| 320 |
DoVHDL -> clash makeVHDL |
| 321 |
DoVerilog -> clash makeVerilog |
| 322 |
DoSystemVerilog -> clash makeSystemVerilog |
| 323 |
|
| 324 |
liftIO $ dumpFinalStats dflags6 |
| 325 |
|
| 326 |
ghciUI :: IORef ClashOpts -> HscEnv -> DynFlags -> [(FilePath, Maybe Phase)] -> Maybe [String] |
| 327 |
-> Ghc () |
| 328 |
#if !defined(GHCI) |
| 329 |
ghciUI _ _ _ _ _ = throwGhcException (CmdLineError "not built for interactive use") |
| 330 |
#else |
| 331 |
ghciUI clashOpts hsc_env dflags0 srcs maybe_expr = do |
| 332 |
dflags1 <- liftIO (initializePlugins hsc_env dflags0) |
| 333 |
_ <- GHC.setSessionDynFlags dflags1 |
| 334 |
interactiveUI (defaultGhciSettings clashOpts) srcs maybe_expr |
| 335 |
#endif |
| 336 |
|
| 337 |
-- ----------------------------------------------------------------------------- |
| 338 |
-- Splitting arguments into source files and object files. This is where we |
| 339 |
-- interpret the -x <suffix> option, and attach a (Maybe Phase) to each source |
| 340 |
-- file indicating the phase specified by the -x option in force, if any. |
| 341 |
|
| 342 |
partition_args :: [String] -> [(String, Maybe Phase)] -> [String] |
| 343 |
-> ([(String, Maybe Phase)], [String]) |
| 344 |
partition_args [] srcs objs = (reverse srcs, reverse objs) |
| 345 |
partition_args ("-x":suff:args) srcs objs |
| 346 |
| "none" <- suff = partition_args args srcs objs |
| 347 |
| StopLn <- phase = partition_args args srcs (slurp ++ objs) |
| 348 |
| otherwise = partition_args rest (these_srcs ++ srcs) objs |
| 349 |
where phase = startPhase suff |
| 350 |
(slurp,rest) = break (== "-x") args |
| 351 |
these_srcs = zip slurp (repeat (Just phase)) |
| 352 |
partition_args (arg:args) srcs objs |
| 353 |
| looks_like_an_input arg = partition_args args ((arg,Nothing):srcs) objs |
| 354 |
| otherwise = partition_args args srcs (arg:objs) |
| 355 |
|
| 356 |
{- |
| 357 |
We split out the object files (.o, .dll) and add them |
| 358 |
to ldInputs for use by the linker. |
| 359 |
|
| 360 |
The following things should be considered compilation manager inputs: |
| 361 |
|
| 362 |
- haskell source files (strings ending in .hs, .lhs or other |
| 363 |
haskellish extension), |
| 364 |
|
| 365 |
- module names (not forgetting hierarchical module names), |
| 366 |
|
| 367 |
- things beginning with '-' are flags that were not recognised by |
| 368 |
the flag parser, and we want them to generate errors later in |
| 369 |
checkOptions, so we class them as source files (#5921) |
| 370 |
|
| 371 |
- and finally we consider everything without an extension to be |
| 372 |
a comp manager input, as shorthand for a .hs or .lhs filename. |
| 373 |
|
| 374 |
Everything else is considered to be a linker object, and passed |
| 375 |
straight through to the linker. |
| 376 |
-} |
| 377 |
looks_like_an_input :: String -> Bool |
| 378 |
looks_like_an_input m = isSourceFilename m |
| 379 |
|| looksLikeModuleName m |
| 380 |
|| "-" `isPrefixOf` m |
| 381 |
|| not (hasExtension m) |
| 382 |
|
| 383 |
-- ----------------------------------------------------------------------------- |
| 384 |
-- Option sanity checks |
| 385 |
|
| 386 |
-- | Ensure sanity of options. |
| 387 |
-- |
| 388 |
-- Throws 'UsageError' or 'CmdLineError' if not. |
| 389 |
checkOptions :: PostLoadMode -> DynFlags -> [(String,Maybe Phase)] -> [String] -> IO () |
| 390 |
-- Final sanity checking before kicking off a compilation (pipeline). |
| 391 |
checkOptions mode dflags srcs objs = do |
| 392 |
-- Complain about any unknown flags |
| 393 |
let unknown_opts = [ f | (f@('-':_), _) <- srcs ] |
| 394 |
when (notNull unknown_opts) (unknownFlagsErr unknown_opts) |
| 395 |
|
| 396 |
when (notNull (filter wayRTSOnly (ways dflags)) |
| 397 |
&& isInterpretiveMode mode) $ |
| 398 |
hPutStrLn stderr ("Warning: -debug, -threaded and -ticky are ignored by GHCi") |
| 399 |
|
| 400 |
-- -prof and --interactive are not a good combination |
| 401 |
when ((filter (not . wayRTSOnly) (ways dflags) /= interpWays) |
| 402 |
&& isInterpretiveMode mode |
| 403 |
&& not (gopt Opt_ExternalInterpreter dflags)) $ |
| 404 |
do throwGhcException (UsageError |
| 405 |
"-fexternal-interpreter is required when using --interactive with a non-standard way (-prof, -static, or -dynamic).") |
| 406 |
-- -ohi sanity check |
| 407 |
if (isJust (outputHi dflags) && |
| 408 |
(isCompManagerMode mode || srcs `lengthExceeds` 1)) |
| 409 |
then throwGhcException (UsageError "-ohi can only be used when compiling a single source file") |
| 410 |
else do |
| 411 |
|
| 412 |
-- -o sanity checking |
| 413 |
if (srcs `lengthExceeds` 1 && isJust (outputFile dflags) |
| 414 |
&& not (isLinkMode mode)) |
| 415 |
then throwGhcException (UsageError "can't apply -o to multiple source files") |
| 416 |
else do |
| 417 |
|
| 418 |
let not_linking = not (isLinkMode mode) || isNoLink (ghcLink dflags) |
| 419 |
|
| 420 |
when (not_linking && not (null objs)) $ |
| 421 |
hPutStrLn stderr ("Warning: the following files would be used as linker inputs, but linking is not being done: " ++ unwords objs) |
| 422 |
|
| 423 |
-- Check that there are some input files |
| 424 |
-- (except in the interactive case) |
| 425 |
if null srcs && (null objs || not_linking) && needsInputsMode mode |
| 426 |
then throwGhcException (UsageError "no input files") |
| 427 |
else do |
| 428 |
|
| 429 |
case mode of |
| 430 |
StopBefore HCc | hscTarget dflags /= HscC |
| 431 |
-> throwGhcException $ UsageError $ |
| 432 |
"the option -C is only available with an unregisterised GHC" |
| 433 |
_ -> return () |
| 434 |
|
| 435 |
-- Verify that output files point somewhere sensible. |
| 436 |
verifyOutputFiles dflags |
| 437 |
|
| 438 |
-- Compiler output options |
| 439 |
|
| 440 |
-- Called to verify that the output files point somewhere valid. |
| 441 |
-- |
| 442 |
-- The assumption is that the directory portion of these output |
| 443 |
-- options will have to exist by the time 'verifyOutputFiles' |
| 444 |
-- is invoked. |
| 445 |
-- |
| 446 |
-- We create the directories for -odir, -hidir, -outputdir etc. ourselves if |
| 447 |
-- they don't exist, so don't check for those here (#2278). |
| 448 |
verifyOutputFiles :: DynFlags -> IO () |
| 449 |
verifyOutputFiles dflags = do |
| 450 |
let ofile = outputFile dflags |
| 451 |
when (isJust ofile) $ do |
| 452 |
let fn = fromJust ofile |
| 453 |
flg <- doesDirNameExist fn |
| 454 |
when (not flg) (nonExistentDir "-o" fn) |
| 455 |
let ohi = outputHi dflags |
| 456 |
when (isJust ohi) $ do |
| 457 |
let hi = fromJust ohi |
| 458 |
flg <- doesDirNameExist hi |
| 459 |
when (not flg) (nonExistentDir "-ohi" hi) |
| 460 |
where |
| 461 |
nonExistentDir flg dir = |
| 462 |
throwGhcException (CmdLineError ("error: directory portion of " ++ |
| 463 |
show dir ++ " does not exist (used with " ++ |
| 464 |
show flg ++ " option.)")) |
| 465 |
|
| 466 |
----------------------------------------------------------------------------- |
| 467 |
-- GHC modes of operation |
| 468 |
|
| 469 |
type Mode = Either PreStartupMode PostStartupMode |
| 470 |
type PostStartupMode = Either PreLoadMode PostLoadMode |
| 471 |
|
| 472 |
data PreStartupMode |
| 473 |
= ShowVersion -- ghc -V/--version |
| 474 |
| ShowNumVersion -- ghc --numeric-version |
| 475 |
| ShowSupportedExtensions -- ghc --supported-extensions |
| 476 |
| ShowOptions Bool {- isInteractive -} -- ghc --show-options |
| 477 |
|
| 478 |
showVersionMode, showNumVersionMode, showSupportedExtensionsMode, showOptionsMode :: Mode |
| 479 |
showVersionMode = mkPreStartupMode ShowVersion |
| 480 |
showNumVersionMode = mkPreStartupMode ShowNumVersion |
| 481 |
showSupportedExtensionsMode = mkPreStartupMode ShowSupportedExtensions |
| 482 |
showOptionsMode = mkPreStartupMode (ShowOptions False) |
| 483 |
|
| 484 |
mkPreStartupMode :: PreStartupMode -> Mode |
| 485 |
mkPreStartupMode = Left |
| 486 |
|
| 487 |
isShowVersionMode :: Mode -> Bool |
| 488 |
isShowVersionMode (Left ShowVersion) = True |
| 489 |
isShowVersionMode _ = False |
| 490 |
|
| 491 |
isShowNumVersionMode :: Mode -> Bool |
| 492 |
isShowNumVersionMode (Left ShowNumVersion) = True |
| 493 |
isShowNumVersionMode _ = False |
| 494 |
|
| 495 |
data PreLoadMode |
| 496 |
= ShowGhcUsage -- ghc -? |
| 497 |
| ShowGhciUsage -- ghci -? |
| 498 |
| ShowInfo -- ghc --info |
| 499 |
| PrintWithDynFlags (DynFlags -> String) -- ghc --print-foo |
| 500 |
|
| 501 |
showGhcUsageMode, showGhciUsageMode, showInfoMode :: Mode |
| 502 |
showGhcUsageMode = mkPreLoadMode ShowGhcUsage |
| 503 |
showGhciUsageMode = mkPreLoadMode ShowGhciUsage |
| 504 |
showInfoMode = mkPreLoadMode ShowInfo |
| 505 |
|
| 506 |
printSetting :: String -> Mode |
| 507 |
printSetting k = mkPreLoadMode (PrintWithDynFlags f) |
| 508 |
where f dflags = fromMaybe (panic ("Setting not found: " ++ show k)) |
| 509 |
$ lookup k (compilerInfo dflags) |
| 510 |
|
| 511 |
mkPreLoadMode :: PreLoadMode -> Mode |
| 512 |
mkPreLoadMode = Right . Left |
| 513 |
|
| 514 |
isShowGhcUsageMode :: Mode -> Bool |
| 515 |
isShowGhcUsageMode (Right (Left ShowGhcUsage)) = True |
| 516 |
isShowGhcUsageMode _ = False |
| 517 |
|
| 518 |
isShowGhciUsageMode :: Mode -> Bool |
| 519 |
isShowGhciUsageMode (Right (Left ShowGhciUsage)) = True |
| 520 |
isShowGhciUsageMode _ = False |
| 521 |
|
| 522 |
data PostLoadMode |
| 523 |
= ShowInterface FilePath -- ghc --show-iface |
| 524 |
| DoMkDependHS -- ghc -M |
| 525 |
| StopBefore Phase -- ghc -E | -C | -S |
| 526 |
-- StopBefore StopLn is the default |
| 527 |
| DoMake -- ghc --make |
| 528 |
| DoBackpack -- ghc --backpack foo.bkp |
| 529 |
| DoInteractive -- ghc --interactive |
| 530 |
| DoEval [String] -- ghc -e foo -e bar => DoEval ["bar", "foo"] |
| 531 |
| DoAbiHash -- ghc --abi-hash |
| 532 |
| ShowPackages -- ghc --show-packages |
| 533 |
| DoFrontend ModuleName -- ghc --frontend Plugin.Module |
| 534 |
| DoVHDL -- ghc --vhdl |
| 535 |
| DoVerilog -- ghc --verilog |
| 536 |
| DoSystemVerilog -- ghc --systemverilog |
| 537 |
|
| 538 |
doMkDependHSMode, doMakeMode, doInteractiveMode, |
| 539 |
doAbiHashMode, showPackagesMode, doVHDLMode, doVerilogMode, |
| 540 |
doSystemVerilogMode :: Mode |
| 541 |
doMkDependHSMode = mkPostLoadMode DoMkDependHS |
| 542 |
doMakeMode = mkPostLoadMode DoMake |
| 543 |
doInteractiveMode = mkPostLoadMode DoInteractive |
| 544 |
doAbiHashMode = mkPostLoadMode DoAbiHash |
| 545 |
showPackagesMode = mkPostLoadMode ShowPackages |
| 546 |
doVHDLMode = mkPostLoadMode DoVHDL |
| 547 |
doVerilogMode = mkPostLoadMode DoVerilog |
| 548 |
doSystemVerilogMode = mkPostLoadMode DoSystemVerilog |
| 549 |
|
| 550 |
showInterfaceMode :: FilePath -> Mode |
| 551 |
showInterfaceMode fp = mkPostLoadMode (ShowInterface fp) |
| 552 |
|
| 553 |
stopBeforeMode :: Phase -> Mode |
| 554 |
stopBeforeMode phase = mkPostLoadMode (StopBefore phase) |
| 555 |
|
| 556 |
doEvalMode :: String -> Mode |
| 557 |
doEvalMode str = mkPostLoadMode (DoEval [str]) |
| 558 |
|
| 559 |
doFrontendMode :: String -> Mode |
| 560 |
doFrontendMode str = mkPostLoadMode (DoFrontend (mkModuleName str)) |
| 561 |
|
| 562 |
doBackpackMode :: Mode |
| 563 |
doBackpackMode = mkPostLoadMode DoBackpack |
| 564 |
|
| 565 |
mkPostLoadMode :: PostLoadMode -> Mode |
| 566 |
mkPostLoadMode = Right . Right |
| 567 |
|
| 568 |
isDoInteractiveMode :: Mode -> Bool |
| 569 |
isDoInteractiveMode (Right (Right DoInteractive)) = True |
| 570 |
isDoInteractiveMode _ = False |
| 571 |
|
| 572 |
isStopLnMode :: Mode -> Bool |
| 573 |
isStopLnMode (Right (Right (StopBefore StopLn))) = True |
| 574 |
isStopLnMode _ = False |
| 575 |
|
| 576 |
isDoMakeMode :: Mode -> Bool |
| 577 |
isDoMakeMode (Right (Right DoMake)) = True |
| 578 |
isDoMakeMode _ = False |
| 579 |
|
| 580 |
isDoEvalMode :: Mode -> Bool |
| 581 |
isDoEvalMode (Right (Right (DoEval _))) = True |
| 582 |
isDoEvalMode _ = False |
| 583 |
|
| 584 |
#if defined(GHCI) |
| 585 |
isInteractiveMode :: PostLoadMode -> Bool |
| 586 |
isInteractiveMode DoInteractive = True |
| 587 |
isInteractiveMode _ = False |
| 588 |
#endif |
| 589 |
|
| 590 |
-- isInterpretiveMode: byte-code compiler involved |
| 591 |
isInterpretiveMode :: PostLoadMode -> Bool |
| 592 |
isInterpretiveMode DoInteractive = True |
| 593 |
isInterpretiveMode (DoEval _) = True |
| 594 |
isInterpretiveMode _ = False |
| 595 |
|
| 596 |
needsInputsMode :: PostLoadMode -> Bool |
| 597 |
needsInputsMode DoMkDependHS = True |
| 598 |
needsInputsMode (StopBefore _) = True |
| 599 |
needsInputsMode DoMake = True |
| 600 |
needsInputsMode DoVHDL = True |
| 601 |
needsInputsMode DoVerilog = True |
| 602 |
needsInputsMode DoSystemVerilog = True |
| 603 |
needsInputsMode _ = False |
| 604 |
|
| 605 |
-- True if we are going to attempt to link in this mode. |
| 606 |
-- (we might not actually link, depending on the GhcLink flag) |
| 607 |
isLinkMode :: PostLoadMode -> Bool |
| 608 |
isLinkMode (StopBefore StopLn) = True |
| 609 |
isLinkMode DoMake = True |
| 610 |
isLinkMode DoInteractive = True |
| 611 |
isLinkMode (DoEval _) = True |
| 612 |
isLinkMode _ = False |
| 613 |
|
| 614 |
isCompManagerMode :: PostLoadMode -> Bool |
| 615 |
isCompManagerMode DoMake = True |
| 616 |
isCompManagerMode DoInteractive = True |
| 617 |
isCompManagerMode (DoEval _) = True |
| 618 |
isCompManagerMode DoVHDL = True |
| 619 |
isCompManagerMode DoVerilog = True |
| 620 |
isCompManagerMode DoSystemVerilog = True |
| 621 |
isCompManagerMode _ = False |
| 622 |
|
| 623 |
-- ----------------------------------------------------------------------------- |
| 624 |
-- Parsing the mode flag |
| 625 |
|
| 626 |
parseModeFlags :: [Located String] |
| 627 |
-> IO (Mode, |
| 628 |
[Located String], |
| 629 |
[Warn]) |
| 630 |
parseModeFlags args = do |
| 631 |
let ((leftover, errs1, warns), (mModeFlag, errs2, flags')) = |
| 632 |
runCmdLine (processArgs mode_flags args) |
| 633 |
(Nothing, [], []) |
| 634 |
mode = case mModeFlag of |
| 635 |
Nothing -> doMakeMode |
| 636 |
Just (m, _) -> m |
| 637 |
|
| 638 |
-- See Note [Handling errors when parsing commandline flags] |
| 639 |
unless (null errs1 && null errs2) $ throwGhcException $ errorsToGhcException $ |
| 640 |
map (("on the commandline", )) $ map (unLoc . errMsg) errs1 ++ errs2 |
| 641 |
|
| 642 |
return (mode, flags' ++ leftover, warns) |
| 643 |
|
| 644 |
type ModeM = CmdLineP (Maybe (Mode, String), [String], [Located String]) |
| 645 |
-- mode flags sometimes give rise to new DynFlags (eg. -C, see below) |
| 646 |
-- so we collect the new ones and return them. |
| 647 |
|
| 648 |
mode_flags :: [Flag ModeM] |
| 649 |
mode_flags = |
| 650 |
[ ------- help / version ---------------------------------------------- |
| 651 |
defFlag "?" (PassFlag (setMode showGhcUsageMode)) |
| 652 |
, defFlag "-help" (PassFlag (setMode showGhcUsageMode)) |
| 653 |
, defFlag "V" (PassFlag (setMode showVersionMode)) |
| 654 |
, defFlag "-version" (PassFlag (setMode showVersionMode)) |
| 655 |
, defFlag "-numeric-version" (PassFlag (setMode showNumVersionMode)) |
| 656 |
, defFlag "-info" (PassFlag (setMode showInfoMode)) |
| 657 |
, defFlag "-show-options" (PassFlag (setMode showOptionsMode)) |
| 658 |
, defFlag "-supported-languages" (PassFlag (setMode showSupportedExtensionsMode)) |
| 659 |
, defFlag "-supported-extensions" (PassFlag (setMode showSupportedExtensionsMode)) |
| 660 |
, defFlag "-show-packages" (PassFlag (setMode showPackagesMode)) |
| 661 |
] ++ |
| 662 |
[ defFlag k' (PassFlag (setMode (printSetting k))) |
| 663 |
| k <- ["Project version", |
| 664 |
"Project Git commit id", |
| 665 |
"Booter version", |
| 666 |
"Stage", |
| 667 |
"Build platform", |
| 668 |
"Host platform", |
| 669 |
"Target platform", |
| 670 |
"Have interpreter", |
| 671 |
"Object splitting supported", |
| 672 |
"Have native code generator", |
| 673 |
"Support SMP", |
| 674 |
"Unregisterised", |
| 675 |
"Tables next to code", |
| 676 |
"RTS ways", |
| 677 |
"Leading underscore", |
| 678 |
"Debug on", |
| 679 |
"LibDir", |
| 680 |
"Global Package DB", |
| 681 |
"C compiler flags", |
| 682 |
"C compiler link flags", |
| 683 |
"ld flags"], |
| 684 |
let k' = "-print-" ++ map (replaceSpace . toLower) k |
| 685 |
replaceSpace ' ' = '-' |
| 686 |
replaceSpace c = c |
| 687 |
] ++ |
| 688 |
------- interfaces ---------------------------------------------------- |
| 689 |
[ defFlag "-show-iface" (HasArg (\f -> setMode (showInterfaceMode f) |
| 690 |
"--show-iface")) |
| 691 |
|
| 692 |
------- primary modes ------------------------------------------------ |
| 693 |
, defFlag "c" (PassFlag (\f -> do setMode (stopBeforeMode StopLn) f |
| 694 |
addFlag "-no-link" f)) |
| 695 |
, defFlag "M" (PassFlag (setMode doMkDependHSMode)) |
| 696 |
, defFlag "E" (PassFlag (setMode (stopBeforeMode anyHsc))) |
| 697 |
, defFlag "C" (PassFlag (setMode (stopBeforeMode HCc))) |
| 698 |
, defFlag "S" (PassFlag (setMode (stopBeforeMode (As False)))) |
| 699 |
, defFlag "-make" (PassFlag (setMode doMakeMode)) |
| 700 |
, defFlag "-backpack" (PassFlag (setMode doBackpackMode)) |
| 701 |
, defFlag "-interactive" (PassFlag (setMode doInteractiveMode)) |
| 702 |
, defFlag "-abi-hash" (PassFlag (setMode doAbiHashMode)) |
| 703 |
, defFlag "e" (SepArg (\s -> setMode (doEvalMode s) "-e")) |
| 704 |
, defFlag "-frontend" (SepArg (\s -> setMode (doFrontendMode s) "-frontend")) |
| 705 |
, defFlag "-vhdl" (PassFlag (setMode doVHDLMode)) |
| 706 |
, defFlag "-verilog" (PassFlag (setMode doVerilogMode)) |
| 707 |
, defFlag "-systemverilog" (PassFlag (setMode doSystemVerilogMode)) |
| 708 |
] |
| 709 |
|
| 710 |
setMode :: Mode -> String -> EwM ModeM () |
| 711 |
setMode newMode newFlag = liftEwM $ do |
| 712 |
(mModeFlag, errs, flags') <- getCmdLineState |
| 713 |
let (modeFlag', errs') = |
| 714 |
case mModeFlag of |
| 715 |
Nothing -> ((newMode, newFlag), errs) |
| 716 |
Just (oldMode, oldFlag) -> |
| 717 |
case (oldMode, newMode) of |
| 718 |
-- -c/--make are allowed together, and mean --make -no-link |
| 719 |
_ | isStopLnMode oldMode && isDoMakeMode newMode |
| 720 |
|| isStopLnMode newMode && isDoMakeMode oldMode -> |
| 721 |
((doMakeMode, "--make"), []) |
| 722 |
|
| 723 |
-- If we have both --help and --interactive then we |
| 724 |
-- want showGhciUsage |
| 725 |
_ | isShowGhcUsageMode oldMode && |
| 726 |
isDoInteractiveMode newMode -> |
| 727 |
((showGhciUsageMode, oldFlag), []) |
| 728 |
| isShowGhcUsageMode newMode && |
| 729 |
isDoInteractiveMode oldMode -> |
| 730 |
((showGhciUsageMode, newFlag), []) |
| 731 |
|
| 732 |
-- If we have both -e and --interactive then -e always wins |
| 733 |
_ | isDoEvalMode oldMode && |
| 734 |
isDoInteractiveMode newMode -> |
| 735 |
((oldMode, oldFlag), []) |
| 736 |
| isDoEvalMode newMode && |
| 737 |
isDoInteractiveMode oldMode -> |
| 738 |
((newMode, newFlag), []) |
| 739 |
|
| 740 |
-- Otherwise, --help/--version/--numeric-version always win |
| 741 |
| isDominantFlag oldMode -> ((oldMode, oldFlag), []) |
| 742 |
| isDominantFlag newMode -> ((newMode, newFlag), []) |
| 743 |
-- We need to accumulate eval flags like "-e foo -e bar" |
| 744 |
(Right (Right (DoEval esOld)), |
| 745 |
Right (Right (DoEval [eNew]))) -> |
| 746 |
((Right (Right (DoEval (eNew : esOld))), oldFlag), |
| 747 |
errs) |
| 748 |
-- Saying e.g. --interactive --interactive is OK |
| 749 |
_ | oldFlag == newFlag -> ((oldMode, oldFlag), errs) |
| 750 |
|
| 751 |
-- --interactive and --show-options are used together |
| 752 |
(Right (Right DoInteractive), Left (ShowOptions _)) -> |
| 753 |
((Left (ShowOptions True), |
| 754 |
"--interactive --show-options"), errs) |
| 755 |
(Left (ShowOptions _), (Right (Right DoInteractive))) -> |
| 756 |
((Left (ShowOptions True), |
| 757 |
"--show-options --interactive"), errs) |
| 758 |
-- Otherwise, complain |
| 759 |
_ -> let err = flagMismatchErr oldFlag newFlag |
| 760 |
in ((oldMode, oldFlag), err : errs) |
| 761 |
putCmdLineState (Just modeFlag', errs', flags') |
| 762 |
where isDominantFlag f = isShowGhcUsageMode f || |
| 763 |
isShowGhciUsageMode f || |
| 764 |
isShowVersionMode f || |
| 765 |
isShowNumVersionMode f |
| 766 |
|
| 767 |
flagMismatchErr :: String -> String -> String |
| 768 |
flagMismatchErr oldFlag newFlag |
| 769 |
= "cannot use `" ++ oldFlag ++ "' with `" ++ newFlag ++ "'" |
| 770 |
|
| 771 |
addFlag :: String -> String -> EwM ModeM () |
| 772 |
addFlag s flag = liftEwM $ do |
| 773 |
(m, e, flags') <- getCmdLineState |
| 774 |
putCmdLineState (m, e, mkGeneralLocated loc s : flags') |
| 775 |
where loc = "addFlag by " ++ flag ++ " on the commandline" |
| 776 |
|
| 777 |
-- ---------------------------------------------------------------------------- |
| 778 |
-- Run --make mode |
| 779 |
|
| 780 |
doMake :: [(String,Maybe Phase)] -> Ghc () |
| 781 |
doMake srcs = do |
| 782 |
let (hs_srcs, non_hs_srcs) = partition isHaskellishTarget srcs |
| 783 |
|
| 784 |
hsc_env <- GHC.getSession |
| 785 |
|
| 786 |
-- if we have no haskell sources from which to do a dependency |
| 787 |
-- analysis, then just do one-shot compilation and/or linking. |
| 788 |
-- This means that "ghc Foo.o Bar.o -o baz" links the program as |
| 789 |
-- we expect. |
| 790 |
if (null hs_srcs) |
| 791 |
then liftIO (oneShot hsc_env StopLn srcs) |
| 792 |
else do |
| 793 |
|
| 794 |
o_files <- mapM (\x -> liftIO $ compileFile hsc_env StopLn x) |
| 795 |
non_hs_srcs |
| 796 |
dflags <- GHC.getSessionDynFlags |
| 797 |
let dflags' = dflags { ldInputs = map (FileOption "") o_files |
| 798 |
++ ldInputs dflags } |
| 799 |
_ <- GHC.setSessionDynFlags dflags' |
| 800 |
|
| 801 |
targets <- mapM (uncurry GHC.guessTarget) hs_srcs |
| 802 |
GHC.setTargets targets |
| 803 |
ok_flag <- GHC.load LoadAllTargets |
| 804 |
|
| 805 |
when (failed ok_flag) (liftIO $ exitWith (ExitFailure 1)) |
| 806 |
return () |
| 807 |
|
| 808 |
|
| 809 |
-- --------------------------------------------------------------------------- |
| 810 |
-- --show-iface mode |
| 811 |
|
| 812 |
doShowIface :: DynFlags -> FilePath -> IO () |
| 813 |
doShowIface dflags file = do |
| 814 |
hsc_env <- newHscEnv dflags |
| 815 |
showIface hsc_env file |
| 816 |
|
| 817 |
-- --------------------------------------------------------------------------- |
| 818 |
-- Various banners and verbosity output. |
| 819 |
|
| 820 |
showBanner :: PostLoadMode -> DynFlags -> IO () |
| 821 |
showBanner _postLoadMode dflags = do |
| 822 |
let verb = verbosity dflags |
| 823 |
|
| 824 |
#if defined(GHCI) |
| 825 |
-- Show the GHCi banner |
| 826 |
when (isInteractiveMode _postLoadMode && verb >= 1) $ putStrLn ghciWelcomeMsg |
| 827 |
#endif |
| 828 |
|
| 829 |
-- Display details of the configuration in verbose mode |
| 830 |
when (verb >= 2) $ |
| 831 |
do hPutStr stderr "Glasgow Haskell Compiler, Version " |
| 832 |
hPutStr stderr cProjectVersion |
| 833 |
hPutStr stderr ", stage " |
| 834 |
hPutStr stderr cStage |
| 835 |
hPutStr stderr " booted by GHC version " |
| 836 |
hPutStrLn stderr cBooterVersion |
| 837 |
|
| 838 |
-- We print out a Read-friendly string, but a prettier one than the |
| 839 |
-- Show instance gives us |
| 840 |
showInfo :: DynFlags -> IO () |
| 841 |
showInfo dflags = do |
| 842 |
let sq x = " [" ++ x ++ "\n ]" |
| 843 |
putStrLn $ sq $ intercalate "\n ," $ map show $ compilerInfo dflags |
| 844 |
|
| 845 |
showSupportedExtensions :: IO () |
| 846 |
showSupportedExtensions = mapM_ putStrLn supportedLanguagesAndExtensions |
| 847 |
|
| 848 |
showVersion :: IO () |
| 849 |
showVersion = putStrLn $ concat [ "Clash, version " |
| 850 |
, Data.Version.showVersion Paths_clash_ghc.version |
| 851 |
, " (using clash-lib, version: " |
| 852 |
, Data.Version.showVersion clashLibVersion |
| 853 |
, ")" |
| 854 |
] |
| 855 |
|
| 856 |
showOptions :: Bool -> IORef ClashOpts -> IO () |
| 857 |
showOptions isInteractive = putStr . unlines . availableOptions |
| 858 |
where |
| 859 |
availableOptions opts = concat |
| 860 |
[ flagsForCompletion isInteractive |
| 861 |
, map ('-':) (getFlagNames mode_flags) |
| 862 |
, map ('-':) (getFlagNames (flagsClash opts)) |
| 863 |
] |
| 864 |
getFlagNames opts = map flagName opts |
| 865 |
|
| 866 |
showGhcUsage :: DynFlags -> IO () |
| 867 |
showGhcUsage = showUsage False |
| 868 |
|
| 869 |
showGhciUsage :: DynFlags -> IO () |
| 870 |
showGhciUsage = showUsage True |
| 871 |
|
| 872 |
showUsage :: Bool -> DynFlags -> IO () |
| 873 |
showUsage ghci dflags = do |
| 874 |
let usage_path = if ghci then ghciUsagePath dflags |
| 875 |
else ghcUsagePath dflags |
| 876 |
usage <- readFile usage_path |
| 877 |
dump usage |
| 878 |
where |
| 879 |
dump "" = return () |
| 880 |
dump ('$':'$':s) = putStr progName >> dump s |
| 881 |
dump (c:s) = putChar c >> dump s |
| 882 |
|
| 883 |
dumpFinalStats :: DynFlags -> IO () |
| 884 |
dumpFinalStats dflags = |
| 885 |
when (gopt Opt_D_faststring_stats dflags) $ dumpFastStringStats dflags |
| 886 |
|
| 887 |
dumpFastStringStats :: DynFlags -> IO () |
| 888 |
dumpFastStringStats dflags = do |
| 889 |
segments <- getFastStringTable |
| 890 |
let buckets = concat segments |
| 891 |
bucketsPerSegment = map length segments |
| 892 |
entriesPerBucket = map length buckets |
| 893 |
entries = sum entriesPerBucket |
| 894 |
hasZ = sum $ map (length . filter hasZEncoding) buckets |
| 895 |
msg = text "FastString stats:" $$ nest 4 (vcat |
| 896 |
[ text "segments: " <+> int (length segments) |
| 897 |
, text "buckets: " <+> int (sum bucketsPerSegment) |
| 898 |
, text "entries: " <+> int entries |
| 899 |
, text "largest segment: " <+> int (maximum bucketsPerSegment) |
| 900 |
, text "smallest segment: " <+> int (minimum bucketsPerSegment) |
| 901 |
, text "longest bucket: " <+> int (maximum entriesPerBucket) |
| 902 |
, text "has z-encoding: " <+> (hasZ `pcntOf` entries) |
| 903 |
]) |
| 904 |
-- we usually get more "has z-encoding" than "z-encoded", because |
| 905 |
-- when we z-encode a string it might hash to the exact same string, |
| 906 |
-- which is not counted as "z-encoded". Only strings whose |
| 907 |
-- Z-encoding is different from the original string are counted in |
| 908 |
-- the "z-encoded" total. |
| 909 |
putMsg dflags msg |
| 910 |
where |
| 911 |
x `pcntOf` y = int ((x * 100) `quot` y) Outputable.<> char '%' |
| 912 |
|
| 913 |
showPackages, dumpPackages, dumpPackagesSimple :: DynFlags -> IO () |
| 914 |
showPackages dflags = putStrLn (showSDoc dflags (pprPackages dflags)) |
| 915 |
dumpPackages dflags = putMsg dflags (pprPackages dflags) |
| 916 |
dumpPackagesSimple dflags = putMsg dflags (pprPackagesSimple dflags) |
| 917 |
|
| 918 |
-- ----------------------------------------------------------------------------- |
| 919 |
-- Frontend plugin support |
| 920 |
|
| 921 |
doFrontend :: ModuleName -> [(String, Maybe Phase)] -> Ghc () |
| 922 |
#if !defined(GHCI) |
| 923 |
doFrontend modname _ = pluginError [modname] |
| 924 |
#else |
| 925 |
doFrontend modname srcs = do |
| 926 |
hsc_env <- getSession |
| 927 |
frontend_plugin <- liftIO $ loadFrontendPlugin hsc_env modname |
| 928 |
frontend frontend_plugin |
| 929 |
(reverse $ frontendPluginOpts (hsc_dflags hsc_env)) srcs |
| 930 |
#endif |
| 931 |
|
| 932 |
-- ----------------------------------------------------------------------------- |
| 933 |
-- ABI hash support |
| 934 |
|
| 935 |
{- |
| 936 |
ghc --abi-hash Data.Foo System.Bar |
| 937 |
|
| 938 |
Generates a combined hash of the ABI for modules Data.Foo and |
| 939 |
System.Bar. The modules must already be compiled, and appropriate -i |
| 940 |
options may be necessary in order to find the .hi files. |
| 941 |
|
| 942 |
This is used by Cabal for generating the ComponentId for a |
| 943 |
package. The ComponentId must change when the visible ABI of |
| 944 |
the package chagnes, so during registration Cabal calls ghc --abi-hash |
| 945 |
to get a hash of the package's ABI. |
| 946 |
-} |
| 947 |
|
| 948 |
-- | Print ABI hash of input modules. |
| 949 |
-- |
| 950 |
-- The resulting hash is the MD5 of the GHC version used (Trac #5328, |
| 951 |
-- see 'hiVersion') and of the existing ABI hash from each module (see |
| 952 |
-- 'mi_mod_hash'). |
| 953 |
abiHash :: [String] -- ^ List of module names |
| 954 |
-> Ghc () |
| 955 |
abiHash strs = do |
| 956 |
hsc_env <- getSession |
| 957 |
let dflags = hsc_dflags hsc_env |
| 958 |
|
| 959 |
liftIO $ do |
| 960 |
|
| 961 |
let find_it str = do |
| 962 |
let modname = mkModuleName str |
| 963 |
r <- findImportedModule hsc_env modname Nothing |
| 964 |
case r of |
| 965 |
Found _ m -> return m |
| 966 |
_error -> throwGhcException $ CmdLineError $ showSDoc dflags $ |
| 967 |
cannotFindModule dflags modname r |
| 968 |
|
| 969 |
mods <- mapM find_it strs |
| 970 |
|
| 971 |
let get_iface modl = loadUserInterface False (text "abiHash") modl |
| 972 |
ifaces <- initIfaceCheck (text "abiHash") hsc_env $ mapM get_iface mods |
| 973 |
|
| 974 |
bh <- openBinMem (3*1024) -- just less than a block |
| 975 |
put_ bh hiVersion |
| 976 |
-- package hashes change when the compiler version changes (for now) |
| 977 |
-- see #5328 |
| 978 |
mapM_ (put_ bh . mi_mod_hash) ifaces |
| 979 |
f <- fingerprintBinMem bh |
| 980 |
|
| 981 |
putStrLn (showPpr dflags f) |
| 982 |
|
| 983 |
----------------------------------------------------------------------------- |
| 984 |
-- HDL Generation |
| 985 |
|
| 986 |
makeHDL' |
| 987 |
:: forall backend |
| 988 |
. Backend backend |
| 989 |
=> Proxy backend |
| 990 |
-> Ghc () |
| 991 |
-> IORef ClashOpts |
| 992 |
-> [(String,Maybe Phase)] |
| 993 |
-> Ghc () |
| 994 |
makeHDL' _ _ _ [] = throwGhcException (CmdLineError "No input files") |
| 995 |
makeHDL' proxy startAction r srcs = makeHDL proxy startAction r $ fmap fst srcs |
| 996 |
|
| 997 |
makeVHDL :: Ghc () -> IORef ClashOpts -> [(String, Maybe Phase)] -> Ghc () |
| 998 |
makeVHDL = makeHDL' (Proxy @VHDLState) |
| 999 |
|
| 1000 |
makeVerilog :: Ghc () -> IORef ClashOpts -> [(String, Maybe Phase)] -> Ghc () |
| 1001 |
makeVerilog = makeHDL' (Proxy @VerilogState) |
| 1002 |
|
| 1003 |
makeSystemVerilog :: Ghc () -> IORef ClashOpts -> [(String, Maybe Phase)] -> Ghc () |
| 1004 |
makeSystemVerilog = makeHDL' (Proxy @SystemVerilogState) |
| 1005 |
|
| 1006 |
-- ----------------------------------------------------------------------------- |
| 1007 |
-- Util |
| 1008 |
|
| 1009 |
unknownFlagsErr :: [String] -> a |
| 1010 |
unknownFlagsErr fs = throwGhcException $ UsageError $ concatMap oneError fs |
| 1011 |
where |
| 1012 |
oneError f = |
| 1013 |
"unrecognised flag: " ++ f ++ "\n" ++ |
| 1014 |
(case match f (nubSort allNonDeprecatedFlags) of |
| 1015 |
[] -> "" |
| 1016 |
suggs -> "did you mean one of:\n" ++ unlines (map (" " ++) suggs)) |
| 1017 |
-- fixes #11789 |
| 1018 |
-- If the flag contains '=', |
| 1019 |
-- this uses both the whole and the left side of '=' for comparing. |
| 1020 |
match f allFlags |
| 1021 |
| elem '=' f = |
| 1022 |
let (flagsWithEq, flagsWithoutEq) = partition (elem '=') allFlags |
| 1023 |
fName = takeWhile (/= '=') f |
| 1024 |
in (fuzzyMatch f flagsWithEq) ++ (fuzzyMatch fName flagsWithoutEq) |
| 1025 |
| otherwise = fuzzyMatch f allFlags |
| 1026 |
|
| 1027 |
{- Note [-Bsymbolic and hooks] |
| 1028 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 1029 |
-Bsymbolic is a flag that prevents the binding of references to global |
| 1030 |
symbols to symbols outside the shared library being compiled (see `man |
| 1031 |
ld`). When dynamically linking, we don't use -Bsymbolic on the RTS |
| 1032 |
package: that is because we want hooks to be overridden by the user, |
| 1033 |
we don't want to constrain them to the RTS package. |
| 1034 |
|
| 1035 |
Unfortunately this seems to have broken somehow on OS X: as a result, |
| 1036 |
defaultHooks (in hschooks.c) is not called, which does not initialize |
| 1037 |
the GC stats. As a result, this breaks things like `:set +s` in GHCi |
| 1038 |
(#8754). As a hacky workaround, we instead call 'defaultHooks' |
| 1039 |
directly to initalize the flags in the RTS. |
| 1040 |
|
| 1041 |
A byproduct of this, I believe, is that hooks are likely broken on OS |
| 1042 |
X when dynamically linking. But this probably doesn't affect most |
| 1043 |
people since we're linking GHC dynamically, but most things themselves |
| 1044 |
link statically. |
| 1045 |
-} |
| 1046 |
|
| 1047 |
-- If GHC_LOADED_INTO_GHCI is not set when GHC is loaded into GHCi, then |
| 1048 |
-- running it causes an error like this: |
| 1049 |
-- |
| 1050 |
-- Loading temp shared object failed: |
| 1051 |
-- /tmp/ghc13836_0/libghc_1872.so: undefined symbol: initGCStatistics |
| 1052 |
-- |
| 1053 |
-- Skipping the foreign call fixes this problem, and the outer GHCi |
| 1054 |
-- should have already made this call anyway. |
| 1055 |
#if defined(GHC_LOADED_INTO_GHCI) |
| 1056 |
initGCStatistics :: IO () |
| 1057 |
initGCStatistics = return () |
| 1058 |
#else |
| 1059 |
foreign import ccall safe "initGCStatistics" |
| 1060 |
initGCStatistics :: IO () |
| 1061 |
#endif |