dpkg-gencontrol.pl 13 KB

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