dpkg-gencontrol.pl 14 KB

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