dpkg-gencontrol.pl 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  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 qw(:errno_h :fcntl_h);
  24. use Dpkg;
  25. use Dpkg::Gettext;
  26. use Dpkg::ErrorHandling;
  27. use Dpkg::File;
  28. use Dpkg::Arch qw(get_host_arch debarch_eq debarch_is);
  29. use Dpkg::Package;
  30. use Dpkg::Deps;
  31. use Dpkg::Control;
  32. use Dpkg::Control::Info;
  33. use Dpkg::Control::Fields;
  34. use Dpkg::Substvars;
  35. use Dpkg::Vars;
  36. use Dpkg::Changelog::Parse;
  37. textdomain("dpkg-dev");
  38. my $controlfile = 'debian/control';
  39. my $changelogfile = 'debian/changelog';
  40. my $changelogformat;
  41. my $fileslistfile = 'debian/files';
  42. my $packagebuilddir = 'debian/tmp';
  43. my $sourceversion;
  44. my $binaryversion;
  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. . "\n\n" . _g(
  64. "Options:
  65. -p<package> print control file for package.
  66. -c<control-file> get control info from this file.
  67. -l<changelog-file> get per-version info from this file.
  68. -F<changelog-format> force changelog format.
  69. -v<force-version> set version of binary package.
  70. -f<files-list-file> write files here instead of debian/files.
  71. -P<package-build-dir> temporary build dir instead of debian/tmp.
  72. -n<filename> assume the package filename will be <filename>.
  73. -O write to stdout, not .../DEBIAN/control.
  74. -is, -ip, -isp, -ips deprecated, ignored for compatibility.
  75. -D<field>=<value> override or add a field and value.
  76. -U<field> remove a field.
  77. -V<name>=<value> set a substitution variable.
  78. -T<substvars-file> read variables here, not debian/substvars.
  79. -?, --help show this help message.
  80. --version show the version.
  81. "), $progname;
  82. }
  83. while (@ARGV) {
  84. $_=shift(@ARGV);
  85. if (m/^-p/) {
  86. $oppackage = $';
  87. my $err = pkg_name_is_illegal($oppackage);
  88. error(_g("illegal package name '%s': %s"), $oppackage, $err) if $err;
  89. } elsif (m/^-c/) {
  90. $controlfile= $';
  91. } elsif (m/^-l/) {
  92. $changelogfile= $';
  93. } elsif (m/^-P/) {
  94. $packagebuilddir= $';
  95. } elsif (m/^-f/) {
  96. $fileslistfile= $';
  97. } elsif (m/^-v(.+)$/) {
  98. $forceversion= $1;
  99. } elsif (m/^-O$/) {
  100. $stdout= 1;
  101. } elsif (m/^-i[sp][sp]?$/) {
  102. # ignored for backwards compatibility
  103. } elsif (m/^-F([0-9a-z]+)$/) {
  104. $changelogformat=$1;
  105. } elsif (m/^-D([^\=:]+)[=:]/) {
  106. $override{$1}= $';
  107. } elsif (m/^-U([^\=:]+)$/) {
  108. $remove{$1}= 1;
  109. } elsif (m/^-V(\w[-:0-9A-Za-z]*)[=:]/) {
  110. $substvars->set_as_used($1, $');
  111. } elsif (m/^-T(.*)$/) {
  112. $substvars->load($1) if -e $1;
  113. $substvars_loaded = 1;
  114. } elsif (m/^-n/) {
  115. $forcefilename= $';
  116. } elsif (m/^-(\?|-help)$/) {
  117. usage();
  118. exit(0);
  119. } elsif (m/^--version$/) {
  120. version();
  121. exit(0);
  122. } else {
  123. usageerr(_g("unknown option \`%s'"), $_);
  124. }
  125. }
  126. umask 0022; # ensure sane default permissions for created files
  127. my %options = (file => $changelogfile);
  128. $options{"changelogformat"} = $changelogformat if $changelogformat;
  129. my $changelog = changelog_parse(%options);
  130. if ($changelog->{"Binary-Only"}) {
  131. $options{"count"} = 1;
  132. $options{"offset"} = 1;
  133. my $prev_changelog = changelog_parse(%options);
  134. $sourceversion = $prev_changelog->{"Version"};
  135. } else {
  136. $sourceversion = $changelog->{"Version"};
  137. }
  138. if (defined $forceversion) {
  139. $binaryversion = $forceversion;
  140. } else {
  141. $binaryversion = $changelog->{"Version"};
  142. }
  143. $substvars->set_version_substvars($sourceversion, $binaryversion);
  144. $substvars->set_arch_substvars();
  145. $substvars->load("debian/substvars") if -e "debian/substvars" and not $substvars_loaded;
  146. my $control = Dpkg::Control::Info->new($controlfile);
  147. my $fields = Dpkg::Control->new(type => CTRL_PKG_DEB);
  148. # Old-style bin-nmus change the source version submitted to
  149. # set_version_substvars()
  150. $sourceversion = $substvars->get("source:Version");
  151. my $pkg;
  152. if (defined($oppackage)) {
  153. $pkg = $control->get_pkg_by_name($oppackage);
  154. defined($pkg) || error(_g("package %s not in control info"), $oppackage);
  155. } else {
  156. my @packages = map { $_->{'Package'} } $control->get_packages();
  157. if (@packages == 0) {
  158. error(_g("no package stanza found in control info"));
  159. } elsif (@packages > 1) {
  160. error(_g("must specify package since control info has many (%s)"),
  161. "@packages");
  162. }
  163. $pkg = $control->get_pkg_by_idx(1);
  164. }
  165. $substvars->set_msg_prefix(sprintf(_g("package %s: "), $pkg->{Package}));
  166. # Scan source package
  167. my $src_fields = $control->get_source();
  168. foreach $_ (keys %{$src_fields}) {
  169. if (m/^Source$/) {
  170. set_source_package($src_fields->{$_});
  171. } else {
  172. field_transfer_single($src_fields, $fields);
  173. }
  174. }
  175. # Scan binary package
  176. foreach $_ (keys %{$pkg}) {
  177. my $v = $pkg->{$_};
  178. if (field_get_dep_type($_)) {
  179. # Delay the parsing until later
  180. } elsif (m/^Architecture$/) {
  181. my $host_arch = get_host_arch();
  182. if (debarch_eq('all', $v)) {
  183. $fields->{$_} = $v;
  184. } else {
  185. my @archlist = split(/\s+/, $v);
  186. my @invalid_archs = grep m/[^\w-]/, @archlist;
  187. warning(ngettext("`%s' is not a legal architecture string.",
  188. "`%s' are not legal architecture strings.",
  189. scalar(@invalid_archs)),
  190. join("' `", @invalid_archs))
  191. if @invalid_archs >= 1;
  192. grep(debarch_is($host_arch, $_), @archlist) ||
  193. error(_g("current host architecture '%s' does not " .
  194. "appear in package's architecture list (%s)"),
  195. $host_arch, "@archlist");
  196. $fields->{$_} = $host_arch;
  197. }
  198. } else {
  199. field_transfer_single($pkg, $fields);
  200. }
  201. }
  202. # Scan fields of dpkg-parsechangelog
  203. foreach $_ (keys %{$changelog}) {
  204. my $v = $changelog->{$_};
  205. if (m/^Source$/) {
  206. set_source_package($v);
  207. } elsif (m/^Version$/) {
  208. # Already handled previously.
  209. } elsif (m/^Maintainer$/) {
  210. # That field must not be copied from changelog even if it's
  211. # allowed in the binary package control information
  212. } else {
  213. field_transfer_single($changelog, $fields);
  214. }
  215. }
  216. $fields->{'Version'} = $binaryversion;
  217. # Process dependency fields in a second pass, now that substvars have been
  218. # initialized.
  219. my $facts = Dpkg::Deps::KnownFacts->new();
  220. $facts->add_installed_package($fields->{'Package'}, $fields->{'Version'},
  221. $fields->{'Architecture'}, $fields->{'Multi-Arch'});
  222. if (exists $pkg->{"Provides"}) {
  223. my $provides = deps_parse($substvars->substvars($pkg->{"Provides"}, no_warn => 1),
  224. reduce_arch => 1, union => 1);
  225. if (defined $provides) {
  226. foreach my $subdep ($provides->get_deps()) {
  227. if ($subdep->isa('Dpkg::Deps::Simple')) {
  228. $facts->add_provided_package($subdep->{package},
  229. $subdep->{relation}, $subdep->{version},
  230. $fields->{'Package'});
  231. }
  232. }
  233. }
  234. }
  235. my (@seen_deps);
  236. foreach my $field (field_list_pkg_dep()) {
  237. # Arch: all can't be simplified as the host architecture is not known
  238. my $reduce_arch = debarch_eq('all', $pkg->{Architecture} || "all") ? 0 : 1;
  239. if (exists $pkg->{$field}) {
  240. my $dep;
  241. my $field_value = $substvars->substvars($pkg->{$field},
  242. msg_prefix => sprintf(_g("%s field of package %s: "), $field, $pkg->{Package}));
  243. if (field_get_dep_type($field) eq 'normal') {
  244. $dep = deps_parse($field_value, use_arch => 1,
  245. reduce_arch => $reduce_arch);
  246. error(_g("error occurred while parsing %s field: %s"), $field,
  247. $field_value) unless defined $dep;
  248. $dep->simplify_deps($facts, @seen_deps);
  249. # Remember normal deps to simplify even further weaker deps
  250. push @seen_deps, $dep;
  251. } else {
  252. $dep = deps_parse($field_value, use_arch => 1,
  253. reduce_arch => $reduce_arch, union => 1);
  254. error(_g("error occurred while parsing %s field: %s"), $field,
  255. $field_value) unless defined $dep;
  256. $dep->simplify_deps($facts);
  257. $dep->sort();
  258. }
  259. error(_g("the %s field contains an arch-specific dependency but the " .
  260. "package is architecture all"), $field)
  261. if $dep->has_arch_restriction();
  262. $fields->{$field} = $dep->output();
  263. delete $fields->{$field} unless $fields->{$field}; # Delete empty field
  264. }
  265. }
  266. for my $f (qw(Package Version)) {
  267. defined($fields->{$f}) || error(_g("missing information for output field %s"), $f);
  268. }
  269. for my $f (qw(Maintainer Description Architecture)) {
  270. defined($fields->{$f}) || warning(_g("missing information for output field %s"), $f);
  271. }
  272. $oppackage = $fields->{'Package'};
  273. my $pkg_type = $pkg->{'Package-Type'} ||
  274. $pkg->get_custom_field('Package-Type') || 'deb';
  275. if ($pkg_type eq 'udeb') {
  276. delete $fields->{'Package-Type'};
  277. delete $fields->{'Homepage'};
  278. } else {
  279. for my $f (qw(Subarchitecture Kernel-Version Installer-Menu-Item)) {
  280. warning(_g("%s package with udeb specific field %s"), $pkg_type, $f)
  281. if defined($fields->{$f});
  282. }
  283. }
  284. my $verdiff = $binaryversion ne $sourceversion;
  285. if ($oppackage ne $sourcepackage || $verdiff) {
  286. $fields->{'Source'} = $sourcepackage;
  287. $fields->{'Source'} .= " (" . $sourceversion . ")" if $verdiff;
  288. }
  289. if (!defined($substvars->get('Installed-Size'))) {
  290. my $du_fh;
  291. defined(my $c = open($du_fh, "-|")) || 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_fh>) {
  300. $duo .= $_;
  301. }
  302. close($du_fh);
  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(my $fileslistnew_fh, ">", "$fileslistfile.new") ||
  329. syserr(_g("open new files list file"));
  330. binmode($fileslistnew_fh);
  331. if (open(my $fileslist_fh, "<", $fileslistfile)) {
  332. binmode($fileslist_fh);
  333. while (<$fileslist_fh>) {
  334. chomp;
  335. next if m/^([-+0-9a-z.]+)_[^_]+_([\w-]+)\.(a-z+) /
  336. && ($1 eq $oppackage)
  337. && ($3 eq $pkg_type)
  338. && (debarch_eq($2, $fields->{'Architecture'} || "")
  339. || debarch_eq($2, 'all'));
  340. print($fileslistnew_fh "$_\n") ||
  341. syserr(_g("copy old entry to new files list file"));
  342. }
  343. close($fileslist_fh) || syserr(_g("close old files list file"));
  344. } elsif ($! != ENOENT) {
  345. syserr(_g("read old files list file"));
  346. }
  347. my $sversion = $fields->{'Version'};
  348. $sversion =~ s/^\d+://;
  349. $forcefilename = sprintf("%s_%s_%s.%s", $oppackage, $sversion,
  350. $fields->{'Architecture'} || "", $pkg_type)
  351. unless ($forcefilename);
  352. print($fileslistnew_fh $substvars->substvars(sprintf("%s %s %s\n",
  353. $forcefilename,
  354. $fields->{'Section'} || '-',
  355. $fields->{'Priority'} || '-')))
  356. || syserr(_g("write new entry to new files list file"));
  357. close($fileslistnew_fh) || syserr(_g("close new files list file"));
  358. rename("$fileslistfile.new", $fileslistfile) || syserr(_g("install new files list file"));
  359. # Release the lock
  360. close($lockfh) || syserr(_g("cannot close %s"), "debian/control");
  361. my $cf;
  362. my $fh_output;
  363. if (!$stdout) {
  364. $cf= "$packagebuilddir/DEBIAN/control";
  365. $cf= "./$cf" if $cf =~ m/^\s/;
  366. open($fh_output, ">", "$cf.new") ||
  367. syserr(_g("cannot open new output control file \`%s'"), "$cf.new");
  368. } else {
  369. $fh_output = \*STDOUT;
  370. }
  371. $fields->apply_substvars($substvars);
  372. $fields->output($fh_output);
  373. if (!$stdout) {
  374. close($fh_output) || syserr(_g("cannot close %s"), "$cf.new");
  375. rename("$cf.new", "$cf") ||
  376. syserr(_g("cannot install output control file \`%s'"), $cf);
  377. }
  378. $substvars->warn_about_unused();