dpkg-gencontrol.pl 13 KB

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