Created
September 27, 2016 21:21
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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