This file is indexed.

/usr/bin/build-rdeps is in devscripts 2.16.2ubuntu3.

This file is owned by root:root, with mode 0o755.

The actual contents of the file can be viewed below.

  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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
#!/usr/bin/perl
# -*- tab-width: 8; indent-tabs-mode: t; cperl-indent-level: 4 -*-
# vim: set shiftwidth=4 tabstop=8 noexpandtab:
#   Copyright (C) Patrick Schoenfeld
#                 2015 Johannes Schauer <josch@debian.org>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

=head1 NAME

build-rdeps - find packages that depend on a specific package to build (reverse build depends)

=head1 SYNOPSIS

B<build-rdeps> I<package>

=head1 DESCRIPTION

B<build-rdeps> searches for all packages that build-depend on the specified package.

The default behaviour is to just `grep` for the given dependency in the
Build-Depends field of apt's Sources files.

If the package dose-extra >= 4.0 is installed, then a more complete reverse
build dependency computation is carried out. In particular, with that package
installed, build-rdeps will find transitive reverse dependencies, respect
architecture and build profile restrictions, take Provides relationships,
Conflicts, Pre-Depends, Build-Depends-Arch and versioned dependencies into
account and correctly resolve multiarch relationships for crossbuild reverse
dependency resolution.  (This tends to be a slow process due to the complexity
of the package interdependencies.)

=head1 OPTIONS

=over 4

=item B<-u>, B<--update>

Run apt-get update before searching for build-depends.

=item B<-s>, B<--sudo>

Use sudo when running apt-get update. Has no effect if -u is omitted.

=item B<--distribution>

Select another distribution, which is searched for build-depends.

=item B<--only-main>

Ignore contrib and non-free

=item B<--exclude-component>

Ignore the given component (e.g. main, contrib, non-free).

=item B<--origin>

Restrict the search to only the specified origin (such as "Debian").

=item B<-m>, B<--print-maintainer>

Print the value of the maintainer field for each package.

=item B<--host-arch>

Explicitly set the host architecture. The default is the value of
`dpkg-architecture -qDEB_HOST_ARCH`. This option only works if dose-extra >=
4.0 is installed.

=item B<--old>

Force the old simple behaviour without dose-ceve support even if dose-extra >=
4.0 is installed.  (This tends to be faster.)

Notice, that the old behaviour only finds direct dependencies, ignores virtual
dependencies, does not find transitive dependencies and does not take version
relationships, architecture restrictions, build profiles or multiarch
relationships into account.

=item B<--build-arch>

Explicitly set the build architecture. The default is the value of
`dpkg-architecture -qDEB_BUILD_ARCH`. This option only works if dose-extra >=
4.0 is installed.

=item B<-d>, B<--debug>

Run the debug mode

=item B<--help>

Show the usage information.

=item B<--version>

Show the version information.

=back

=head1 REQUIREMENTS

The tool requires apt Sources files to be around for the checked components.
In the default case this means that in /var/lib/apt/lists files need to be
around for main, contrib and non-free.

In practice this means one needs to add one deb-src line for each component,
e.g.

deb-src http://<mirror>/debian <dist> main contrib non-free

and run apt-get update afterwards or use the update option of this tool.

=cut

use warnings;
use strict;
use File::Basename;
use Getopt::Long qw(:config gnu_getopt);
use Pod::Usage;
use Data::Dumper;
my $progname = basename($0);
my $version = '1.0';
my $dctrl = "grep-dctrl";
my $sources_path = "/var/lib/apt/lists/";
my $release_pattern = '(.*_dists_(sid|unstable))_(?:In)*Release$';
my $use_ceve = 0;
my $ceve_compatible;
my %seen_origins;
my @source_files;
my $opt_debug;
my $opt_update;
my $opt_sudo;
my $opt_maintainer;
my $opt_mainonly;
my $opt_distribution;
my $opt_origin = 'Debian';
my @opt_exclude_components;
my $opt_buildarch;
my $opt_hostarch;
my $opt_without_ceve;

if (system('command -v grep-dctrl >/dev/null 2>&1')) {
    die "$progname: Fatal error. grep-dctrl is not available.\nPlease install the 'dctrl-tools' package.\n";
}

