nixpkgs mirror (for testing)
github.com/NixOS/nixpkgs
nix
1# This function builds and tests an Autoconf-style source tarball.
2# The result can be installed normally in an environment (e.g., after
3# making it available through a channel). If `doCoverageAnalysis' is
4# true, it does an ordinary build from a source tarball, except that
5# it turns on GCC's coverage analysis feature. It then runs `make
6# check' and produces a coverage analysis report using `lcov'.
7
8{
9 buildOutOfSourceTree ? false,
10 preConfigure ? null,
11 doCoverageAnalysis ? false,
12 doClangAnalysis ? false,
13 doCoverityAnalysis ? false,
14 lcovFilter ? [ ],
15 lcovExtraTraceFiles ? [ ],
16 src,
17 lib,
18 stdenv,
19 name ? if doCoverageAnalysis then "nix-coverage" else "nix-build",
20 failureHook ? null,
21 prePhases ? [ ],
22 postPhases ? [ ],
23 buildInputs ? [ ],
24 preHook ? "",
25 postHook ? "",
26 ...
27}@args:
28
29let
30 doingAnalysis = doCoverageAnalysis || doClangAnalysis || doCoverityAnalysis;
31in
32stdenv.mkDerivation (
33
34 {
35 # Also run a `make check'.
36 doCheck = true;
37
38 # When doing coverage analysis, we don't care about the result.
39 dontInstall = doingAnalysis;
40 useTempPrefix = doingAnalysis;
41
42 showBuildStats = true;
43
44 finalPhase = ''
45 # Propagate the release name of the source tarball. This is
46 # to get nice package names in channels.
47 if test -e $origSrc/nix-support/hydra-release-name; then
48 cp $origSrc/nix-support/hydra-release-name $out/nix-support/hydra-release-name
49 fi
50
51 # Package up Coverity analysis results
52 if [ ! -z "${toString doCoverityAnalysis}" ]; then
53 if [ -d "_coverity_$name/cov-int" ]; then
54 mkdir -p $out/tarballs
55 NAME=`cat $out/nix-support/hydra-release-name`
56 cd _coverity_$name
57 tar caf $out/tarballs/$NAME-coverity-int.xz cov-int
58 echo "file cov-build $out/tarballs/$NAME-coverity-int.xz" >> $out/nix-support/hydra-build-products
59 fi
60 fi
61
62 # Package up Clang analysis results
63 if [ ! -z "${toString doClangAnalysis}" ]; then
64 if [ ! -z "`ls _clang_analyze_$name`" ]; then
65 cd _clang_analyze_$name && mv * $out/analysis
66 else
67 mkdir -p $out/analysis
68 echo "No bugs found." >> $out/analysis/index.html
69 fi
70
71 echo "report analysis $out/analysis" >> $out/nix-support/hydra-build-products
72 fi
73 '';
74
75 failureHook = (lib.optionalString (failureHook != null) failureHook) + ''
76 if test -n "$succeedOnFailure"; then
77 if test -n "$keepBuildDirectory"; then
78 KEEPBUILDDIR="$out/`basename $TMPDIR`"
79 echo "Copying build directory to $KEEPBUILDDIR"
80 mkdir -p $KEEPBUILDDIR
81 cp -R "$TMPDIR/"* $KEEPBUILDDIR
82 fi
83 fi
84 '';
85 }
86
87 // removeAttrs args [ "lib" ] # Propagating lib causes the evaluation to fail, because lib is a function that can't be converted to a string
88
89 // {
90 name = name + (lib.optionalString (src ? version) "-${src.version}");
91
92 postHook = ''
93 . ${./functions.sh}
94 origSrc=$src
95 src=$(findTarball $src)
96 ${postHook}
97 '';
98
99 preHook = ''
100 # Perform Coverity Analysis
101 if [ ! -z "${toString doCoverityAnalysis}" ]; then
102 shopt -s expand_aliases
103 mkdir _coverity_$name
104 alias make="cov-build --dir _coverity_$name/cov-int make"
105 fi
106
107 # Perform Clang Analysis
108 if [ ! -z "${toString doClangAnalysis}" ]; then
109 shopt -s expand_aliases
110 alias make="scan-build -o _clang_analyze_$name --html-title='Scan results for $name' make"
111 fi
112
113 ${preHook}
114 '';
115
116 # Clean up after analysis
117 postBuild = ''
118 if [ ! -z "${toString (doCoverityAnalysis || doClangAnalysis)}" ]; then
119 unalias make
120 fi
121 '';
122
123 initPhase = ''
124 mkdir -p $out/nix-support
125 echo "$system" > $out/nix-support/system
126
127 if [ -z "${toString doingAnalysis}" ]; then
128 for i in $(getAllOutputNames); do
129 if [ "$i" = out ]; then j=none; else j="$i"; fi
130 mkdir -p ''${!i}/nix-support
131 echo "nix-build $j ''${!i}" >> ''${!i}/nix-support/hydra-build-products
132 done
133 fi
134 '';
135
136 prePhases = [ "initPhase" ] ++ prePhases;
137
138 buildInputs =
139 buildInputs
140 ++ (lib.optional doCoverageAnalysis args.makeGCOVReport)
141 ++ (lib.optional doClangAnalysis args.clang-analyzer)
142 ++ (lib.optional doCoverityAnalysis args.cov-build)
143 ++ (lib.optional doCoverityAnalysis args.xz);
144
145 lcovFilter = [ "${builtins.storeDir}/*" ] ++ lcovFilter;
146
147 inherit lcovExtraTraceFiles;
148
149 postPhases = postPhases ++ [ "finalPhase" ];
150
151 meta = (lib.optionalAttrs (args ? meta) args.meta) // {
152 description =
153 if doCoverageAnalysis then "Coverage analysis" else "Nix package for ${stdenv.hostPlatform.system}";
154 };
155
156 }
157
158 //
159
160 (lib.optionalAttrs buildOutOfSourceTree {
161 preConfigure =
162 # Build out of source tree and make the source tree read-only. This
163 # helps catch violations of the GNU Coding Standards (info
164 # "(standards) Configuration"), like `make distcheck' does.
165 ''
166 mkdir "../build"
167 cd "../build"
168 configureScript="../$sourceRoot/configure"
169 chmod -R a-w "../$sourceRoot"
170
171 echo "building out of source tree, from \`$PWD'..."
172
173 ${lib.optionalString (preConfigure != null) preConfigure}
174 '';
175 })
176)