dpkg-source.pl 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675
  1. #!/usr/bin/perl
  2. #
  3. # dpkg-source
  4. #
  5. # Copyright © 1996 Ian Jackson <ijackson@chiark.greenend.org.uk>
  6. # Copyright © 1997 Klee Dienes <klee@debian.org>
  7. # Copyright © 1999-2003 Wichert Akkerman <wakkerma@debian.org>
  8. # Copyright © 1999 Ben Collins <bcollins@debian.org>
  9. # Copyright © 2000-2003 Adam Heath <doogie@debian.org>
  10. # Copyright © 2005 Brendan O'Dea <bod@debian.org>
  11. # Copyright © 2006-2008 Frank Lichtenheld <djpig@debian.org>
  12. # Copyright © 2006-2009,2012 Guillem Jover <guillem@debian.org>
  13. # Copyright © 2008-2011 Raphaël Hertzog <hertzog@debian.org>
  14. #
  15. # This program is free software; you can redistribute it and/or modify
  16. # it under the terms of the GNU General Public License as published by
  17. # the Free Software Foundation; either version 2 of the License, or
  18. # (at your option) any later version.
  19. #
  20. # This program is distributed in the hope that it will be useful,
  21. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. # GNU General Public License for more details.
  24. #
  25. # You should have received a copy of the GNU General Public License
  26. # along with this program. If not, see <https://www.gnu.org/licenses/>.
  27. use strict;
  28. use warnings;
  29. use Cwd;
  30. use File::Basename;
  31. use File::Spec;
  32. use Dpkg ();
  33. use Dpkg::Gettext;
  34. use Dpkg::ErrorHandling;
  35. use Dpkg::Util qw(:list);
  36. use Dpkg::Arch qw(:operators);
  37. use Dpkg::Deps;
  38. use Dpkg::Compression;
  39. use Dpkg::Conf;
  40. use Dpkg::Control::Info;
  41. use Dpkg::Control::Tests;
  42. use Dpkg::Control::Fields;
  43. use Dpkg::Index;
  44. use Dpkg::Substvars;
  45. use Dpkg::Version;
  46. use Dpkg::Vars;
  47. use Dpkg::Changelog::Parse;
  48. use Dpkg::Source::Package qw(get_default_diff_ignore_regex
  49. set_default_diff_ignore_regex
  50. get_default_tar_ignore_pattern);
  51. use Dpkg::Vendor qw(run_vendor_hook);
  52. textdomain('dpkg-dev');
  53. my $controlfile;
  54. my $changelogfile;
  55. my $changelogformat;
  56. my $build_format;
  57. my %options = (
  58. # Ignore files
  59. tar_ignore => [],
  60. diff_ignore_regex => '',
  61. # Misc options
  62. copy_orig_tarballs => 1,
  63. no_check => 0,
  64. no_overwrite_dir => 1,
  65. require_valid_signature => 0,
  66. require_strong_checksums => 0,
  67. );
  68. # Fields to remove/override
  69. my %remove;
  70. my %override;
  71. my $substvars = Dpkg::Substvars->new();
  72. my $tar_ignore_default_pattern_done;
  73. my $diff_ignore_regex = get_default_diff_ignore_regex();
  74. my @options;
  75. my @cmdline_options;
  76. while (@ARGV && $ARGV[0] =~ m/^-/) {
  77. my $arg = shift @ARGV;
  78. if ($arg eq '-b' or $arg eq '--build') {
  79. setopmode('build');
  80. } elsif ($arg eq '-x' or $arg eq '--extract') {
  81. setopmode('extract');
  82. } elsif ($arg eq '--before-build') {
  83. setopmode('before-build');
  84. } elsif ($arg eq '--after-build') {
  85. setopmode('after-build');
  86. } elsif ($arg eq '--commit') {
  87. setopmode('commit');
  88. } elsif ($arg eq '--print-format') {
  89. setopmode('print-format');
  90. report_options(info_fh => \*STDERR); # Avoid clutter on STDOUT
  91. } else {
  92. push @options, $arg;
  93. }
  94. }
  95. my $dir;
  96. if (defined($options{opmode}) &&
  97. $options{opmode} =~ /^(build|print-format|(before|after)-build|commit)$/) {
  98. if (not scalar(@ARGV)) {
  99. usageerr(g_('--%s needs a directory'), $options{opmode})
  100. unless $1 eq 'commit';
  101. $dir = '.';
  102. } else {
  103. $dir = File::Spec->catdir(shift(@ARGV));
  104. }
  105. stat($dir) or syserr(g_('cannot stat directory %s'), $dir);
  106. if (not -d $dir) {
  107. error(g_('directory argument %s is not a directory'), $dir);
  108. }
  109. if ($dir eq '.') {
  110. # . is never correct, adjust automatically
  111. $dir = basename(cwd());
  112. chdir '..' or syserr(g_("unable to chdir to '%s'"), '..');
  113. }
  114. # --format options are not allowed, they would take precedence
  115. # over real command line options, debian/source/format should be used
  116. # instead
  117. # --unapply-patches is only allowed in local-options as it's a matter
  118. # of personal taste and the default should be to keep patches applied
  119. my $forbidden_opts_re = {
  120. 'options' => qr/^--(?:format=|unapply-patches$|abort-on-upstream-changes$)/,
  121. 'local-options' => qr/^--format=/,
  122. };
  123. foreach my $filename ('local-options', 'options') {
  124. my $conf = Dpkg::Conf->new();
  125. my $optfile = File::Spec->catfile($dir, 'debian', 'source', $filename);
  126. next unless -f $optfile;
  127. $conf->load($optfile);
  128. $conf->filter(remove => sub { $_[0] =~ $forbidden_opts_re->{$filename} });
  129. if (@$conf) {
  130. info(g_('using options from %s: %s'), $optfile, join(' ', @$conf))
  131. unless $options{opmode} eq 'print-format';
  132. unshift @options, @$conf;
  133. }
  134. }
  135. }
  136. while (@options) {
  137. $_ = shift(@options);
  138. if (m/^--format=(.*)$/) {
  139. $build_format //= $1;
  140. } elsif (m/^-(?:Z|-compression=)(.*)$/) {
  141. my $compression = $1;
  142. $options{compression} = $compression;
  143. usageerr(g_('%s is not a supported compression'), $compression)
  144. unless compression_is_supported($compression);
  145. compression_set_default($compression);
  146. } elsif (m/^-(?:z|-compression-level=)(.*)$/) {
  147. my $comp_level = $1;
  148. $options{comp_level} = $comp_level;
  149. usageerr(g_('%s is not a compression level'), $comp_level)
  150. unless compression_is_valid_level($comp_level);
  151. compression_set_default_level($comp_level);
  152. } elsif (m/^-c(.*)$/) {
  153. $controlfile = $1;
  154. } elsif (m/^-l(.*)$/) {
  155. $changelogfile = $1;
  156. } elsif (m/^-F([0-9a-z]+)$/) {
  157. $changelogformat = $1;
  158. } elsif (m/^-D([^\=:]+)[=:](.*)$/s) {
  159. $override{$1} = $2;
  160. } elsif (m/^-U([^\=:]+)$/) {
  161. $remove{$1} = 1;
  162. } elsif (m/^-(?:i|-diff-ignore(?:$|=))(.*)$/) {
  163. $options{diff_ignore_regex} = $1 ? $1 : $diff_ignore_regex;
  164. } elsif (m/^--extend-diff-ignore=(.+)$/) {
  165. $diff_ignore_regex .= "|$1";
  166. if ($options{diff_ignore_regex}) {
  167. $options{diff_ignore_regex} .= "|$1";
  168. }
  169. set_default_diff_ignore_regex($diff_ignore_regex);
  170. } elsif (m/^-(?:I|-tar-ignore=)(.+)$/) {
  171. push @{$options{tar_ignore}}, $1;
  172. } elsif (m/^-(?:I|-tar-ignore)$/) {
  173. unless ($tar_ignore_default_pattern_done) {
  174. push @{$options{tar_ignore}}, get_default_tar_ignore_pattern();
  175. # Prevent adding multiple times
  176. $tar_ignore_default_pattern_done = 1;
  177. }
  178. } elsif (m/^--no-copy$/) {
  179. $options{copy_orig_tarballs} = 0;
  180. } elsif (m/^--no-check$/) {
  181. $options{no_check} = 1;
  182. } elsif (m/^--no-overwrite-dir$/) {
  183. $options{no_overwrite_dir} = 1;
  184. } elsif (m/^--require-valid-signature$/) {
  185. $options{require_valid_signature} = 1;
  186. } elsif (m/^--require-strong-checksums$/) {
  187. $options{require_strong_checksums} = 1;
  188. } elsif (m/^-V(\w[-:0-9A-Za-z]*)[=:](.*)$/s) {
  189. $substvars->set($1, $2);
  190. } elsif (m/^-T(.*)$/) {
  191. $substvars->load($1) if -e $1;
  192. } elsif (m/^-(?:\?|-help)$/) {
  193. usage();
  194. exit(0);
  195. } elsif (m/^--version$/) {
  196. version();
  197. exit(0);
  198. } elsif (m/^-[EW]$/) {
  199. # Deprecated option
  200. warning(g_('-E and -W are deprecated, they are without effect'));
  201. } elsif (m/^-q$/) {
  202. report_options(quiet_warnings => 1);
  203. $options{quiet} = 1;
  204. } elsif (m/^--$/) {
  205. last;
  206. } else {
  207. push @cmdline_options, $_;
  208. }
  209. }
  210. unless (defined($options{opmode})) {
  211. usageerr(g_('need an action option'));
  212. }
  213. if ($options{opmode} =~ /^(build|print-format|(before|after)-build|commit)$/) {
  214. $options{ARGV} = \@ARGV;
  215. $changelogfile ||= "$dir/debian/changelog";
  216. $controlfile ||= "$dir/debian/control";
  217. my %ch_options = (file => $changelogfile);
  218. $ch_options{changelogformat} = $changelogformat if $changelogformat;
  219. my $changelog = changelog_parse(%ch_options);
  220. my $control = Dpkg::Control::Info->new($controlfile);
  221. # <https://reproducible-builds.org/specs/source-date-epoch/>
  222. $ENV{SOURCE_DATE_EPOCH} ||= $changelog->{timestamp} || time;
  223. my $srcpkg = Dpkg::Source::Package->new(options => \%options);
  224. my $fields = $srcpkg->{fields};
  225. my @sourcearch;
  226. my %archadded;
  227. my @binarypackages;
  228. # Scan control info of source package
  229. my $src_fields = $control->get_source();
  230. error(g_("%s doesn't contain any information about the source package"),
  231. $controlfile) unless defined $src_fields;
  232. my $src_sect = $src_fields->{'Section'} || 'unknown';
  233. my $src_prio = $src_fields->{'Priority'} || 'unknown';
  234. foreach (keys %{$src_fields}) {
  235. my $v = $src_fields->{$_};
  236. if (m/^Source$/i) {
  237. set_source_package($v);
  238. $fields->{$_} = $v;
  239. } elsif (m/^Uploaders$/i) {
  240. ($fields->{$_} = $v) =~ s/\s*[\r\n]\s*/ /g; # Merge in a single-line
  241. } elsif (m/^Build-(?:Depends|Conflicts)(?:-Arch|-Indep)?$/i) {
  242. my $dep;
  243. my $type = field_get_dep_type($_);
  244. $dep = deps_parse($v, build_dep => 1, union => $type eq 'union');
  245. error(g_('error occurred while parsing %s'), $_) unless defined $dep;
  246. my $facts = Dpkg::Deps::KnownFacts->new();
  247. $dep->simplify_deps($facts);
  248. $dep->sort() if $type eq 'union';
  249. $fields->{$_} = $dep->output();
  250. } else {
  251. field_transfer_single($src_fields, $fields);
  252. }
  253. }
  254. # Scan control info of binary packages
  255. my @pkglist;
  256. foreach my $pkg ($control->get_packages()) {
  257. my $p = $pkg->{'Package'};
  258. my $sect = $pkg->{'Section'} || $src_sect;
  259. my $prio = $pkg->{'Priority'} || $src_prio;
  260. my $type = $pkg->{'Package-Type'} ||
  261. $pkg->get_custom_field('Package-Type') || 'deb';
  262. my $arch = $pkg->{'Architecture'};
  263. my $profile = $pkg->{'Build-Profiles'};
  264. my $pkg_summary = sprintf('%s %s %s %s', $p, $type, $sect, $prio);
  265. $pkg_summary .= ' arch=' . join ',', split /\s+/, $arch;
  266. if (defined $profile) {
  267. # If the string does not contain brackets then it is using the
  268. # old syntax. Emit a fatal error.
  269. if ($profile !~ m/^\s*<.*>\s*$/) {
  270. error(g_('binary package stanza %s is using an obsolete ' .
  271. 'Build-Profiles field syntax'), $p);
  272. }
  273. # Instead of splitting twice and then joining twice, we just do
  274. # simple string replacements:
  275. # Remove the enclosing <>
  276. $profile =~ s/^\s*<(.*)>\s*$/$1/;
  277. # Join lists with a plus (OR)
  278. $profile =~ s/>\s+</+/g;
  279. # Join their elements with a comma (AND)
  280. $profile =~ s/\s+/,/g;
  281. $pkg_summary .= " profile=$profile";
  282. }
  283. if (defined $pkg->{'Essential'} and $pkg->{'Essential'} eq 'yes') {
  284. $pkg_summary .= ' essential=yes';
  285. }
  286. push @pkglist, $pkg_summary;
  287. push @binarypackages, $p;
  288. foreach (keys %{$pkg}) {
  289. my $v = $pkg->{$_};
  290. if (m/^Architecture$/) {
  291. # Gather all binary architectures in one set. 'any' and 'all'
  292. # are special-cased as they need to be the only ones in the
  293. # current stanza if present.
  294. if (debarch_eq($v, 'any') || debarch_eq($v, 'all')) {
  295. push(@sourcearch, $v) unless $archadded{$v}++;
  296. } else {
  297. for my $a (split(/\s+/, $v)) {
  298. error(g_("'%s' is not a legal architecture string"), $a)
  299. if debarch_is_illegal($a);
  300. error(g_('architecture %s only allowed on its ' .
  301. "own (list for package %s is '%s')"),
  302. $a, $p, $a)
  303. if $a eq 'any' or $a eq 'all';
  304. push(@sourcearch, $a) unless $archadded{$a}++;
  305. }
  306. }
  307. } elsif (m/^Homepage$/) {
  308. # Do not overwrite the same field from the source entry
  309. } else {
  310. field_transfer_single($pkg, $fields);
  311. }
  312. }
  313. }
  314. unless (scalar(@pkglist)) {
  315. error(g_("%s doesn't list any binary package"), $controlfile);
  316. }
  317. if (any { $_ eq 'any' } @sourcearch) {
  318. # If we encounter one 'any' then the other arches become insignificant
  319. # except for 'all' that must also be kept
  320. if (any { $_ eq 'all' } @sourcearch) {
  321. @sourcearch = qw(any all);
  322. } else {
  323. @sourcearch = qw(any);
  324. }
  325. } else {
  326. # Minimize arch list, by removing arches already covered by wildcards
  327. my @arch_wildcards = grep { debarch_is_wildcard($_) } @sourcearch;
  328. my @mini_sourcearch = @arch_wildcards;
  329. foreach my $arch (@sourcearch) {
  330. if (none { debarch_is($arch, $_) } @arch_wildcards) {
  331. push @mini_sourcearch, $arch;
  332. }
  333. }
  334. @sourcearch = @mini_sourcearch;
  335. }
  336. $fields->{'Architecture'} = join(' ', @sourcearch);
  337. $fields->{'Package-List'} = "\n" . join("\n", sort @pkglist);
  338. # Check if we have a testsuite, and handle manual and automatic values.
  339. set_testsuite_fields($fields, @binarypackages);
  340. # Scan fields of dpkg-parsechangelog
  341. foreach (keys %{$changelog}) {
  342. my $v = $changelog->{$_};
  343. if (m/^Source$/) {
  344. set_source_package($v);
  345. $fields->{$_} = $v;
  346. } elsif (m/^Version$/) {
  347. my ($ok, $error) = version_check($v);
  348. error($error) unless $ok;
  349. $fields->{$_} = $v;
  350. } elsif (m/^Binary-Only$/) {
  351. error(g_('building source for a binary-only release'))
  352. if $v eq 'yes' and $options{opmode} eq 'build';
  353. } elsif (m/^Maintainer$/i) {
  354. # Do not replace the field coming from the source entry
  355. } else {
  356. field_transfer_single($changelog, $fields);
  357. }
  358. }
  359. $fields->{'Binary'} = join(', ', @binarypackages);
  360. # Avoid overly long line by splitting over multiple lines
  361. if (length($fields->{'Binary'}) > 980) {
  362. $fields->{'Binary'} =~ s/(.{0,980}), ?/$1,\n/g;
  363. }
  364. # Select the format to use
  365. if (not defined $build_format) {
  366. if (-e "$dir/debian/source/format") {
  367. open(my $format_fh, '<', "$dir/debian/source/format")
  368. or syserr(g_('cannot read %s'), "$dir/debian/source/format");
  369. $build_format = <$format_fh>;
  370. chomp($build_format) if defined $build_format;
  371. error(g_('%s is empty'), "$dir/debian/source/format")
  372. unless defined $build_format and length $build_format;
  373. close($format_fh);
  374. } else {
  375. warning(g_('no source format specified in %s, ' .
  376. 'see dpkg-source(1)'), 'debian/source/format')
  377. if $options{opmode} eq 'build';
  378. $build_format = '1.0';
  379. }
  380. }
  381. $fields->{'Format'} = $build_format;
  382. $srcpkg->upgrade_object_type(); # Fails if format is unsupported
  383. # Parse command line options
  384. $srcpkg->init_options();
  385. $srcpkg->parse_cmdline_options(@cmdline_options);
  386. if ($options{opmode} eq 'print-format') {
  387. print $fields->{'Format'} . "\n";
  388. exit(0);
  389. } elsif ($options{opmode} eq 'before-build') {
  390. $srcpkg->before_build($dir);
  391. exit(0);
  392. } elsif ($options{opmode} eq 'after-build') {
  393. $srcpkg->after_build($dir);
  394. exit(0);
  395. } elsif ($options{opmode} eq 'commit') {
  396. $srcpkg->commit($dir);
  397. exit(0);
  398. }
  399. # Verify pre-requisites are met
  400. my ($res, $msg) = $srcpkg->can_build($dir);
  401. error(g_("can't build with source format '%s': %s"), $build_format, $msg) unless $res;
  402. # Only -b left
  403. info(g_("using source format '%s'"), $fields->{'Format'});
  404. run_vendor_hook('before-source-build', $srcpkg);
  405. # Build the files (.tar.gz, .diff.gz, etc)
  406. $srcpkg->build($dir);
  407. # Write the .dsc
  408. my $dscname = $srcpkg->get_basename(1) . '.dsc';
  409. info(g_('building %s in %s'), get_source_package(), $dscname);
  410. $srcpkg->write_dsc(filename => $dscname,
  411. remove => \%remove,
  412. override => \%override,
  413. substvars => $substvars);
  414. exit(0);
  415. } elsif ($options{opmode} eq 'extract') {
  416. # Check command line
  417. unless (scalar(@ARGV)) {
  418. usageerr(g_('--%s needs at least one argument, the .dsc'),
  419. $options{opmode});
  420. }
  421. if (scalar(@ARGV) > 2) {
  422. usageerr(g_('--%s takes no more than two arguments'), $options{opmode});
  423. }
  424. my $dsc = shift(@ARGV);
  425. if (-d $dsc) {
  426. usageerr(g_('--%s needs the .dsc file as first argument, not a directory'),
  427. $options{opmode});
  428. }
  429. # Create the object that does everything
  430. my $srcpkg = Dpkg::Source::Package->new(filename => $dsc,
  431. options => \%options);
  432. # Parse command line options
  433. $srcpkg->parse_cmdline_options(@cmdline_options);
  434. # Decide where to unpack
  435. my $newdirectory = $srcpkg->get_basename();
  436. $newdirectory =~ s/_/-/g;
  437. if (@ARGV) {
  438. $newdirectory = File::Spec->catdir(shift(@ARGV));
  439. if (-e $newdirectory) {
  440. error(g_('unpack target exists: %s'), $newdirectory);
  441. }
  442. }
  443. # Various checks before unpacking
  444. unless ($options{no_check}) {
  445. if ($srcpkg->is_signed()) {
  446. $srcpkg->check_signature();
  447. } else {
  448. if ($options{require_valid_signature}) {
  449. error(g_("%s doesn't contain a valid OpenPGP signature"), $dsc);
  450. } else {
  451. warning(g_('extracting unsigned source package (%s)'), $dsc);
  452. }
  453. }
  454. $srcpkg->check_checksums();
  455. }
  456. # Unpack the source package (delegated to Dpkg::Source::Package::*)
  457. info(g_('extracting %s in %s'), $srcpkg->{fields}{'Source'}, $newdirectory);
  458. $srcpkg->extract($newdirectory);
  459. exit(0);
  460. }
  461. sub set_testsuite_fields
  462. {
  463. my ($fields, @binarypackages) = @_;
  464. my $testsuite_field = $fields->{'Testsuite'} // '';
  465. my %testsuite = map { $_ => 1 } split /\s*,\s*/, $testsuite_field;
  466. if (-e "$dir/debian/tests/control") {
  467. error(g_('test control %s is not a regular file'),
  468. 'debian/tests/control') unless -f _;
  469. $testsuite{autopkgtest} = 1;
  470. my $tests = Dpkg::Control::Tests->new();
  471. $tests->load("$dir/debian/tests/control");
  472. set_testsuite_triggers_field($tests, $fields, @binarypackages);
  473. } elsif ($testsuite{autopkgtest}) {
  474. warning(g_('%s field contains value %s, but no tests control file %s'),
  475. 'Testsuite', 'autopkgtest', 'debian/tests/control');
  476. delete $testsuite{autopkgtest};
  477. }
  478. $fields->{'Testsuite'} = join ', ', sort keys %testsuite;
  479. }
  480. sub set_testsuite_triggers_field
  481. {
  482. my ($tests, $fields, @binarypackages) = @_;
  483. my %testdeps;
  484. # Never overwrite a manually defined field.
  485. return if $fields->{'Testsuite-Triggers'};
  486. foreach my $test ($tests->get()) {
  487. next unless $test->{Depends};
  488. my $deps = deps_parse($test->{Depends}, use_arch => 0, tests_dep => 1);
  489. deps_iterate($deps, sub { $testdeps{$_[0]->{package}} = 1 });
  490. }
  491. # Remove our own binaries and meta-depends.
  492. foreach my $pkg (@binarypackages, qw(@ @builddeps@)) {
  493. delete $testdeps{$pkg};
  494. }
  495. $fields->{'Testsuite-Triggers'} = join ', ', sort keys %testdeps;
  496. }
  497. sub setopmode {
  498. my $opmode = shift;
  499. if (defined($options{opmode})) {
  500. usageerr(g_('two commands specified: --%s and --%s'),
  501. $options{opmode}, $opmode);
  502. }
  503. $options{opmode} = $opmode;
  504. }
  505. sub print_option {
  506. my $opt = shift;
  507. my $help;
  508. if (length $opt->{name} > 25) {
  509. $help .= sprintf " %-25s\n%s%s.\n", $opt->{name}, ' ' x 27, $opt->{help};
  510. } else {
  511. $help .= sprintf " %-25s%s.\n", $opt->{name}, $opt->{help};
  512. }
  513. }
  514. sub get_format_help {
  515. $build_format //= '1.0';
  516. my $srcpkg = Dpkg::Source::Package->new();
  517. $srcpkg->{fields}->{'Format'} = $build_format;
  518. $srcpkg->upgrade_object_type(); # Fails if format is unsupported
  519. my @cmdline = $srcpkg->describe_cmdline_options();
  520. my $help_build = my $help_extract = '';
  521. my $help;
  522. foreach my $opt (@cmdline) {
  523. $help_build .= print_option($opt) if $opt->{when} eq 'build';
  524. $help_extract .= print_option($opt) if $opt->{when} eq 'extract';
  525. }
  526. if ($help_build) {
  527. $help .= "\n";
  528. $help .= "Build format $build_format options:\n";
  529. $help .= $help_build || C_('source options', '<none>');
  530. }
  531. if ($help_extract) {
  532. $help .= "\n";
  533. $help .= "Extract format $build_format options:\n";
  534. $help .= $help_extract || C_('source options', '<none>');
  535. }
  536. return $help;
  537. }
  538. sub version {
  539. printf g_("Debian %s version %s.\n"), $Dpkg::PROGNAME, $Dpkg::PROGVERSION;
  540. print g_('
  541. This is free software; see the GNU General Public License version 2 or
  542. later for copying conditions. There is NO warranty.
  543. ');
  544. }
  545. sub usage {
  546. printf g_(
  547. 'Usage: %s [<option>...] <command>')
  548. . "\n\n" . g_(
  549. 'Commands:
  550. -x, --extract <filename>.dsc [<output-dir>]
  551. extract source package.
  552. -b, --build <dir> build source package.
  553. --print-format <dir> print the format to be used for the source package.
  554. --before-build <dir> run the corresponding source package format hook.
  555. --after-build <dir> run the corresponding source package format hook.
  556. --commit [<dir> [<patch-name>]]
  557. store upstream changes in a new patch.')
  558. . "\n\n" . g_(
  559. "Build options:
  560. -c<control-file> get control info from this file.
  561. -l<changelog-file> get per-version info from this file.
  562. -F<changelog-format> force changelog format.
  563. --format=<source-format> set the format to be used for the source package.
  564. -V<name>=<value> set a substitution variable.
  565. -T<substvars-file> read variables here.
  566. -D<field>=<value> override or add a .dsc field and value.
  567. -U<field> remove a field.
  568. -i, --diff-ignore[=<regex>]
  569. filter out files to ignore diffs of
  570. (defaults to: '%s').
  571. -I, --tar-ignore[=<pattern>]
  572. filter out files when building tarballs
  573. (defaults to: %s).
  574. -Z, --compression=<compression>
  575. select compression to use (defaults to '%s',
  576. supported are: %s).
  577. -z, --compression-level=<level>
  578. compression level to use (defaults to '%d',
  579. supported are: '1'-'9', 'best', 'fast')")
  580. . "\n\n" . g_(
  581. "Extract options:
  582. --no-copy don't copy .orig tarballs
  583. --no-check don't check signature and checksums before unpacking
  584. --no-overwrite-dir do not overwrite directory on extraction
  585. --require-valid-signature abort if the package doesn't have a valid signature
  586. --require-strong-checksums
  587. abort if the package contains no strong checksums
  588. --ignore-bad-version allow bad source package versions.")
  589. . "\n" .
  590. get_format_help()
  591. . "\n" . g_(
  592. 'General options:
  593. -q quiet mode.
  594. -?, --help show this help message.
  595. --version show the version.')
  596. . "\n\n" . g_(
  597. 'Source format specific build and extract options are available;
  598. use --format with --help to see them.') . "\n",
  599. $Dpkg::PROGNAME,
  600. get_default_diff_ignore_regex(),
  601. join(' ', map { "-I$_" } get_default_tar_ignore_pattern()),
  602. compression_get_default(),
  603. join(' ', compression_get_list()),
  604. compression_get_default_level();
  605. }