sub version {
    print <<"EOT";
This is $progname $version, from the Debian devscripts package, v. 2.16.2ubuntu3
This code is copyright by Patrick Schoenfeld, all rights reserved.
It comes with ABSOLUTELY NO WARRANTY. You are free to redistribute this code
under the terms of the GNU General Public License, version 2 or later.
EOT
exit (0);
}

sub usage {
    print <<"EOT";
usage: $progname packagename
       $progname --help
       $progname --version

Searches for all packages that build-depend on the specified package.

Options:
   -u, --update                   Run apt-get update before searching for build-depends.
                                  (needs root privileges)
   -s, --sudo                     Use sudo when running apt-get update
                                  (has no effect when -u is omitted)
   -d, --debug                    Enable the debug mode
   -m, --print-maintainer         Print the maintainer information (experimental)
   --distribution distribution    Select a distribution to search for build-depends
                                  (Default: unstable)
   --origin origin                Select an origin to search for build-depends
                                  (Default: Debian)
   --only-main                    Ignore contrib and non-free
   --exclude-component COMPONENT  Ignore the specified component (can be given multiple times)
   --host-arch                    Set the host architecture (requires dose-extra >= 4.0)
   --build-arch                   Set the build architecture (requires dose-extra >= 4.0)
   --old                          Use the old simple reverse dependency resolution

EOT
version;
}

sub test_ceve {
    return $ceve_compatible if defined $ceve_compatible;

    # test if the debsrc input and output format is supported by the installed
    # ceve version
    system('dose-ceve -T debsrc debsrc:///dev/null > /dev/null 2>&1');
    if ($? == -1) {
	print STDERR "DEBUG: dose-ceve cannot be executed: $!\n" if ($opt_debug);
	$ceve_compatible = 0;
    } elsif ($? == 0) {
	$ceve_compatible = 1;
    } else {
	print STDERR "DEBUG: dose-ceve is too old\n" if ($opt_debug);
	$ceve_compatible = 0;
    }
    return $ceve_compatible;
}

# Sub to test if a given section shall be included in the result
sub test_for_valid_component {
    my $filebase = shift;

    if ($opt_mainonly and $filebase =~ /(contrib|non-free)/) {
	return -1;
    }
    foreach my $component (@opt_exclude_components) {
	if ($filebase =~ /$component/) {
	    return -1;
	}
    }

    if (! -e "$sources_path/$filebase") {
	print STDERR "Warning: Ignoring missing sources file $filebase. (Missing component in sources.list?)\n";
	return -1;
    }

    if ($use_ceve) {
	die "build arch undefined" if ! defined $opt_buildarch;
	die "host arch undefined" if ! defined $opt_hostarch;
	my $packages_path = "$sources_path/$filebase";
	if ($filebase !~ /_source_Sources$/) {
	    print STDERR "Warning: Ignoring sources file $filebase because of unexpected postfix\n";
	    return -1;
	}
	$packages_path =~ s/_source_Sources$/_binary-${opt_buildarch}_Packages/;
	if (! -e $packages_path) {
	    print STDERR "Warning: Ignoring sources file $filebase because no corresponding buildarch Packages file for $opt_buildarch was found (required for ceve)\n";
	    return -1;
	}
	if ($opt_buildarch ne $opt_hostarch) {
	    $packages_path =~ s/_source_Sources$/_binary-${opt_hostarch}_Packages/;
	    if (! -e $packages_path) {
		print STDERR "Warning: Ignoring sources file $filebase because no corresponding buildarch Packages file for $opt_hostarch was found (required for ceve)\n";
		return -1;
	    }
	}
    }

    print STDERR "DEBUG: Component ($filebase) may not be excluded.\n" if ($opt_debug);
    return 0;
}

# Scan Release files and add appropriate Sources files
sub readrelease {
    my ($file, $base) = @_;
    open(RELEASE, '<', "$sources_path/$file");
    while (<RELEASE>) {
	if (/^Origin:\s*(.+)\s*$/) {
	    my $origin = $1;
	    # skip undesired (non-specified or already seen) origins
	    if (($opt_origin && $origin !~ /^\s*\Q$opt_origin\E\s*$/)
	        || $seen_origins{$origin}) {
		last;
	    }
	    $seen_origins{$origin} = 1;
	}
	elsif (/^(?:MD5|SHA)\w+:/) {
	    # from a list of checksums, grab names of Sources files
	    while (<RELEASE>) {
		last unless /^ /;
		if (/([^ ]+\/Sources)$/) {
		    addsources($base, $1);
		}
	    }
	    last;
	}
    }
    close(RELEASE);
}

