Skip to content

Instantly share code, notes, and snippets.

@ezyang
Created September 27, 2016 21:21
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ezyang/5bdcd632a6f983967dc98acdf50c4744 to your computer and use it in GitHub Desktop.
Save ezyang/5bdcd632a6f983967dc98acdf50c4744 to your computer and use it in GitHub Desktop.
A simple frontend plugin that prints all modules of a project in topological order
module GhcSort where
import GHC
import GhcPlugins
import DriverPhases
import Control.Monad
import Data.List
import Data.Maybe
import System.FilePath
import Data.Graph
frontendPlugin :: FrontendPlugin
frontendPlugin = defaultFrontendPlugin {
frontend = doDiff
}
doDiff :: [String] -> [(String, Maybe Phase)] -> Ghc ()
doDiff _args srcs = do
-- API infelicity: we should be CompManager by default...
dflags <- getSessionDynFlags
_ <- setSessionDynFlags dflags { ghcMode = CompManager }
let (hs_srcs, _non_hs_srcs) = partition isHaskellishTarget srcs
targets <- mapM (uncurry guessTarget) hs_srcs
setTargets targets
mod_graph <- depanal [] False
let full_mg :: [SCC ModSummary]
full_mg = topSortModuleGraph False mod_graph Nothing
forM_ (flattenSCCs full_mg) $ \ms ->
liftIO $ putStrLn (normalise (fromJust (ml_hs_file (ms_location ms))))
return ()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment