+-- Parsing the dynamic flags.
+
+-- | Parse dynamic flags from a list of command line arguments. Returns the
+-- the parsed 'DynFlags', the left-over arguments, and a list of warnings.
+-- Throws a 'UsageError' if errors occurred during parsing (such as unknown
+-- flags or missing arguments).
+parseDynamicFlags :: Monad m =>
+ DynFlags -> [Located String]
+ -> m (DynFlags, [Located String], [Located String])
+ -- ^ Updated 'DynFlags', left-over arguments, and
+ -- list of warnings.
+parseDynamicFlags dflags args = parseDynamicFlags_ dflags args True
+
+-- | Like 'parseDynamicFlags' but does not allow the package flags (-package,
+-- -hide-package, -ignore-package, -hide-all-packages, -package-conf).
+parseDynamicNoPackageFlags :: Monad m =>
+ DynFlags -> [Located String]
+ -> m (DynFlags, [Located String], [Located String])
+ -- ^ Updated 'DynFlags', left-over arguments, and
+ -- list of warnings.
+parseDynamicNoPackageFlags dflags args = parseDynamicFlags_ dflags args False
+
+parseDynamicFlags_ :: Monad m =>
+ DynFlags -> [Located String] -> Bool
+ -> m (DynFlags, [Located String], [Located String])
+parseDynamicFlags_ dflags0 args pkg_flags = 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
+
+ -- Note: -ignore-package (package_flags) must precede -i* (dynamic_flags)
+ flag_spec | pkg_flags = package_flags ++ dynamic_flags
+ | otherwise = dynamic_flags
+
+ let ((leftover, errs, warns), dflags1)
+ = runCmdLine (processArgs flag_spec args') dflags0
+ when (not (null errs)) $ ghcError $ errorsToGhcException errs
+
+ -- Cannot use -fPIC with registerised -fvia-C, because the mangler
+ -- isn't up to the job. We know that if hscTarget == HscC, then the
+ -- user has explicitly used -fvia-C, because -fasm is the default,
+ -- unless there is no NCG on this platform. The latter case is
+ -- checked when the -fPIC flag is parsed.
+ --
+ let (pic_warns, dflags2)
+ | opt_PIC && hscTarget dflags1 == HscC && cGhcUnregisterised == "NO"
+ = ([L noSrcSpan $ "Warning: -fvia-C is incompatible with -fPIC; ignoring -fvia-C"],
+ dflags1{ hscTarget = HscAsm })
+#if !(x86_64_TARGET_ARCH && linux_TARGET_OS)
+ | (not opt_Static || opt_PIC) && hscTarget dflags1 == HscLlvm
+ = ([L noSrcSpan $ "Warning: -fllvm is incompatible with -fPIC and -"
+ ++ "dynamic on this platform;\n ignoring -fllvm"],
+ dflags1{ hscTarget = HscAsm })
+#endif
+ | otherwise = ([], dflags1)
+
+ return (dflags2, leftover, pic_warns ++ warns)
+
+
+{- **********************************************************************
+%* *
+ DynFlags specifications
+%* *
+%********************************************************************* -}