 Parses the dynamically set flags for GHC. This is the most general form of
-- the dynamic flag parser that the other methods simply wrap. It allows
-- saying which flags are valid flags and indicating if we are parsing
-- arguments from the command line or from a file pragma.
parseDynamicFlagsFull :: MonadIO m
                  => [Flag (CmdLineP DynFlags)]    -- ^ valid flags to match against
                  -> Bool                          -- ^ are the arguments from the command line?
                  -> DynFlags                      -- ^ current dynamic flags
                  -> [Located String]              -- ^ arguments to parse
                  -> m (DynFlags, [Located String], [Located String])
parseDynamicFlagsFull activeFlags cmdline dflags0 args = do
  -- XXX Legacy support code
  -- We used to accept things like
  --     optdep-f  -optdepdepend
  --     optdep-f  -optdep depend
  --     optdep -f -optdepdepend
  --     optdep -f -optdep depend
  -- but the spaces trip up proper argument handling. So get rid of them.
  let f (L p "-optdep" : L _ x : xs) = (L p ("-optdep" ++ x)) : f xs
      f (x : xs) = x : f xs
      f xs = xs
      args' = f args

  let ((leftover, errs, warns), dflags1)
          = runCmdLine (processArgs activeFlags args') dflags0
  when (not (null errs)) $ liftIO $
      throwGhcExceptionIO $ errorsToGhcException errs

  -- check for disabled flags in safe haskell
  let (dflags2, sh_warns) = safeFlagCheck cmdline dflags1
      dflags3 = updateWays dflags2
      theWays = ways dflags3

  unless (allowed_combination theWays) $ liftIO $
      throwGhcExceptionIO (CmdLineError ("combination not supported: " ++
                               intercalate "/" (map wayDesc theWays)))

  whenGeneratingDynamicToo dflags3 $
      unless (isJust (outputFile dflags3) == isJust (dynOutputFile dflags3)) $
          liftIO $ throwGhcExceptionIO $ CmdLineError
              "With -dynamic-too, must give -dyno iff giving -o"

  let (dflags4, consistency_warnings) = makeDynFlagsConsistent dflags3

  dflags5 <- case dllSplitFile dflags4 of
             Nothing -> return (dflags4 { dllSplit = Nothing })
             Just f ->
                 case dllSplit dflags4 of
                 Just _ ->
                     -- If dllSplit is out of date then it would have
                     -- been set to Nothing. As it's a Just, it must be
                     -- up-to-date.
                     return dflags4
                 Nothing ->
                     do xs <- liftIO $ readFile f
                        let ss = map (Set.fromList . words) (lines xs)
                        return $ dflags4 { dllSplit = Just ss }

  liftIO $ setUnsafeGlobalDynFlags dflags5

  return (dflags5, leftover, consistency_warnings ++ sh_warns ++ warns)

up
parseDynamicFlagsFull :: MonadIO m
                  => [Flag (CmdLineP DynFlags)]    -- ^ valid flags to match against
                  -> Bool                          -- ^ are the arguments from the command line?
                  -> DynFlags                      -- ^ current dynamic flags
                  -> [Located String]              -- ^ arguments to parse
                  -> m (DynFlags, [Located String], [Located String])
parseDynamicFlagsFull activeFlags cmdline dflags0 args = do
  -- XXX Legacy support code
  -- We used to accept things like
  --     optdep-f  -optdepdepend
  --     optdep-f  -optdep depend
  --     optdep -f -optdepdepend
  --     optdep -f -optdep depend
  -- but the spaces trip up proper argument handling. So get rid of them.
  let f (L p "-optdep" : L _ x : xs) = (L p ("-optdep" ++ x)) : f xs
      f (x : xs) = x : f xs
      f xs = xs
      args' = f args

  let ((leftover, errs, warns), dflags1)
          = runCmdLine (processArgs activeFlags args') dflags0
  when (not (null errs)) $ liftIO $
      throwGhcExceptionIO $ errorsToGhcException errs

  -- check for disabled flags in safe haskell
  let (dflags2, sh_warns) = safeFlagCheck cmdline dflags1
      dflags3 = updateWays dflags2
      theWays = ways dflags3

  unless (allowed_combination theWays) $ liftIO $
      throwGhcExceptionIO (CmdLineError ("combination not supported: " ++
                               intercalate "/" (map wayDesc theWays)))

  whenGeneratingDynamicToo dflags3 $
      unless (isJust (outputFile dflags3) == isJust (dynOutputFile dflags3)) $
          liftIO $ throwGhcExceptionIO $ CmdLineError
              "With -dynamic-too, must give -dyno iff giving -o"

  let (dflags4, consistency_warnings) = makeDynFlagsConsistent dflags3

  dflags5 <- case dllSplitFile dflags4 of
             Nothing -> return (dflags4 { dllSplit = Nothing })
             Just f ->
                 case dllSplit dflags4 of
                 Just _ ->
                     -- If dllSplit is out of date then it would have
                     -- been set to Nothing. As it's a Just, it must be
                     -- up-to-date.
                     return dflags4
                 Nothing ->
                     do xs <- liftIO $ readFile f
                        let ss = map (Set.fromList . words) (lines xs)
                        return $ dflags4 { dllSplit = Just ss }

  liftIO $ setUnsafeGlobalDynFlags dflags5

  return (dflags5, leftover, consistency_warnings ++ sh_warns ++ warns)
parseDynamicFlagsFull activeFlags cmdline dflags0 args = do
  -- XXX Legacy support code
  -- We used to accept things like
  --     optdep-f  -optdepdepend
  --     optdep-f  -optdep depend
  --     optdep -f -optdepdepend
  --     optdep -f -optdep depend
  -- but the spaces trip up proper argument handling. So get rid of them.
  let f (L p "-optdep" : L _ x : xs) = (L p ("-optdep" ++ x)) : f xs
      f (x : xs) = x : f xs
      f xs = xs
      args' = f args

  let ((leftover, errs, warns), dflags1)
          = runCmdLine (processArgs activeFlags args') dflags0
  when (not (null errs)) $ liftIO $
      throwGhcExceptionIO $ errorsToGhcException errs

  -- check for disabled flags in safe haskell
  let (dflags2, sh_warns) = safeFlagCheck cmdline dflags1
      dflags3 = updateWays dflags2
      theWays = ways dflags3

  unless (allowed_combination theWays) $ liftIO $
      throwGhcExceptionIO (CmdLineError ("combination not supported: " ++
                               intercalate "/" (map wayDesc theWays)))

  whenGeneratingDynamicToo dflags3 $
      unless (isJust (outputFile dflags3) == isJust (dynOutputFile dflags3)) $
          liftIO $ throwGhcExceptionIO $ CmdLineError
              "With -dynamic-too, must give -dyno iff giving -o"

  let (dflags4, consistency_warnings) = makeDynFlagsConsistent dflags3

  dflags5 <- case dllSplitFile dflags4 of
             Nothing -> return (dflags4 { dllSplit = Nothing })
             Just f ->
                 case dllSplit dflags4 of
                 Just _ ->
                     -- If dllSplit is out of date then it would have
                     -- been set to Nothing. As it's a Just, it must be
                     -- up-to-date.
                     return dflags4
                 Nothing ->
                     do xs <- liftIO $ readFile f
                        let ss = map (Set.fromList . words) (lines xs)
                        return $ dflags4 { dllSplit = Just ss }

  liftIO $ setUnsafeGlobalDynFlags dflags5

  return (dflags5, leftover, consistency_warnings ++ sh_warns ++ warns)