dpkg (1.18.25) stretch; urgency=medium
[dpkg] / scripts / dpkg-buildpackage.pl
1 #!/usr/bin/perl
2 #
3 # dpkg-buildpackage
4 #
5 # Copyright © 1996 Ian Jackson
6 # Copyright © 2000 Wichert Akkerman
7 # Copyright © 2006-2010, 2012-2015 Guillem Jover <guillem@debian.org>
8 # Copyright © 2007 Frank Lichtenheld
9 #
10 # This program is free software; you can redistribute it and/or modify
11 # it under the terms of the GNU General Public License as published by
12 # the Free Software Foundation; either version 2 of the License, or
13 # (at your option) any later version.
14 #
15 # This program is distributed in the hope that it will be useful,
16 # but WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 # GNU General Public License for more details.
19 #
20 # You should have received a copy of the GNU General Public License
21 # along with this program. If not, see <https://www.gnu.org/licenses/>.
22
23 use strict;
24 use warnings;
25
26 use Cwd;
27 use File::Temp qw(tempdir);
28 use File::Basename;
29 use File::Copy;
30 use POSIX qw(:sys_wait_h);
31
32 use Dpkg ();
33 use Dpkg::Gettext;
34 use Dpkg::ErrorHandling;
35 use Dpkg::Build::Types;
36 use Dpkg::BuildOptions;
37 use Dpkg::BuildProfiles qw(set_build_profiles);
38 use Dpkg::Conf;
39 use Dpkg::Compression;
40 use Dpkg::Checksums;
41 use Dpkg::Package;
42 use Dpkg::Version;
43 use Dpkg::Control;
44 use Dpkg::Control::Info;
45 use Dpkg::Changelog::Parse;
46 use Dpkg::Path qw(find_command);
47 use Dpkg::IPC;
48
49 textdomain('dpkg-dev');
50
51 sub showversion {
52 printf g_("Debian %s version %s.\n"), $Dpkg::PROGNAME, $Dpkg::PROGVERSION;
53
54 print g_('
55 This is free software; see the GNU General Public License version 2 or
56 later for copying conditions. There is NO warranty.
57 ');
58 }
59
60 sub usage {
61 printf g_(
62 'Usage: %s [<option>...]')
63 . "\n\n" . g_(
64 'Options:
65 --build=<type>[,...] specify the build <type>: full, source, binary,
66 any, all (default is \'full\').
67 -F normal full build (source and binary; default).
68 -g source and arch-indep build.
69 -G source and arch-specific build.
70 -b binary-only, no source files.
71 -B binary-only, only arch-specific files.
72 -A binary-only, only arch-indep files.
73 -S source-only, no binary files.
74 -nc, --no-pre-clean do not pre clean source tree (implies -b).
75 --pre-clean pre clean source tree (default).
76 -tc, --post-clean clean source tree when finished.
77 -D check build dependencies and conflicts (default).
78 -d do not check build dependencies and conflicts.
79 --[no-]check-builddeps ditto.
80 --ignore-builtin-builddeps
81 do not check builtin build dependencies.
82 -P, --build-profiles=<profiles>
83 assume comma-separated build profiles as active.
84 -R, --rules-file=<rules> rules file to execute (default is debian/rules).
85 -T, --rules-target=<target> call debian/rules <target>.
86 --as-root ensure -T calls the target with root rights.
87 -j, --jobs[=<number>|auto] jobs to run simultaneously (passed to <rules>),
88 forced mode.
89 -J, --jobs-try[=<number>|auto]
90 jobs to run simultaneously (passed to <rules>),
91 opt-in mode (default is auto).
92 -r, --root-command=<command>
93 command to gain root rights (default is fakeroot).
94 --check-command=<command>
95 command to check the .changes file (no default).
96 --check-option=<opt> pass <opt> to check <command>.
97 --hook-<name>=<command> set <command> as the hook <name>, known hooks:
98 init preclean source build binary buildinfo
99 changes postclean check sign done
100 --buildinfo-option=<opt>
101 pass option <opt> to dpkg-genbuildinfo.
102 -p, --sign-command=<command>
103 command to sign .dsc and/or .changes files
104 (default is gpg2 or gpg).
105 -k, --sign-key=<keyid> the key to use for signing.
106 -ap, --sign-pause add pause before starting signature process.
107 -us, --unsigned-source unsigned source package.
108 -ui, --unsigned-buildinfo unsigned .buildinfo file.
109 -uc, --unsigned-changes unsigned .buildinfo and .changes file.
110 --no-sign do not sign any file.
111 --force-sign force signing the resulting files.
112 --admindir=<directory> change the administrative directory.
113 -?, --help show this help message.
114 --version show the version.')
115 . "\n\n" . g_(
116 'Options passed to dpkg-architecture:
117 -a, --host-arch <arch> set the host Debian architecture.
118 -t, --host-type <type> set the host GNU system type.
119 --target-arch <arch> set the target Debian architecture.
120 --target-type <type> set the target GNU system type.')
121 . "\n\n" . g_(
122 'Options passed to dpkg-genchanges:
123 -si source includes orig, if new upstream (default).
124 -sa source includes orig, always.
125 -sd source is diff and .dsc only.
126 -v<version> changes since version <version>.
127 -m, --release-by=<maint> maintainer for this release is <maint>.
128 -e, --build-by=<maint> maintainer for this build is <maint>.
129 -C<descfile> changes are described in <descfile>.
130 --changes-option=<opt> pass option <opt> to dpkg-genchanges.')
131 . "\n\n" . g_(
132 'Options passed to dpkg-source:
133 -sn force Debian native source format.
134 -s[sAkurKUR] see dpkg-source for explanation.
135 -z, --compression-level=<level>
136 compression level to use for source.
137 -Z, --compression=<compressor>
138 compression to use for source (gz|xz|bzip2|lzma).
139 -i, --diff-ignore[=<regex>] ignore diffs of files matching <regex>.
140 -I, --tar-ignore[=<pattern>]
141 filter out files when building tarballs.
142 --source-option=<opt> pass option <opt> to dpkg-source.
143 '), $Dpkg::PROGNAME;
144 }
145
146 my $admindir;
147 my @debian_rules = ('debian/rules');
148 my @rootcommand = ();
149 my $signcommand;
150 my $noclean;
151 my $cleansource;
152 my $parallel;
153 my $parallel_force = 0;
154 my $checkbuilddep = 1;
155 my $check_builtin_builddep = 1;
156 my @source_opts;
157 my $check_command = $ENV{DEB_CHECK_COMMAND};
158 my @check_opts;
159 my $signpause;
160 my $signkey = $ENV{DEB_SIGN_KEYID};
161 my $signforce = 0;
162 my $signreleased = 1;
163 my $signsource = 1;
164 my $signbuildinfo = 1;
165 my $signchanges = 1;
166 my $buildtarget = 'build';
167 my $binarytarget = 'binary';
168 my $host_arch = '';
169 my $host_type = '';
170 my $target_arch = '';
171 my $target_type = '';
172 my @build_profiles = ();
173 my @call_target = ();
174 my $call_target_as_root = 0;
175 my $since;
176 my $maint;
177 my $changedby;
178 my $desc;
179 my @buildinfo_opts;
180 my @changes_opts;
181 my @hook_names = qw(
182 init preclean source build binary buildinfo changes postclean check sign done
183 );
184 my %hook;
185 $hook{$_} = undef foreach @hook_names;
186
187
188 my $conf = Dpkg::Conf->new();
189 $conf->load_config('buildpackage.conf');
190
191 # Inject config options for command-line parser.
192 unshift @ARGV, @{$conf};
193
194 my $build_opts = Dpkg::BuildOptions->new();
195
196 if ($build_opts->has('nocheck')) {
197 $check_command = undef;
198 } elsif (not find_command($check_command)) {
199 $check_command = undef;
200 }
201
202 while (@ARGV) {
203 $_ = shift @ARGV;
204
205 if (/^(?:--help|-\?)$/) {
206 usage;
207 exit 0;
208 } elsif (/^--version$/) {
209 showversion;
210 exit 0;
211 } elsif (/^--admindir$/) {
212 $admindir = shift @ARGV;
213 } elsif (/^--admindir=(.*)$/) {
214 $admindir = $1;
215 } elsif (/^--source-option=(.*)$/) {
216 push @source_opts, $1;
217 } elsif (/^--buildinfo-option=(.*)$/) {
218 push @buildinfo_opts, $1;
219 } elsif (/^--changes-option=(.*)$/) {
220 push @changes_opts, $1;
221 } elsif (/^(?:-j|--jobs=)(\d*|auto)$/) {
222 $parallel = $1 || '';
223 $parallel_force = 1;
224 } elsif (/^(?:-J|--jobs-try=)(\d*|auto)$/) {
225 $parallel = $1 || '';
226 $parallel_force = 0;
227 } elsif (/^(?:-r|--root-command=)(.*)$/) {
228 my $arg = $1;
229 @rootcommand = split /\s+/, $arg;
230 } elsif (/^--check-command=(.*)$/) {
231 $check_command = $1;
232 } elsif (/^--check-option=(.*)$/) {
233 push @check_opts, $1;
234 } elsif (/^--hook-(.+)=(.*)$/) {
235 my ($hook_name, $hook_cmd) = ($1, $2);
236 usageerr(g_('unknown hook name %s'), $hook_name)
237 if not exists $hook{$hook_name};
238 usageerr(g_('missing hook %s command'), $hook_name)
239 if not defined $hook_cmd;
240 $hook{$hook_name} = $hook_cmd;
241 } elsif (/^--buildinfo-id=.*$/) {
242 # Deprecated option
243 warning('--buildinfo-id is deprecated, it is without effect');
244 } elsif (/^(?:-p|--sign-command=)(.*)$/) {
245 $signcommand = $1;
246 } elsif (/^(?:-k|--sign-key=)(.*)$/) {
247 $signkey = $1;
248 } elsif (/^--(no-)?check-builddeps$/) {
249 $checkbuilddep = !(defined $1 and $1 eq 'no-');
250 } elsif (/^-([dD])$/) {
251 $checkbuilddep = ($1 eq 'D');
252 } elsif (/^--ignore-builtin-builddeps$/) {
253 $check_builtin_builddep = 0;
254 } elsif (/^-s(gpg|pgp)$/) {
255 # Deprecated option
256 warning(g_('-s%s is deprecated; always using gpg style interface'), $1);
257 } elsif (/^--force-sign$/) {
258 $signforce = 1;
259 } elsif (/^--no-sign$/) {
260 $signforce = 0;
261 $signsource = 0;
262 $signbuildinfo = 0;
263 $signchanges = 0;
264 } elsif (/^-us$/ or /^--unsigned-source$/) {
265 $signsource = 0;
266 } elsif (/^-ui$/ or /^--unsigned-buildinfo$/) {
267 $signbuildinfo = 0;
268 } elsif (/^-uc$/ or /^--unsigned-changes$/) {
269 $signbuildinfo = 0;
270 $signchanges = 0;
271 } elsif (/^-ap$/ or /^--sign-pausa$/) {
272 $signpause = 1;
273 } elsif (/^-a$/ or /^--host-arch$/) {
274 $host_arch = shift;
275 } elsif (/^-a(.*)$/ or /^--host-arch=(.*)$/) {
276 $host_arch = $1;
277 } elsif (/^-P(.*)$/ or /^--build-profiles=(.*)$/) {
278 my $arg = $1;
279 @build_profiles = split /,/, $arg;
280 } elsif (/^-s[iad]$/) {
281 push @changes_opts, $_;
282 } elsif (/^--(?:compression-level|compression)=.+$/) {
283 push @source_opts, $_;
284 } elsif (/^--(?:diff-ignore|tar-ignore)(?:=.+)?$/) {
285 push @source_opts, $_;
286 } elsif (/^-(?:s[nsAkurKUR]|[zZ].*|i.*|I.*)$/) {
287 push @source_opts, $_; # passed to dpkg-source
288 } elsif (/^-tc$/ or /^--post-clean$/) {
289 $cleansource = 1;
290 } elsif (/^-t$/ or /^--host-type$/) {
291 $host_type = shift; # Order DOES matter!
292 } elsif (/^-t(.*)$/ or /^--host-type=(.*)$/) {
293 $host_type = $1; # Order DOES matter!
294 } elsif (/^--target-arch$/) {
295 $target_arch = shift;
296 } elsif (/^--target-arch=(.*)$/) {
297 $target_arch = $1;
298 } elsif (/^--target-type$/) {
299 $target_type = shift;
300 } elsif (/^--target-type=(.*)$/) {
301 $target_type = $1;
302 } elsif (/^(?:--target|--rules-target|-T)$/) {
303 push @call_target, split /,/, shift @ARGV;
304 } elsif (/^(?:--target=|--rules-target=|-T)(.+)$/) {
305 my $arg = $1;
306 push @call_target, split /,/, $arg;
307 } elsif (/^--as-root$/) {
308 $call_target_as_root = 1;
309 } elsif (/^--pre-clean$/) {
310 $noclean = 0;
311 } elsif (/^-nc$/ or /^--no-pre-clean$/) {
312 $noclean = 1;
313 } elsif (/^--build=(.*)$/) {
314 set_build_type_from_options($1, $_);
315 } elsif (/^-b$/) {
316 set_build_type(BUILD_BINARY, $_);
317 } elsif (/^-B$/) {
318 set_build_type(BUILD_ARCH_DEP, $_);
319 } elsif (/^-A$/) {
320 set_build_type(BUILD_ARCH_INDEP, $_);
321 } elsif (/^-S$/) {
322 set_build_type(BUILD_SOURCE, $_);
323 } elsif (/^-G$/) {
324 set_build_type(BUILD_SOURCE | BUILD_ARCH_DEP, $_);
325 } elsif (/^-g$/) {
326 set_build_type(BUILD_SOURCE | BUILD_ARCH_INDEP, $_);
327 } elsif (/^-F$/) {
328 set_build_type(BUILD_FULL, $_);
329 } elsif (/^-v(.*)$/) {
330 $since = $1;
331 } elsif (/^-m(.*)$/ or /^--release-by=(.*)$/) {
332 $maint = $1;
333 } elsif (/^-e(.*)$/ or /^--build-by=(.*)$/) {
334 $changedby = $1;
335 } elsif (/^-C(.*)$/) {
336 $desc = $1;
337 } elsif (m/^-[EW]$/) {
338 # Deprecated option
339 warning(g_('-E and -W are deprecated, they are without effect'));
340 } elsif (/^-R(.*)$/ or /^--rules-target=(.*)$/) {
341 my $arg = $1;
342 @debian_rules = split /\s+/, $arg;
343 } else {
344 usageerr(g_('unknown option or argument %s'), $_);
345 }
346 }
347
348 if (build_has_all(BUILD_BINARY)) {
349 $buildtarget = 'build';
350 $binarytarget = 'binary';
351 } elsif (build_has_any(BUILD_ARCH_DEP)) {
352 $buildtarget = 'build-arch';
353 $binarytarget = 'binary-arch';
354 } elsif (build_has_any(BUILD_ARCH_INDEP)) {
355 $buildtarget = 'build-indep';
356 $binarytarget = 'binary-indep';
357 }
358
359 if ($noclean) {
360 # -nc without -b/-B/-A/-S/-F implies -b
361 set_build_type(BUILD_BINARY) if build_has_any(BUILD_DEFAULT);
362 # -nc with -S implies no dependency checks
363 $checkbuilddep = 0 if build_is(BUILD_SOURCE);
364 }
365
366 if ($< == 0) {
367 warning(g_('using a gain-root-command while being root')) if (@rootcommand);
368 } else {
369 push @rootcommand, 'fakeroot' unless @rootcommand;
370 }
371
372 if (@rootcommand and not find_command($rootcommand[0])) {
373 if ($rootcommand[0] eq 'fakeroot' and $< != 0) {
374 error(g_("fakeroot not found, either install the fakeroot\n" .
375 'package, specify a command with the -r option, ' .
376 'or run this as root'));
377 } else {
378 error(g_("gain-root-command '%s' not found"), $rootcommand[0]);
379 }
380 }
381
382 if ($check_command and not find_command($check_command)) {
383 error(g_("check-command '%s' not found"), $check_command);
384 }
385
386 if ($signcommand) {
387 if (!find_command($signcommand)) {
388 error(g_("sign-command '%s' not found"), $signcommand);
389 }
390 } elsif (($ENV{GNUPGHOME} && -e $ENV{GNUPGHOME}) ||
391 ($ENV{HOME} && -e "$ENV{HOME}/.gnupg")) {
392 if (find_command('gpg2')) {
393 $signcommand = 'gpg2';
394 } elsif (find_command('gpg')) {
395 $signcommand = 'gpg';
396 }
397 }
398
399 # Default to auto if none of parallel=N, -J or -j have been specified.
400 if (not defined $parallel and not $build_opts->has('parallel')) {
401 $parallel = 'auto';
402 }
403
404 if (defined $parallel) {
405 if ($parallel eq 'auto') {
406 # Most Unices.
407 $parallel = qx(getconf _NPROCESSORS_ONLN 2>/dev/null);
408 # Fallback for at least Irix.
409 $parallel = qx(getconf _NPROC_ONLN 2>/dev/null) if $?;
410 # Fallback to serial execution if cannot infer the number of online
411 # processors.
412 $parallel = '1' if $?;
413 chomp $parallel;
414 }
415 if ($parallel_force) {
416 $ENV{MAKEFLAGS} //= '';
417 $ENV{MAKEFLAGS} .= " -j$parallel";
418 }
419 $build_opts->set('parallel', $parallel);
420 $build_opts->export();
421 }
422
423 set_build_profiles(@build_profiles) if @build_profiles;
424
425 my $cwd = cwd();
426 my $dir = basename($cwd);
427
428 my $changelog = changelog_parse();
429
430 my $pkg = mustsetvar($changelog->{source}, g_('source package'));
431 my $version = mustsetvar($changelog->{version}, g_('source version'));
432 my $v = Dpkg::Version->new($version);
433 my ($ok, $error) = version_check($v);
434 error($error) unless $ok;
435
436 my $sversion = $v->as_string(omit_epoch => 1);
437 my $uversion = $v->version();
438
439 my $distribution = mustsetvar($changelog->{distribution}, g_('source distribution'));
440
441 my $maintainer;
442 if ($changedby) {
443 $maintainer = $changedby;
444 } elsif ($maint) {
445 $maintainer = $maint;
446 } else {
447 $maintainer = mustsetvar($changelog->{maintainer}, g_('source changed by'));
448 }
449
450 # <https://reproducible-builds.org/specs/source-date-epoch/>
451 $ENV{SOURCE_DATE_EPOCH} ||= $changelog->{timestamp} || time;
452
453 my @arch_opts;
454 push @arch_opts, ('--host-arch', $host_arch) if $host_arch;
455 push @arch_opts, ('--host-type', $host_type) if $host_type;
456 push @arch_opts, ('--target-arch', $target_arch) if $target_arch;
457 push @arch_opts, ('--target-type', $target_type) if $target_type;
458
459 open my $arch_env, '-|', 'dpkg-architecture', '-f', @arch_opts
460 or subprocerr('dpkg-architecture');
461 while (<$arch_env>) {
462 chomp;
463 my ($key, $value) = split /=/, $_, 2;
464 $ENV{$key} = $value;
465 }
466 close $arch_env or subprocerr('dpkg-architecture');
467
468 my $arch;
469 if (build_has_any(BUILD_ARCH_DEP)) {
470 $arch = mustsetvar($ENV{DEB_HOST_ARCH}, g_('host architecture'));
471 } elsif (build_has_any(BUILD_ARCH_INDEP)) {
472 $arch = 'all';
473 } elsif (build_has_any(BUILD_SOURCE)) {
474 $arch = 'source';
475 }
476
477 my $pv = "${pkg}_$sversion";
478 my $pva = "${pkg}_${sversion}_$arch";
479
480 if (not $signcommand) {
481 $signsource = 0;
482 $signbuildinfo = 0;
483 $signchanges = 0;
484 } elsif ($signforce) {
485 $signsource = 1;
486 $signbuildinfo = 1;
487 $signchanges = 1;
488 } elsif (($signsource or $signbuildinfo or $signchanges) and
489 $distribution eq 'UNRELEASED') {
490 $signreleased = 0;
491 $signsource = 0;
492 $signbuildinfo = 0;
493 $signchanges = 0;
494 }
495
496 if ($signsource && build_has_none(BUILD_SOURCE)) {
497 $signsource = 0;
498 }
499
500 #
501 # Preparation of environment stops here
502 #
503
504 run_hook('init', 1);
505
506 if (not -x 'debian/rules') {
507 warning(g_('debian/rules is not executable; fixing that'));
508 chmod(0755, 'debian/rules'); # No checks of failures, non fatal
509 }
510
511 if (scalar @call_target == 0) {
512 chdir('..') or syserr('chdir ..');
513 withecho('dpkg-source', @source_opts, '--before-build', $dir);
514 chdir($dir) or syserr("chdir $dir");
515 }
516
517 if ($checkbuilddep) {
518 my @checkbuilddep_opts;
519
520 push @checkbuilddep_opts, '-A' if build_has_none(BUILD_ARCH_DEP);
521 push @checkbuilddep_opts, '-B' if build_has_none(BUILD_ARCH_INDEP);
522 push @checkbuilddep_opts, '-I' if not $check_builtin_builddep;
523 push @checkbuilddep_opts, "--admindir=$admindir" if $admindir;
524
525 system('dpkg-checkbuilddeps', @checkbuilddep_opts);
526 if (not WIFEXITED($?)) {
527 subprocerr('dpkg-checkbuilddeps');
528 } elsif (WEXITSTATUS($?)) {
529 warning(g_('build dependencies/conflicts unsatisfied; aborting'));
530 warning(g_('(Use -d flag to override.)'));
531 exit 3;
532 }
533 }
534
535 foreach my $call_target (@call_target) {
536 if ($call_target_as_root or
537 $call_target =~ /^(clean|binary(|-arch|-indep))$/)
538 {
539 withecho(@rootcommand, @debian_rules, $call_target);
540 } else {
541 withecho(@debian_rules, $call_target);
542 }
543 }
544 exit 0 if scalar @call_target;
545
546 run_hook('preclean', ! $noclean);
547
548 unless ($noclean) {
549 withecho(@rootcommand, @debian_rules, 'clean');
550 }
551
552 run_hook('source', build_has_any(BUILD_SOURCE));
553
554 if (build_has_any(BUILD_SOURCE)) {
555 warning(g_('building a source package without cleaning up as you asked; ' .
556 'it might contain undesired files')) if $noclean;
557 chdir('..') or syserr('chdir ..');
558 withecho('dpkg-source', @source_opts, '-b', $dir);
559 chdir($dir) or syserr("chdir $dir");
560 }
561
562 run_hook('build', build_has_any(BUILD_BINARY));
563
564 # XXX Use some heuristics to decide whether to use build-{arch,indep} targets.
565 # This is a temporary measure to not break too many packages on a flag day.
566 build_target_fallback();
567
568 my $build_types = get_build_options_from_type();
569
570 if (build_has_any(BUILD_BINARY)) {
571 withecho(@debian_rules, $buildtarget);
572 run_hook('binary', 1);
573 withecho(@rootcommand, @debian_rules, $binarytarget);
574 }
575
576 run_hook('buildinfo', 1);
577
578 push @buildinfo_opts, "--build=$build_types" if build_has_none(BUILD_DEFAULT);
579 push @buildinfo_opts, "--admindir=$admindir" if $admindir;
580
581 withecho('dpkg-genbuildinfo', @buildinfo_opts);
582
583 run_hook('changes', 1);
584
585 push @changes_opts, "--build=$build_types" if build_has_none(BUILD_DEFAULT);
586 push @changes_opts, "-m$maint" if defined $maint;
587 push @changes_opts, "-e$changedby" if defined $changedby;
588 push @changes_opts, "-v$since" if defined $since;
589 push @changes_opts, "-C$desc" if defined $desc;
590
591 my $chg = "../$pva.changes";
592 my $changes = Dpkg::Control->new(type => CTRL_FILE_CHANGES);
593
594 printcmd("dpkg-genchanges @changes_opts >$chg");
595
596 open my $changes_fh, '-|', 'dpkg-genchanges', @changes_opts
597 or subprocerr('dpkg-genchanges');
598 $changes->parse($changes_fh, g_('parse changes file'));
599 $changes->save($chg);
600 close $changes_fh or subprocerr(g_('dpkg-genchanges'));
601
602 run_hook('postclean', $cleansource);
603
604 if ($cleansource) {
605 withecho(@rootcommand, @debian_rules, 'clean');
606 }
607
608 chdir('..') or syserr('chdir ..');
609 withecho('dpkg-source', @source_opts, '--after-build', $dir);
610 chdir($dir) or syserr("chdir $dir");
611
612 info(describe_build($changes->{'Files'}));
613
614 run_hook('check', $check_command);
615
616 if ($check_command) {
617 withecho($check_command, @check_opts, $chg);
618 }
619
620 if ($signpause && ($signsource || $signbuildinfo || $signchanges)) {
621 print g_("Press <enter> to start the signing process.\n");
622 getc();
623 }
624
625 run_hook('sign', $signsource || $signbuildinfo || $signchanges);
626
627 if ($signsource) {
628 if (signfile("$pv.dsc")) {
629 error(g_('failed to sign %s file'), '.dsc');
630 }
631
632 # Recompute the checksums as the .dsc have changed now.
633 my $buildinfo = Dpkg::Control->new(type => CTRL_FILE_BUILDINFO);
634 $buildinfo->load("../$pva.buildinfo");
635 my $checksums = Dpkg::Checksums->new();
636 $checksums->add_from_control($buildinfo);
637 $checksums->add_from_file("../$pv.dsc", update => 1, key => "$pv.dsc");
638 $checksums->export_to_control($buildinfo);
639 $buildinfo->save("../$pva.buildinfo");
640 }
641 if ($signbuildinfo && signfile("$pva.buildinfo")) {
642 error(g_('failed to sign %s file'), '.buildinfo');
643 }
644 if ($signsource or $signbuildinfo) {
645 # Recompute the checksums as the .dsc and/or .buildinfo have changed.
646 my $checksums = Dpkg::Checksums->new();
647 $checksums->add_from_control($changes);
648 $checksums->add_from_file("../$pv.dsc", update => 1, key => "$pv.dsc")
649 if $signsource;
650 $checksums->add_from_file("../$pva.buildinfo", update => 1, key => "$pva.buildinfo");
651 $checksums->export_to_control($changes);
652 delete $changes->{'Checksums-Md5'};
653 update_files_field($changes, $checksums, "$pv.dsc")
654 if $signsource;
655 update_files_field($changes, $checksums, "$pva.buildinfo");
656 $changes->save($chg);
657 }
658 if ($signchanges && signfile("$pva.changes")) {
659 error(g_('failed to sign %s file'), '.changes');
660 }
661
662 if (not $signreleased) {
663 warning(g_('not signing UNRELEASED build; use --force-sign to override'));
664 }
665
666 run_hook('done', 1);
667
668 sub mustsetvar {
669 my ($var, $text) = @_;
670
671 error(g_('unable to determine %s'), $text)
672 unless defined($var);
673
674 info("$text $var");
675 return $var;
676 }
677
678 sub withecho {
679 printcmd(@_);
680 system(@_)
681 and subprocerr("@_");
682 }
683
684 sub run_hook {
685 my ($name, $enabled) = @_;
686 my $cmd = $hook{$name};
687
688 return if not $cmd;
689
690 info("running hook $name");
691
692 my %hook_vars = (
693 '%' => '%',
694 'a' => $enabled ? 1 : 0,
695 'p' => $pkg,
696 'v' => $version,
697 's' => $sversion,
698 'u' => $uversion,
699 );
700
701 my $subst_hook_var = sub {
702 my $var = shift;
703
704 if (exists $hook_vars{$var}) {
705 return $hook_vars{$var};
706 } else {
707 warning(g_('unknown %% substitution in hook: %%%s'), $var);
708 return "\%$var";
709 }
710 };
711
712 $cmd =~ s/\%(.)/&$subst_hook_var($1)/eg;
713
714 withecho($cmd);
715 }
716
717 sub update_files_field {
718 my ($ctrl, $checksums, $filename) = @_;
719
720 my $md5sum_regex = checksums_get_property('md5', 'regex');
721 my $md5sum = $checksums->get_checksum($filename, 'md5');
722 my $size = $checksums->get_size($filename);
723 my $file_regex = qr/$md5sum_regex\s+\d+\s+(\S+\s+\S+\s+\Q$filename\E)/;
724
725 $ctrl->{'Files'} =~ s/^$file_regex$/$md5sum $size $1/m;
726 }
727
728 sub signfile {
729 my $file = shift;
730
731 printcmd("signfile $file");
732
733 my $signdir = tempdir('dpkg-sign.XXXXXXXX', CLEANUP => 1);
734 my $signfile = "$signdir/$file";
735
736 # Make sure the file to sign ends with a newline.
737 copy("../$file", $signfile);
738 open my $signfh, '>>', $signfile or syserr(g_('cannot open %s'), $signfile);
739 print { $signfh } "\n";
740 close $signfh or syserr(g_('cannot close %s'), $signfile);
741
742 system($signcommand, '--utf8-strings', '--textmode', '--armor',
743 '--local-user', $signkey || $maintainer, '--clearsign',
744 '--output', "$signfile.asc", $signfile);
745 my $status = $?;
746 if ($status == 0) {
747 system('mv', '--', "$signfile.asc", "../$file")
748 and subprocerr('mv');
749 }
750
751 print "\n";
752 return $status
753 }
754
755 sub fileomitted {
756 my ($files, $regex) = @_;
757
758 return $files !~ /$regex/
759 }
760
761 sub describe_build {
762 my $files = shift;
763 my $ext = compression_get_file_extension_regex();
764
765 if (fileomitted($files, qr/\.deb/)) {
766 # source-only upload
767 if (fileomitted($files, qr/\.diff\.$ext/) and
768 fileomitted($files, qr/\.debian\.tar\.$ext/)) {
769 return g_('source-only upload: Debian-native package');
770 } elsif (fileomitted($files, qr/\.orig\.tar\.$ext/)) {
771 return g_('source-only, diff-only upload (original source NOT included)');
772 } else {
773 return g_('source-only upload (original source is included)');
774 }
775 } elsif (fileomitted($files, qr/\.dsc/)) {
776 return g_('binary-only upload (no source included)');
777 } elsif (fileomitted($files, qr/\.diff\.$ext/) and
778 fileomitted($files, qr/\.debian\.tar\.$ext/)) {
779 return g_('full upload; Debian-native package (full source is included)');
780 } elsif (fileomitted($files, qr/\.orig\.tar\.$ext/)) {
781 return g_('binary and diff upload (original source NOT included)');
782 } else {
783 return g_('full upload (original source is included)');
784 }
785 }
786
787 sub build_target_fallback {
788 return if $buildtarget eq 'build';
789 return if scalar @debian_rules != 1;
790
791 # Check if we are building both arch:all and arch:any packages, in which
792 # case we now require working build-indep and build-arch targets.
793 my $pkg_arch = 0;
794 my $ctrl = Dpkg::Control::Info->new();
795
796 foreach my $bin ($ctrl->get_packages()) {
797 if ($bin->{Architecture} eq 'all') {
798 $pkg_arch |= BUILD_ARCH_INDEP;
799 } else {
800 $pkg_arch |= BUILD_ARCH_DEP;
801 }
802 }
803
804 return if $pkg_arch == BUILD_BINARY;
805
806 # Check if the build-{arch,indep} targets are supported. If not, fallback
807 # to build.
808 my $pid = spawn(exec => [ $Dpkg::PROGMAKE, '-f', @debian_rules, '-qn', $buildtarget ],
809 from_file => '/dev/null', to_file => '/dev/null',
810 error_to_file => '/dev/null');
811 my $cmdline = "make -f @debian_rules -qn $buildtarget";
812 wait_child($pid, nocheck => 1, cmdline => $cmdline);
813 my $exitcode = WEXITSTATUS($?);
814 subprocerr($cmdline) unless WIFEXITED($?);
815 if ($exitcode == 2) {
816 warning(g_("%s must be updated to support the 'build-arch' and " .
817 "'build-indep' targets (at least '%s' seems to be " .
818 'missing)'), "@debian_rules", $buildtarget);
819 $buildtarget = 'build';
820 }
821 }