chiark / gitweb /
lib/dpkg/tarfn.c: Kludge `tar_header_decode' to handle spurious `errno'.
[dpkg] / scripts / dpkg-gencontrol.pl
1 #!/usr/bin/perl
2 #
3 # dpkg-gencontrol
4 #
5 # Copyright © 1996 Ian Jackson
6 # Copyright © 2000,2002 Wichert Akkerman
7 # Copyright © 2006-2015 Guillem Jover <guillem@debian.org>
8 #
9 # This program is free software; you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License as published by
11 # the Free Software Foundation; either version 2 of the License, or
12 # (at your option) any later version.
13 #
14 # This program is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 # GNU General Public License for more details.
18 #
19 # You should have received a copy of the GNU General Public License
20 # along with this program.  If not, see <https://www.gnu.org/licenses/>.
21
22 use strict;
23 use warnings;
24
25 use POSIX qw(:errno_h :fcntl_h);
26 use File::Find;
27
28 use Dpkg ();
29 use Dpkg::Gettext;
30 use Dpkg::ErrorHandling;
31 use Dpkg::Util qw(:list);
32 use Dpkg::File;
33 use Dpkg::Arch qw(get_host_arch debarch_eq debarch_is debarch_list_parse);
34 use Dpkg::Package;
35 use Dpkg::BuildProfiles qw(get_build_profiles);
36 use Dpkg::Deps;
37 use Dpkg::Control;
38 use Dpkg::Control::Info;
39 use Dpkg::Control::Fields;
40 use Dpkg::Substvars;
41 use Dpkg::Vars;
42 use Dpkg::Changelog::Parse;
43 use Dpkg::Dist::Files;
44
45 textdomain('dpkg-dev');
46
47
48 my $controlfile = 'debian/control';
49 my $changelogfile = 'debian/changelog';
50 my $changelogformat;
51 my $fileslistfile = 'debian/files';
52 my $packagebuilddir = 'debian/tmp';
53 my $outputfile;
54
55 my $sourceversion;
56 my $binaryversion;
57 my $forceversion;
58 my $forcefilename;
59 my $stdout;
60 my %remove;
61 my %override;
62 my $oppackage;
63 my $substvars = Dpkg::Substvars->new();
64 my $substvars_loaded = 0;
65
66
67 sub version {
68     printf g_("Debian %s version %s.\n"), $Dpkg::PROGNAME, $Dpkg::PROGVERSION;
69
70     printf g_('
71 This is free software; see the GNU General Public License version 2 or
72 later for copying conditions. There is NO warranty.
73 ');
74 }
75
76 sub usage {
77     printf g_(
78 'Usage: %s [<option>...]')
79     . "\n\n" . g_(
80 'Options:
81   -p<package>              print control file for package.
82   -c<control-file>         get control info from this file.
83   -l<changelog-file>       get per-version info from this file.
84   -F<changelog-format>     force changelog format.
85   -v<force-version>        set version of binary package.
86   -f<files-list-file>      write files here instead of debian/files.
87   -P<package-build-dir>    temporary build directory instead of debian/tmp.
88   -n<filename>             assume the package filename will be <filename>.
89   -O[<file>]               write to stdout (or <file>), not .../DEBIAN/control.
90   -is, -ip, -isp, -ips     deprecated, ignored for compatibility.
91   -D<field>=<value>        override or add a field and value.
92   -U<field>                remove a field.
93   -V<name>=<value>         set a substitution variable.
94   -T<substvars-file>       read variables here, not debian/substvars.
95   -?, --help               show this help message.
96       --version            show the version.
97 '), $Dpkg::PROGNAME;
98 }
99
100 while (@ARGV) {
101     $_=shift(@ARGV);
102     if (m/^-p/p) {
103         $oppackage = ${^POSTMATCH};
104         my $err = pkg_name_is_illegal($oppackage);
105         error(g_("illegal package name '%s': %s"), $oppackage, $err) if $err;
106     } elsif (m/^-c/p) {
107         $controlfile = ${^POSTMATCH};
108     } elsif (m/^-l/p) {
109         $changelogfile = ${^POSTMATCH};
110     } elsif (m/^-P/p) {
111         $packagebuilddir = ${^POSTMATCH};
112     } elsif (m/^-f/p) {
113         $fileslistfile = ${^POSTMATCH};
114     } elsif (m/^-v(.+)$/) {
115         $forceversion= $1;
116     } elsif (m/^-O$/) {
117         $stdout= 1;
118     } elsif (m/^-O(.+)$/) {
119         $outputfile = $1;
120     } elsif (m/^-i([sp][sp]?)$/) {
121         warning(g_('-i%s is deprecated; it is without effect'), $1);
122     } elsif (m/^-F([0-9a-z]+)$/) {
123         $changelogformat=$1;
124     } elsif (m/^-D([^\=:]+)[=:]/p) {
125         $override{$1} = ${^POSTMATCH};
126     } elsif (m/^-U([^\=:]+)$/) {
127         $remove{$1}= 1;
128     } elsif (m/^-V(\w[-:0-9A-Za-z]*)[=:]/p) {
129         $substvars->set_as_used($1, ${^POSTMATCH});
130     } elsif (m/^-T(.*)$/) {
131         $substvars->load($1) if -e $1;
132         $substvars_loaded = 1;
133     } elsif (m/^-n/p) {
134         $forcefilename = ${^POSTMATCH};
135     } elsif (m/^-(?:\?|-help)$/) {
136         usage();
137         exit(0);
138     } elsif (m/^--version$/) {
139         version();
140         exit(0);
141     } else {
142         usageerr(g_("unknown option '%s'"), $_);
143     }
144 }
145
146 umask 0022; # ensure sane default permissions for created files
147 my %options = (file => $changelogfile);
148 $options{changelogformat} = $changelogformat if $changelogformat;
149 my $changelog = changelog_parse(%options);
150 if ($changelog->{'Binary-Only'}) {
151     $options{count} = 1;
152     $options{offset} = 1;
153     my $prev_changelog = changelog_parse(%options);
154     $sourceversion = $prev_changelog->{'Version'};
155 } else {
156     $sourceversion = $changelog->{'Version'};
157 }
158
159 if (defined $forceversion) {
160     $binaryversion = $forceversion;
161 } else {
162     $binaryversion = $changelog->{'Version'};
163 }
164
165 $substvars->set_version_substvars($sourceversion, $binaryversion);
166 $substvars->set_arch_substvars();
167 $substvars->load('debian/substvars') if -e 'debian/substvars' and not $substvars_loaded;
168 my $control = Dpkg::Control::Info->new($controlfile);
169 my $fields = Dpkg::Control->new(type => CTRL_PKG_DEB);
170
171 # Old-style bin-nmus change the source version submitted to
172 # set_version_substvars()
173 $sourceversion = $substvars->get('source:Version');
174
175 my $pkg;
176
177 if (defined($oppackage)) {
178     $pkg = $control->get_pkg_by_name($oppackage);
179     if (not defined $pkg) {
180         error(g_('package %s not in control info'), $oppackage)
181     }
182 } else {
183     my @packages = map { $_->{'Package'} } $control->get_packages();
184     if (@packages == 0) {
185         error(g_('no package stanza found in control info'));
186     } elsif (@packages > 1) {
187         error(g_('must specify package since control info has many (%s)'),
188               "@packages");
189     }
190     $pkg = $control->get_pkg_by_idx(1);
191 }
192 $substvars->set_msg_prefix(sprintf(g_('package %s: '), $pkg->{Package}));
193
194 # Scan source package
195 my $src_fields = $control->get_source();
196 foreach (keys %{$src_fields}) {
197     if (m/^Source$/) {
198         set_source_package($src_fields->{$_});
199     } else {
200         field_transfer_single($src_fields, $fields);
201     }
202 }
203 $substvars->set_field_substvars($src_fields, 'S');
204
205 # Scan binary package
206 foreach (keys %{$pkg}) {
207     my $v = $pkg->{$_};
208     if (field_get_dep_type($_)) {
209         # Delay the parsing until later
210     } elsif (m/^Architecture$/) {
211         my $host_arch = get_host_arch();
212
213         if (debarch_eq('all', $v)) {
214             $fields->{$_} = $v;
215         } else {
216             my @archlist = debarch_list_parse($v);
217
218             if (none { debarch_is($host_arch, $_) } @archlist) {
219                 error(g_("current host architecture '%s' does not " .
220                          "appear in package's architecture list (%s)"),
221                       $host_arch, "@archlist");
222             }
223             $fields->{$_} = $host_arch;
224         }
225     } else {
226         field_transfer_single($pkg, $fields);
227     }
228 }
229
230 # Scan fields of dpkg-parsechangelog
231 foreach (keys %{$changelog}) {
232     my $v = $changelog->{$_};
233
234     if (m/^Source$/) {
235         set_source_package($v);
236     } elsif (m/^Version$/) {
237         # Already handled previously.
238     } elsif (m/^Maintainer$/) {
239         # That field must not be copied from changelog even if it's
240         # allowed in the binary package control information
241     } else {
242         field_transfer_single($changelog, $fields);
243     }
244 }
245
246 $fields->{'Version'} = $binaryversion;
247
248 # Process dependency fields in a second pass, now that substvars have been
249 # initialized.
250
251 my $facts = Dpkg::Deps::KnownFacts->new();
252 $facts->add_installed_package($fields->{'Package'}, $fields->{'Version'},
253                               $fields->{'Architecture'}, $fields->{'Multi-Arch'});
254 if (exists $pkg->{'Provides'}) {
255     my $provides = deps_parse($substvars->substvars($pkg->{'Provides'}, no_warn => 1),
256                               reduce_restrictions => 1, union => 1);
257     if (defined $provides) {
258         foreach my $subdep ($provides->get_deps()) {
259             if ($subdep->isa('Dpkg::Deps::Simple')) {
260                 $facts->add_provided_package($subdep->{package},
261                         $subdep->{relation}, $subdep->{version},
262                         $fields->{'Package'});
263             }
264         }
265     }
266 }
267
268 my (@seen_deps);
269 foreach my $field (field_list_pkg_dep()) {
270     # Arch: all can't be simplified as the host architecture is not known
271     my $reduce_arch = debarch_eq('all', $pkg->{Architecture} || 'all') ? 0 : 1;
272     if (exists $pkg->{$field}) {
273         my $dep;
274         my $field_value = $substvars->substvars($pkg->{$field},
275             msg_prefix => sprintf(g_('%s field of package %s: '), $field, $pkg->{Package}));
276         if (field_get_dep_type($field) eq 'normal') {
277             $dep = deps_parse($field_value, use_arch => 1,
278                               reduce_arch => $reduce_arch,
279                               reduce_profiles => 1);
280             error(g_('error occurred while parsing %s field: %s'), $field,
281                   $field_value) unless defined $dep;
282             $dep->simplify_deps($facts, @seen_deps);
283             # Remember normal deps to simplify even further weaker deps
284             push @seen_deps, $dep;
285         } else {
286             $dep = deps_parse($field_value, use_arch => 1,
287                               reduce_arch => $reduce_arch,
288                               reduce_profiles => 1, union => 1);
289             error(g_('error occurred while parsing %s field: %s'), $field,
290                   $field_value) unless defined $dep;
291             $dep->simplify_deps($facts);
292             $dep->sort();
293         }
294         error(g_('the %s field contains an arch-specific dependency but the ' .
295                  'package is architecture all'), $field)
296             if $dep->has_arch_restriction();
297         $fields->{$field} = $dep->output();
298         delete $fields->{$field} unless $fields->{$field}; # Delete empty field
299     }
300 }
301
302 for my $f (qw(Package Version Architecture)) {
303     error(g_('missing information for output field %s'), $f)
304         unless defined $fields->{$f};
305 }
306 for my $f (qw(Maintainer Description)) {
307     warning(g_('missing information for output field %s'), $f)
308         unless defined $fields->{$f};
309 }
310
311 my $pkg_type = $pkg->{'Package-Type'} ||
312                $pkg->get_custom_field('Package-Type') || 'deb';
313
314 if ($pkg_type eq 'udeb') {
315     delete $fields->{'Package-Type'};
316     delete $fields->{'Homepage'};
317 } else {
318     for my $f (qw(Subarchitecture Kernel-Version Installer-Menu-Item)) {
319         warning(g_('%s package with udeb specific field %s'), $pkg_type, $f)
320             if defined($fields->{$f});
321     }
322 }
323
324 my $sourcepackage = get_source_package();
325 my $binarypackage = $override{'Package'} // $fields->{'Package'};
326 my $verdiff = $binaryversion ne $sourceversion;
327 if ($binarypackage ne $sourcepackage || $verdiff) {
328     $fields->{'Source'} = $sourcepackage;
329     $fields->{'Source'} .= ' (' . $sourceversion . ')' if $verdiff;
330 }
331
332 if (!defined($substvars->get('Installed-Size'))) {
333     my $installed_size = 0;
334     my $scan_installed_size = sub {
335         lstat or syserr(g_('cannot stat %s'), $File::Find::name);
336
337         if (-f _ or -l _) {
338             # For filesystem objects with actual content accumulate the size
339             # in 1 KiB units.
340             $installed_size += POSIX::ceil((-s _) / 1024);
341         } else {
342             # For other filesystem objects assume a minimum 1 KiB baseline,
343             # as directories are shared resources between packages, and other
344             # object types are mainly metadata-only, supposedly consuming
345             # at most an inode.
346             $installed_size += 1;
347         }
348     };
349     find($scan_installed_size, $packagebuilddir);
350
351     $substvars->set_as_auto('Installed-Size', $installed_size);
352 }
353 if (defined($substvars->get('Extra-Size'))) {
354     my $size = $substvars->get('Extra-Size') + $substvars->get('Installed-Size');
355     $substvars->set_as_auto('Installed-Size', $size);
356 }
357 if (defined($substvars->get('Installed-Size'))) {
358     $fields->{'Installed-Size'} = $substvars->get('Installed-Size');
359 }
360
361 for my $f (keys %override) {
362     $fields->{$f} = $override{$f};
363 }
364 for my $f (keys %remove) {
365     delete $fields->{$f};
366 }
367
368 $fields->apply_substvars($substvars);
369
370 if ($stdout) {
371     $fields->output(\*STDOUT);
372 } else {
373     $outputfile //= "$packagebuilddir/DEBIAN/control";
374
375     my $sversion = $fields->{'Version'};
376     $sversion =~ s/^\d+://;
377     $forcefilename //= sprintf('%s_%s_%s.%s', $fields->{'Package'}, $sversion,
378                                $fields->{'Architecture'}, $pkg_type);
379     my $section = $fields->{'Section'} || '-';
380     my $priority = $fields->{'Priority'} || '-';
381
382     # Obtain a lock on debian/control to avoid simultaneous updates
383     # of debian/files when parallel building is in use
384     my $lockfh;
385     my $lockfile = 'debian/control';
386     $lockfile = $controlfile if not -e $lockfile;
387
388     sysopen $lockfh, $lockfile, O_WRONLY
389         or syserr(g_('cannot write %s'), $lockfile);
390     file_lock($lockfh, $lockfile);
391
392     my $dist = Dpkg::Dist::Files->new();
393     $dist->load($fileslistfile) if -e $fileslistfile;
394
395     foreach my $file ($dist->get_files()) {
396         if (defined $file->{package} &&
397             ($file->{package} eq $fields->{'Package'}) &&
398             ($file->{package_type} eq $pkg_type) &&
399             (debarch_eq($file->{arch}, $fields->{'Architecture'}) ||
400              debarch_eq($file->{arch}, 'all'))) {
401             $dist->del_file($file->{filename});
402         }
403     }
404
405     $dist->add_file($forcefilename, $section, $priority);
406     $dist->save("$fileslistfile.new");
407
408     rename "$fileslistfile.new", $fileslistfile
409         or syserr(g_('install new files list file'));
410
411     # Release the lock
412     close $lockfh or syserr(g_('cannot close %s'), $lockfile);
413
414     $fields->save("$outputfile.new");
415
416     rename "$outputfile.new", $outputfile
417         or syserr(g_("cannot install output control file '%s'"), $outputfile);
418 }
419
420 $substvars->warn_about_unused();