dpkg-gencontrol.pl 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  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. defined($pkg) || error(_g('package %s not in control info'), $oppackage);
  159. } else {
  160. my @packages = map { $_->{'Package'} } $control->get_packages();
  161. if (@packages == 0) {
  162. error(_g('no package stanza found in control info'));
  163. } elsif (@packages > 1) {
  164. error(_g('must specify package since control info has many (%s)'),
  165. "@packages");
  166. }
  167. $pkg = $control->get_pkg_by_idx(1);
  168. }
  169. $substvars->set_msg_prefix(sprintf(_g('package %s: '), $pkg->{Package}));
  170. # Scan source package
  171. my $src_fields = $control->get_source();
  172. foreach (keys %{$src_fields}) {
  173. if (m/^Source$/) {
  174. set_source_package($src_fields->{$_});
  175. } else {
  176. field_transfer_single($src_fields, $fields);
  177. }
  178. }
  179. # Scan binary package
  180. foreach (keys %{$pkg}) {
  181. my $v = $pkg->{$_};
  182. if (field_get_dep_type($_)) {
  183. # Delay the parsing until later
  184. } elsif (m/^Architecture$/) {
  185. my $host_arch = get_host_arch();
  186. if (debarch_eq('all', $v)) {
  187. $fields->{$_} = $v;
  188. } else {
  189. my @archlist = split(/\s+/, $v);
  190. my @invalid_archs = grep { m/[^\w-]/ } @archlist;
  191. warning(ngettext("`%s' is not a legal architecture string.",
  192. "`%s' are not legal architecture strings.",
  193. scalar(@invalid_archs)),
  194. join("' `", @invalid_archs))
  195. if @invalid_archs >= 1;
  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_arch => 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. error(_g('error occurred while parsing %s field: %s'), $field,
  252. $field_value) unless defined $dep;
  253. $dep->simplify_deps($facts, @seen_deps);
  254. # Remember normal deps to simplify even further weaker deps
  255. push @seen_deps, $dep;
  256. } else {
  257. $dep = deps_parse($field_value, use_arch => 1,
  258. reduce_arch => $reduce_arch, union => 1);
  259. error(_g('error occurred while parsing %s field: %s'), $field,
  260. $field_value) unless defined $dep;
  261. $dep->simplify_deps($facts);
  262. $dep->sort();
  263. }
  264. error(_g('the %s field contains an arch-specific dependency but the ' .
  265. 'package is architecture all'), $field)
  266. if $dep->has_arch_restriction();
  267. $fields->{$field} = $dep->output();
  268. delete $fields->{$field} unless $fields->{$field}; # Delete empty field
  269. }
  270. }
  271. for my $f (qw(Package Version)) {
  272. defined($fields->{$f}) || error(_g('missing information for output field %s'), $f);
  273. }
  274. for my $f (qw(Maintainer Description Architecture)) {
  275. defined($fields->{$f}) || warning(_g('missing information for output field %s'), $f);
  276. }
  277. $oppackage = $fields->{'Package'};
  278. my $pkg_type = $pkg->{'Package-Type'} ||
  279. $pkg->get_custom_field('Package-Type') || 'deb';
  280. if ($pkg_type eq 'udeb') {
  281. delete $fields->{'Package-Type'};
  282. delete $fields->{'Homepage'};
  283. } else {
  284. for my $f (qw(Subarchitecture Kernel-Version Installer-Menu-Item)) {
  285. warning(_g('%s package with udeb specific field %s'), $pkg_type, $f)
  286. if defined($fields->{$f});
  287. }
  288. }
  289. my $sourcepackage = get_source_package();
  290. my $verdiff = $binaryversion ne $sourceversion;
  291. if ($oppackage ne $sourcepackage || $verdiff) {
  292. $fields->{'Source'} = $sourcepackage;
  293. $fields->{'Source'} .= ' (' . $sourceversion . ')' if $verdiff;
  294. }
  295. if (!defined($substvars->get('Installed-Size'))) {
  296. my $du_fh;
  297. defined(my $c = open($du_fh, '-|')) || syserr(_g('cannot fork for %s'), 'du');
  298. if (!$c) {
  299. chdir("$packagebuilddir") ||
  300. syserr(_g("chdir for du to \`%s'"), $packagebuilddir);
  301. exec('du', '-k', '-s', '--apparent-size', '.') or
  302. syserr(_g('unable to execute %s'), 'du');
  303. }
  304. my $duo = '';
  305. while (<$du_fh>) {
  306. $duo .= $_;
  307. }
  308. close($du_fh);
  309. $? && subprocerr(_g("du in \`%s'"), $packagebuilddir);
  310. $duo =~ m/^(\d+)\s+\.$/ ||
  311. error(_g("du gave unexpected output \`%s'"), $duo);
  312. $substvars->set_as_used('Installed-Size', $1);
  313. }
  314. if (defined($substvars->get('Extra-Size'))) {
  315. my $size = $substvars->get('Extra-Size') + $substvars->get('Installed-Size');
  316. $substvars->set_as_used('Installed-Size', $size);
  317. }
  318. if (defined($substvars->get('Installed-Size'))) {
  319. $fields->{'Installed-Size'} = $substvars->get('Installed-Size');
  320. }
  321. for my $f (keys %override) {
  322. $fields->{$f} = $override{$f};
  323. }
  324. for my $f (keys %remove) {
  325. delete $fields->{$f};
  326. }
  327. # Obtain a lock on debian/control to avoid simultaneous updates
  328. # of debian/files when parallel building is in use
  329. my $lockfh;
  330. my $lockfile = 'debian/control';
  331. $lockfile = $controlfile if not -e $lockfile;
  332. sysopen($lockfh, $lockfile, O_WRONLY) ||
  333. syserr(_g('cannot write %s'), $lockfile);
  334. file_lock($lockfh, $lockfile);
  335. open(my $fileslistnew_fh, '>', "$fileslistfile.new") ||
  336. syserr(_g('open new files list file'));
  337. binmode($fileslistnew_fh);
  338. if (open(my $fileslist_fh, '<', $fileslistfile)) {
  339. binmode($fileslist_fh);
  340. while (<$fileslist_fh>) {
  341. chomp;
  342. next if m/^([-+0-9a-z.]+)_[^_]+_([\w-]+)\.(a-z+) /
  343. && ($1 eq $oppackage)
  344. && ($3 eq $pkg_type)
  345. && (debarch_eq($2, $fields->{'Architecture'} || '')
  346. || debarch_eq($2, 'all'));
  347. print($fileslistnew_fh "$_\n") ||
  348. syserr(_g('copy old entry to new files list file'));
  349. }
  350. close($fileslist_fh) || syserr(_g('close old files list file'));
  351. } elsif ($! != ENOENT) {
  352. syserr(_g('read old files list file'));
  353. }
  354. my $sversion = $fields->{'Version'};
  355. $sversion =~ s/^\d+://;
  356. $forcefilename //= sprintf('%s_%s_%s.%s', $oppackage, $sversion,
  357. $fields->{'Architecture'} || '', $pkg_type);
  358. print($fileslistnew_fh $substvars->substvars(sprintf("%s %s %s\n",
  359. $forcefilename,
  360. $fields->{'Section'} || '-',
  361. $fields->{'Priority'} || '-')))
  362. || syserr(_g('write new entry to new files list file'));
  363. close($fileslistnew_fh) || syserr(_g('close new files list file'));
  364. rename("$fileslistfile.new", $fileslistfile) || syserr(_g('install new files list file'));
  365. # Release the lock
  366. close($lockfh) || syserr(_g('cannot close %s'), $lockfile);
  367. my $cf;
  368. my $fh_output;
  369. if (!$stdout) {
  370. $cf = $outputfile // "$packagebuilddir/DEBIAN/control";
  371. open($fh_output, '>', "$cf.new") ||
  372. syserr(_g("cannot open new output control file \`%s'"), "$cf.new");
  373. } else {
  374. $fh_output = \*STDOUT;
  375. }
  376. $fields->apply_substvars($substvars);
  377. $fields->output($fh_output);
  378. if (!$stdout) {
  379. close($fh_output) || syserr(_g('cannot close %s'), "$cf.new");
  380. rename("$cf.new", "$cf") ||
  381. syserr(_g("cannot install output control file \`%s'"), $cf);
  382. }
  383. $substvars->warn_about_unused();