dpkg-gencontrol.pl 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  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;
  24. use POSIX qw(:errno_h);
  25. use Dpkg;
  26. use Dpkg::Gettext;
  27. use Dpkg::ErrorHandling;
  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"), $progname, $version;
  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. "), $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. grep(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. $fields->{$_} = $host_arch;
  198. }
  199. } else {
  200. field_transfer_single($pkg, $fields);
  201. }
  202. }
  203. # Scan fields of dpkg-parsechangelog
  204. foreach $_ (keys %{$changelog}) {
  205. my $v = $changelog->{$_};
  206. if (m/^Source$/) {
  207. set_source_package($v);
  208. } elsif (m/^Version$/) {
  209. # Already handled previously.
  210. } elsif (m/^Maintainer$/) {
  211. # That field must not be copied from changelog even if it's
  212. # allowed in the binary package control information
  213. } else {
  214. field_transfer_single($changelog, $fields);
  215. }
  216. }
  217. $fields->{'Version'} = $binaryversion;
  218. # Process dependency fields in a second pass, now that substvars have been
  219. # initialized.
  220. my $facts = Dpkg::Deps::KnownFacts->new();
  221. $facts->add_installed_package($fields->{'Package'}, $fields->{'Version'},
  222. $fields->{'Architecture'}, $fields->{'Multi-Arch'});
  223. if (exists $pkg->{"Provides"}) {
  224. my $provides = deps_parse($substvars->substvars($pkg->{"Provides"}, no_warn => 1),
  225. reduce_arch => 1, union => 1);
  226. if (defined $provides) {
  227. foreach my $subdep ($provides->get_deps()) {
  228. if ($subdep->isa('Dpkg::Deps::Simple')) {
  229. $facts->add_provided_package($subdep->{package},
  230. $subdep->{relation}, $subdep->{version},
  231. $fields->{'Package'});
  232. }
  233. }
  234. }
  235. }
  236. my (@seen_deps);
  237. foreach my $field (field_list_pkg_dep()) {
  238. # Arch: all can't be simplified as the host architecture is not known
  239. my $reduce_arch = debarch_eq('all', $pkg->{Architecture} || "all") ? 0 : 1;
  240. if (exists $pkg->{$field}) {
  241. my $dep;
  242. my $field_value = $substvars->substvars($pkg->{$field},
  243. msg_prefix => sprintf(_g("%s field of package %s: "), $field, $pkg->{Package}));
  244. if (field_get_dep_type($field) eq 'normal') {
  245. $dep = deps_parse($field_value, use_arch => 1,
  246. reduce_arch => $reduce_arch);
  247. error(_g("error occurred while parsing %s field: %s"), $field,
  248. $field_value) unless defined $dep;
  249. $dep->simplify_deps($facts, @seen_deps);
  250. # Remember normal deps to simplify even further weaker deps
  251. push @seen_deps, $dep;
  252. } else {
  253. $dep = deps_parse($field_value, use_arch => 1,
  254. reduce_arch => $reduce_arch, union => 1);
  255. error(_g("error occurred while parsing %s field: %s"), $field,
  256. $field_value) unless defined $dep;
  257. $dep->simplify_deps($facts);
  258. $dep->sort();
  259. }
  260. error(_g("the %s field contains an arch-specific dependency but the " .
  261. "package is architecture all"), $field)
  262. if $dep->has_arch_restriction();
  263. $fields->{$field} = $dep->output();
  264. delete $fields->{$field} unless $fields->{$field}; # Delete empty field
  265. }
  266. }
  267. for my $f (qw(Package Version)) {
  268. defined($fields->{$f}) || error(_g("missing information for output field %s"), $f);
  269. }
  270. for my $f (qw(Maintainer Description Architecture)) {
  271. defined($fields->{$f}) || warning(_g("missing information for output field %s"), $f);
  272. }
  273. $oppackage = $fields->{'Package'};
  274. my $pkg_type = $pkg->{'Package-Type'} ||
  275. $pkg->get_custom_field('Package-Type') || 'deb';
  276. if ($pkg_type eq 'udeb') {
  277. delete $fields->{'Package-Type'};
  278. delete $fields->{'Homepage'};
  279. } else {
  280. for my $f (qw(Subarchitecture Kernel-Version Installer-Menu-Item)) {
  281. warning(_g("%s package with udeb specific field %s"), $pkg_type, $f)
  282. if defined($fields->{$f});
  283. }
  284. }
  285. my $verdiff = $binaryversion ne $sourceversion;
  286. if ($oppackage ne $sourcepackage || $verdiff) {
  287. $fields->{'Source'} = $sourcepackage;
  288. $fields->{'Source'} .= " (" . $sourceversion . ")" if $verdiff;
  289. }
  290. if (!defined($substvars->get('Installed-Size'))) {
  291. defined(my $c = open(DU, "-|")) || syserr(_g("cannot fork for %s"), "du");
  292. if (!$c) {
  293. chdir("$packagebuilddir") ||
  294. syserr(_g("chdir for du to \`%s'"), $packagebuilddir);
  295. exec("du", "-k", "-s", "--apparent-size", ".") or
  296. syserr(_g("unable to execute %s"), "du");
  297. }
  298. my $duo = '';
  299. while (<DU>) {
  300. $duo .= $_;
  301. }
  302. close(DU);
  303. $? && subprocerr(_g("du in \`%s'"), $packagebuilddir);
  304. $duo =~ m/^(\d+)\s+\.$/ ||
  305. error(_g("du gave unexpected output \`%s'"), $duo);
  306. $substvars->set_as_used('Installed-Size', $1);
  307. }
  308. if (defined($substvars->get('Extra-Size'))) {
  309. my $size = $substvars->get('Extra-Size') + $substvars->get('Installed-Size');
  310. $substvars->set_as_used('Installed-Size', $size);
  311. }
  312. if (defined($substvars->get('Installed-Size'))) {
  313. $fields->{'Installed-Size'} = $substvars->get('Installed-Size');
  314. }
  315. for my $f (keys %override) {
  316. $fields->{$f} = $override{$f};
  317. }
  318. for my $f (keys %remove) {
  319. delete $fields->{$f};
  320. }
  321. # Obtain a lock on debian/control to avoid simultaneous updates
  322. # of debian/files when parallel building is in use
  323. my $lockfh;
  324. sysopen($lockfh, "debian/control", O_WRONLY) ||
  325. syserr(_g("cannot write %s"), "debian/control");
  326. file_lock($lockfh, "debian/control");
  327. $fileslistfile="./$fileslistfile" if $fileslistfile =~ m/^\s/;
  328. open(Y, ">", "$fileslistfile.new") || syserr(_g("open new files list file"));
  329. binmode(Y);
  330. if (open(X, "<", $fileslistfile)) {
  331. binmode(X);
  332. while (<X>) {
  333. chomp;
  334. next if m/^([-+0-9a-z.]+)_[^_]+_([\w-]+)\.(a-z+) /
  335. && ($1 eq $oppackage)
  336. && ($3 eq $pkg_type)
  337. && (debarch_eq($2, $fields->{'Architecture'} || "")
  338. || debarch_eq($2, 'all'));
  339. print(Y "$_\n") || syserr(_g("copy old entry to new files list file"));
  340. }
  341. close(X) || syserr(_g("close old files list file"));
  342. } elsif ($! != ENOENT) {
  343. syserr(_g("read old files list file"));
  344. }
  345. my $sversion = $fields->{'Version'};
  346. $sversion =~ s/^\d+://;
  347. $forcefilename = sprintf("%s_%s_%s.%s", $oppackage, $sversion,
  348. $fields->{'Architecture'} || "", $pkg_type)
  349. unless ($forcefilename);
  350. print(Y $substvars->substvars(sprintf("%s %s %s\n", $forcefilename,
  351. $fields->{'Section'} || '-',
  352. $fields->{'Priority'} || '-')))
  353. || syserr(_g("write new entry to new files list file"));
  354. close(Y) || syserr(_g("close new files list file"));
  355. rename("$fileslistfile.new", $fileslistfile) || syserr(_g("install new files list file"));
  356. # Release the lock
  357. close($lockfh) || syserr(_g("cannot close %s"), "debian/control");
  358. my $cf;
  359. my $fh_output;
  360. if (!$stdout) {
  361. $cf= "$packagebuilddir/DEBIAN/control";
  362. $cf= "./$cf" if $cf =~ m/^\s/;
  363. open($fh_output, ">", "$cf.new") ||
  364. syserr(_g("cannot open new output control file \`%s'"), "$cf.new");
  365. } else {
  366. $fh_output = \*STDOUT;
  367. }
  368. $fields->apply_substvars($substvars);
  369. $fields->output($fh_output);
  370. if (!$stdout) {
  371. close($fh_output) || syserr(_g("cannot close %s"), "$cf.new");
  372. rename("$cf.new", "$cf") ||
  373. syserr(_g("cannot install output control file \`%s'"), $cf);
  374. }
  375. $substvars->warn_about_unused();