Trim unused imports detected by new unused-import code
[ghc-hetmet.git] / compiler / main / DriverPipeline.hs
index 5253a2a..1849c6b 100644 (file)
@@ -48,12 +48,12 @@ import Maybes               ( expectJust )
 import ParserCoreUtils ( getCoreModuleName )
 import SrcLoc
 import FastString
-import MonadUtils
+-- import MonadUtils
 
-import Data.Either
+-- import Data.Either
 import Exception
-import Data.IORef      ( readIORef, writeIORef, IORef )
-import GHC.Exts                ( Int(..) )
+import Data.IORef      ( readIORef )
+-- import GHC.Exts             ( Int(..) )
 import System.Directory
 import System.FilePath
 import System.IO
@@ -187,7 +187,7 @@ compile' (nothingCompiler, interactiveCompiler, batchCompiler)
                             -> return ([], ms_hs_date summary)
                           -- We're in --make mode: finish the compilation pipeline.
                           _other
-                            -> do runPipeline StopLn hsc_env' (output_fn,Nothing)
+                            -> do _ <- runPipeline StopLn hsc_env' (output_fn,Nothing)
                                               (Just basename)
                                               Persistent
                                               (Just location)
@@ -260,12 +260,11 @@ compile' (nothingCompiler, interactiveCompiler, batchCompiler)
 compileStub :: GhcMonad m => HscEnv -> Module -> ModLocation
             -> m FilePath
 compileStub hsc_env mod location = do
-       let (o_base, o_ext) = splitExtension (ml_obj_file location)
-           stub_o = (o_base ++ "_stub") <.> o_ext
-
        -- compile the _stub.c file w/ gcc
-       let (stub_c,_,_) = mkStubPaths (hsc_dflags hsc_env) (moduleName mod) location
-       runPipeline StopLn hsc_env (stub_c,Nothing)  Nothing
+       let (stub_c,_,stub_o) = mkStubPaths (hsc_dflags hsc_env) 
+                                   (moduleName mod) location
+
+       _ <- runPipeline StopLn hsc_env (stub_c,Nothing)  Nothing
                (SpecificFile stub_o) Nothing{-no ModLocation-}
 
        return stub_o
@@ -297,6 +296,26 @@ link NoLink _ _ _
    = return Succeeded
 
 link LinkBinary dflags batch_attempt_linking hpt
+   = link' dflags batch_attempt_linking hpt
+
+link LinkDynLib dflags batch_attempt_linking hpt
+   = link' dflags batch_attempt_linking hpt
+
+#ifndef GHCI
+-- warning suppression
+link other _ _ _ = panicBadLink other
+#endif
+
+panicBadLink :: GhcLink -> a
+panicBadLink other = panic ("link: GHC not built to link this way: " ++
+                            show other)
+
+link' :: DynFlags                -- dynamic flags
+      -> Bool                    -- attempt linking in batch mode?
+      -> HomePackageTable        -- what to link
+      -> IO SuccessFlag
+
+link' dflags batch_attempt_linking hpt
    | batch_attempt_linking
    = do
         let
@@ -348,13 +367,6 @@ link LinkBinary dflags batch_attempt_linking hpt
                                 text "   Main.main not exported; not linking.")
         return Succeeded
 
--- warning suppression
-link other _ _ _ = panicBadLink other
-
-panicBadLink :: GhcLink -> a
-panicBadLink other = panic ("link: GHC not built to link this way: " ++
-                            show other)
-
 
 linkingNeeded :: DynFlags -> [Linkable] -> [PackageId] -> IO Bool
 linkingNeeded dflags linkables pkg_deps = do
@@ -479,7 +491,7 @@ data PipelineOutput
 -- at which stage to stop.
 --
 -- The DynFlags can be modified by phases in the pipeline (eg. by
--- GHC_OPTIONS pragmas), and the changes affect later phases in the
+-- OPTIONS_GHC pragmas), and the changes affect later phases in the
 -- pipeline.
 runPipeline
   :: GhcMonad m =>
@@ -993,6 +1005,13 @@ runPhase cc_phase _stop hsc_env _basename _suff input_fn get_output_fn maybe_loc
         -- This is a temporary hack.
                        ++ ["-mcpu=v9"]
 #endif
