5 # Copyright © 1996 Ian Jackson
6 # Copyright © 2000,2002 Wichert Akkerman
7 # Copyright © 2006-2015 Guillem Jover <guillem@debian.org>
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.
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.
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/>.
25 use List
::Util
qw(none);
26 use POSIX
qw(:errno_h :fcntl_h);
31 use Dpkg
::ErrorHandling
;
33 use Dpkg
::Arch
qw(get_host_arch debarch_eq debarch_is debarch_list_parse);
34 use Dpkg
::BuildProfiles
qw(get_build_profiles);
37 use Dpkg
::Control
::Info
;
38 use Dpkg
::Control
::Fields
;
41 use Dpkg
::Changelog
::Parse
;
42 use Dpkg
::Dist
::Files
;
44 textdomain
('dpkg-dev');
47 my $controlfile = 'debian/control';
48 my $changelogfile = 'debian/changelog';
50 my $fileslistfile = 'debian/files';
51 my $packagebuilddir = 'debian/tmp';
62 my $substvars = Dpkg
::Substvars
->new();
63 my $substvars_loaded = 0;
67 printf g_
("Debian %s version %s.\n"), $Dpkg::PROGNAME
, $Dpkg::PROGVERSION
;
70 This is free software; see the GNU General Public License version 2 or
71 later for copying conditions. There is NO warranty.
77 'Usage: %s [<option>...]')
80 -p<package> print control file for package.
81 -c<control-file> get control info from this file.
82 -l<changelog-file> get per-version info from this file.
83 -F<changelog-format> force changelog format.
84 -v<force-version> set version of binary package.
85 -f<files-list-file> write files here instead of debian/files.
86 -P<package-build-dir> temporary build directory instead of debian/tmp.
87 -n<filename> assume the package filename will be <filename>.
88 -O[<file>] write to stdout (or <file>), not .../DEBIAN/control.
89 -is, -ip, -isp, -ips deprecated, ignored for compatibility.
90 -D<field>=<value> override or add a field and value.
91 -U<field> remove a field.
92 -V<name>=<value> set a substitution variable.
93 -T<substvars-file> read variables here, not debian/substvars.
94 -?, --help show this help message.
95 --version show the version.
102 $oppackage = ${^POSTMATCH
};
103 my $err = pkg_name_is_illegal
($oppackage);
104 error
(g_
("illegal package name '%s': %s"), $oppackage, $err) if $err;
106 $controlfile = ${^POSTMATCH
};
108 $changelogfile = ${^POSTMATCH
};
110 $packagebuilddir = ${^POSTMATCH
};
112 $fileslistfile = ${^POSTMATCH
};
113 } elsif (m/^-v(.+)$/) {
117 } elsif (m/^-O(.+)$/) {
119 } elsif (m/^-i[sp][sp]?$/) {
120 warning
(g_
('%s is deprecated; it is without effect'), $_);
121 } elsif (m/^-F([0-9a-z]+)$/) {
122 $changelogformat = $1;
123 } elsif (m/^-D([^\=:]+)[=:]/p) {
124 $override{$1} = ${^POSTMATCH
};
125 } elsif (m/^-U([^\=:]+)$/) {
127 } elsif (m/^-V(\w[-:0-9A-Za-z]*)[=:]/p) {
128 $substvars->set_as_used($1, ${^POSTMATCH
});
129 } elsif (m/^-T(.*)$/) {
130 $substvars->load($1) if -e
$1;
131 $substvars_loaded = 1;
133 $forcefilename = ${^POSTMATCH
};
134 } elsif (m/^-(?:\?|-help)$/) {
137 } elsif (m/^--version$/) {
141 usageerr
(g_
("unknown option '%s'"), $_);
145 umask 0022; # ensure sane default permissions for created files
146 my %options = (file
=> $changelogfile);
147 $options{changelogformat
} = $changelogformat if $changelogformat;
148 my $changelog = changelog_parse
(%options);
149 if ($changelog->{'Binary-Only'}) {
151 $options{offset
} = 1;
152 my $prev_changelog = changelog_parse
(%options);
153 $sourceversion = $prev_changelog->{'Version'};
155 $sourceversion = $changelog->{'Version'};
158 if (defined $forceversion) {
159 $binaryversion = $forceversion;
161 $binaryversion = $changelog->{'Version'};
164 $substvars->set_version_substvars($sourceversion, $binaryversion);
165 $substvars->set_vendor_substvars();
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
);
171 # Old-style bin-nmus change the source version submitted to
172 # set_version_substvars()
173 $sourceversion = $substvars->get('source:Version');
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)
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)'),
190 $pkg = $control->get_pkg_by_idx(1);
192 $substvars->set_msg_prefix(sprintf(g_
('package %s: '), $pkg->{Package
}));
194 # Scan source package
195 my $src_fields = $control->get_source();
196 foreach my $f (keys %{$src_fields}) {
197 if ($f eq 'Source') {
198 set_source_name
($src_fields->{$f});
199 } elsif ($f eq 'Description') {
200 # Description in binary packages is not inherited, do not copy this
201 # field, only initialize the description substvars.
202 $substvars->set_desc_substvars($src_fields->{$f});
204 field_transfer_single
($src_fields, $fields, $f);
207 $substvars->set_field_substvars($src_fields, 'S');
209 # Scan binary package
210 foreach my $f (keys %{$pkg}) {
213 if (field_get_dep_type
($f)) {
214 # Delay the parsing until later
215 } elsif ($f eq 'Architecture') {
216 my $host_arch = get_host_arch
();
218 if (debarch_eq
('all', $v)) {
221 my @archlist = debarch_list_parse
($v, positive
=> 1);
223 if (none
{ debarch_is
($host_arch, $_) } @archlist) {
224 error
(g_
("current host architecture '%s' does not " .
225 "appear in package '%s' architecture list (%s)"),
226 $host_arch, $oppackage, "@archlist");
228 $fields->{$f} = $host_arch;
231 field_transfer_single
($pkg, $fields, $f);
235 # Scan fields of dpkg-parsechangelog
236 foreach my $f (keys %{$changelog}) {
237 my $v = $changelog->{$f};
239 if ($f eq 'Source') {
241 } elsif ($f eq 'Version') {
242 # Already handled previously.
243 } elsif ($f eq 'Maintainer') {
244 # That field must not be copied from changelog even if it's
245 # allowed in the binary package control information
247 field_transfer_single
($changelog, $fields, $f);
251 $fields->{'Version'} = $binaryversion;
253 # Process dependency fields in a second pass, now that substvars have been
256 my $facts = Dpkg
::Deps
::KnownFacts
->new();
257 $facts->add_installed_package($fields->{'Package'}, $fields->{'Version'},
258 $fields->{'Architecture'}, $fields->{'Multi-Arch'});
259 if (exists $pkg->{'Provides'}) {
260 my $provides = deps_parse
($substvars->substvars($pkg->{'Provides'}, no_warn
=> 1),
261 reduce_restrictions
=> 1, virtual
=> 1, union
=> 1);
262 if (defined $provides) {
263 foreach my $subdep ($provides->get_deps()) {
264 if ($subdep->isa('Dpkg::Deps::Simple')) {
265 $facts->add_provided_package($subdep->{package},
266 $subdep->{relation
}, $subdep->{version
},
267 $fields->{'Package'});
274 foreach my $field (field_list_pkg_dep
()) {
275 # Arch: all can't be simplified as the host architecture is not known
276 my $reduce_arch = debarch_eq
('all', $pkg->{Architecture
} || 'all') ?
0 : 1;
277 if (exists $pkg->{$field}) {
279 my $field_value = $substvars->substvars($pkg->{$field},
280 msg_prefix
=> sprintf(g_
('%s field of package %s: '), $field, $pkg->{Package
}));
281 if (field_get_dep_type
($field) eq 'normal') {
282 $dep = deps_parse
($field_value, use_arch
=> 1,
283 reduce_arch
=> $reduce_arch,
284 reduce_profiles
=> 1);
285 error
(g_
("parsing package '%s' %s field: %s"), $oppackage,
286 $field, $field_value) unless defined $dep;
287 $dep->simplify_deps($facts, @seen_deps);
288 # Remember normal deps to simplify even further weaker deps
289 push @seen_deps, $dep;
291 $dep = deps_parse
($field_value, use_arch
=> 1,
292 reduce_arch
=> $reduce_arch,
293 reduce_profiles
=> 1, union
=> 1);
294 error
(g_
("parsing package '%s' %s field: %s"), $oppackage,
295 $field, $field_value) unless defined $dep;
296 $dep->simplify_deps($facts);
299 error
(g_
('the %s field contains an arch-specific dependency but the ' .
300 "package '%s' is architecture all"), $field, $oppackage)
301 if $dep->has_arch_restriction();
302 $fields->{$field} = $dep->output();
303 delete $fields->{$field} unless $fields->{$field}; # Delete empty field
307 for my $f (qw(Package Version Architecture)) {
308 error
(g_
('missing information for output field %s'), $f)
309 unless defined $fields->{$f};
311 for my $f (qw(Maintainer Description)) {
312 warning
(g_
('missing information for output field %s'), $f)
313 unless defined $fields->{$f};
316 my $pkg_type = $pkg->{'Package-Type'} ||
317 $pkg->get_custom_field('Package-Type') || 'deb';
319 if ($pkg_type eq 'udeb') {
320 delete $fields->{'Package-Type'};
321 delete $fields->{'Homepage'};
323 for my $f (qw(Subarchitecture Kernel-Version Installer-Menu-Item)) {
324 warning
(g_
("%s package '%s' with udeb specific field %s"),
325 $pkg_type, $oppackage, $f)
326 if defined($fields->{$f});
330 my $sourcepackage = get_source_name
();
331 my $binarypackage = $override{'Package'} // $fields->{'Package'};
332 my $verdiff = $binaryversion ne $sourceversion;
333 if ($binarypackage ne $sourcepackage || $verdiff) {
334 $fields->{'Source'} = $sourcepackage;
335 $fields->{'Source'} .= ' (' . $sourceversion . ')' if $verdiff;
338 if (!defined($substvars->get('Installed-Size'))) {
339 my $installed_size = 0;
341 my $scan_installed_size = sub {
342 lstat or syserr
(g_
('cannot stat %s'), $File::Find
::name
);
345 my ($dev, $ino, $nlink) = (lstat _
)[0, 1, 3];
347 # For filesystem objects with actual content accumulate the size
349 $installed_size += POSIX
::ceil
((-s _
) / 1024)
350 if not exists $hardlink{"$dev:$ino"};
352 # Track hardlinks to avoid repeated additions.
353 $hardlink{"$dev:$ino"} = 1 if $nlink > 1;
355 # For other filesystem objects assume a minimum 1 KiB baseline,
356 # as directories are shared resources between packages, and other
357 # object types are mainly metadata-only, supposedly consuming
359 $installed_size += 1;
362 find
($scan_installed_size, $packagebuilddir) if -d
$packagebuilddir;
364 $substvars->set_as_auto('Installed-Size', $installed_size);
366 if (defined($substvars->get('Extra-Size'))) {
367 my $size = $substvars->get('Extra-Size') + $substvars->get('Installed-Size');
368 $substvars->set_as_auto('Installed-Size', $size);
370 if (defined($substvars->get('Installed-Size'))) {
371 $fields->{'Installed-Size'} = $substvars->get('Installed-Size');
374 for my $f (keys %override) {
375 $fields->{$f} = $override{$f};
377 for my $f (keys %remove) {
378 delete $fields->{$f};
381 $fields->apply_substvars($substvars);
384 $fields->output(\
*STDOUT
);
386 $outputfile //= "$packagebuilddir/DEBIAN/control";
388 my $sversion = $fields->{'Version'};
389 $sversion =~ s/^\d+://;
390 $forcefilename //= sprintf('%s_%s_%s.%s', $fields->{'Package'}, $sversion,
391 $fields->{'Architecture'}, $pkg_type);
392 my $section = $fields->{'Section'} || '-';
393 my $priority = $fields->{'Priority'} || '-';
395 # Obtain a lock on debian/control to avoid simultaneous updates
396 # of debian/files when parallel building is in use
398 my $lockfile = 'debian/control';
399 $lockfile = $controlfile if not -e
$lockfile;
401 sysopen $lockfh, $lockfile, O_WRONLY
402 or syserr
(g_
('cannot write %s'), $lockfile);
403 file_lock
($lockfh, $lockfile);
405 my $dist = Dpkg
::Dist
::Files
->new();
406 $dist->load($fileslistfile) if -e
$fileslistfile;
408 foreach my $file ($dist->get_files()) {
409 if (defined $file->{package} &&
410 ($file->{package} eq $fields->{'Package'}) &&
411 ($file->{package_type
} eq $pkg_type) &&
412 (debarch_eq
($file->{arch
}, $fields->{'Architecture'}) ||
413 debarch_eq
($file->{arch
}, 'all'))) {
414 $dist->del_file($file->{filename
});
419 $fileattrs{automatic
} = 'yes' if $fields->{'Auto-Built-Package'};
421 $dist->add_file($forcefilename, $section, $priority, %fileattrs);
422 $dist->save("$fileslistfile.new");
424 rename "$fileslistfile.new", $fileslistfile
425 or syserr
(g_
('install new files list file'));
428 close $lockfh or syserr
(g_
('cannot close %s'), $lockfile);
430 $fields->save("$outputfile.new");
432 rename "$outputfile.new", $outputfile
433 or syserr
(g_
("cannot install output control file '%s'"), $outputfile);
436 $substvars->warn_about_unused();