-- | External ("system") linker
module GHC.Linker.External
  ( LinkerConfig(..)
  , runLink
  )
where

import GHC.Prelude
import GHC.Utils.TmpFs
import GHC.Utils.Logger
import GHC.Utils.Error
import GHC.Utils.CliOption
import GHC.SysTools.Process
import GHC.Linker.Config

-- | Run the external linker
runLink :: Logger -> TmpFs -> LinkerConfig -> [Option] -> IO ()
runLink :: Logger -> TmpFs -> LinkerConfig -> [Option] -> IO ()
runLink Logger
logger TmpFs
tmpfs LinkerConfig
cfg [Option]
args = Logger -> String -> IO () -> IO ()
forall a. Logger -> String -> IO a -> IO a
traceSystoolCommand Logger
logger String
"linker" (IO () -> IO ()) -> IO () -> IO ()
forall a b. (a -> b) -> a -> b
$ do
  let all_args :: [Option]
all_args = LinkerConfig -> [Option]
linkerOptionsPre LinkerConfig
cfg [Option] -> [Option] -> [Option]
forall a. [a] -> [a] -> [a]
++ [Option]
args [Option] -> [Option] -> [Option]
forall a. [a] -> [a] -> [a]
++ LinkerConfig -> [Option]
linkerOptionsPost LinkerConfig
cfg

  -- on Windows, mangle environment variables to account for a bug in Windows
  -- Vista
  Maybe [(String, String)]
mb_env <- [Option] -> IO (Maybe [(String, String)])
getGccEnv [Option]
all_args

  Logger
-> TmpFs
-> TempDir
-> (String -> String)
-> String
-> String
-> [Option]
-> Maybe [(String, String)]
-> IO ()
runSomethingResponseFile Logger
logger TmpFs
tmpfs (LinkerConfig -> TempDir
linkerTempDir LinkerConfig
cfg) (LinkerConfig -> String -> String
linkerFilter LinkerConfig
cfg)
    String
"Linker" (LinkerConfig -> String
linkerProgram LinkerConfig
cfg) [Option]
all_args Maybe [(String, String)]
mb_env