# Add a *_Sources file if test_for_valid_component likes it
sub addsources {
    my ($base, $filename) = @_;
    # main/source/Sources
    $filename =~ s/\//_/g;
    # -> ftp.debian.org_..._main_source_Sources
    $filename = "${base}_${filename}";
    if (test_for_valid_component($filename) == 0) {
	push(@source_files, $filename);
	print STDERR "DEBUG: Added source file: $filename\n" if ($opt_debug);
    }
}

sub findreversebuilddeps {
    my ($package, $source_file) = @_;
    my $count=0;

    if ($use_ceve) {
	die "build arch undefined" if ! defined $opt_buildarch;
	die "host arch undefined" if ! defined $opt_hostarch;

	(my $buildarch_file = $source_file) =~ s/_source_Sources$/_binary-${opt_buildarch}_Packages/;

	my @ceve_cmd = ('dose-ceve', '-T', 'debsrc', '-r', $package, '-G', 'pkg',
	    "--deb-native-arch=$opt_buildarch", "deb://$buildarch_file", "debsrc://$source_file");
	if ($opt_buildarch ne $opt_hostarch) {
	    (my $hostarch_file = $source_file) =~ s/_source_Sources(\.\w+)?$/_binary-${opt_hostarch}_Packages$1/;
	    push(@ceve_cmd, "--deb-host-arch=$opt_hostarch", "deb://$hostarch_file");
	}
	my %sources;
	print STDERR 'DEBUG: executing: '.join(' ', @ceve_cmd) if ($opt_debug);
	open(SOURCES, '-|', @ceve_cmd);
	while(<SOURCES>) {
	    next unless s/^Package:\s+//;
	    chomp;
	    $sources{$_} = 1;
	}
	for my $source (sort keys %sources)
	{
	    print $source;
	    if ($opt_maintainer) {
		my $maintainer = `apt-cache showsrc $source | grep-dctrl -n -s Maintainer '' | sort -u`;
		print " ($maintainer)";
	    }
	    print "\n";
	    $count += 1;
	}
    } else {
	my %packages;
	my $depending_package;
	open(PACKAGES, '-|', $dctrl, '-r', '-F', 'Build-Depends,Build-Depends-Indep', "\\(^\\|, \\)$package", '-s', 'Package,Build-Depends,Build-Depends-Indep,Maintainer', $source_file);

	while(<PACKAGES>) {
	    chomp;
	    print STDERR "$_\n" if ($opt_debug);
	    if (/Package: (.*)$/) {
		$depending_package = $1;
		$packages{$depending_package}->{'Build-Depends'} = 0;
	    }
	    elsif (/Maintainer: (.*)$/) {
		if ($depending_package) {
		    $packages{$depending_package}->{'Maintainer'} = $1;
		}
	    }
	    elsif (/Build-Depends: (.*)$/ or /Build-Depends-Indep: (.*)$/) {
		if ($depending_package) {
		    print STDERR "$1\n" if ($opt_debug);
		    if ($1 =~ /^(.*\s)?\Q$package\E(?::[a-zA-Z0-9][a-zA-Z0-9-]*)?([\s,]|$)/) {
			$packages{$depending_package}->{'Build-Depends'} = 1;
		    }
		}
	    }
	}

	while($depending_package = each(%packages)) {
	    if ($packages{$depending_package}->{'Build-Depends'} != 1) {
		print STDERR "Ignoring package $depending_package because its not really build depending on $package.\n" if ($opt_debug);
		next;
	    }
	    print $depending_package;
	    if ($opt_maintainer) {
		print " ($packages{$depending_package}->{'Maintainer'})";
	    }
	    print "\n";
	    $count+=1;
	}
    }

    if ($count == 0) {
	print "No reverse build-depends found for $package.\n\n"
    }
    else {
	print "\nFound a total of $count reverse build-depend(s) for $package.\n\n";
    }
}

