dpkg-gencontrol.pl 14 KB

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