-fno-code shouldn't be a mode.
[ghc-hetmet.git] / ghc / compiler / main / DynFlags.hs
index 62d269d..f0c95bc 100644 (file)
@@ -37,9 +37,11 @@ module DynFlags (
        getOpts,                        -- (DynFlags -> [a]) -> IO [a]
        getVerbFlag,
        updOptLevel,
+       setTmpDir,
        
        -- parsing DynFlags
        parseDynamicFlags,
+        allFlags,
 
        -- misc stuff
        machdepCCOpts, picCCOpts,
@@ -47,6 +49,8 @@ module DynFlags (
 
 #include "HsVersions.h"
 
+import Module          ( Module, mkModule )
+import PrelNames       ( mAIN )
 import StaticFlags     ( opt_Static, opt_PIC, 
                          WayName(..), v_Ways, v_Build_tag, v_RTS_Build_tag )
 import {-# SOURCE #-} Packages (PackageState)
@@ -54,13 +58,20 @@ import DriverPhases ( Phase(..), phaseInputExt )
 import Config
 import CmdLineParser
 import Panic           ( panic, GhcException(..) )
-import Util            ( notNull, splitLongestPrefix, split )
+import Util            ( notNull, splitLongestPrefix, split, normalisePath )
+import SrcLoc           ( SrcSpan )
 
 import DATA_IOREF      ( readIORef )
 import EXCEPTION       ( throwDyn )
 import Monad           ( when )
+#ifdef mingw32_TARGET_OS
+import Data.List       ( isPrefixOf )
+#endif
 import Maybe           ( fromJust )
 import Char            ( isDigit, isUpper )
+import Outputable
+import System.IO        ( hPutStrLn, stderr )
+import {-# SOURCE #-} ErrUtils ( Severity(..), Message, mkLocMessage )
 
 -- -----------------------------------------------------------------------------
 -- DynFlags
@@ -105,6 +116,7 @@ data DynFlag
    | Opt_D_dump_hi
    | Opt_D_dump_hi_diffs
    | Opt_D_dump_minimal_imports
+   | Opt_D_faststring_stats
    | Opt_DoCoreLinting
    | Opt_DoStgLinting
    | Opt_DoCmmLinting
@@ -142,6 +154,7 @@ data DynFlag
    | Opt_Generics
    | Opt_ImplicitPrelude 
    | Opt_ScopedTypeVariables
+   | Opt_BangPatterns
 
    -- optimisation opts
    | Opt_Strictness
@@ -161,11 +174,13 @@ data DynFlag
    | Opt_RecompChecking
    | Opt_DryRun
    | Opt_DoAsmMangling
+   | Opt_WriteIface
    | Opt_ExcessPrecision
    | Opt_ReadUserPackageConf
    | Opt_NoHsMain
    | Opt_SplitObjs
    | Opt_StgStats
+   | Opt_HideAllPackages
 
    -- keeping stuff
    | Opt_KeepHiDiffs
@@ -175,7 +190,7 @@ data DynFlag
    | Opt_KeepTmpFiles
 
    deriving (Eq)
-
 data DynFlags = DynFlags {
   ghcMode              :: GhcMode,
   ghcLink              :: GhcLink,
@@ -183,8 +198,6 @@ data DynFlags = DynFlags {
   stgToDo              :: Maybe [StgToDo],  -- similarly
   hscTarget                    :: HscTarget,
   hscOutName           :: String,      -- name of the output file
-  hscStubHOutName      :: String,      -- name of the .stub_h output file
-  hscStubCOutName      :: String,      -- name of the .stub_c output file
   extCoreName          :: String,      -- name of the .core output file
   verbosity            :: Int,         -- verbosity level
   optLevel             :: Int,         -- optimisation level
@@ -193,7 +206,7 @@ data DynFlags = DynFlags {
   stolen_x86_regs      :: Int,         
   cmdlineHcIncludes    :: [String],    -- -#includes
   importPaths          :: [FilePath],
-  mainModIs            :: Maybe String,
+  mainModIs            :: Module,
   mainFunIs            :: Maybe String,
 
   -- ways
@@ -202,18 +215,22 @@ data DynFlags = DynFlags {
   rtsBuildTag          :: String,      -- the RTS "way"
   
   -- paths etc.
-  outputDir            :: Maybe String,
-  outputFile           :: Maybe String,
-  outputHi             :: Maybe String,
+  objectDir            :: Maybe String,
+  hiDir                        :: Maybe String,
+  stubDir              :: Maybe String,
+
   objectSuf            :: String,
   hcSuf                        :: String,
-  hiDir                        :: Maybe String,
   hiSuf                        :: String,
+
+  outputFile           :: Maybe String,
+  outputHi             :: Maybe String,
+
   includePaths         :: [String],
   libraryPaths         :: [String],
   frameworkPaths       :: [String],    -- used on darwin only
   cmdlineFrameworks    :: [String],    -- ditto
-  tmpDir               :: String,
+  tmpDir               :: String,      -- no trailing '/'
   
   -- options for particular phases
   opt_L                        :: [String],
@@ -237,7 +254,7 @@ data DynFlags = DynFlags {
   pgm_l                        :: (String,[Option]),
   pgm_dll              :: (String,[Option]),
 
-  -- ** Package flags
+  --  ** Package flags
   extraPkgConfs                :: [FilePath],
        -- The -package-conf flags given on the command line, in the order
        -- they appeared.
@@ -245,11 +262,14 @@ data DynFlags = DynFlags {
   packageFlags         :: [PackageFlag],
        -- The -package and -hide-package flags from the command-line
 
-  -- ** Package state
+  --  ** Package state
   pkgState             :: PackageState,
 
   -- hsc dynamic flags
-  flags                :: [DynFlag]
+  flags                :: [DynFlag],
+  
+  -- message output
+  log_action            :: Severity -> SrcSpan -> PprStyle -> Message -> IO ()
  }
 
 data HscTarget
@@ -290,9 +310,7 @@ data PackageFlag
   | IgnorePackage  String
 
 defaultHscTarget
-#if defined(i386_TARGET_ARCH) || defined(sparc_TARGET_ARCH) || defined(powerpc_TARGET_ARCH)
   | cGhcWithNativeCodeGen == "YES"     =  HscAsm
-#endif
   | otherwise                          =  HscC
 
 initDynFlags dflags = do
@@ -314,8 +332,6 @@ defaultDynFlags =
        stgToDo                 = Nothing, 
        hscTarget               = defaultHscTarget, 
        hscOutName              = "", 
-       hscStubHOutName         = "",
-       hscStubCOutName         = "",
        extCoreName             = "",
        verbosity               = 0, 
        optLevel                = 0,
@@ -324,25 +340,28 @@ defaultDynFlags =
        stolen_x86_regs         = 4,
        cmdlineHcIncludes       = [],
        importPaths             = ["."],
-       mainModIs               = Nothing,
+       mainModIs               = mAIN,
        mainFunIs               = Nothing,
        
        wayNames                = panic "ways",
        buildTag                = panic "buildTag",
        rtsBuildTag             = panic "rtsBuildTag",
 
-       outputDir               = Nothing,
-       outputFile              = Nothing,
-       outputHi                = Nothing,
+       objectDir               = Nothing,
+       hiDir                   = Nothing,
+       stubDir                 = Nothing,
+
        objectSuf               = phaseInputExt StopLn,
        hcSuf                   = phaseInputExt HCc,
-       hiDir                   = Nothing,
        hiSuf                   = "hi",
+
+       outputFile              = Nothing,
+       outputHi                = Nothing,
        includePaths            = [],
        libraryPaths            = [],
        frameworkPaths          = [],
        cmdlineFrameworks       = [],
-       tmpDir                  = [],
+       tmpDir                  = cDEFAULT_TMPDIR,
        
        opt_L                   = [],
        opt_P                   = [],
@@ -387,12 +406,19 @@ defaultDynFlags =
                        -- a good thing anyway, but it seems fragile.
     
            Opt_DoAsmMangling,
+            Opt_WriteIface,
     
            -- and the default no-optimisation options:
            Opt_IgnoreInterfacePragmas,
            Opt_OmitInterfacePragmas
     
-               ] ++ standardWarnings
+               ] ++ standardWarnings,
+               
+        log_action = \severity srcSpan style msg -> 
+                        case severity of
+                          SevInfo  -> hPutStrLn stderr (show (msg style))
+                          SevFatal -> hPutStrLn stderr (show (msg style))
+                          _        -> hPutStrLn stderr ('\n':show ((mkLocMessage srcSpan msg) style))
       }
 
 {- 
@@ -424,14 +450,16 @@ getVerbFlag dflags
   | verbosity dflags >= 3  = "-v" 
   | otherwise =  ""
 
-setOutputDir  f d = d{ outputDir  = f}
-setOutputFile f d = d{ outputFile = f}
-setOutputHi   f d = d{ outputHi   = f}
+setObjectDir  f d = d{ objectDir  = f}
+setHiDir      f d = d{ hiDir      = f}
+setStubDir    f d = d{ stubDir    = f}
+
 setObjectSuf  f d = d{ objectSuf  = f}
-setHcSuf      f d = d{ hcSuf      = f}
 setHiSuf      f d = d{ hiSuf      = f}
-setHiDir      f d = d{ hiDir      = f}
-setTmpDir     f d = d{ tmpDir     = f}
+setHcSuf      f d = d{ hcSuf      = f}
+
+setOutputFile f d = d{ outputFile = f}
+setOutputHi   f d = d{ outputHi   = f}
 
 -- XXX HACK: Prelude> words "'does not' work" ===> ["'does","not'","work"]
 -- Config.hs should really use Option.
@@ -600,7 +628,6 @@ getCoreToDo dflags
            MaxSimplifierIterations max_iter
        ]
       ]
-
      else {- opt_level >= 1 -} [ 
 
        -- initial simplify: mk specialiser happy: minimum effort please
@@ -740,6 +767,15 @@ getStgToDo dflags
 -- -----------------------------------------------------------------------------
 -- DynFlags parser
 
+allFlags :: [String]
+allFlags = map ('-':) $
+           [ name | (name, optkind) <- dynamic_flags, ok optkind ] ++
+           map ("fno-"++) flags ++
+           map ("f"++) flags
+    where ok (PrefixPred _ _) = False
+          ok _ = True
+          flags = map fst fFlags
+
 dynamic_flags :: [(String, OptKind DynP)]
 dynamic_flags = [
      ( "n"              , NoArg  (setDynFlag Opt_DryRun) )
@@ -769,6 +805,10 @@ dynamic_flags = [
   ,  ( "optdll"                , HasArg (upd . addOptdll) )  
   ,  ( "optdep"                , HasArg (upd . addOptdep) )
 
+  ,  ( "split-objs"    , NoArg (if can_split
+                                   then setDynFlag Opt_SplitObjs
+                                   else return ()) )
+
        -------- Linking ----------------------------------------------------
   ,  ( "c"             , NoArg (upd $ \d -> d{ ghcLink=NoLink } ))
   ,  ( "no-link"       , NoArg (upd $ \d -> d{ ghcLink=NoLink } )) -- Dep.
@@ -785,7 +825,7 @@ dynamic_flags = [
   ,  ( "framework"     , HasArg (upd . addCmdlineFramework) )
 
        ------- Output Redirection ------------------------------------------
-  ,  ( "odir"          , HasArg (upd . setOutputDir  . Just))
+  ,  ( "odir"          , HasArg (upd . setObjectDir  . Just))
   ,  ( "o"             , SepArg (upd . setOutputFile . Just))
   ,  ( "ohi"           , HasArg (upd . setOutputHi   . Just ))
   ,  ( "osuf"          , HasArg (upd . setObjectSuf))
@@ -793,10 +833,13 @@ dynamic_flags = [
   ,  ( "hisuf"         , HasArg (upd . setHiSuf))
   ,  ( "hidir"         , HasArg (upd . setHiDir . Just))
   ,  ( "tmpdir"                , HasArg (upd . setTmpDir))
+  ,  ( "stubdir"       , HasArg (upd . setStubDir . Just))
 
        ------- Keeping temporary files -------------------------------------
-  ,  ( "keep-hc-file"   , AnySuffix (\_ -> setDynFlag Opt_KeepHcFiles))
-  ,  ( "keep-s-file"    , AnySuffix (\_ -> setDynFlag Opt_KeepSFiles))
+  ,  ( "keep-hc-file"   , AnySuffix (\_ -> do setDynFlag Opt_KeepHcFiles
+                                              setTarget HscC))
+  ,  ( "keep-s-file"    , AnySuffix (\_ -> do setDynFlag Opt_KeepSFiles
+                                              setTarget HscAsm))
   ,  ( "keep-raw-s-file", AnySuffix (\_ -> setDynFlag Opt_KeepRawSFiles))
   ,  ( "keep-tmp-files" , AnySuffix (\_ -> setDynFlag Opt_KeepTmpFiles))
 
@@ -814,6 +857,7 @@ dynamic_flags = [
   ,  ( "package-name"   , HasArg ignorePackage ) -- for compatibility
   ,  ( "package"        , HasArg exposePackage )
   ,  ( "hide-package"   , HasArg hidePackage )
+  ,  ( "hide-all-packages", NoArg (setDynFlag Opt_HideAllPackages) )
   ,  ( "ignore-package" , HasArg ignorePackage )
   ,  ( "syslib"         , HasArg exposePackage )  -- for compatibility
 
@@ -861,15 +905,16 @@ dynamic_flags = [
   ,  ( "dsource-stats",          setDumpFlag Opt_D_source_stats)
   ,  ( "dverbose-core2core",     setDumpFlag Opt_D_verbose_core2core)
   ,  ( "dverbose-stg2stg",       setDumpFlag Opt_D_verbose_stg2stg)
-  ,  ( "ddump-hi-diffs",         setDumpFlag Opt_D_dump_hi_diffs)
+  ,  ( "ddump-hi-diffs",         NoArg (setDynFlag Opt_D_dump_hi_diffs))
   ,  ( "ddump-hi",               setDumpFlag Opt_D_dump_hi)
-  ,  ( "ddump-minimal-imports",  setDumpFlag Opt_D_dump_minimal_imports)
+  ,  ( "ddump-minimal-imports",  NoArg (setDynFlag Opt_D_dump_minimal_imports))
   ,  ( "ddump-vect",            setDumpFlag Opt_D_dump_vect)
   ,  ( "dcore-lint",            NoArg (setDynFlag Opt_DoCoreLinting))
   ,  ( "dstg-lint",             NoArg (setDynFlag Opt_DoStgLinting))
   ,  ( "dcmm-lint",             NoArg (setDynFlag Opt_DoCmmLinting))
   ,  ( "dshow-passes",           NoArg (do unSetDynFlag Opt_RecompChecking
                                           setVerbosity "2") )
+  ,  ( "dfaststring-stats",     NoArg (setDynFlag Opt_D_faststring_stats))
 
        ------ Machine dependant (-m<blah>) stuff ---------------------------
 
@@ -900,6 +945,8 @@ dynamic_flags = [
 
         ------ Compiler flags -----------------------------------------------
 
+  ,  ( "fno-code",     NoArg (do setTarget HscNothing
+                                  unSetDynFlag Opt_WriteIface))
   ,  ( "fasm",         AnySuffix (\_ -> setTarget HscAsm) )
   ,  ( "fvia-c",       NoArg (setTarget HscC) )
   ,  ( "fvia-C",       NoArg (setTarget HscC) )
@@ -939,6 +986,7 @@ fFlags = [
   ( "th",                              Opt_TH ),
   ( "implicit-prelude",                Opt_ImplicitPrelude ),
   ( "scoped-type-variables",           Opt_ScopedTypeVariables ),
+  ( "bang-patterns",                   Opt_BangPatterns ),
   ( "monomorphism-restriction",                Opt_MonomorphismRestriction ),
   ( "implicit-params",                 Opt_ImplicitParams ),
   ( "allow-overlapping-instances",     Opt_AllowOverlappingInstances ),
@@ -956,7 +1004,8 @@ fFlags = [
   ( "case-merge",                      Opt_CaseMerge ),
   ( "unbox-strict-fields",             Opt_UnboxStrictFields ),
   ( "excess-precision",                        Opt_ExcessPrecision ),
-  ( "asm-mangling",                    Opt_DoAsmMangling )
+  ( "asm-mangling",                    Opt_DoAsmMangling ),
+  ( "write-iface",                      Opt_WriteIface )
   ]
 
 glasgowExtsFlags = [ 
@@ -964,7 +1013,8 @@ glasgowExtsFlags = [
   Opt_FFI, 
   Opt_TH, 
   Opt_ImplicitParams, 
-  Opt_ScopedTypeVariables ]
+  Opt_ScopedTypeVariables,
+  Opt_BangPatterns ]
 
 isFFlag f = f `elem` (map fst fFlags)
 getFFlag f = fromJust (lookup f fFlags)
@@ -1034,18 +1084,17 @@ setOptLevel n dflags
 
 setMainIs :: String -> DynP ()
 setMainIs arg
-  | not (null main_mod)                -- The arg looked like "Foo.baz"
+  | not (null main_fn)         -- The arg looked like "Foo.baz"
   = upd $ \d -> d{ mainFunIs = Just main_fn,
-                  mainModIs = Just main_mod }
+                  mainModIs = mkModule main_mod }
 
-  | isUpper (head main_fn)     -- The arg looked like "Foo"
-  = upd $ \d -> d{ mainModIs = Just main_fn }
+  | isUpper (head main_mod)    -- The arg looked like "Foo"
+  = upd $ \d -> d{ mainModIs = mkModule main_mod }
   
   | otherwise                  -- The arg looked like "baz"
-  = upd $ \d -> d{ mainFunIs = Just main_fn }
+  = upd $ \d -> d{ mainFunIs = Just main_mod }
   where
     (main_mod, main_fn) = splitLongestPrefix arg (== '.')
-  
 
 -----------------------------------------------------------------------------
 -- Paths & Libraries
@@ -1088,20 +1137,20 @@ splitPathList s = filter notNull (splitUp s)
      -- that this will cause too much breakage for users & ':' will
      -- work fine even with DOS paths, if you're not insisting on being silly.
      -- So, use either.
-    splitUp []         = []
-    splitUp (x:':':div:xs) 
-      | div `elem` dir_markers = do
-          let (p,rs) = findNextPath xs
-          in ((x:':':div:p): splitUp rs)
+    splitUp []             = []
+    splitUp (x:':':div:xs) | div `elem` dir_markers
+                          = ((x:':':div:p): splitUp rs)
+                          where
+                             (p,rs) = findNextPath xs
          -- we used to check for existence of the path here, but that
          -- required the IO monad to be threaded through the command-line
          -- parser which is quite inconvenient.  The 
-    splitUp xs = do
-      let (p,rs) = findNextPath xs
-      return (cons p (splitUp rs))
+    splitUp xs = cons p (splitUp rs)
+              where
+                (p,rs) = findNextPath xs
     
-    cons "" xs = xs
-    cons x  xs = x:xs
+                cons "" xs = xs
+                cons x  xs = x:xs
 
     -- will be called either when we've consumed nought or the
     -- "<Drive>:/" part of a DOS path, so splitting is just a Q of
@@ -1118,6 +1167,40 @@ splitPathList s = filter notNull (splitUp s)
     dir_markers = ['/', '\\']
 #endif
 
+-- -----------------------------------------------------------------------------
+-- tmpDir, where we store temporary files.
+
+setTmpDir :: FilePath -> DynFlags -> DynFlags
+setTmpDir dir dflags = dflags{ tmpDir = canonicalise dir }
+  where
+#if !defined(mingw32_HOST_OS)
+     canonicalise p = normalisePath p
+#else
+       -- Canonicalisation of temp path under win32 is a bit more
+       -- involved: (a) strip trailing slash, 
+       --           (b) normalise slashes
+       --           (c) just in case, if there is a prefix /cygdrive/x/, change to x:
+       -- 
+     canonicalise path = normalisePath (xltCygdrive (removeTrailingSlash path))
+
+        -- if we're operating under cygwin, and TMP/TEMP is of
+       -- the form "/cygdrive/drive/path", translate this to
+       -- "drive:/path" (as GHC isn't a cygwin app and doesn't
+       -- understand /cygdrive paths.)
+     xltCygdrive path
+      | "/cygdrive/" `isPrefixOf` path = 
+         case drop (length "/cygdrive/") path of
+           drive:xs@('/':_) -> drive:':':xs
+           _ -> path
+      | otherwise = path
+
+        -- strip the trailing backslash (awful, but we only do this once).
+     removeTrailingSlash path = 
+       case last path of
+         '/'  -> init path
+         '\\' -> init path
+         _    -> path
+#endif
 
 -----------------------------------------------------------------------------
 -- Via-C compilation stuff
@@ -1183,7 +1266,22 @@ machdepCCOpts dflags
        = ( [], ["-fomit-frame-pointer", "-G0"] )
 
 #elif x86_64_TARGET_ARCH
-       = ( [], ["-fomit-frame-pointer"] )
+       = ( [], ["-fomit-frame-pointer",
+                "-fno-asynchronous-unwind-tables",
+                       -- the unwind tables are unnecessary for HC code,
+                       -- and get in the way of -split-objs.  Another option
+                       -- would be to throw them away in the mangler, but this
+                       -- is easier.
+                "-fno-unit-at-a-time",
+                       -- unit-at-a-time doesn't do us any good, and screws
+                       -- up -split-objs by moving the split markers around.
+                       -- It's only turned on with -O2, but put it here just
+                       -- in case someone uses -optc-O2.
+                "-fno-builtin"
+                       -- calling builtins like strlen() using the FFI can
+                       -- cause gcc to run out of regs, so use the external
+                       -- version.
+               ] )
 
 #elif mips_TARGET_ARCH
        = ( ["-static"], [] )
@@ -1228,3 +1326,23 @@ picCCOpts dflags
     | otherwise
         = []
 #endif
+
+-- -----------------------------------------------------------------------------
+-- Splitting
+
+can_split :: Bool
+can_split =  
+#if    defined(i386_TARGET_ARCH)     \
+    || defined(x86_64_TARGET_ARCH)   \
+    || defined(alpha_TARGET_ARCH)    \
+    || defined(hppa_TARGET_ARCH)     \
+    || defined(m68k_TARGET_ARCH)     \
+    || defined(mips_TARGET_ARCH)     \
+    || defined(powerpc_TARGET_ARCH)  \
+    || defined(rs6000_TARGET_ARCH)   \
+    || defined(sparc_TARGET_ARCH) 
+   True
+#else
+   False
+#endif
+