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>
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.
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.
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/>.
31 use List
::Util
qw(any none);
38 use Dpkg
::ErrorHandling
;
39 use Dpkg
::Arch
qw(:operators);
41 use Dpkg
::Compression
;
43 use Dpkg
::Control
::Info
;
44 use Dpkg
::Control
::Tests
;
45 use Dpkg
::Control
::Fields
;
49 use Dpkg
::Changelog
::Parse
;
50 use Dpkg
::Source
::Format
;
51 use Dpkg
::Source
::Package
qw(get_default_diff_ignore_regex
52 set_default_diff_ignore_regex
53 get_default_tar_ignore_pattern);
54 use Dpkg
::Vendor
qw(run_vendor_hook);
56 textdomain
('dpkg-dev');
65 diff_ignore_regex
=> '',
67 copy_orig_tarballs
=> 1,
69 no_overwrite_dir
=> 1,
70 require_valid_signature
=> 0,
71 require_strong_checksums
=> 0,
74 # Fields to remove/override
78 my $substvars = Dpkg
::Substvars
->new();
79 my $tar_ignore_default_pattern_done;
80 my $diff_ignore_regex = get_default_diff_ignore_regex
();
84 while (@ARGV && $ARGV[0] =~ m/^-/) {
85 my $arg = shift @ARGV;
87 if ($arg eq '-b' or $arg eq '--build') {
89 } elsif ($arg eq '-x' or $arg eq '--extract') {
91 } elsif ($arg eq '--before-build') {
92 setopmode
('before-build');
93 } elsif ($arg eq '--after-build') {
94 setopmode
('after-build');
95 } elsif ($arg eq '--commit') {
97 } elsif ($arg eq '--print-format') {
98 setopmode
('print-format');
99 report_options
(info_fh
=> \
*STDERR
); # Avoid clutter on STDOUT
106 if (defined($options{opmode
}) &&
107 $options{opmode
} =~ /^(build|print-format|(before|after)-build|commit)$/) {
108 if (not scalar(@ARGV)) {
109 usageerr
(g_
('--%s needs a directory'), $options{opmode
})
110 unless $1 eq 'commit';
113 $dir = File
::Spec
->catdir(shift(@ARGV));
115 stat($dir) or syserr
(g_
('cannot stat directory %s'), $dir);
117 error
(g_
('directory argument %s is not a directory'), $dir);
120 # . is never correct, adjust automatically
121 $dir = basename
(getcwd
());
122 chdir '..' or syserr
(g_
("unable to chdir to '%s'"), '..');
124 # --format options are not allowed, they would take precedence
125 # over real command line options, debian/source/format should be used
127 # --unapply-patches is only allowed in local-options as it's a matter
128 # of personal taste and the default should be to keep patches applied
129 my $forbidden_opts_re = {
130 'options' => qr/^--(?:format=|unapply-patches$|abort-on-upstream-changes$)/,
131 'local-options' => qr/^--format=/,
133 foreach my $filename ('local-options', 'options') {
134 my $conf = Dpkg
::Conf
->new();
135 my $optfile = File
::Spec
->catfile($dir, 'debian', 'source', $filename);
136 next unless -f
$optfile;
137 $conf->load($optfile);
138 $conf->filter(remove
=> sub { $_[0] =~ $forbidden_opts_re->{$filename} });
140 info
(g_
('using options from %s: %s'), $optfile, join(' ', @
$conf))
141 unless $options{opmode
} eq 'print-format';
142 unshift @options, @
$conf;
148 $_ = shift(@options);
149 if (m/^--format=(.*)$/) {
150 $build_format //= $1;
151 } elsif (m/^-(?:Z|-compression=)(.*)$/) {
152 my $compression = $1;
153 $options{compression
} = $compression;
154 usageerr
(g_
('%s is not a supported compression'), $compression)
155 unless compression_is_supported
($compression);
156 compression_set_default
($compression);
157 } elsif (m/^-(?:z|-compression-level=)(.*)$/) {
159 $options{comp_level
} = $comp_level;
160 usageerr
(g_
('%s is not a compression level'), $comp_level)
161 unless compression_is_valid_level
($comp_level);
162 compression_set_default_level
($comp_level);
163 } elsif (m/^--threads-max=(.*)$/) {
165 $options{comp_threads
} = $threads;
166 compression_set_threads
($threads);
167 } elsif (m/^-c(.*)$/) {
169 } elsif (m/^-l(.*)$/) {
170 $options{changelog_file
} = $1;
171 } elsif (m/^-F([0-9a-z]+)$/) {
172 $changelogformat = $1;
173 } elsif (m/^-D([^\=:]+)[=:](.*)$/s) {
175 } elsif (m/^-U([^\=:]+)$/) {
177 } elsif (m/^--diff-ignore$/) {
178 $options{diff_ignore_regex
} = $diff_ignore_regex;
179 } elsif (m/^-(?:i|-diff-ignore=)(.*)$/) {
180 $options{diff_ignore_regex
} = $1 ?
$1 : $diff_ignore_regex;
181 } elsif (m/^--extend-diff-ignore=(.+)$/) {
182 $diff_ignore_regex .= "|$1";
183 if ($options{diff_ignore_regex
}) {
184 $options{diff_ignore_regex
} .= "|$1";
186 set_default_diff_ignore_regex
($diff_ignore_regex);
187 } elsif (m/^-(?:I|-tar-ignore=)(.+)$/) {
188 push @
{$options{tar_ignore
}}, $1;
189 } elsif (m/^-(?:I|-tar-ignore)$/) {
190 unless ($tar_ignore_default_pattern_done) {
191 push @
{$options{tar_ignore
}}, get_default_tar_ignore_pattern
();
192 # Prevent adding multiple times
193 $tar_ignore_default_pattern_done = 1;
195 } elsif (m/^--no-copy$/) {
196 $options{copy_orig_tarballs
} = 0;
197 } elsif (m/^--no-check$/) {
198 $options{no_check
} = 1;
199 } elsif (m/^--no-overwrite-dir$/) {
200 $options{no_overwrite_dir
} = 1;
201 } elsif (m/^--require-valid-signature$/) {
202 $options{require_valid_signature
} = 1;
203 } elsif (m/^--require-strong-checksums$/) {
204 $options{require_strong_checksums
} = 1;
205 } elsif (m/^-V(\w[-:0-9A-Za-z]*)[=:](.*)$/s) {
206 $substvars->set($1, $2);
207 } elsif (m/^-T(.*)$/) {
208 $substvars->load($1) if -e
$1;
209 } elsif (m/^-(?:\?|-help)$/) {
212 } elsif (m/^--version$/) {
215 } elsif (m/^-[EW]$/) {
217 warning
(g_
('-E and -W are deprecated, they are without effect'));
219 report_options
(quiet_warnings
=> 1);
224 push @cmdline_options, $_;
228 unless (defined($options{opmode
})) {
229 usageerr
(g_
('need an action option'));
232 if ($options{opmode
} =~ /^(build|print-format|(before|after)-build|commit)$/) {
233 $options{ARGV
} = \
@ARGV;
234 $options{changelog_file
} ||= "$dir/debian/changelog";
235 $controlfile ||= "$dir/debian/control";
237 my %ch_options = (file
=> $options{changelog_file
});
238 $ch_options{changelogformat
} = $changelogformat if $changelogformat;
239 my $changelog = changelog_parse
(%ch_options);
240 my $control = Dpkg
::Control
::Info
->new($controlfile);
242 # <https://reproducible-builds.org/specs/source-date-epoch/>
243 $ENV{SOURCE_DATE_EPOCH
} ||= $changelog->{timestamp
} || time;
245 # Select the format to use
246 if (not defined $build_format) {
247 my $format_file = "$dir/debian/source/format";
248 if (-e
$format_file) {
249 my $format = Dpkg
::Source
::Format
->new(filename
=> $format_file);
250 $build_format = $format->get();
252 warning
(g_
('no source format specified in %s, ' .
253 'see dpkg-source(1)'), 'debian/source/format')
254 if $options{opmode
} eq 'build';
255 $build_format = '1.0';
259 my $srcpkg = Dpkg
::Source
::Package
->new(format
=> $build_format,
260 options
=> \
%options);
261 my $fields = $srcpkg->{fields
};
263 $srcpkg->parse_cmdline_options(@cmdline_options);
269 # Scan control info of source package
270 my $src_fields = $control->get_source();
271 error
(g_
("%s doesn't contain any information about the source package"),
272 $controlfile) unless defined $src_fields;
273 my $src_sect = $src_fields->{'Section'} || 'unknown';
274 my $src_prio = $src_fields->{'Priority'} || 'unknown';
275 foreach my $f (keys %{$src_fields}) {
276 my $v = $src_fields->{$f};
278 if ($f eq 'Source') {
281 } elsif ($f eq 'Uploaders') {
282 # Merge in a single-line.
283 ($fields->{$f} = $v) =~ s/\s*[\r\n]\s*/ /g;
284 } elsif (any
{ $f eq $_ } field_list_src_dep
()) {
286 my $type = field_get_dep_type
($f);
287 $dep = deps_parse
($v, build_dep
=> 1, union
=> $type eq 'union');
288 error
(g_
('cannot parse %s field'), $f) unless defined $dep;
289 my $facts = Dpkg
::Deps
::KnownFacts
->new();
290 $dep->simplify_deps($facts);
291 $dep->sort() if $type eq 'union';
292 $fields->{$f} = $dep->output();
294 field_transfer_single
($src_fields, $fields, $f);
298 # Scan control info of binary packages
300 foreach my $pkg ($control->get_packages()) {
301 my $p = $pkg->{'Package'};
302 my $sect = $pkg->{'Section'} || $src_sect;
303 my $prio = $pkg->{'Priority'} || $src_prio;
304 my $type = $pkg->{'Package-Type'} ||
305 $pkg->get_custom_field('Package-Type') || 'deb';
306 my $arch = $pkg->{'Architecture'};
307 my $profile = $pkg->{'Build-Profiles'};
309 my $pkg_summary = sprintf('%s %s %s %s', $p, $type, $sect, $prio);
311 $pkg_summary .= ' arch=' . join ',', split ' ', $arch;
313 if (defined $profile) {
314 # Instead of splitting twice and then joining twice, we just do
315 # simple string replacements:
317 # Remove the enclosing <>
318 $profile =~ s/^\s*<(.*)>\s*$/$1/;
319 # Join lists with a plus (OR)
320 $profile =~ s/>\s+</+/g;
321 # Join their elements with a comma (AND)
322 $profile =~ s/\s+/,/g;
323 $pkg_summary .= " profile=$profile";
325 if (defined $pkg->{'Protected'} and $pkg->{'Protected'} eq 'yes') {
326 $pkg_summary .= ' protected=yes';
328 if (defined $pkg->{'Essential'} and $pkg->{'Essential'} eq 'yes') {
329 $pkg_summary .= ' essential=yes';
332 push @pkglist, $pkg_summary;
333 push @binarypackages, $p;
334 foreach my $f (keys %{$pkg}) {
337 if ($f eq 'Architecture') {
338 # Gather all binary architectures in one set. 'any' and 'all'
339 # are special-cased as they need to be the only ones in the
340 # current stanza if present.
341 if (debarch_eq
($v, 'any') || debarch_eq
($v, 'all')) {
342 push(@sourcearch, $v) unless $archadded{$v}++;
344 for my $a (split(/\s+/, $v)) {
345 error
(g_
("'%s' is not a legal architecture string " .
346 "in package '%s'"), $a, $p)
347 if debarch_is_illegal
($a);
348 error
(g_
('architecture %s only allowed on its ' .
349 "own (list for package %s is '%s')"),
351 if $a eq 'any' or $a eq 'all';
352 push(@sourcearch, $a) unless $archadded{$a}++;
355 } elsif (any
{ $f eq $_ } qw(Homepage Description)) {
356 # Do not overwrite the same field from the source entry
358 field_transfer_single
($pkg, $fields, $f);
362 unless (scalar(@pkglist)) {
363 error
(g_
("%s doesn't list any binary package"), $controlfile);
365 if (any
{ $_ eq 'any' } @sourcearch) {
366 # If we encounter one 'any' then the other arches become insignificant
367 # except for 'all' that must also be kept
368 if (any
{ $_ eq 'all' } @sourcearch) {
369 @sourcearch = qw(any all);
371 @sourcearch = qw(any);
374 # Minimize arch list, by removing arches already covered by wildcards
375 my @arch_wildcards = grep { debarch_is_wildcard
($_) } @sourcearch;
376 my @mini_sourcearch = @arch_wildcards;
377 foreach my $arch (@sourcearch) {
378 if (none
{ debarch_is
($arch, $_) } @arch_wildcards) {
379 push @mini_sourcearch, $arch;
382 @sourcearch = @mini_sourcearch;
384 $fields->{'Architecture'} = join(' ', @sourcearch);
385 $fields->{'Package-List'} = "\n" . join("\n", sort @pkglist);
387 # Check if we have a testsuite, and handle manual and automatic values.
388 set_testsuite_fields
($fields, @binarypackages);
390 # Scan fields of dpkg-parsechangelog
391 foreach my $f (keys %{$changelog}) {
392 my $v = $changelog->{$f};
394 if ($f eq 'Source') {
397 } elsif ($f eq 'Version') {
398 my ($ok, $error) = version_check
($v);
399 error
($error) unless $ok;
401 } elsif ($f eq 'Binary-Only') {
402 error
(g_
('building source for a binary-only release'))
403 if $v eq 'yes' and $options{opmode
} eq 'build';
404 } elsif ($f eq 'Maintainer') {
405 # Do not replace the field coming from the source entry
407 field_transfer_single
($changelog, $fields, $f);
411 $fields->{'Binary'} = join(', ', @binarypackages);
412 # Avoid overly long line by splitting over multiple lines
413 if (length($fields->{'Binary'}) > 980) {
414 $fields->{'Binary'} =~ s/(.{0,980}), ?/$1,\n/g;
417 if ($options{opmode
} eq 'print-format') {
418 print $fields->{'Format'} . "\n";
420 } elsif ($options{opmode
} eq 'before-build') {
421 $srcpkg->before_build($dir);
423 } elsif ($options{opmode
} eq 'after-build') {
424 $srcpkg->after_build($dir);
426 } elsif ($options{opmode
} eq 'commit') {
427 $srcpkg->commit($dir);
431 # Verify pre-requisites are met
432 my ($res, $msg) = $srcpkg->can_build($dir);
433 error
(g_
("can't build with source format '%s': %s"), $build_format, $msg) unless $res;
436 info
(g_
("using source format '%s'"), $fields->{'Format'});
437 run_vendor_hook
('before-source-build', $srcpkg);
438 # Build the files (.tar.gz, .diff.gz, etc)
439 $srcpkg->build($dir);
442 my $dscname = $srcpkg->get_basename(1) . '.dsc';
443 info
(g_
('building %s in %s'), get_source_name
(), $dscname);
444 $srcpkg->write_dsc(filename
=> $dscname,
446 override
=> \
%override,
447 substvars
=> $substvars);
449 } elsif ($options{opmode
} eq 'extract') {
451 unless (scalar(@ARGV)) {
452 usageerr
(g_
('--%s needs at least one argument, the .dsc'),
455 if (scalar(@ARGV) > 2) {
456 usageerr
(g_
('--%s takes no more than two arguments'), $options{opmode
});
458 my $dsc = shift(@ARGV);
460 usageerr
(g_
('--%s needs the .dsc file as first argument, not a directory'),
464 # Create the object that does everything
465 my $srcpkg = Dpkg
::Source
::Package
->new(filename
=> $dsc,
466 options
=> \
%options);
468 # Parse command line options
469 $srcpkg->parse_cmdline_options(@cmdline_options);
471 # Decide where to unpack
472 my $newdirectory = $srcpkg->get_basename();
473 $newdirectory =~ s/_/-/g;
475 $newdirectory = File
::Spec
->catdir(shift(@ARGV));
476 if (-e
$newdirectory) {
477 error
(g_
('unpack target exists: %s'), $newdirectory);
481 # Various checks before unpacking
482 unless ($options{no_check
}) {
483 if ($srcpkg->is_signed()) {
484 $srcpkg->check_signature();
486 if ($options{require_valid_signature
}) {
487 error
(g_
("%s doesn't contain a valid OpenPGP signature"), $dsc);
489 warning
(g_
('extracting unsigned source package (%s)'), $dsc);
492 $srcpkg->check_checksums();
495 # Unpack the source package (delegated to Dpkg::Source::Package::*)
496 info
(g_
('extracting %s in %s'), $srcpkg->{fields
}{'Source'}, $newdirectory);
497 $srcpkg->extract($newdirectory);
502 sub set_testsuite_fields
504 my ($fields, @binarypackages) = @_;
506 my $testsuite_field = $fields->{'Testsuite'} // '';
507 my %testsuite = map { $_ => 1 } split /\s*,\s*/, $testsuite_field;
508 if (-e
"$dir/debian/tests/control") {
509 error
(g_
('test control %s is not a regular file'),
510 'debian/tests/control') unless -f _
;
511 $testsuite{autopkgtest
} = 1;
513 my $tests = Dpkg
::Control
::Tests
->new();
514 $tests->load("$dir/debian/tests/control");
516 set_testsuite_triggers_field
($tests, $fields, @binarypackages);
517 } elsif ($testsuite{autopkgtest
}) {
518 warning
(g_
('%s field contains value %s, but no tests control file %s'),
519 'Testsuite', 'autopkgtest', 'debian/tests/control');
520 delete $testsuite{autopkgtest
};
522 $fields->{'Testsuite'} = join ', ', sort keys %testsuite;
525 sub set_testsuite_triggers_field
527 my ($tests, $fields, @binarypackages) = @_;
530 # Never overwrite a manually defined field.
531 return if $fields->{'Testsuite-Triggers'};
533 foreach my $test ($tests->get()) {
534 if (not exists $test->{Tests
} and not exists $test->{'Test-Command'}) {
535 error
(g_
('test control %s is missing %s or %s field'),
536 'debian/tests/control', 'Tests', 'Test-Command');
539 next unless $test->{Depends
};
541 my $deps = deps_parse
($test->{Depends
}, use_arch
=> 0, tests_dep
=> 1);
542 deps_iterate
($deps, sub { $testdeps{$_[0]->{package}} = 1 });
545 # Remove our own binaries and its meta-depends variant.
546 foreach my $pkg (@binarypackages, qw(@)) {
547 delete $testdeps{$pkg};
549 $fields->{'Testsuite-Triggers'} = join ', ', sort keys %testdeps;
555 if (defined($options{opmode
})) {
556 usageerr
(g_
('two commands specified: --%s and --%s'),
557 $options{opmode
}, $opmode);
559 $options{opmode
} = $opmode;
565 my $help = gettext
($opt->{help
});
566 if (length $opt->{name
} > 25) {
567 return sprintf " %-25s\n%s%s.\n", $opt->{name
}, ' ' x
27, $help;
569 return sprintf " %-25s%s.\n", $opt->{name
}, $help;
573 sub get_format_help
{
574 $build_format //= '1.0';
576 my $srcpkg = Dpkg
::Source
::Package
->new(format
=> $build_format);
578 my @cmdline = $srcpkg->describe_cmdline_options();
579 return '' unless @cmdline;
581 my $help_build = my $help_extract = '';
584 foreach my $opt (@cmdline) {
585 $help_build .= print_option
($opt) if $opt->{when} eq 'build';
586 $help_extract .= print_option
($opt) if $opt->{when} eq 'extract';
591 $help .= "Build format $build_format options:\n";
592 $help .= $help_build || C_
('source options', '<none>');
596 $help .= "Extract format $build_format options:\n";
597 $help .= $help_extract || C_
('source options', '<none>');
604 printf g_
("Debian %s version %s.\n"), $Dpkg::PROGNAME
, $Dpkg::PROGVERSION
;
607 This is free software; see the GNU General Public License version 2 or
608 later for copying conditions. There is NO warranty.
614 'Usage: %s [<option>...] <command>')
617 -x, --extract <filename>.dsc [<output-dir>]
618 extract source package.
619 -b, --build <dir> build source package.
620 --print-format <dir> print the format to be used for the source package.
621 --before-build <dir> run the corresponding source package format hook.
622 --after-build <dir> run the corresponding source package format hook.
623 --commit [<dir> [<patch-name>]]
624 store upstream changes in a new patch.')
627 -c<control-file> get control info from this file.
628 -l<changelog-file> get per-version info from this file.
629 -F<changelog-format> force changelog format.
630 --format=<source-format> set the format to be used for the source package.
631 -V<name>=<value> set a substitution variable.
632 -T<substvars-file> read variables here.
633 -D<field>=<value> override or add a .dsc field and value.
634 -U<field> remove a field.
635 -i, --diff-ignore[=<regex>]
636 filter out files to ignore diffs of
638 -I, --tar-ignore[=<pattern>]
639 filter out files when building tarballs
641 -Z, --compression=<compression>
642 select compression to use (defaults to '%s',
644 -z, --compression-level=<level>
645 compression level to use (defaults to '%d',
646 supported are: '1'-'9', 'best', 'fast')")
649 --no-copy don't copy .orig tarballs
650 --no-check don't check signature and checksums before unpacking
651 --no-overwrite-dir do not overwrite directory on extraction
652 --require-valid-signature abort if the package doesn't have a valid signature
653 --require-strong-checksums
654 abort if the package contains no strong checksums
655 --ignore-bad-version allow bad source package versions.")
660 --threads-max=<threads>
661 use at most <threads> with compressor.
663 -?, --help show this help message.
664 --version show the version.')
666 'Source format specific build and extract options are available;
667 use --format with --help to see them.') . "\n",
669 get_default_diff_ignore_regex
(),
670 join(' ', map { "-I$_" } get_default_tar_ignore_pattern
()),
671 compression_get_default
(),
672 join(' ', compression_get_list
()),
673 compression_get_default_level
();