dpkg-gencontrol.pl 14 KB

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