dpkg-gencontrol.pl 13 KB

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