if ($#ARGV < 0) { usage; exit(0); }


GetOptions(
    "u|update" => \$opt_update,
    "s|sudo" => \$opt_sudo,
    "m|print-maintainer" => \$opt_maintainer,
    "distribution=s" => \$opt_distribution,
    "only-main" => \$opt_mainonly,
    "exclude-component=s" => \@opt_exclude_components,
    "origin=s" => \$opt_origin,
    "host-arch=s" => \$opt_hostarch,
    "build-arch=s" => \$opt_buildarch,
#   "profiles=s" => \$opt_profiles, # FIXME: add build profile support
#                                            once dose-ceve has a
#                                            --deb-profiles option
    "old" => \$opt_without_ceve,
    "d|debug" => \$opt_debug,
    "h|help" => sub { usage; },
    "v|version" => sub { version; }
) or do { usage; exit 1; };

my $package = shift;

if (!$package) {
    die "$progname: missing argument. expecting packagename\n";
}

print STDERR "DEBUG: Package => $package\n" if ($opt_debug);

if ($opt_hostarch) {
    if ($opt_without_ceve) {
	die "$progname: the --host-arch option cannot be used together with --old\n";
    }
    if (test_ceve()) {
	$use_ceve = 1;
    } else {
	die "$progname: the --host-arch option requires dose-extra >= 4.0 to be installed\n";
    }
}

if ($opt_buildarch) {
    if ($opt_without_ceve) {
	die "$progname: the --build-arch option cannot be used together with --old\n";
    }
    if (test_ceve()) {
	$use_ceve = 1;
    } else {
	die "$progname: the --build-arch option requires dose-extra >= 4.0 to be installed\n";
    }
}

# if ceve usage has not been activated yet, check if it can be activated
if (!$use_ceve and !$opt_without_ceve) {
    if (test_ceve()) {
	$use_ceve = 1;
    } else {
	print STDERR "WARNING: dose-extra >= 4.0 is not installed. Falling back to old unreliable behaviour.\n";
    }
}

if ($use_ceve) {
    # set hostarch and buildarch if they have not been set yet
    if (!$opt_hostarch) {
	$opt_hostarch = `dpkg-architecture --query DEB_HOST_ARCH`;
	chomp $opt_hostarch;
    }
    if (!$opt_buildarch) {
	$opt_buildarch = `dpkg-architecture --query DEB_BUILD_ARCH`;
	chomp $opt_buildarch;
    }
    print STDERR "DEBUG: running with dose-ceve resolver\n" if ($opt_debug);
    print STDERR "DEBUG: buildarch=$opt_buildarch hostarch=$opt_hostarch\n" if ($opt_debug);
} else {
    print STDERR "DEBUG: running with old resolver\n" if ($opt_debug);
}

if ($opt_update) {
    print STDERR "DEBUG: Updating apt-cache before search\n" if ($opt_debug);
    my @cmd;
    if ($opt_sudo) {
	print STDERR "DEBUG: Using sudo to become root\n" if ($opt_debug);
	push(@cmd, 'sudo');
    }
    push(@cmd, 'apt-get', 'update');
    system @cmd;
}

if ($opt_distribution) {
    print STDERR "DEBUG: Setting distribution to $opt_distribution\n" if ($opt_debug);
    $release_pattern = '(.*_dists_' . $opt_distribution . ')_(?:In)*Release$';
}

# Find sources files
chdir($sources_path);
for my $release_file (glob "*") {
    readrelease($release_file, $1) if $release_file =~ /$release_pattern/;
}

if (!@source_files) {
    die "$progname: unable to find sources files.\nDid you forget to run apt-get update (or add --update to this command)?";
}

foreach my $source_file (@source_files) {
    if ($source_file =~ /main/) {
	print "Reverse Build-depends in main:\n";
	print "------------------------------\n\n";
	findreversebuilddeps($package, "$sources_path/$source_file");
    }

    if ($source_file =~ /contrib/) {
	print "Reverse Build-depends in contrib:\n";
	print "---------------------------------\n\n";
	findreversebuilddeps($package, "$sources_path/$source_file");
    }

    if ($source_file =~ /non-free/) {
	print "Reverse Build-depends in non-free:\n";
	print "----------------------------------\n\n";
	findreversebuilddeps($package, "$sources_path/$source_file");
    }
}

=head1 LICENSE

This code is copyright by Patrick Schoenfeld
<schoenfeld@debian.org>, all rights reserved.
This program comes with ABSOLUTELEY NO WARRANTY.
You are free to redistribute this code under the terms of the
GNU General Public License, version 2 or later.

=head1 AUTHOR

Patrick Schoenfeld <schoenfeld@debian.org>

=cut