dpkg-gencontrol.pl 14 KB

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