File: dh_buildinfo

package info (click to toggle)
dh-buildinfo 0.11+nmu2
  • links: PTS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 124 kB
  • sloc: perl: 685; makefile: 49; sh: 24
file content (382 lines) | stat: -rwxr-xr-x 9,697 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
#!/usr/bin/perl -w

=head1 NAME

dh_buildinfo - register the versions of build-dependencies used to build a package

=cut

# TODO:
# - use cleaner APIs
# - list "also used by" packages after "pulled by" ?

use Data::Dumper;

BEGIN {
  my ($mydir,$me) = $0=~m:(.*)/(.+):;
  push @INC, $mydir;
}
use strict;
use Debian::Debhelper::Dh_Lib;
use BuildDeps;

my $hostarch = Debian::Debhelper::Dh_Lib::hostarch();

=head1 SYNOPSIS

B<dh_buildinfo> [S<I<debhelper options>>] [S<I<generate>>] [S<I<cat>>] [S<I<install>>]

=head1 DESCRIPTION

dh_buildinfo is a debhelper program that registers in a file the list
of packages declared as build-time dependencies, as well as
build-essential packages, together with their versions, as installed
in the build machine.

This will hopefully help to track packages (auto-)built with package
versions which are known to be buggy, and, more generally, to find out
whether a package needs to be rebuilt because of a significant change
in a package it has a build-time dependency on.

You will usually just run this in your package's binary rule. If you
need extra control over the operation, you can specify the actions to
be taken. The default actions are I<generate> I<install>.

=head1 OPTIONS

=over 4

=item I<generate>

Generate the list of versions of the build dependencies in use.

=item I<cat>

Output the list of versions to I<stdout>.

=item I<install>

Installs the compressed list of versions to the package's build directory, in
/usr/share/doc/package/buildinfo.gz

=back

=cut

# This part could be replaced. Silly little status file parser.
# thanks to Matt Zimmerman. Returns two hash references that
# are exactly what the other functions need...
sub parse_status {
	my $status=shift || "/var/lib/dpkg/status";
	
	my %providers;
	my %depends;
	my %version;
	local $/ = '';
	open(STATUS, "<$status") || die "$status: $!\n";
	while (<STATUS>) {
		next unless /^Status: .*ok installed$/m;

                # do not include foreign archs
                next unless /^Architecture: ($hostarch|all)$/m;

		my ($package) = /^Package: (.*)$/m;
		push @{$providers{$package}}, $package;
		($version{$package}) = /^Version: (.*)$/m;
	
		if (/^Provides: (.*)$/m) {
			foreach (split(/,\s*/, $1)) {
				push @{$providers{$_}}, $package;
			}
		}
	
		if (/^(?:Pre-)?Depends: (.*)$/m) {
			foreach (split(/,\s*/, $1)) {
				push @{$depends{$package}}, $_;
			}
		}
	}
	close STATUS;

	return \%version, \%providers, \%depends;
}

# for each package, the packages which cause it to be listed
my %causes;

