Move file target parsing from `ghc/Main` to library
Motivation
A number of downstream consumers want to parse arguments to GHC and process them before actually passing them to the GHC monad.
An example of such an consumer is hie-bios which copies a notable number of argument parsing functions from ghc/Main.hs
.
Example:
- Normalise filepaths like GHC https://github.com/mpickering/hie-bios/blob/master/src/HIE/Bios/Environment.hs#L167 https://gitlab.haskell.org/ghc/ghc/-/blob/master/ghc/Main.hs#L229
- Partition arguments:
https://github.com/mpickering/hie-bios/blob/master/src/HIE/Bios/Environment.hs#L226
https://gitlab.haskell.org/ghc/ghc/-/blob/master/ghc/Main.hs#L299
Proposal
Move the functions in question to the library so that downstream consumers such as hie-bios
can use them.
This should be trivial, the only question for me is, to which module it should be used. Functions in question are:
normalise_hyp :: FilePath -> FilePath
normalise_hyp fp
| strt_dot_sl && "-" `isPrefixOf` nfp = cur_dir ++ nfp
| otherwise = nfp
where
#if defined(mingw32_HOST_OS)
strt_dot_sl = "./" `isPrefixOf` fp || ".\\" `isPrefixOf` fp
#else
strt_dot_sl = "./" `isPrefixOf` fp
#endif
cur_dir = '.' : [pathSeparator]
nfp = normalise fp
parseTargetFiles :: DynFlags -> [Located String] -> (DynFlags, [(String, Maybe Phase)], [String])
parseTargetFiles dflags4 fileish_args =
normal_fileish_paths = map (normalise_hyp . unLoc) fileish_args
(srcs, objs) = partition_args normal_fileish_paths [] []
dflags5 = dflags4 { ldInputs = map (FileOption "") objs
++ ldInputs dflags4 }
in (dflags5, srcs, objs)
-- Optionally, also expose
partition_args :: [String] -> [(String, Maybe Phase)] -> [String]
-> ([(String, Maybe Phase)], [String])
looks_like_an_input :: String -> Bool
I propose to move these functions to compiler/GHC.hs
, next to parseDynamicFlags
and expose at least parseTargetFiles
.