+#if defined(darwin_TARGET_OS) && defined(i386_TARGET_ARCH)
+                          -- By default, gcc on OS X will generate SSE
+                          -- instructions, which need things 16-byte aligned,
+                          -- but we don't 16-byte align things. Thus drop
+                          -- back to generic i686 compatibility. Trac #2983.
+                       ++ ["-march=i686"]
+#endif
                       ++ (if hcc && mangle
                             then md_regd_c_flags
                             else [])
@@ -1068,13 +1087,13 @@ runPhase SplitMangle _stop hsc_env _basename _suff input_fn _get_output_fn maybe
        -- Save the number of split files for future references
        s <- readFile n_files_fn
        let n_files = read s :: Int
-       writeIORef v_Split_info (split_s_prefix, n_files)
+           dflags' = dflags { splitInfo = Just (split_s_prefix, n_files) }
 
        -- Remember to delete all these files
-       addFilesToClean [ split_s_prefix ++ "__" ++ show n ++ ".s"
-                       | n <- [1..n_files]]
+       addFilesToClean dflags' [ split_s_prefix ++ "__" ++ show n ++ ".s"
+                               | n <- [1..n_files]]
 
-       return (SplitAs, dflags, maybe_loc, "**splitmangle**")
+       return (SplitAs, dflags', maybe_loc, "**splitmangle**")
          -- we don't use the filename
 
 -----------------------------------------------------------------------------
@@ -1092,6 +1111,7 @@ runPhase As _stop hsc_env _basename _suff input_fn get_output_fn maybe_loc
        -- might be a hierarchical module.
        createDirectoryHierarchy (takeDirectory output_fn)
 
+       let (md_c_flags, _) = machdepCCOpts dflags
        SysTools.runAs dflags   
                       (map SysTools.Option as_opts
                       ++ [ SysTools.Option ("-I" ++ p) | p <- cmdline_include_paths ]
@@ -1109,7 +1129,8 @@ runPhase As _stop hsc_env _basename _suff input_fn get_output_fn maybe_loc
                          , SysTools.FileOption "" input_fn
                          , SysTools.Option "-o"
                          , SysTools.FileOption "" output_fn
-                         ])
+                         ]
+                      ++ map SysTools.Option md_c_flags)
 
        return (StopLn, dflags, maybe_loc, output_fn)
 
@@ -1132,20 +1153,34 @@ runPhase SplitAs _stop hsc_env _basename _suff _input_fn get_output_fn maybe_loc
 
         let as_opts = getOpts dflags opt_a
 
-        (split_s_prefix, n) <- readIORef v_Split_info
+        let (split_s_prefix, n) = case splitInfo dflags of
+                                  Nothing -> panic "No split info"
+                                  Just x -> x
 
         let split_s   n = split_s_prefix ++ "__" ++ show n <.> "s"
             split_obj n = split_odir </>
                           takeFileName base_o ++ "__" ++ show n <.> osuf
 
+        let (md_c_flags, _) = machdepCCOpts dflags
         let assemble_file n
               = SysTools.runAs dflags
                          (map SysTools.Option as_opts ++
+#ifdef sparc_TARGET_ARCH
+        -- We only support SparcV9 and better because V8 lacks an atomic CAS
+        -- instruction so we have to make sure that the assembler accepts the
+        -- instruction set. Note that the user can still override this
+        -- (e.g., -mcpu=ultrasparc). GCC picks the "best" -mcpu flag
+        -- regardless of the ordering.
+        --
+        -- This is a temporary hack.
+                          [ SysTools.Option "-mcpu=v9" ] ++
+#endif
                           [ SysTools.Option "-c"
                           , SysTools.Option "-o"
                           , SysTools.FileOption "" (split_obj n)
                           , SysTools.FileOption "" (split_s n)
-                          ])
+                          ]
+                       ++ map SysTools.Option md_c_flags)
 
         mapM_ assemble_file [1..n]
 
@@ -1156,7 +1191,9 @@ runPhase SplitAs _stop hsc_env _basename _suff _input_fn get_output_fn maybe_loc
                             SysTools.Option "-Wl,-r",
                             SysTools.Option ld_x_flag,
                             SysTools.Option "-o",
-                            SysTools.FileOption "" output_fn ] ++ args)
+                            SysTools.FileOption "" output_fn ]
+                         ++ map SysTools.Option md_c_flags
+                         ++ args)
             ld_x_flag | null cLD_X = ""
                       | otherwise  = "-Wl,-x"
 
