Skip to content

Move file target parsing from `ghc/Main` to library

Hannes Siebenhandl requested to merge fendor/ghc:expose-target-parsing into master

Thank you for your contribution to GHC!

Please take a few moments to verify that your commits fulfill the following:

  • are either individually buildable or squashed
  • have commit messages which describe what they do (referring to [Notes][notes] and tickets using #NNNN syntax when appropriate)
  • have added source comments describing your change. For larger changes you likely should add a [Note][notes] and cross-reference it from the relevant places.
  • add a testcase to the testsuite. Only code refactoring.
  • replace this message with a description motivating your change

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:

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.

closes #18596 (closed)

Edited by Hannes Siebenhandl

Merge request reports