# Compute the closure in the depends graph of a
# list of packages
#
# $1: arayref - list of packages to process
# $2: hashref to %depends
# $3: hashref for packages to exclude from the list
#     (presumably already listed)
# $4: internal hashref for recursion
#     (should be undef on external calls)
sub deps_closure {
  my @pkgs = @{shift()};
  my %depends = %{shift()};
  my $excludes = shift;
  $excludes = {} unless defined $excludes;
  my $closure = shift;
  my $firstiteration;
  if (defined $closure) {
    $firstiteration = 0;
  } else {
    $firstiteration = 1;
    $closure = {};
  }
  my @unseen;

  foreach my $pkg (@pkgs) {
    # we don't want the initial packages in the final listing
    $excludes->{$pkg} = 1 if $firstiteration;
    #
    if ($firstiteration or
	(! defined $closure->{$pkg} and ! defined $excludes->{$pkg})) {
      push @unseen, $pkg;
    }
    $closure->{$pkg} = 1 unless $firstiteration or defined $excludes->{$pkg};
  }
  foreach my $unseen (@unseen) {
    my @unseendeps = defined $depends{$unseen} ? @{$depends{$unseen}} : ();
    my @cleanunseendeps;
    foreach my $unseendep (@unseendeps) {
      my @split = split /\s*\|\s*/, $unseendep;
      foreach my $split (@split) {
	$split =~ s/\s*\(.*//;
	push @cleanunseendeps, $split;
	# record cause
	$causes{$split}->{$unseen} = 1;
      }
    }
    deps_closure (\@cleanunseendeps, \%depends, $excludes, $closure);
  }

  return keys %{$closure};
}

sub add_to_closure {
  my ($pkgs, $closurehash) = @_;

  foreach my $pkg (@{$pkgs}) {
    $closurehash->{$pkg} = 1;
  }
}

sub pkgformat {
  my $pkgs = shift;
  my $listcauses = shift;
  my @vers = BuildDeps::pkginfo($pkgs, @_);
  my $str = '';

  foreach my $ver (@vers) {
    $str .= sprintf "%-40s %s\n", $ver->[0], $ver->[1];
    $str .= sprintf ("  provides: %s\n", $ver->[2]) if $#$ver >= 2;
    if ($listcauses) {
      my $pkg;
      if ($#$ver >= 2) {
	$pkg = $ver->[2];
      } else {
	$pkg = $ver->[0];
      }
      $str .= sprintf "  pulled by: %s\n", join (', ', sort keys %{$causes{$pkg}})
	if defined $causes{$pkg};
    }
  }
  return $str;
}

my $buildinfo;

sub compute_buildinfo {
  my @status=parse_status();
  my %depends=%{pop @status};
  push (@status,
	1,			# $dump_met
       );
  my $excludes={};

  $buildinfo =
    " *** Build information ***";

  #
  # get list of essential files
  #

  # parse list file
  open (RAWFILE, '/usr/share/build-essential/essential-packages-list')
    or error("cannot read /usr/share/build-essential/essential-packages-list: $!\n");
  my @essentials = <RAWFILE>;
  close RAWFILE;
  chomp @essentials;
  while (shift @essentials ne '') {
  }
  ;
  @essentials = sort @essentials;

  # get output in the same format as build-essential and explicit build-deps
  #@essentials = BuildDeps::depends(join (', ', @essentials), @status);

  # closure
  my @essentialsclosure = deps_closure(\@essentials, \%depends, $excludes);
  @essentialsclosure = sort @essentialsclosure;
  add_to_closure(\@essentialsclosure, $excludes);

  # record
  $buildinfo .=
    "\n\n Essential packages:\n\n" .
      pkgformat (\@essentials, 0, @status) .
	"\n\n Essential packages closure:\n\n" .
	  pkgformat (\@essentialsclosure, 1, @status);


  #
  # get list of build-essential files
  #

  # get a build-dep like expression from list file
  open (RAWFILE, "/usr/share/build-essential/list")
    or error("cannot read /usr/share/build-essential/list: $!\n");
  my $started = 0;
  my $bestring;
  while (<RAWFILE>) {
    chomp;
    last if $_ eq 'END LIST OF PACKAGES';
    next if /^\s/ or $_ eq '';

    if ($started) {
      $bestring .= ', ' if defined $bestring;
      $bestring .= $_;
    }

    $started = 1 if $_ eq 'BEGIN LIST OF PACKAGES';
  }

  # have the expression parsed
  my @buildessentials = BuildDeps::depends($bestring, @status);
  @buildessentials = sort @buildessentials;

  # closure
  my @buildessentialsclosure = deps_closure(\@buildessentials, \%depends, $excludes);
  @buildessentialsclosure = sort @buildessentialsclosure;
  add_to_closure (\@buildessentialsclosure, $excludes);

  # record
  $buildinfo .=
    "\n\n Build-Essential packages:\n\n" .
      pkgformat (\@buildessentials, 0, @status) .
	"\n\n Build-Essential closure:\n\n" .
	  pkgformat (\@buildessentialsclosure, 1, @status);


  #
  # get explicit arch-indep build-dependencies
  #

  my @builddepsindep;
  my %fields = BuildDeps::parse_control ('debian/control');
  if (defined $fields{'Build-Depends-Indep'}) {
    @builddepsindep = BuildDeps::depends($fields{'Build-Depends-Indep'}, @status);
    @builddepsindep = sort @builddepsindep;
  }

  # closure
  my @builddepsindepclosure = deps_closure(\@builddepsindep, \%depends, $excludes);
  @builddepsindepclosure = sort @builddepsindepclosure;
  add_to_closure (\@builddepsindepclosure, $excludes);

  # record
  $buildinfo .=
    "\n\n Declared Arch-indep Build-Dependencies:\n\n" .
      pkgformat (\@builddepsindep, 0, @status) .
	"\n\n Arch-indep Build-Dependencies closure:\n\n" .
	  pkgformat (\@builddepsindepclosure, 1, @status);


  #
  # get explicit build-dependencies
  #

  my @builddeps;
  %fields = BuildDeps::parse_control ('debian/control');
  if (defined $fields{'Build-Depends'}) {
    @builddeps = BuildDeps::depends($fields{'Build-Depends'}, @status);
    @builddeps = sort @builddeps;
  }

  # closure
  my @builddepsclosure = deps_closure(\@builddeps, \%depends, $excludes);
  @builddepsclosure = sort @builddepsclosure;
  #add_to_closure (\@builddepsclosure, $excludes);

  # record
  $buildinfo .=
    "\n\n Declared Arch-dependent Build-Dependencies:\n\n" .
      pkgformat (\@builddeps, 0, @status) .
	"\n\n Arch-dependent Build-Dependencies closure:\n\n" .
	  pkgformat (\@builddepsclosure, 1, @status);
}

sub generate_buildinfo {
  open BUILDINFO, ">debian/buildinfo";
  print BUILDINFO $buildinfo;
  close BUILDINFO;
}

sub install_buildinfo {
  complex_doit("gzip -9nf debian/buildinfo >debian/buildinfo.gz");
  foreach my $package (@{$dh{DOPACKAGES}}) {
    # policy forbids doc files in udebs, comply even if we're not
    # really a docfile
    next if is_udeb($package);

    my $tmp=tmpdir($package);
    my $arch=package_arch($package);

    # If this is a symlink, leave it alone.
    if ( ! -d "$tmp/usr/share/doc/$package" &&
	 ! -l "$tmp/usr/share/doc/$package") {
      install_dir("$tmp/usr/share/doc/$package");
    }

    if ( ! -l "$tmp/usr/share/doc/$package") {
      install_file("debian/buildinfo.gz","$tmp/usr/share/doc/$package/buildinfo_$arch.gz");
    }

  }
  doit("rm","debian/buildinfo.gz");
}

my @commands;

sub ensure_buildinfo {
  if (!-r 'debian/buildinfo' and ! grep { $_ eq 'generate' } @commands) {
    compute_buildinfo();
    generate_buildinfo();
  }
}

#
# The real work
#

init();				# debhelper stuff

@commands = @ARGV;
@commands = ('generate', 'install') unless scalar @commands;

if (grep { $_ eq 'generate' } @commands) {
  compute_buildinfo();
  generate_buildinfo();
}

if (grep { $_ eq 'cat' } @commands) {
  ensure_buildinfo;
  doit ("cat","debian/buildinfo");
}

if (grep { $_ eq 'install' } @commands) {
  ensure_buildinfo;
  install_buildinfo();
}