@@ -1197,7 +1234,7 @@ runPhase_MoveBinary dflags input_fn dep_packages
            pvm_executable_base = "=" ++ input_fn
            pvm_executable = pvm_root ++ "/bin/" ++ pvm_arch ++ "/" ++ pvm_executable_base
         -- nuke old binary; maybe use configur'ed names for cp and rm?
-        tryIO (removeFile pvm_executable)
+        _ <- tryIO (removeFile pvm_executable)
         -- move the newly created binary into PVM land
         copy dflags "copying PVM executable" input_fn pvm_executable
         -- generate a wrapper script for running a parallel prg under PVM
@@ -1342,6 +1379,13 @@ linkBinary dflags o_files dep_packages = do
     let lib_paths = libraryPaths dflags
     let lib_path_opts = map ("-L"++) lib_paths
 
+    -- The C "main" function is not in the rts but in a separate static
+    -- library libHSrtsmain.a that sits next to the rts lib files. Assuming
+    -- we're using a Haskell main function then we need to link it in.
+    let no_hs_main = dopt Opt_NoHsMain dflags
+    let main_lib | no_hs_main = []
+                 | otherwise  = [ "-lHSrtsmain" ]
+
     pkg_link_opts <- getPackageLinkOpts dflags dep_packages
 
 #ifdef darwin_TARGET_OS
@@ -1358,12 +1402,6 @@ linkBinary dflags o_files dep_packages = do
         framework_opts = concat [ ["-framework", fw] | fw <- reverse frameworks ]
         -- reverse because they're added in reverse order from the cmd line
 #endif
-#ifdef mingw32_TARGET_OS
-    let dynMain = if not opt_Static then
-                     (head (libraryDirs (getPackageDetails (pkgState dflags) rtsPackageId))) ++ "/Main.dyn_o"
-                 else
-                     ""
-#endif
        -- probably _stub.o files
     extra_ld_inputs <- readIORef v_Ld_inputs
 
@@ -1405,9 +1443,6 @@ linkBinary dflags o_files dep_packages = do
                      ++ map SysTools.Option (
                         md_c_flags
                      ++ o_files
-#ifdef mingw32_TARGET_OS
-                     ++ [dynMain]
-#endif
                      ++ extra_ld_inputs
                      ++ lib_path_opts
                      ++ extra_ld_opts
@@ -1417,6 +1452,7 @@ linkBinary dflags o_files dep_packages = do
                      ++ framework_opts
 #endif
                      ++ pkg_lib_path_opts
+                      ++ main_lib
                      ++ pkg_link_opts
 #ifdef darwin_TARGET_OS
                      ++ pkg_framework_path_opts
@@ -1501,6 +1537,8 @@ maybeCreateManifest dflags exe_filename = do
         -- no FileOptions here: windres doesn't like seeing
         -- backslashes, apparently
 
+  removeFile manifest_filename
+
   return [rc_obj_filename]
 #endif
 
@@ -1523,7 +1561,13 @@ linkDynLib dflags o_files dep_packages = do
     let pkgs_no_rts = pkgs
 #endif
     let pkg_lib_paths = collectLibraryPaths pkgs_no_rts
-    let pkg_lib_path_opts = map ("-L"++) pkg_lib_paths
+    let pkg_lib_path_opts = concatMap get_pkg_lib_path_opts pkg_lib_paths
+#ifdef linux_TARGET_OS
+        get_pkg_lib_path_opts l | (dynLibLoader dflags)==SystemDependent && not opt_Static = ["-L" ++ l, "-Wl,-rpath", "-Wl," ++ l]
+                                | otherwise = ["-L" ++ l]
+#else
+        get_pkg_lib_path_opts l = ["-L" ++ l]
+#endif
 
     let lib_paths = libraryPaths dflags
     let lib_path_opts = map ("-L"++) lib_paths
@@ -1623,6 +1667,7 @@ linkDynLib dflags o_files dep_packages = do
            md_c_flags
         ++ o_files
         ++ [ "-shared", "-Wl,-Bsymbolic" ] -- we need symbolic linking to resolve non-PIC intra-package-relocations
+         ++ [ "-Wl,-soname," ++ takeFileName output_fn ] -- set the library soname
         ++ extra_ld_inputs
         ++ lib_path_opts
         ++ extra_ld_opts
@@ -1726,5 +1771,3 @@ hscMaybeAdjustTarget dflags stop _ current_hsc_lang
                -- otherwise, stick to the plan
                 | otherwise = current_hsc_lang
 
-GLOBAL_VAR(v_Split_info, ("",0), (String,Int))
-       -- The split prefix and number of files