dpkg-genchanges.pl 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  1. #!/usr/bin/perl
  2. #
  3. # dpkg-genchanges
  4. #
  5. # Copyright © 1996 Ian Jackson
  6. # Copyright © 2000,2001 Wichert Akkerman
  7. # Copyright © 2006-2014 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 Carp;
  24. use Encode;
  25. use POSIX qw(:errno_h);
  26. use Dpkg ();
  27. use Dpkg::Gettext;
  28. use Dpkg::Util qw(:list);
  29. use Dpkg::File;
  30. use Dpkg::Checksums;
  31. use Dpkg::ErrorHandling;
  32. use Dpkg::BuildProfiles qw(get_build_profiles parse_build_profiles
  33. evaluate_restriction_formula);
  34. use Dpkg::Arch qw(get_host_arch debarch_eq debarch_is);
  35. use Dpkg::Compression;
  36. use Dpkg::Control::Info;
  37. use Dpkg::Control::Fields;
  38. use Dpkg::Control;
  39. use Dpkg::Substvars;
  40. use Dpkg::Vars;
  41. use Dpkg::Changelog::Parse;
  42. use Dpkg::Dist::Files;
  43. use Dpkg::Version;
  44. textdomain('dpkg-dev');
  45. my $controlfile = 'debian/control';
  46. my $changelogfile = 'debian/changelog';
  47. my $changelogformat;
  48. my $fileslistfile = 'debian/files';
  49. my $uploadfilesdir = '..';
  50. my $sourcestyle = 'i';
  51. my $quiet = 0;
  52. my $host_arch = get_host_arch();
  53. my @profiles = get_build_profiles();
  54. my $changes_format = '1.8';
  55. my %p2f; # - package to file map, has entries for "packagename"
  56. my %p2arch; # - package to arch map
  57. my %f2seccf; # - package to section map, from control file
  58. my %f2pricf; # - package to priority map, from control file
  59. my %sourcedefault; # - default values as taken from source (used for Section,
  60. # Priority and Maintainer)
  61. my @descriptions;
  62. my $checksums = Dpkg::Checksums->new();
  63. my %remove; # - fields to remove
  64. my %override;
  65. my %archadded;
  66. my @archvalues;
  67. my $changesdescription;
  68. my $forcemaint;
  69. my $forcechangedby;
  70. my $since;
  71. my $substvars_loaded = 0;
  72. my $substvars = Dpkg::Substvars->new();
  73. $substvars->set_as_auto('Format', $changes_format);
  74. use constant BUILD_SOURCE => 1;
  75. use constant BUILD_ARCH_DEP => 2;
  76. use constant BUILD_ARCH_INDEP => 4;
  77. use constant BUILD_BINARY => BUILD_ARCH_DEP | BUILD_ARCH_INDEP;
  78. use constant BUILD_SOURCE_DEP => BUILD_SOURCE | BUILD_ARCH_DEP;
  79. use constant BUILD_SOURCE_INDEP => BUILD_SOURCE | BUILD_ARCH_INDEP;
  80. use constant BUILD_ALL => BUILD_BINARY | BUILD_SOURCE;
  81. my $include = BUILD_ALL;
  82. sub build_is_default() { return ($include & BUILD_ALL) == BUILD_ALL; }
  83. sub build_opt {
  84. if ($include == BUILD_BINARY) {
  85. return '-b';
  86. } elsif ($include == BUILD_ARCH_DEP) {
  87. return '-B';
  88. } elsif ($include == BUILD_ARCH_INDEP) {
  89. return '-A';
  90. } elsif ($include == BUILD_SOURCE) {
  91. return '-S';
  92. } elsif ($include == BUILD_SOURCE_DEP) {
  93. return '-G';
  94. } elsif ($include == BUILD_SOURCE_INDEP) {
  95. return '-g';
  96. } else {
  97. croak "build_opt called with include=$include";
  98. }
  99. }
  100. sub set_build_type
  101. {
  102. my ($build_type, $build_option) = @_;
  103. usageerr(_g('cannot combine %s and %s'), build_opt(), $build_option)
  104. if not build_is_default and $include != $build_type;
  105. $include = $build_type;
  106. }
  107. sub version {
  108. printf _g("Debian %s version %s.\n"), $Dpkg::PROGNAME, $Dpkg::PROGVERSION;
  109. printf _g('
  110. This is free software; see the GNU General Public License version 2 or
  111. later for copying conditions. There is NO warranty.
  112. ');
  113. }
  114. sub usage {
  115. printf _g(
  116. 'Usage: %s [<option>...]')
  117. . "\n\n" . _g(
  118. "Options:
  119. -g source and arch-indep build.
  120. -G source and arch-specific build.
  121. -b binary-only, no source files.
  122. -B binary-only, only arch-specific files.
  123. -A binary-only, only arch-indep files.
  124. -S source-only, no binary files.
  125. -c<control-file> get control info from this file.
  126. -l<changelog-file> get per-version info from this file.
  127. -f<files-list-file> get .deb files list from this file.
  128. -v<since-version> include all changes later than version.
  129. -C<changes-description> use change description from this file.
  130. -m<maintainer> override control's maintainer value.
  131. -e<maintainer> override changelog's maintainer value.
  132. -u<upload-files-dir> directory with files (default is '..').
  133. -si (default) source includes orig, if new upstream.
  134. -sa source includes orig, always.
  135. -sd source is diff and .dsc only.
  136. -q quiet - no informational messages on stderr.
  137. -F<changelog-format> force changelog format.
  138. -V<name>=<value> set a substitution variable.
  139. -T<substvars-file> read variables here, not debian/substvars.
  140. -D<field>=<value> override or add a field and value.
  141. -U<field> remove a field.
  142. -?, --help show this help message.
  143. --version show the version.
  144. "), $Dpkg::PROGNAME;
  145. }
  146. while (@ARGV) {
  147. $_=shift(@ARGV);
  148. if (m/^-b$/) {
  149. set_build_type(BUILD_BINARY, $_);
  150. } elsif (m/^-B$/) {
  151. set_build_type(BUILD_ARCH_DEP, $_);
  152. } elsif (m/^-A$/) {
  153. set_build_type(BUILD_ARCH_INDEP, $_);
  154. } elsif (m/^-S$/) {
  155. set_build_type(BUILD_SOURCE, $_);
  156. } elsif (m/^-G$/) {
  157. set_build_type(BUILD_SOURCE_DEP, $_);
  158. } elsif (m/^-g$/) {
  159. set_build_type(BUILD_SOURCE_INDEP, $_);
  160. } elsif (m/^-s([iad])$/) {
  161. $sourcestyle= $1;
  162. } elsif (m/^-q$/) {
  163. $quiet= 1;
  164. } elsif (m/^-c(.*)$/) {
  165. $controlfile = $1;
  166. } elsif (m/^-l(.*)$/) {
  167. $changelogfile = $1;
  168. } elsif (m/^-C(.*)$/) {
  169. $changesdescription = $1;
  170. } elsif (m/^-f(.*)$/) {
  171. $fileslistfile = $1;
  172. } elsif (m/^-v(.*)$/) {
  173. $since = $1;
  174. } elsif (m/^-T(.*)$/) {
  175. $substvars->load($1) if -e $1;
  176. $substvars_loaded = 1;
  177. } elsif (m/^-m(.*)$/s) {
  178. $forcemaint = $1;
  179. } elsif (m/^-e(.*)$/s) {
  180. $forcechangedby = $1;
  181. } elsif (m/^-F([0-9a-z]+)$/) {
  182. $changelogformat = $1;
  183. } elsif (m/^-D([^\=:]+)[=:](.*)$/s) {
  184. $override{$1} = $2;
  185. } elsif (m/^-u(.*)$/) {
  186. $uploadfilesdir = $1;
  187. } elsif (m/^-U([^\=:]+)$/) {
  188. $remove{$1} = 1;
  189. } elsif (m/^-V(\w[-:0-9A-Za-z]*)[=:](.*)$/s) {
  190. $substvars->set($1, $2);
  191. } elsif (m/^-(?:\?|-help)$/) {
  192. usage();
  193. exit(0);
  194. } elsif (m/^--version$/) {
  195. version();
  196. exit(0);
  197. } else {
  198. usageerr(_g("unknown option \`%s'"), $_);
  199. }
  200. }
  201. # Retrieve info from the current changelog entry
  202. my %options = (file => $changelogfile);
  203. $options{changelogformat} = $changelogformat if $changelogformat;
  204. $options{since} = $since if defined($since);
  205. my $changelog = changelog_parse(%options);
  206. # Change options to retrieve info of the former changelog entry
  207. delete $options{since};
  208. $options{count} = 1;
  209. $options{offset} = 1;
  210. my $prev_changelog = changelog_parse(%options);
  211. # Other initializations
  212. my $control = Dpkg::Control::Info->new($controlfile);
  213. my $fields = Dpkg::Control->new(type => CTRL_FILE_CHANGES);
  214. my $sourceversion = $changelog->{'Binary-Only'} ?
  215. $prev_changelog->{'Version'} : $changelog->{'Version'};
  216. my $binaryversion = $changelog->{'Version'};
  217. $substvars->set_version_substvars($sourceversion, $binaryversion);
  218. $substvars->set_arch_substvars();
  219. $substvars->load('debian/substvars') if -e 'debian/substvars' and not $substvars_loaded;
  220. if (defined($prev_changelog) and
  221. version_compare_relation($changelog->{'Version'}, REL_LT,
  222. $prev_changelog->{'Version'}))
  223. {
  224. warning(_g('the current version (%s) is earlier than the previous one (%s)'),
  225. $changelog->{'Version'}, $prev_changelog->{'Version'})
  226. # ~bpo and ~vola are backports and have lower version number by definition
  227. unless $changelog->{'Version'} =~ /~(?:bpo|vola)/;
  228. }
  229. # Scan control info of source package
  230. my $src_fields = $control->get_source();
  231. foreach (keys %{$src_fields}) {
  232. my $v = $src_fields->{$_};
  233. if (m/^Source$/) {
  234. set_source_package($v);
  235. } elsif (m/^Section$|^Priority$/i) {
  236. $sourcedefault{$_} = $v;
  237. } else {
  238. field_transfer_single($src_fields, $fields);
  239. }
  240. }
  241. my $dist = Dpkg::Dist::Files->new();
  242. my $origsrcmsg;
  243. if ($include & BUILD_SOURCE) {
  244. my $sec = $sourcedefault{'Section'} // '-';
  245. my $pri = $sourcedefault{'Priority'} // '-';
  246. warning(_g('missing Section for source files')) if $sec eq '-';
  247. warning(_g('missing Priority for source files')) if $pri eq '-';
  248. my $spackage = get_source_package();
  249. (my $sversion = $substvars->get('source:Version')) =~ s/^\d+://;
  250. my $dsc = "${spackage}_${sversion}.dsc";
  251. my $dsc_pathname = "$uploadfilesdir/$dsc";
  252. my $dsc_fields = Dpkg::Control->new(type => CTRL_PKG_SRC);
  253. $dsc_fields->load($dsc_pathname) or error(_g('%s is empty', $dsc_pathname));
  254. $checksums->add_from_file($dsc_pathname, key => $dsc);
  255. $checksums->add_from_control($dsc_fields, use_files_for_md5 => 1);
  256. # Compare upstream version to previous upstream version to decide if
  257. # the .orig tarballs must be included
  258. my $include_tarball;
  259. if (defined($prev_changelog)) {
  260. my $cur = Dpkg::Version->new($changelog->{'Version'});
  261. my $prev = Dpkg::Version->new($prev_changelog->{'Version'});
  262. $include_tarball = ($cur->version() ne $prev->version()) ? 1 : 0;
  263. } else {
  264. # No previous entry means first upload, tarball required
  265. $include_tarball = 1;
  266. }
  267. my $ext = compression_get_file_extension_regex();
  268. if ((($sourcestyle =~ m/i/ && !$include_tarball) ||
  269. $sourcestyle =~ m/d/) &&
  270. any { m/\.(?:debian\.tar|diff)\.$ext$/ } $checksums->get_files())
  271. {
  272. $origsrcmsg = _g('not including original source code in upload');
  273. foreach my $f (grep { m/\.orig(-.+)?\.tar\.$ext$/ } $checksums->get_files()) {
  274. $checksums->remove_file($f);
  275. }
  276. } else {
  277. if ($sourcestyle =~ m/d/ &&
  278. none { m/\.(?:debian\.tar|diff)\.$ext$/ } $checksums->get_files()) {
  279. warning(_g('ignoring -sd option for native Debian package'));
  280. }
  281. $origsrcmsg = _g('including full source code in upload');
  282. }
  283. # Only add attributes for files being distributed.
  284. for my $f ($checksums->get_files()) {
  285. $dist->add_file($f, $sec, $pri);
  286. }
  287. } elsif ($include == BUILD_ARCH_DEP) {
  288. $origsrcmsg = _g('binary-only arch-specific upload ' .
  289. '(source code and arch-indep packages not included)');
  290. } elsif ($include == BUILD_ARCH_INDEP) {
  291. $origsrcmsg = _g('binary-only arch-indep upload ' .
  292. '(source code and arch-specific packages not included)');
  293. } else {
  294. $origsrcmsg = _g('binary-only upload (no source code included)');
  295. }
  296. if ($include & BUILD_BINARY) {
  297. my $dist_count = 0;
  298. $dist_count = $dist->load($fileslistfile) if -e $fileslistfile;
  299. error(_g('binary build with no binary artifacts found; cannot distribute'))
  300. if $dist_count == 0;
  301. foreach my $file ($dist->get_files()) {
  302. if (defined $file->{package} && $file->{package_type} =~ m/^u?deb$/) {
  303. $p2f{$file->{package}} //= [];
  304. push @{$p2f{$file->{package}}}, $file->{filename};
  305. }
  306. if (defined $file->{arch}) {
  307. push @archvalues, $file->{arch}
  308. if $file->{arch} and not $archadded{$file->{arch}}++;
  309. }
  310. }
  311. }
  312. # Scan control info of all binary packages
  313. foreach my $pkg ($control->get_packages()) {
  314. my $p = $pkg->{'Package'};
  315. my $a = $pkg->{'Architecture'} // '';
  316. my $bp = $pkg->{'Build-Profiles'};
  317. my $d = $pkg->{'Description'} || 'no description available';
  318. $d = $1 if $d =~ /^(.*)\n/;
  319. my $pkg_type = $pkg->{'Package-Type'} ||
  320. $pkg->get_custom_field('Package-Type') || 'deb';
  321. my @f; # List of files for this binary package
  322. push @f, @{$p2f{$p}} if defined $p2f{$p};
  323. # Add description of all binary packages
  324. my $desc = encode_utf8(sprintf('%-10s - %-.65s', $p, decode_utf8($d)));
  325. $desc .= ' (udeb)' if $pkg_type eq 'udeb';
  326. push @descriptions, $desc;
  327. my @restrictions;
  328. @restrictions = parse_build_profiles($bp) if defined $bp;
  329. if (not defined($p2f{$p})) {
  330. # No files for this package... warn if it's unexpected
  331. if (((debarch_eq('all', $a) and ($include & BUILD_ARCH_INDEP)) ||
  332. ((any { debarch_is($host_arch, $_) } split /\s+/, $a)
  333. and ($include & BUILD_ARCH_DEP))) and
  334. (@restrictions == 0 or
  335. evaluate_restriction_formula(\@restrictions, \@profiles)))
  336. {
  337. warning(_g('package %s in control file but not in files list'),
  338. $p);
  339. }
  340. next; # and skip it
  341. }
  342. $p2arch{$p} = $a;
  343. foreach (keys %{$pkg}) {
  344. my $v = $pkg->{$_};
  345. if (m/^Section$/) {
  346. $f2seccf{$_} = $v foreach (@f);
  347. } elsif (m/^Priority$/) {
  348. $f2pricf{$_} = $v foreach (@f);
  349. } elsif (m/^Architecture$/) {
  350. if ((any { debarch_is($host_arch, $_) } split /\s+/, $v)
  351. and ($include & BUILD_ARCH_DEP)) {
  352. $v = $host_arch;
  353. } elsif (!debarch_eq('all', $v)) {
  354. $v = '';
  355. }
  356. push(@archvalues, $v) if $v and not $archadded{$v}++;
  357. } elsif (m/^Description$/) {
  358. # Description in changes is computed, do not copy this field
  359. } else {
  360. field_transfer_single($pkg, $fields);
  361. }
  362. }
  363. }
  364. # Scan fields of dpkg-parsechangelog
  365. foreach (keys %{$changelog}) {
  366. my $v = $changelog->{$_};
  367. if (m/^Source$/i) {
  368. set_source_package($v);
  369. } elsif (m/^Maintainer$/i) {
  370. $fields->{'Changed-By'} = $v;
  371. } else {
  372. field_transfer_single($changelog, $fields);
  373. }
  374. }
  375. if ($changesdescription) {
  376. open(my $changes_fh, '<', $changesdescription)
  377. or syserr(_g('read changesdescription'));
  378. $fields->{'Changes'} = "\n" . file_slurp($changes_fh);
  379. close($changes_fh);
  380. }
  381. for my $p (keys %p2f) {
  382. warning(_g('package %s listed in files list but not in control info'), $p)
  383. unless defined $control->get_pkg_by_name($p);
  384. }
  385. for my $p (keys %p2f) {
  386. my @f = @{$p2f{$p}};
  387. foreach my $f (@f) {
  388. my $file = $dist->get_file($f);
  389. my $sec = $f2seccf{$f} || $sourcedefault{'Section'} // '-';
  390. if ($sec eq '-') {
  391. warning(_g("missing Section for binary package %s; using '-'"), $p);
  392. }
  393. if ($sec ne $file->{section}) {
  394. error(_g('package %s has section %s in control file but %s in ' .
  395. 'files list'), $p, $sec, $file->{section});
  396. }
  397. my $pri = $f2pricf{$f} || $sourcedefault{'Priority'} // '-';
  398. if ($pri eq '-') {
  399. warning(_g("missing Priority for binary package %s; using '-'"), $p);
  400. }
  401. if ($pri ne $file->{priority}) {
  402. error(_g('package %s has priority %s in control file but %s in ' .
  403. 'files list'), $p, $pri, $file->{priority});
  404. }
  405. }
  406. }
  407. print { *STDERR } "$Dpkg::PROGNAME: $origsrcmsg\n"
  408. or syserr(_g('write original source message')) unless $quiet;
  409. $fields->{'Format'} = $substvars->get('Format');
  410. if (!defined($fields->{'Date'})) {
  411. chomp(my $date822 = `date -R`);
  412. subprocerr('date -R') if $?;
  413. $fields->{'Date'}= $date822;
  414. }
  415. $fields->{'Binary'} = join(' ', map { $_->{'Package'} } $control->get_packages());
  416. # Avoid overly long line by splitting over multiple lines
  417. if (length($fields->{'Binary'}) > 980) {
  418. $fields->{'Binary'} =~ s/(.{0,980}) /$1\n/g;
  419. }
  420. unshift @archvalues, 'source' if $include & BUILD_SOURCE;
  421. @archvalues = ('all') if $include == BUILD_ARCH_INDEP;
  422. @archvalues = grep { !debarch_eq('all', $_) } @archvalues
  423. unless $include & BUILD_ARCH_INDEP;
  424. @archvalues = grep { !debarch_eq($host_arch, $_) } @archvalues
  425. unless $include & BUILD_ARCH_DEP;
  426. $fields->{'Architecture'} = join ' ', @archvalues;
  427. $fields->{'Built-For-Profiles'} = join ' ', get_build_profiles();
  428. $fields->{'Description'} = "\n" . join("\n", sort @descriptions);
  429. $fields->{'Files'} = '';
  430. for my $file ($dist->get_files()) {
  431. my $f = $file->{filename};
  432. if (defined $file->{package}) {
  433. my $arch_all = debarch_eq('all', $p2arch{$file->{package}});
  434. next if (not ($include & BUILD_ARCH_INDEP) and $arch_all);
  435. next if (not ($include & BUILD_ARCH_DEP) and not $arch_all);
  436. }
  437. $checksums->add_from_file("$uploadfilesdir/$f", key => $f);
  438. $fields->{'Files'} .= "\n" . $checksums->get_checksum($f, 'md5') .
  439. ' ' . $checksums->get_size($f) .
  440. " $file->{section} $file->{priority} $f";
  441. }
  442. $checksums->export_to_control($fields);
  443. # redundant with the Files field
  444. delete $fields->{'Checksums-Md5'};
  445. $fields->{'Source'} = get_source_package();
  446. if ($fields->{'Version'} ne $substvars->get('source:Version')) {
  447. $fields->{'Source'} .= ' (' . $substvars->get('source:Version') . ')';
  448. }
  449. $fields->{'Maintainer'} = $forcemaint if defined($forcemaint);
  450. $fields->{'Changed-By'} = $forcechangedby if defined($forcechangedby);
  451. for my $f (qw(Version Distribution Maintainer Changes)) {
  452. error(_g('missing information for critical output field %s'), $f)
  453. unless defined $fields->{$f};
  454. }
  455. for my $f (qw(Urgency)) {
  456. warning(_g('missing information for output field %s'), $f)
  457. unless defined $fields->{$f};
  458. }
  459. for my $f (keys %override) {
  460. $fields->{$f} = $override{$f};
  461. }
  462. for my $f (keys %remove) {
  463. delete $fields->{$f};
  464. }
  465. # Note: do not perform substitution of variables, one of the reasons is that
  466. # they could interfere with field values, for example the Changes field.
  467. $fields->output(\*STDOUT);