dpkg-source.pl 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  1. #! /usr/bin/perl
  2. #
  3. # dpkg-source
  4. #
  5. # Copyright © 1996 Ian Jackson <ian@davenant.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 Dpkg ();
  30. use Dpkg::Gettext;
  31. use Dpkg::ErrorHandling;
  32. use Dpkg::Util qw(:list);
  33. use Dpkg::Arch qw(debarch_eq debarch_is debarch_is_wildcard);
  34. use Dpkg::Deps;
  35. use Dpkg::Compression;
  36. use Dpkg::Conf;
  37. use Dpkg::Control::Info;
  38. use Dpkg::Control::Fields;
  39. use Dpkg::Substvars;
  40. use Dpkg::Version;
  41. use Dpkg::Vars;
  42. use Dpkg::Changelog::Parse;
  43. use Dpkg::Source::Package qw(get_default_diff_ignore_regex
  44. set_default_diff_ignore_regex
  45. get_default_tar_ignore_pattern);
  46. use Dpkg::Vendor qw(run_vendor_hook);
  47. use Cwd;
  48. use File::Basename;
  49. use File::Spec;
  50. textdomain('dpkg-dev');
  51. my $controlfile;
  52. my $changelogfile;
  53. my $changelogformat;
  54. my $build_format;
  55. my %options = (
  56. # Ignore files
  57. tar_ignore => [],
  58. diff_ignore_regex => '',
  59. # Misc options
  60. copy_orig_tarballs => 1,
  61. no_check => 0,
  62. require_valid_signature => 0,
  63. );
  64. # Fields to remove/override
  65. my %remove;
  66. my %override;
  67. my $substvars = Dpkg::Substvars->new();
  68. my $tar_ignore_default_pattern_done;
  69. my $diff_ignore_regex = get_default_diff_ignore_regex();
  70. my @options;
  71. my @cmdline_options;
  72. while (@ARGV && $ARGV[0] =~ m/^-/) {
  73. $_ = shift(@ARGV);
  74. if (m/^-b$/) {
  75. setopmode('-b');
  76. } elsif (m/^-x$/) {
  77. setopmode('-x');
  78. } elsif (m/^--(before|after)-build$/) {
  79. setopmode($_);
  80. } elsif (m/^--commit$/) {
  81. setopmode($_);
  82. } elsif (m/^--print-format$/) {
  83. setopmode('--print-format');
  84. report_options(info_fh => \*STDERR); # Avoid clutter on STDOUT
  85. } else {
  86. push @options, $_;
  87. }
  88. }
  89. my $dir;
  90. if (defined($options{opmode}) &&
  91. $options{opmode} =~ /^(-b|--print-format|--(before|after)-build|--commit)$/) {
  92. if (not scalar(@ARGV)) {
  93. usageerr(_g('%s needs a directory'), $options{opmode})
  94. unless $1 eq '--commit';
  95. $dir = '.';
  96. } else {
  97. $dir = File::Spec->catdir(shift(@ARGV));
  98. }
  99. stat($dir) or syserr(_g('cannot stat directory %s'), $dir);
  100. if (not -d $dir) {
  101. error(_g('directory argument %s is not a directory'), $dir);
  102. }
  103. if ($dir eq '.') {
  104. # . is never correct, adjust automatically
  105. $dir = basename(cwd());
  106. chdir('..') or syserr(_g("unable to chdir to `%s'"), '..');
  107. }
  108. # --format options are not allowed, they would take precedence
  109. # over real command line options, debian/source/format should be used
  110. # instead
  111. # --unapply-patches is only allowed in local-options as it's a matter
  112. # of personal taste and the default should be to keep patches applied
  113. my $forbidden_opts_re = {
  114. 'options' => qr/^--(?:format=|unapply-patches$|abort-on-upstream-changes$)/,
  115. 'local-options' => qr/^--format=/,
  116. };
  117. foreach my $filename ('local-options', 'options') {
  118. my $conf = Dpkg::Conf->new();
  119. my $optfile = File::Spec->catfile($dir, 'debian', 'source', $filename);
  120. next unless -f $optfile;
  121. $conf->load($optfile);
  122. $conf->filter(remove => sub { $_[0] =~ $forbidden_opts_re->{$filename} });
  123. if (@$conf) {
  124. info(_g('using options from %s: %s'), $optfile, join(' ', @$conf))
  125. unless $options{opmode} eq '--print-format';
  126. unshift @options, @$conf;
  127. }
  128. }
  129. }
  130. while (@options) {
  131. $_ = shift(@options);
  132. if (m/^--format=(.*)$/) {
  133. $build_format //= $1;
  134. } elsif (m/^-(?:Z|-compression=)(.*)$/) {
  135. my $compression = $1;
  136. $options{compression} = $compression;
  137. usageerr(_g('%s is not a supported compression'), $compression)
  138. unless compression_is_supported($compression);
  139. compression_set_default($compression);
  140. } elsif (m/^-(?:z|-compression-level=)(.*)$/) {
  141. my $comp_level = $1;
  142. $options{comp_level} = $comp_level;
  143. usageerr(_g('%s is not a compression level'), $comp_level)
  144. unless compression_is_valid_level($comp_level);
  145. compression_set_default_level($comp_level);
  146. } elsif (m/^-c(.*)$/) {
  147. $controlfile = $1;
  148. } elsif (m/^-l(.*)$/) {
  149. $changelogfile = $1;
  150. } elsif (m/^-F([0-9a-z]+)$/) {
  151. $changelogformat = $1;
  152. } elsif (m/^-D([^\=:]+)[=:](.*)$/s) {
  153. $override{$1} = $2;
  154. } elsif (m/^-U([^\=:]+)$/) {
  155. $remove{$1} = 1;
  156. } elsif (m/^-(?:i|-diff-ignore(?:$|=))(.*)$/) {
  157. $options{diff_ignore_regex} = $1 ? $1 : $diff_ignore_regex;
  158. } elsif (m/^--extend-diff-ignore=(.+)$/) {
  159. $diff_ignore_regex .= "|$1";
  160. if ($options{diff_ignore_regex}) {
  161. $options{diff_ignore_regex} .= "|$1";
  162. }
  163. set_default_diff_ignore_regex($diff_ignore_regex);
  164. } elsif (m/^-(?:I|-tar-ignore=)(.+)$/) {
  165. push @{$options{tar_ignore}}, $1;
  166. } elsif (m/^-(?:I|-tar-ignore)$/) {
  167. unless ($tar_ignore_default_pattern_done) {
  168. push @{$options{tar_ignore}}, get_default_tar_ignore_pattern();
  169. # Prevent adding multiple times
  170. $tar_ignore_default_pattern_done = 1;
  171. }
  172. } elsif (m/^--no-copy$/) {
  173. $options{copy_orig_tarballs} = 0;
  174. } elsif (m/^--no-check$/) {
  175. $options{no_check} = 1;
  176. } elsif (m/^--require-valid-signature$/) {
  177. $options{require_valid_signature} = 1;
  178. } elsif (m/^-V(\w[-:0-9A-Za-z]*)[=:](.*)$/s) {
  179. $substvars->set($1, $2);
  180. } elsif (m/^-T(.*)$/) {
  181. $substvars->load($1) if -e $1;
  182. } elsif (m/^-(\?|-help)$/) {
  183. usage();
  184. exit(0);
  185. } elsif (m/^--version$/) {
  186. version();
  187. exit(0);
  188. } elsif (m/^-[EW]$/) {
  189. # Deprecated option
  190. warning(_g('-E and -W are deprecated, they are without effect'));
  191. } elsif (m/^-q$/) {
  192. report_options(quiet_warnings => 1);
  193. $options{quiet} = 1;
  194. } elsif (m/^--$/) {
  195. last;
  196. } else {
  197. push @cmdline_options, $_;
  198. }
  199. }
  200. unless (defined($options{opmode})) {
  201. usageerr(_g('need a command (-x, -b, --before-build, --after-build, --print-format, --commit)'));
  202. }
  203. if ($options{opmode} =~ /^(-b|--print-format|--(before|after)-build|--commit)$/) {
  204. $options{ARGV} = \@ARGV;
  205. $changelogfile ||= "$dir/debian/changelog";
  206. $controlfile ||= "$dir/debian/control";
  207. my %ch_options = (file => $changelogfile);
  208. $ch_options{changelogformat} = $changelogformat if $changelogformat;
  209. my $changelog = changelog_parse(%ch_options);
  210. my $control = Dpkg::Control::Info->new($controlfile);
  211. my $srcpkg = Dpkg::Source::Package->new(options => \%options);
  212. my $fields = $srcpkg->{fields};
  213. my @sourcearch;
  214. my %archadded;
  215. my @binarypackages;
  216. # Scan control info of source package
  217. my $src_fields = $control->get_source();
  218. error(_g("%s doesn't contain any information about the source package"),
  219. $controlfile) unless defined $src_fields;
  220. my $src_sect = $src_fields->{'Section'} || 'unknown';
  221. my $src_prio = $src_fields->{'Priority'} || 'unknown';
  222. foreach (keys %{$src_fields}) {
  223. my $v = $src_fields->{$_};
  224. if (m/^Source$/i) {
  225. set_source_package($v);
  226. $fields->{$_} = $v;
  227. } elsif (m/^Uploaders$/i) {
  228. ($fields->{$_} = $v) =~ s/\s*[\r\n]\s*/ /g; # Merge in a single-line
  229. } elsif (m/^Build-(Depends|Conflicts)(-Arch|-Indep)?$/i) {
  230. my $dep;
  231. my $type = field_get_dep_type($_);
  232. $dep = deps_parse($v, build_dep => 1, union => $type eq 'union');
  233. error(_g('error occurred while parsing %s'), $_) unless defined $dep;
  234. my $facts = Dpkg::Deps::KnownFacts->new();
  235. $dep->simplify_deps($facts);
  236. $dep->sort() if $type eq 'union';
  237. $fields->{$_} = $dep->output();
  238. } else {
  239. field_transfer_single($src_fields, $fields);
  240. }
  241. }
  242. # Scan control info of binary packages
  243. my @pkglist;
  244. foreach my $pkg ($control->get_packages()) {
  245. my $p = $pkg->{'Package'};
  246. my $sect = $pkg->{'Section'} || $src_sect;
  247. my $prio = $pkg->{'Priority'} || $src_prio;
  248. my $type = $pkg->{'Package-Type'} ||
  249. $pkg->get_custom_field('Package-Type') || 'deb';
  250. my $arch = $pkg->{'Architecture'};
  251. my $profile = $pkg->{'Build-Profiles'};
  252. my $pkg_summary = sprintf('%s %s %s %s', $p, $type, $sect, $prio);
  253. $pkg_summary .= ' arch=' . join ',', split /\s+/, $arch;
  254. $pkg_summary .= ' profile=' . join ',', split /\s+/, $profile
  255. if defined $profile;
  256. push @pkglist, $pkg_summary;
  257. push @binarypackages, $p;
  258. foreach (keys %{$pkg}) {
  259. my $v = $pkg->{$_};
  260. if (m/^Architecture$/) {
  261. # Gather all binary architectures in one set. 'any' and 'all'
  262. # are special-cased as they need to be the only ones in the
  263. # current stanza if present.
  264. if (debarch_eq($v, 'any') || debarch_eq($v, 'all')) {
  265. push(@sourcearch, $v) unless $archadded{$v}++;
  266. } else {
  267. for my $a (split(/\s+/, $v)) {
  268. error(_g("`%s' is not a legal architecture string"),
  269. $a)
  270. unless $a =~ /^[\w-]+$/;
  271. error(_g('architecture %s only allowed on its ' .
  272. "own (list for package %s is `%s')"),
  273. $a, $p, $a)
  274. if $a eq 'any' or $a eq 'all';
  275. push(@sourcearch, $a) unless $archadded{$a}++;
  276. }
  277. }
  278. } elsif (m/^Homepage$/) {
  279. # Do not overwrite the same field from the source entry
  280. } else {
  281. field_transfer_single($pkg, $fields);
  282. }
  283. }
  284. }
  285. unless (scalar(@pkglist)) {
  286. error(_g("%s doesn't list any binary package"), $controlfile);
  287. }
  288. if (any { $_ eq 'any' } @sourcearch) {
  289. # If we encounter one 'any' then the other arches become insignificant
  290. # except for 'all' that must also be kept
  291. if (any { $_ eq 'all' } @sourcearch) {
  292. @sourcearch = qw(any all);
  293. } else {
  294. @sourcearch = qw(any);
  295. }
  296. } else {
  297. # Minimize arch list, by removing arches already covered by wildcards
  298. my @arch_wildcards = grep { debarch_is_wildcard($_) } @sourcearch;
  299. my @mini_sourcearch = @arch_wildcards;
  300. foreach my $arch (@sourcearch) {
  301. if (none { debarch_is($arch, $_) } @arch_wildcards) {
  302. push @mini_sourcearch, $arch;
  303. }
  304. }
  305. @sourcearch = @mini_sourcearch;
  306. }
  307. $fields->{'Architecture'} = join(' ', @sourcearch);
  308. $fields->{'Package-List'} = "\n" . join("\n", sort @pkglist);
  309. # Scan fields of dpkg-parsechangelog
  310. foreach (keys %{$changelog}) {
  311. my $v = $changelog->{$_};
  312. if (m/^Source$/) {
  313. set_source_package($v);
  314. $fields->{$_} = $v;
  315. } elsif (m/^Version$/) {
  316. my ($ok, $error) = version_check($v);
  317. error($error) unless $ok;
  318. $fields->{$_} = $v;
  319. } elsif (m/^Binary-Only$/) {
  320. error(_g('building source for a binary-only release'))
  321. if $v eq 'yes' and $options{opmode} eq '-b';
  322. } elsif (m/^Maintainer$/i) {
  323. # Do not replace the field coming from the source entry
  324. } else {
  325. field_transfer_single($changelog, $fields);
  326. }
  327. }
  328. $fields->{'Binary'} = join(', ', @binarypackages);
  329. # Avoid overly long line by splitting over multiple lines
  330. if (length($fields->{'Binary'}) > 980) {
  331. $fields->{'Binary'} =~ s/(.{0,980}), ?/$1,\n/g;
  332. }
  333. # Select the format to use
  334. if (not defined $build_format) {
  335. if (-e "$dir/debian/source/format") {
  336. open(my $format_fh, '<', "$dir/debian/source/format")
  337. or syserr(_g('cannot read %s'), "$dir/debian/source/format");
  338. $build_format = <$format_fh>;
  339. chomp($build_format) if defined $build_format;
  340. error(_g('%s is empty'), "$dir/debian/source/format")
  341. unless defined $build_format and length $build_format;
  342. close($format_fh);
  343. } else {
  344. warning(_g('no source format specified in %s, ' .
  345. 'see dpkg-source(1)'), 'debian/source/format')
  346. if $options{opmode} eq '-b';
  347. $build_format = '1.0';
  348. }
  349. }
  350. $fields->{'Format'} = $build_format;
  351. $srcpkg->upgrade_object_type(); # Fails if format is unsupported
  352. # Parse command line options
  353. $srcpkg->init_options();
  354. $srcpkg->parse_cmdline_options(@cmdline_options);
  355. if ($options{opmode} eq '--print-format') {
  356. print $fields->{'Format'} . "\n";
  357. exit(0);
  358. } elsif ($options{opmode} eq '--before-build') {
  359. $srcpkg->before_build($dir);
  360. exit(0);
  361. } elsif ($options{opmode} eq '--after-build') {
  362. $srcpkg->after_build($dir);
  363. exit(0);
  364. } elsif ($options{opmode} eq '--commit') {
  365. $srcpkg->commit($dir);
  366. exit(0);
  367. }
  368. # Verify pre-requisites are met
  369. my ($res, $msg) = $srcpkg->can_build($dir);
  370. error(_g("can't build with source format '%s': %s"), $build_format, $msg) unless $res;
  371. # Only -b left
  372. info(_g("using source format `%s'"), $fields->{'Format'});
  373. run_vendor_hook('before-source-build', $srcpkg);
  374. # Build the files (.tar.gz, .diff.gz, etc)
  375. $srcpkg->build($dir);
  376. # Write the .dsc
  377. my $dscname = $srcpkg->get_basename(1) . '.dsc';
  378. info(_g('building %s in %s'), get_source_package(), $dscname);
  379. $srcpkg->write_dsc(filename => $dscname,
  380. remove => \%remove,
  381. override => \%override,
  382. substvars => $substvars);
  383. exit(0);
  384. } elsif ($options{opmode} eq '-x') {
  385. # Check command line
  386. unless (scalar(@ARGV)) {
  387. usageerr(_g('-x needs at least one argument, the .dsc'));
  388. }
  389. if (scalar(@ARGV) > 2) {
  390. usageerr(_g('-x takes no more than two arguments'));
  391. }
  392. my $dsc = shift(@ARGV);
  393. if (-d $dsc) {
  394. usageerr(_g('-x needs the .dsc file as first argument, not a directory'));
  395. }
  396. # Create the object that does everything
  397. my $srcpkg = Dpkg::Source::Package->new(filename => $dsc,
  398. options => \%options);
  399. # Parse command line options
  400. $srcpkg->parse_cmdline_options(@cmdline_options);
  401. # Decide where to unpack
  402. my $newdirectory = $srcpkg->get_basename();
  403. $newdirectory =~ s/_/-/g;
  404. if (@ARGV) {
  405. $newdirectory = File::Spec->catdir(shift(@ARGV));
  406. if (-e $newdirectory) {
  407. error(_g('unpack target exists: %s'), $newdirectory);
  408. }
  409. }
  410. # Various checks before unpacking
  411. unless ($options{no_check}) {
  412. if ($srcpkg->is_signed()) {
  413. $srcpkg->check_signature();
  414. } else {
  415. if ($options{require_valid_signature}) {
  416. error(_g("%s doesn't contain a valid OpenPGP signature"), $dsc);
  417. } else {
  418. warning(_g('extracting unsigned source package (%s)'), $dsc);
  419. }
  420. }
  421. $srcpkg->check_checksums();
  422. }
  423. # Unpack the source package (delegated to Dpkg::Source::Package::*)
  424. info(_g('extracting %s in %s'), $srcpkg->{fields}{'Source'}, $newdirectory);
  425. $srcpkg->extract($newdirectory);
  426. exit(0);
  427. }
  428. sub setopmode {
  429. if (defined($options{opmode})) {
  430. usageerr(_g('only one of -x, -b or --print-format allowed, and only once'));
  431. }
  432. $options{opmode} = $_[0];
  433. }
  434. sub version {
  435. printf _g("Debian %s version %s.\n"), $Dpkg::PROGNAME, $Dpkg::PROGVERSION;
  436. print _g('
  437. This is free software; see the GNU General Public License version 2 or
  438. later for copying conditions. There is NO warranty.
  439. ');
  440. }
  441. sub usage {
  442. printf _g(
  443. 'Usage: %s [<option>...] <command>')
  444. . "\n\n" . _g(
  445. 'Commands:
  446. -x <filename>.dsc [<output-dir>]
  447. extract source package.
  448. -b <dir> build source package.
  449. --print-format <dir> print the source format that would be
  450. used to build the source package.
  451. --commit [<dir> [<patch-name>]]
  452. store upstream changes in a new patch.')
  453. . "\n\n" . _g(
  454. "Build options:
  455. -c<control-file> get control info from this file.
  456. -l<changelog-file> get per-version info from this file.
  457. -F<changelog-format> force changelog format.
  458. -V<name>=<value> set a substitution variable.
  459. -T<substvars-file> read variables here.
  460. -D<field>=<value> override or add a .dsc field and value.
  461. -U<field> remove a field.
  462. -q quiet mode.
  463. -i[<regex>] filter out files to ignore diffs of
  464. (defaults to: '%s').
  465. -I[<pattern>] filter out files when building tarballs
  466. (defaults to: %s).
  467. -Z<compression> select compression to use (defaults to '%s',
  468. supported are: %s).
  469. -z<level> compression level to use (defaults to '%d',
  470. supported are: '1'-'9', 'best', 'fast')")
  471. . "\n\n" . _g(
  472. "Extract options:
  473. --no-copy don't copy .orig tarballs
  474. --no-check don't check signature and checksums before unpacking
  475. --require-valid-signature abort if the package doesn't have a valid signature")
  476. . "\n\n" . _g(
  477. 'General options:
  478. -?, --help show this help message.
  479. --version show the version.')
  480. . "\n\n" . _g(
  481. 'More options are available but they depend on the source package format.
  482. See dpkg-source(1) for more info.') . "\n",
  483. $Dpkg::PROGNAME,
  484. get_default_diff_ignore_regex(),
  485. join(' ', map { "-I$_" } get_default_tar_ignore_pattern()),
  486. compression_get_default(),
  487. join(' ', compression_get_list()),
  488. compression_get_default_level();
  489. }