Commit 5442acef authored by ttuegel's avatar ttuegel
Browse files

Support building libraries and test suites with HPC; automatic report generation.

parent c632330b
......@@ -82,6 +82,7 @@ Library
Distribution.Simple.GHC,
Distribution.Simple.LHC,
Distribution.Simple.Haddock,
Distribution.Simple.Hpc,
Distribution.Simple.Hugs,
Distribution.Simple.Install,
Distribution.Simple.InstallDirs,
......
......@@ -87,6 +87,7 @@ import Distribution.PackageDescription.Configuration
( finalizePackageDescription, mapTreeData )
import Distribution.PackageDescription.Check
( PackageCheck(..), checkPackage, checkPackageFiles )
import Distribution.Simple.Hpc ( enableCoverage )
import Distribution.Simple.Program
( Program(..), ProgramLocation(..), ConfiguredProgram(..)
, ProgramConfiguration, defaultProgramConfiguration
......@@ -340,7 +341,9 @@ configure (pkg_descr0, pbi) cfg
-- add extra include/lib dirs as specified in cfg
-- we do it here so that those get checked too
let pkg_descr = addExtraIncludeLibDirs pkg_descr0'
let pkg_descr =
enableCoverage (fromFlag (configLibCoverage cfg)) distPref
$ addExtraIncludeLibDirs pkg_descr0'
when (not (null flags)) $
info verbosity $ "Flags chosen: "
......
-----------------------------------------------------------------------------
-- |
-- Module : Distribution.Simple.Hpc
-- Copyright : Thomas Tuegel 2011
--
-- Maintainer : cabal-devel@haskell.org
-- Portability : portable
--
-- This module provides functions for locating various HPC-related paths and
-- a function for adding the necessary options to a PackageDescription to
-- build test suites with HPC enabled.
{- All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following
disclaimer in the documentation and/or other materials provided
with the distribution.
* Neither the name of Isaac Jones nor the names of other
contributors may be used to endorse or promote products derived
from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -}
module Distribution.Simple.Hpc
( hpcDir
, enableCoverage
, tixDir
, tixFilePath
, doHpcMarkup
, findTixFiles
) where
import Control.Exception ( bracket )
import Control.Monad ( unless, when )
import Distribution.Compiler ( CompilerFlavor(..) )
import Distribution.ModuleName ( main )
import Distribution.PackageDescription
( BuildInfo(..)
, Library(..)
, PackageDescription(..)
, TestSuite(..)
, testModules
)
import Distribution.Simple.Utils ( die, notice )
import Distribution.Text
import Distribution.Verbosity ( Verbosity() )
import System.Directory ( doesFileExist, getDirectoryContents, removeFile )
import System.Exit ( ExitCode(..) )
import System.FilePath
import System.IO ( hClose, IOMode(..), openFile, openTempFile )
import System.Process ( runProcess, waitForProcess )
-- -------------------------------------------------------------------------
-- Haskell Program Coverage
-- | Conditionally enable Haskell Program Coverage by adding the necessary
-- GHC options to a PackageDescription.
enableCoverage :: Bool -- ^ Enable coverage?
-> String -- ^ \"dist/\" prefix
-> PackageDescription
-> PackageDescription
enableCoverage False _ x = x
enableCoverage True distPref p =
p { library = fmap enableLibCoverage (library p)
, testSuites = map enableTestCoverage (testSuites p)
}
where
enableBICoverage name oldBI =
let oldOptions = options oldBI
oldGHCOpts = lookup GHC oldOptions
newGHCOpts = case oldGHCOpts of
Just xs -> (GHC, hpcOpts ++ xs)
_ -> (GHC, hpcOpts)
newOptions = (:) newGHCOpts $ filter ((== GHC) . fst) oldOptions
hpcOpts = ["-fhpc", "-hpcdir", hpcDir distPref name]
in oldBI { options = newOptions }
enableLibCoverage l =
l { libBuildInfo = enableBICoverage (display $ package p)
(libBuildInfo l)
}
enableTestCoverage t =
t { testBuildInfo = enableBICoverage (testName t) (testBuildInfo t) }
hpcDir :: FilePath -- ^ \"dist/\" prefix
-> FilePath -- ^ Component subdirectory name
-> FilePath -- ^ Directory containing component's HPC .mix files
hpcDir distPref name = distPref </> "hpc" </> name
tixDir :: FilePath -- ^ \"dist/\" prefix
-> TestSuite -- ^ Test suite
-> FilePath -- ^ Directory containing test suite's .tix files
tixDir distPref suite = distPref </> "test" </> testName suite
-- | Path to the .tix file containing a test suite's sum statistics.
tixFilePath :: FilePath -- ^ \"dist/\" prefix
-> TestSuite -- ^ Test suite
-> FilePath -- Path to test suite's .tix file
tixFilePath distPref suite = tixDir distPref suite </> testName suite <.> "tix"
-- | Returns a list of all the .tix files in a test suite's .tix file
-- directory. Returned paths are the complete relative path to each file.
findTixFiles :: FilePath -- ^ \"dist/\" prefix
-> TestSuite -- ^ Test suite
-> IO [FilePath] -- ^ All .tix files belonging to test suite
findTixFiles distPref suite = do
files <- getDirectoryContents $ tixDir distPref suite
let tixFiles = flip filter files $ \x -> takeExtension x == ".tix"
return $ map (tixDir distPref suite </>) tixFiles
-- | Generate the HTML markup for a test suite.
doHpcMarkup :: Verbosity
-> FilePath -- ^ \"dist/\" prefix
-> String -- ^ Library name
-> TestSuite
-> IO ()
doHpcMarkup verbosity distPref libName suite = do
tixFiles <- findTixFiles distPref suite
when (not $ null tixFiles) $ do
let hpcOptions = map (\x -> "--exclude=" ++ display x) excluded
unionOptions = [ "sum"
, "--union"
, "--output=" ++ tixFilePath distPref suite
]
++ hpcOptions ++ tixFiles
markupOptions = [ "markup"
, tixFilePath distPref suite
, "--hpcdir=" ++ hpcDir distPref libName
, "--destdir=" ++ tixDir distPref suite
]
++ hpcOptions
excluded = testModules suite ++ [ main ]
runHpc opts h = runProcess "hpc" opts Nothing Nothing Nothing
(Just h) (Just h)
bracket (openHpcTemp $ tixDir distPref suite) deleteIfExists
$ \hpcOut -> do
hUnion <- openFile hpcOut AppendMode
procUnion <- runHpc unionOptions hUnion
exitUnion <- waitForProcess procUnion
success <- case exitUnion of
ExitSuccess -> do
hMarkup <- openFile hpcOut AppendMode
procMarkup <- runHpc markupOptions hMarkup
exitMarkup <- waitForProcess procMarkup
case exitMarkup of
ExitSuccess -> return True
_ -> return False
_ -> return False
unless success $ do
errs <- readFile hpcOut
die $ "HPC failed:\n" ++ errs
when success $ notice verbosity
$ "Test coverage report written to "
++ tixDir distPref suite </> "hpc_index"
<.> "html"
return ()
where openHpcTemp dir = do
(f, h) <- openTempFile dir $ "cabal-test-hpc-" <.> "log"
hClose h >> return f
deleteIfExists path = do
exists <- doesFileExist path
when exists $ removeFile path
......@@ -290,7 +290,8 @@ data ConfigFlags = ConfigFlags {
configConstraints :: [Dependency], -- ^Additional constraints for
-- dependencies
configConfigurationsFlags :: FlagAssignment,
configTests :: Flag Bool -- ^Enable test suite compilation
configTests :: Flag Bool, -- ^Enable test suite compilation
configLibCoverage :: Flag Bool -- ^ Enable test suite program coverage
}
deriving (Read,Show)
......@@ -311,7 +312,8 @@ defaultConfigFlags progConf = emptyConfigFlags {
configGHCiLib = Flag True,
configSplitObjs = Flag False, -- takes longer, so turn off by default
configStripExes = Flag True,
configTests = Flag False
configTests = Flag False,
configLibCoverage = Flag False
}
configureCommand :: ProgramConfiguration -> CommandUI ConfigFlags
......@@ -464,6 +466,10 @@ configureOptions showOrParseArgs =
"dependency checking and compilation for test suites listed in the package description file."
configTests (\v flags -> flags { configTests = v })
(boolOpt [] [])
,option "" ["library-coverage"]
"build library and test suites with Haskell Program Coverage enabled. (GHC only)"
configLibCoverage (\v flags -> flags { configLibCoverage = v })
(boolOpt [] [])
]
where
readFlagList :: String -> FlagAssignment
......@@ -571,7 +577,8 @@ instance Monoid ConfigFlags where
configConstraints = mempty,
configExtraIncludeDirs = mempty,
configConfigurationsFlags = mempty,
configTests = mempty
configTests = mempty,
configLibCoverage = mempty
}
mappend a b = ConfigFlags {
configPrograms = configPrograms b,
......@@ -601,7 +608,8 @@ instance Monoid ConfigFlags where
configConstraints = combine configConstraints,
configExtraIncludeDirs = combine configExtraIncludeDirs,
configConfigurationsFlags = combine configConfigurationsFlags,
configTests = combine configTests
configTests = combine configTests,
configLibCoverage = combine configLibCoverage
}
where combine field = field a `mappend` field b
......@@ -1234,6 +1242,7 @@ data TestFlags = TestFlags {
testHumanLog :: Flag PathTemplate,
testMachineLog :: Flag PathTemplate,
testShowDetails :: Flag TestShowDetails,
testKeepTix :: Flag Bool,
--TODO: eliminate the test list and pass it directly as positional args to the testHook
testList :: Flag [String],
-- TODO: think about if/how options are passed to test exes
......@@ -1247,6 +1256,7 @@ defaultTestFlags = TestFlags {
testHumanLog = toFlag $ toPathTemplate $ "$pkgid-$test-suite.log",
testMachineLog = toFlag $ toPathTemplate $ "$pkgid.log",
testShowDetails = toFlag Failures,
testKeepTix = toFlag False,
testList = Flag [],
testOptions = Flag []
}
......@@ -1287,6 +1297,10 @@ testCommand = makeCommand name shortDesc longDesc defaultTestFlags options
(map display knownTestShowDetails))
(fmap toFlag parse))
(flagToList . fmap display))
, option [] ["keep-tix-files"]
"keep .tix files for HPC between test runs"
testKeepTix (\v flags -> flags { testKeepTix = v})
trueArg
, option [] ["test-options"]
("give extra options to test executables "
++ "(name templates can use $pkgid, $compiler, "
......@@ -1314,6 +1328,7 @@ instance Monoid TestFlags where
testHumanLog = mempty,
testMachineLog = mempty,
testShowDetails = mempty,
testKeepTix = mempty,
testList = mempty,
testOptions = mempty
}
......@@ -1323,6 +1338,7 @@ instance Monoid TestFlags where
testHumanLog = combine testHumanLog,
testMachineLog = combine testMachineLog,
testShowDetails = combine testShowDetails,
testKeepTix = combine testKeepTix,
testList = combine testList,
testOptions = combine testOptions
}
......
......@@ -62,6 +62,7 @@ import qualified Distribution.PackageDescription as PD
import Distribution.Simple.Build.PathsModule ( pkgPathEnvVar )
import Distribution.Simple.BuildPaths ( exeExtension )
import Distribution.Simple.Compiler ( Compiler(..), CompilerId )
import Distribution.Simple.Hpc ( doHpcMarkup, findTixFiles, tixDir )
import Distribution.Simple.InstallDirs
( fromPathTemplate, initialPathTemplateEnv, PathTemplateVariable(..)
, substPathTemplate , toPathTemplate, PathTemplate )
......@@ -173,11 +174,20 @@ testController flags pkg_descr lbi suite preTest cmd postTest logNamer = do
existingEnv <- getEnvironment
let dataDirPath = pwd </> PD.dataDir pkg_descr
shellEnv = Just $ (pkgPathEnvVar pkg_descr "datadir", dataDirPath)
: ("HPCTIXDIR", pwd </> tixDir distPref suite)
: existingEnv
bracket (openCabalTemp testLogDir) deleteIfExists $ \tempLog ->
bracket (openCabalTemp testLogDir) deleteIfExists $ \tempInput -> do
-- Create directory for HPC files.
createDirectoryIfMissing True $ tixDir distPref suite
-- Remove old .tix files if appropriate.
tixFiles <- findTixFiles distPref suite
unless (fromFlag $ testKeepTix flags)
$ mapM_ deleteIfExists tixFiles
-- Write summary notices indicating start of test suite
notice verbosity $ summarizeSuiteStart $ PD.testName suite
appendFile tempLog $ summarizeSuiteStart $ PD.testName suite
......@@ -221,6 +231,8 @@ testController flags pkg_descr lbi suite preTest cmd postTest logNamer = do
-- Write summary notice to terminal indicating end of test suite
notice verbosity $ summarizeSuiteFinish suiteLog'
doHpcMarkup verbosity distPref (display $ PD.package pkg_descr) suite
return suiteLog'
where
deleteIfExists file = do
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment