-- | 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
  mb_env <- [Option] -> IO (Maybe [(String, String)])
getGccEnv [Option]
all_args

  runSomethingResponseFile logger tmpfs (linkerTempDir cfg) (linkerFilter cfg)
    "Linker" (linkerProgram cfg) all_args mb_env