Turn the coverage analysis stdenv adapters into setup hooks

Stdenv adapters are kinda weird and un-idiomatic (especially when they
don't actually change stdenv).  It's more idiomatic to say

  buildInputs = [ makeCoverageAnalysisReport ];
This commit is contained in:
Eelco Dolstra 2014-02-05 14:11:21 +01:00
parent bea2b3c517
commit 80647127a3
7 changed files with 45 additions and 52 deletions

View File

@ -27,7 +27,7 @@ rec {
} // args); } // args);
coverageAnalysis = args: nixBuild ( coverageAnalysis = args: nixBuild (
{ inherit lcov; { inherit lcov enableCoverageInstrumentation makeCoverageAnalysisReport;
doCoverageAnalysis = true; doCoverageAnalysis = true;
} // args); } // args);

View File

@ -15,6 +15,7 @@
, failureHook ? null , failureHook ? null
, prePhases ? [] , prePhases ? []
, postPhases ? [] , postPhases ? []
, buildInputs ? []
, ... } @ args: , ... } @ args:
stdenv.mkDerivation ( stdenv.mkDerivation (
@ -61,13 +62,6 @@ stdenv.mkDerivation (
. ${./functions.sh} . ${./functions.sh}
origSrc=$src origSrc=$src
src=$(findTarballs $src | head -1) src=$(findTarballs $src | head -1)
# Set GCC flags for coverage analysis, if desired.
if test -n "${toString doCoverageAnalysis}"; then
export NIX_CFLAGS_COMPILE="-O0 --coverage $NIX_CFLAGS_COMPILE"
export CFLAGS="-O0"
export CXXFLAGS="-O0"
fi
''; '';
initPhase = '' initPhase = ''
@ -85,30 +79,13 @@ stdenv.mkDerivation (
prePhases = ["initPhase"] ++ prePhases; prePhases = ["initPhase"] ++ prePhases;
# In the report phase, create a coverage analysis report. buildInputs = buildInputs ++ [ args.makeCoverageAnalysisReport ];
coverageReportPhase = if doCoverageAnalysis then ''
${args.lcov}/bin/lcov --directory . --capture --output-file app.info
set -o noglob
${args.lcov}/bin/lcov --remove app.info $lcovFilter > app2.info
set +o noglob
mv app2.info app.info
mkdir $out/coverage
${args.lcov}/bin/genhtml app.info $lcovExtraTraceFiles -o $out/coverage > log
# Grab the overall coverage percentage for use in release overviews.
grep "Overall coverage rate" log | sed 's/^.*(\(.*\)%).*$/\1/' > $out/nix-support/coverage-rate
echo "report coverage $out/coverage" >> $out/nix-support/hydra-build-products
'' else "";
lcovFilter = ["/nix/store/*"] ++ lcovFilter; lcovFilter = ["/nix/store/*"] ++ lcovFilter;
inherit lcovExtraTraceFiles; inherit lcovExtraTraceFiles;
postPhases = postPhases ++ postPhases = postPhases ++ ["finalPhase"];
(stdenv.lib.optional doCoverageAnalysis "coverageReportPhase") ++ ["finalPhase"];
meta = (if args ? meta then args.meta else {}) // { meta = (if args ? meta then args.meta else {}) // {
description = if doCoverageAnalysis then "Coverage analysis" else "Nix package for ${stdenv.system}"; description = if doCoverageAnalysis then "Coverage analysis" else "Nix package for ${stdenv.system}";

View File

@ -0,0 +1,14 @@
# Force GCC to build with coverage instrumentation. Also disable
# optimisation, since it may confuse things.
export NIX_CFLAGS_COMPILE="$NIX_CFLAGS_COMPILE -O0 --coverage"
# FIXME: Handle the case where postUnpack is already set.
postUnpack() {
# This is an uberhack to prevent libtool from remoaving gcno
# files. This has been fixed in libtool, but there are packages
# out there with old ltmain.sh scripts. See
# http://www.mail-archive.com/libtool@gnu.org/msg10725.html
for i in $(find -name ltmain.sh); do
substituteInPlace $i --replace '*.$objext)' '*.$objext | *.gcno)'
done
}

View File

@ -0,0 +1,18 @@
postPhases+=" coverageReportPhase"
coverageReportPhase() {
lcov --directory . --capture --output-file app.info
set -o noglob
lcov --remove app.info $lcovFilter > app2.info
set +o noglob
mv app2.info app.info
mkdir -p $out/coverage
genhtml app.info $lcovExtraTraceFiles -o $out/coverage > log
# Grab the overall coverage percentage for use in release overviews.
mkdir -p $out/nix-support
grep "Overall coverage rate" log | sed 's/^.*(\(.*\)%).*$/\1/' > $out/nix-support/coverage-rate
echo "report coverage $out/coverage" >> $out/nix-support/hydra-build-products
}

View File

@ -231,29 +231,7 @@ rec {
programs like lcov to produce pretty-printed reports. programs like lcov to produce pretty-printed reports.
*/ */
addCoverageInstrumentation = stdenv: addCoverageInstrumentation = stdenv:
# Object files instrumented with coverage analysis write runtime cleanupBuildTree (keepBuildTree (overrideInStdenv stdenv [ pkgs.enableCoverageInstrumentation ]));
# coverage data to <path>/<object>.gcda, where <path> is the
# location where gcc originally created the object file. That
# would be /tmp/nix-build-<something>, which will be long gone by
# the time we run the program. Furthermore, the <object>.gcno
# files created at compile time are also written there. And to
# make nice coverage reports with lcov, we need the source code.
# So we have to use the `keepBuildTree' adapter as well.
let stdenv' = cleanupBuildTree (keepBuildTree stdenv); in
{ mkDerivation = args: stdenv'.mkDerivation (args // {
NIX_CFLAGS_COMPILE = toString (args.NIX_CFLAGS_COMPILE or "") + " -O0 --coverage";
postUnpack =
''
# This is an uberhack to prevent libtool from removing gcno
# files. This has been fixed in libtool, but there are
# packages out there with old ltmain.sh scripts.
# See http://www.mail-archive.com/libtool@gnu.org/msg10725.html
for i in $(find -name ltmain.sh); do
substituteInPlace $i --replace '*.$objext)' '*.$objext | *.gcno)'
done
'' + args.postUnpack or "";
});
};
/* Replace the meta.maintainers field of a derivation. This is useful /* Replace the meta.maintainers field of a derivation. This is useful

View File

@ -87,10 +87,10 @@ let
userHook = config.stdenv.userHook or null; userHook = config.stdenv.userHook or null;
# Inputs built by the cross compiler. # Inputs built by the cross compiler.
buildInputs = lib.optionals (crossConfig != null) buildInputs ++ extraBuildInputs; buildInputs = lib.optionals (crossConfig != null) (buildInputs ++ extraBuildInputs);
propagatedBuildInputs = lib.optionals (crossConfig != null) propagatedBuildInputs; propagatedBuildInputs = lib.optionals (crossConfig != null) propagatedBuildInputs;
# Inputs built by the usual native compiler. # Inputs built by the usual native compiler.
nativeBuildInputs = nativeBuildInputs ++ lib.optionals (crossConfig == null) buildInputs; nativeBuildInputs = nativeBuildInputs ++ lib.optionals (crossConfig == null) (buildInputs ++ extraBuildInputs);
propagatedNativeBuildInputs = propagatedNativeBuildInputs ++ propagatedNativeBuildInputs = propagatedNativeBuildInputs ++
lib.optionals (crossConfig == null) propagatedBuildInputs; lib.optionals (crossConfig == null) propagatedBuildInputs;
}))) ( }))) (

View File

@ -396,6 +396,12 @@ let
fixDarwinDylibNames = makeSetupHook { } ../build-support/setup-hooks/fix-darwin-dylib-names.sh; fixDarwinDylibNames = makeSetupHook { } ../build-support/setup-hooks/fix-darwin-dylib-names.sh;
enableCoverageInstrumentation = makeSetupHook { } ../build-support/setup-hooks/enable-coverage-instrumentation.sh;
makeCoverageAnalysisReport = makeSetupHook
{ deps = [ pkgs.lcov pkgs.enableCoverageInstrumentation ]; }
../build-support/setup-hooks/make-coverage-analysis-report.sh;
### TOOLS ### TOOLS