dpkg-gencontrol.pl 14 KB

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