hpc-tools: improving flag processing and help messages, small bug fixes.
[ghc-hetmet.git] / utils / hpc / HpcMarkup.hs
index 53eaf32..4b3b976 100644 (file)
@@ -12,18 +12,22 @@ import Trace.Hpc.Util
 import HpcFlags
 
 import System.Environment
+import System.Directory
 import Data.List
 import Data.Maybe(fromJust)
 import Data.Array
-import qualified Data.Set as Set
+import qualified HpcSet as Set
 
 ------------------------------------------------------------------------------
 
-markup_options = 
-  [ excludeOpt,includeOpt,hpcDirOpt,hsDirOpt,funTotalsOpt
-  , altHighlightOpt
-  , destDirOpt
-  ]
+markup_options 
+        = excludeOpt
+        . includeOpt
+        . srcDirOpt
+        . hpcDirOpt
+        . funTotalsOpt
+        . altHighlightOpt
+        . destDirOpt
                 
 markup_plugin = Plugin { name = "markup"
                       , usage = "[OPTION] .. <TIX_FILE> [<MODULE> [<MODULE> ..]]" 
@@ -43,20 +47,23 @@ markup_main flags (prog:modNames) = do
                                   `Set.union` 
                                includeMods flags }
   let Flags
-       { hpcDirs = hpcDirs
-       , hsDirs = theHsPath
-       , funTotals = theFunTotals
+       { funTotals = theFunTotals
        , altHighlight = invertOutput
        , destDir = dest_dir
        }  = hpcflags1
 
   mtix <- readTix (getTixFileName prog)
   Tix tixs <- case mtix of
-    Nothing -> error $ "unable to find tix file for: " ++ prog
+    Nothing -> hpcError markup_plugin $ "unable to find tix file for: " ++ prog
     Just a -> return a
 
+#if __GLASGOW_HASKELL__ >= 604 
+  -- create the dest_dir if needed
+  createDirectoryIfMissing True dest_dir
+#endif
+
   mods <-
-     sequence [ genHtmlFromMod dest_dir hpcDirs tix theFunTotals theHsPath invertOutput
+     sequence [ genHtmlFromMod dest_dir hpcflags1 tix theFunTotals invertOutput
              | tix <- tixs
              , allowModule hpcflags1 (tixModuleName tix)
              ]
@@ -68,6 +75,9 @@ markup_main flags (prog:modNames) = do
 
   let writeSummary name cmp = do
         let mods' = sortBy cmp mods
+
+
+
    
         putStrLn $ "Writing: " ++ (name ++ ".html")
         writeFile (dest_dir ++ "/" ++ name ++ ".html") $ 
@@ -75,6 +85,7 @@ markup_main flags (prog:modNames) = do
            "<style type=\"text/css\">" ++
            "table.bar { background-color: #f25913; }\n" ++
            "td.bar { background-color: #60de51;  }\n" ++
+           "td.invbar { background-color: #f25913;  }\n" ++
            "table.dashboard { border-collapse: collapse  ; border: solid 1px black }\n" ++
            ".dashboard td { border: solid 1px black }\n" ++
            ".dashboard th { border: solid 1px black }\n" ++
@@ -119,20 +130,20 @@ markup_main flags (prog:modNames) = do
                (percent (expTicked s1) (expTotal s1))
 
 
-markup_main flags [] = error $ "no .tix file or executable name specified" 
+markup_main flags [] = hpcError markup_plugin $ "no .tix file or executable name specified" 
 
 genHtmlFromMod
   :: String
-  -> [FilePath]
+  -> Flags
   -> TixModule
   -> Bool
-  -> [String]
   -> Bool
   -> IO (String, [Char], ModuleSummary)
-genHtmlFromMod dest_dir hpcDirs tix theFunTotals theHsPath invertOutput = do
+genHtmlFromMod dest_dir flags tix theFunTotals invertOutput = do
+  let theHsPath = srcDirs flags
   let modName0 = tixModuleName tix 
 
-  (Mix origFile _ mixHash tabStop mix') <- readMix hpcDirs modName0
+  (Mix origFile _ mixHash tabStop mix') <- readMixWithFlags flags modName0
 
   let arr_tix :: Array Int Integer
       arr_tix = listArray (0,length (tixModuleTixs tix) - 1)
@@ -411,14 +422,16 @@ showSummary ticked total =
                "<td width=100>" ++ 
                    (case percent ticked total of
                       Nothing -> "&nbsp;"
-                      Just w -> "<table cellpadding=0 cellspacing=0 width=\"100\" class=\"bar\">" ++
-                                    "<tr><td><table cellpadding=0 cellspacing=0 width=\"" ++ show w ++ "%\">" ++
-                             "<tr><td height=12 class=\"bar\"></td></tr>" ++
-                             "</table></td></tr></table>")
-                             ++ "</td>"
+                      Just w -> bar w "bar"
+                    )  ++ "</td>"
      where
         showP Nothing = "-&nbsp;"
         showP (Just x) = show x ++ "%"
+        bar 0 inner = bar 100 "invbar"
+        bar w inner = "<table cellpadding=0 cellspacing=0 width=\"100\" class=\"bar\">" ++
+                        "<tr><td><table cellpadding=0 cellspacing=0 width=\"" ++ show w ++ "%\">" ++
+                             "<tr><td height=12 class=" ++ show inner ++ "></td></tr>" ++
+                             "</table></td></tr></table>"
 
 percent :: (Integral a) => a -> a -> Maybe a
 percent ticked total = if total == 0 then Nothing else Just (ticked * 100 `div` total)
@@ -444,7 +457,8 @@ readFileFromPath filename@('/':_) _ = readFile filename
 readFileFromPath filename path0 = readTheFile path0
   where
        readTheFile :: [String] -> IO String
-       readTheFile [] = error $ "could not find " ++ show filename 
+       readTheFile [] = hpcError markup_plugin
+                            $ "could not find " ++ show filename 
                                 ++ " in path " ++ show path0
        readTheFile (dir:dirs) = 
                catch (do str <- readFile (dir ++ "/" ++ filename)