po: Update German man pages translation
[dpkg.git] / scripts / dpkg-buildpackage.pl
blobd849d6e90aced9d417e8f180a86f5306857c54f0
1 #!/usr/bin/perl
3 # dpkg-buildpackage
5 # Copyright © 1996 Ian Jackson
6 # Copyright © 2000 Wichert Akkerman
7 # Copyright © 2006-2024 Guillem Jover <guillem@debian.org>
8 # Copyright © 2007 Frank Lichtenheld
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.
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.
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/>.
23 use strict;
24 use warnings;
26 use File::Path qw(remove_tree);
27 use File::Copy;
28 use File::Glob qw(bsd_glob GLOB_TILDE GLOB_NOCHECK);
29 use POSIX qw(:sys_wait_h);
31 use Dpkg ();
32 use Dpkg::Gettext;
33 use Dpkg::ErrorHandling;
34 use Dpkg::BuildTypes;
35 use Dpkg::BuildOptions;
36 use Dpkg::BuildProfiles qw(set_build_profiles);
37 use Dpkg::BuildDriver;
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::OpenPGP;
47 use Dpkg::OpenPGP::ErrorCodes;
48 use Dpkg::OpenPGP::KeyHandle;
49 use Dpkg::Path qw(find_command);
50 use Dpkg::IPC;
51 use Dpkg::Vendor qw(run_vendor_hook);
53 textdomain('dpkg-dev');
55 sub showversion {
56 printf g_("Debian %s version %s.\n"), $Dpkg::PROGNAME, $Dpkg::PROGVERSION;
58 print g_('
59 This is free software; see the GNU General Public License version 2 or
60 later for copying conditions. There is NO warranty.
61 ');
64 sub usage {
65 printf g_(
66 'Usage: %s [<option>...] [--] [<filename.dsc>|<directory>]')
67 . "\n\n" . g_(
68 'Options:
69 --build=<type>[,...] specify the build <type>: full, source, binary,
70 any, all (default is \'full\').
71 -F, --build=full normal full build (source and binary; default).
72 -g, --build=source,all source and arch-indep build.
73 -G, --build=source,any source and arch-specific build.
74 -b, --build=binary binary-only, no source files.
75 -B, --build=any binary-only, only arch-specific files.
76 -A, --build=all binary-only, only arch-indep files.
77 -S, --build=source source-only, no binary files.
78 -nc, --no-pre-clean do not pre clean source tree (implies -b).
79 --pre-clean pre clean source tree (default).
80 --no-post-clean do not post clean source tree (default).
81 -tc, --post-clean post clean source tree.
82 --sanitize-env sanitize the build environment.
83 -D, --check-builddeps check build dependencies and conflicts (default).
84 -d, --no-check-builddeps do not check build dependencies and conflicts.
85 --ignore-builtin-builddeps
86 do not check builtin build dependencies.
87 -P, --build-profiles=<profiles>
88 assume comma-separated build <profiles> as active.
89 --rules-requires-root assume legacy Rules-Requires-Root field value.
90 -R, --rules-file=<rules> rules file to execute (default is debian/rules).
91 -T, --rules-target=<target> call debian/rules <target>.
92 --as-root ensure -T calls the target with root rights.
93 -j, --jobs[=<jobs>|auto] jobs to run simultaneously (passed to <rules>),
94 (default; default is auto, opt-in mode).
95 -J, --jobs-try[=<jobs>|auto]
96 alias for -j, --jobs.
97 --jobs-force[=<jobs>|auto]
98 jobs to run simultaneously (passed to <rules>),
99 (default is auto, forced mode).
100 -r, --root-command=<command>
101 command to gain root rights (default is fakeroot).
102 --check-command=<command>
103 command to check the .changes file (no default).
104 --check-option=<opt> pass <opt> to check <command>.
105 --hook-<name>=<command> set <command> as the hook <name>, known hooks:
106 preinit init preclean source build binary
107 buildinfo changes postclean check sign done
108 --buildinfo-file=<file> set the .buildinfo filename to generate.
109 --buildinfo-option=<opt>
110 pass option <opt> to dpkg-genbuildinfo.
111 --changes-file=<file> set the .changes filename to generate.
112 --sign-backend=<backend>
113 OpenPGP backend to use to sign
114 (default is auto).
115 -p, --sign-command=<command>
116 command to sign .dsc and/or .changes files
117 (default is gpg).
118 --sign-keyfile=<file> the key file to use for signing.
119 -k, --sign-keyid=<keyid> the key id to use for signing.
120 --sign-key=<keyid> alias for -k, --sign-keyid.
121 -ap, --sign-pause add pause before starting signature process.
122 -us, --unsigned-source unsigned source package.
123 -ui, --unsigned-buildinfo unsigned .buildinfo file.
124 -uc, --unsigned-changes unsigned .buildinfo and .changes file.
125 --no-sign do not sign any file.
126 --force-sign force signing the resulting files.
127 --admindir=<directory> change the administrative directory.
128 -?, --help show this help message.
129 --version show the version.')
130 . "\n\n" . g_(
131 'Options passed to dpkg-architecture:
132 -a, --host-arch <arch> set the host Debian architecture.
133 -t, --host-type <type> set the host GNU system type.
134 --target-arch <arch> set the target Debian architecture.
135 --target-type <type> set the target GNU system type.')
136 . "\n\n" . g_(
137 'Options passed to dpkg-genchanges:
138 -si source includes orig, if new upstream (default).
139 -sa source includes orig, always.
140 -sd source is diff and .dsc only.
141 -v<version> changes since version <version>.
142 -m, --source-by=<maint> maintainer for this source or build is <maint>.
143 --build-by=<maint> ditto.
144 -e, --release-by=<maint> maintainer for this change or release is <maint>.
145 --changed-by=<maint> ditto.
146 -C<descfile> changes are described in <descfile>.
147 --changes-option=<opt> pass option <opt> to dpkg-genchanges.')
148 . "\n\n" . g_(
149 'Options passed to dpkg-source:
150 -sn force Debian native source format.
151 -s[sAkurKUR] see dpkg-source for explanation.
152 -z, --compression-level=<level>
153 compression level to use for source.
154 -Z, --compression=<compressor>
155 compression to use for source (gz|xz|bzip2|lzma).
156 -i, --diff-ignore[=<regex>] ignore diffs of files matching <regex>.
157 -I, --tar-ignore[=<pattern>]
158 filter out files when building tarballs.
159 --source-option=<opt> pass option <opt> to dpkg-source.
160 '), $Dpkg::PROGNAME;
163 my $admindir;
164 my @debian_rules = ('debian/rules');
165 my @rootcommand = ();
166 my $signbackend;
167 my $signcommand;
168 my $preclean = 1;
169 my $postclean = 0;
170 my $sanitize_env = 0;
171 my $parallel;
172 my $parallel_force = 0;
173 my $checkbuilddep = 1;
174 my $check_builtin_builddep = 1;
175 my $source;
176 my $source_from_dsc = 0;
177 my @source_opts;
178 my $srcdir;
179 my $check_command = $ENV{DEB_CHECK_COMMAND};
180 my @check_opts;
181 my $signpause;
182 my $signkeyfile = $ENV{DEB_SIGN_KEYFILE};
183 my $signkeyid = $ENV{DEB_SIGN_KEYID};
184 my $signforce = 0;
185 my $signreleased = 1;
186 my $signsource = 1;
187 my $signbuildinfo = 1;
188 my $signchanges = 1;
189 my $buildtarget = 'build';
190 my $binarytarget = 'binary';
191 my $host_arch = '';
192 my $host_type = '';
193 my $target_arch = '';
194 my $target_type = '';
195 my @build_profiles = ();
196 my $rrr_override;
197 my @call_target = ();
198 my $call_target_as_root = 0;
199 my $since;
200 my $maint;
201 my $changedby;
202 my $desc;
203 my $buildinfo_file;
204 my @buildinfo_opts;
205 my $changes_file;
206 my @changes_opts;
207 my @hook_names = qw(
208 preinit
209 init
210 preclean
211 source
212 build
213 binary
214 buildinfo
215 changes
216 postclean
217 check
218 sign
219 done
221 my %hook;
222 $hook{$_} = undef foreach @hook_names;
225 my $conf = Dpkg::Conf->new();
226 $conf->load_config('buildpackage.conf');
228 # Inject config options for command-line parser.
229 unshift @ARGV, @{$conf};
231 my $build_opts = Dpkg::BuildOptions->new();
233 if ($build_opts->has('nocheck')) {
234 $check_command = undef;
235 } elsif (not find_command($check_command)) {
236 $check_command = undef;
239 while (@ARGV) {
240 $_ = shift @ARGV;
242 if (/^(?:--help|-\?)$/) {
243 usage;
244 exit 0;
245 } elsif (/^--version$/) {
246 showversion;
247 exit 0;
248 } elsif (/^--admindir$/) {
249 $admindir = shift @ARGV;
250 } elsif (/^--admindir=(.*)$/) {
251 $admindir = $1;
252 } elsif (/^--source-option=(.*)$/) {
253 push @source_opts, $1;
254 } elsif (/^--buildinfo-file=(.*)$/) {
255 $buildinfo_file = $1;
256 usageerr(g_('missing .buildinfo filename')) if not length $buildinfo_file;
257 } elsif (/^--buildinfo-option=(.*)$/) {
258 my $buildinfo_opt = $1;
259 if ($buildinfo_opt =~ m/^-O(.*)/) {
260 warning(g_('passing %s via %s is not supported; please use %s instead'),
261 '-O', '--buildinfo-option', '--buildinfo-file');
262 $buildinfo_file = $1;
263 } else {
264 push @buildinfo_opts, $buildinfo_opt;
266 } elsif (/^--changes-file=(.*)$/) {
267 $changes_file = $1;
268 usageerr(g_('missing .changes filename')) if not length $changes_file;
269 } elsif (/^--changes-option=(.*)$/) {
270 my $changes_opt = $1;
271 if ($changes_opt =~ m/^-O(.*)/) {
272 warning(g_('passing %s via %s is not supported; please use %s instead'),
273 '-O', '--changes-option', '--changes-file');
274 $changes_file = $1;
275 } else {
276 push @changes_opts, $changes_opt;
278 } elsif (/^--jobs(?:-try)?$/) {
279 $parallel = '';
280 $parallel_force = 0;
281 } elsif (/^(?:-[jJ]|--jobs(?:-try)?=)(\d*|auto)$/) {
282 $parallel = $1 || '';
283 $parallel_force = 0;
284 } elsif (/^--jobs-force(?:=(\d*|auto))?$/) {
285 $parallel = $1 || '';
286 $parallel_force = 1;
287 } elsif (/^(?:-r|--root-command=)(.*)$/) {
288 my $arg = $1;
289 @rootcommand = split ' ', $arg;
290 } elsif (/^--check-command=(.*)$/) {
291 $check_command = $1;
292 } elsif (/^--check-option=(.*)$/) {
293 push @check_opts, $1;
294 } elsif (/^--hook-([^=]+)=(.*)$/) {
295 my ($hook_name, $hook_cmd) = ($1, $2);
296 usageerr(g_('unknown hook name %s'), $hook_name)
297 if not exists $hook{$hook_name};
298 usageerr(g_('missing hook %s command'), $hook_name)
299 if not defined $hook_cmd;
300 $hook{$hook_name} = $hook_cmd;
301 } elsif (/^(--buildinfo-id)=.*$/) {
302 # Deprecated option
303 warning(g_('%s is deprecated; it is without effect'), $1);
304 } elsif (/^--sign-backend=(.*)$/) {
305 $signbackend = $1;
306 } elsif (/^(?:-p|--sign-command=)(.*)$/) {
307 $signcommand = $1;
308 } elsif (/^--sign-keyfile=(.*)$/) {
309 $signkeyfile = $1;
310 } elsif (/^(?:-k|--sign-keyid=|--sign-key=)(.*)$/) {
311 $signkeyid = $1;
312 } elsif (/^--(no-)?check-builddeps$/) {
313 $checkbuilddep = !(defined $1 and $1 eq 'no-');
314 } elsif (/^-([dD])$/) {
315 $checkbuilddep = ($1 eq 'D');
316 } elsif (/^--ignore-builtin-builddeps$/) {
317 $check_builtin_builddep = 0;
318 } elsif (/^-s(gpg|pgp)$/) {
319 # Deprecated option
320 warning(g_('-s%s is deprecated; always using gpg style interface'), $1);
321 } elsif (/^--force-sign$/) {
322 $signforce = 1;
323 } elsif (/^--no-sign$/) {
324 $signforce = 0;
325 $signsource = 0;
326 $signbuildinfo = 0;
327 $signchanges = 0;
328 } elsif (/^-us$/ or /^--unsigned-source$/) {
329 $signsource = 0;
330 } elsif (/^-ui$/ or /^--unsigned-buildinfo$/) {
331 $signbuildinfo = 0;
332 } elsif (/^-uc$/ or /^--unsigned-changes$/) {
333 $signbuildinfo = 0;
334 $signchanges = 0;
335 } elsif (/^-ap$/ or /^--sign-pausa$/) {
336 $signpause = 1;
337 } elsif (/^-a$/ or /^--host-arch$/) {
338 $host_arch = shift;
339 } elsif (/^-a(.*)$/ or /^--host-arch=(.*)$/) {
340 $host_arch = $1;
341 } elsif (/^-P(.*)$/ or /^--build-profiles=(.*)$/) {
342 my $arg = $1;
343 @build_profiles = split /,/, $arg;
344 } elsif (/^-s[iad]$/) {
345 push @changes_opts, $_;
346 } elsif (/^--(?:compression-level|compression)=.+$/) {
347 push @source_opts, $_;
348 } elsif (/^--(?:diff-ignore|tar-ignore)(?:=.+)?$/) {
349 push @source_opts, $_;
350 } elsif (/^-(?:s[nsAkurKUR]|[zZ].*|i.*|I.*)$/) {
351 push @source_opts, $_; # passed to dpkg-source
352 } elsif (/^-tc$/ or /^--post-clean$/) {
353 $postclean = 1;
354 } elsif (/^--no-post-clean$/) {
355 $postclean = 0;
356 } elsif (/^--sanitize-env$/) {
357 $sanitize_env = 1;
358 } elsif (/^-t$/ or /^--host-type$/) {
359 $host_type = shift; # Order DOES matter!
360 } elsif (/^-t(.*)$/ or /^--host-type=(.*)$/) {
361 $host_type = $1; # Order DOES matter!
362 } elsif (/^--target-arch$/) {
363 $target_arch = shift;
364 } elsif (/^--target-arch=(.*)$/) {
365 $target_arch = $1;
366 } elsif (/^--target-type$/) {
367 $target_type = shift;
368 } elsif (/^--target-type=(.*)$/) {
369 $target_type = $1;
370 } elsif (/^(?:--target|--rules-target|-T)$/) {
371 push @call_target, split /,/, shift @ARGV;
372 } elsif (/^(?:--target=|--rules-target=|-T)(.+)$/) {
373 my $arg = $1;
374 push @call_target, split /,/, $arg;
375 } elsif (/^--rules-requires-root$/) {
376 $rrr_override = 'binary-targets';
377 } elsif (/^--as-root$/) {
378 $call_target_as_root = 1;
379 } elsif (/^--pre-clean$/) {
380 $preclean = 1;
381 } elsif (/^-nc$/ or /^--no-pre-clean$/) {
382 $preclean = 0;
383 } elsif (/^--build=(.*)$/) {
384 set_build_type_from_options($1, $_);
385 } elsif (/^-b$/) {
386 set_build_type(BUILD_BINARY, $_);
387 } elsif (/^-B$/) {
388 set_build_type(BUILD_ARCH_DEP, $_);
389 } elsif (/^-A$/) {
390 set_build_type(BUILD_ARCH_INDEP, $_);
391 } elsif (/^-S$/) {
392 set_build_type(BUILD_SOURCE, $_);
393 } elsif (/^-G$/) {
394 set_build_type(BUILD_SOURCE | BUILD_ARCH_DEP, $_);
395 } elsif (/^-g$/) {
396 set_build_type(BUILD_SOURCE | BUILD_ARCH_INDEP, $_);
397 } elsif (/^-F$/) {
398 set_build_type(BUILD_FULL, $_);
399 } elsif (/^-v(.*)$/) {
400 $since = $1;
401 } elsif (/^-m(.*)$/ or /^--(?:source|build)-by=(.*)$/) {
402 $maint = $1;
403 } elsif (/^-e(.*)$/ or /^--(?:changed|release)-by=(.*)$/) {
404 $changedby = $1;
405 } elsif (/^-C(.*)$/) {
406 $desc = $1;
407 } elsif (m/^-[EW]$/) {
408 # Deprecated option
409 warning(g_('%s is deprecated; it is without effect'), $_);
410 } elsif (/^-R(.*)$/ or /^--rules-file=(.*)$/) {
411 my $arg = $1;
412 @debian_rules = split ' ', $arg;
413 } elsif ($_ eq '--') {
414 $source = shift @ARGV;
415 last;
416 } elsif (/^-/) {
417 usageerr(g_('unknown option or argument %s'), $_);
418 } else {
419 $source = $_;
420 last;
424 if (@call_target) {
425 my $targets = join ',', @call_target;
426 set_build_type_from_targets($targets, '--rules-target', nocheck => 1);
429 if (build_has_all(BUILD_BINARY)) {
430 $buildtarget = 'build';
431 $binarytarget = 'binary';
432 } elsif (build_has_any(BUILD_ARCH_DEP)) {
433 $buildtarget = 'build-arch';
434 $binarytarget = 'binary-arch';
435 } elsif (build_has_any(BUILD_ARCH_INDEP)) {
436 $buildtarget = 'build-indep';
437 $binarytarget = 'binary-indep';
440 if (not $preclean) {
441 # -nc without -b/-B/-A/-S/-F implies -b
442 set_build_type(BUILD_BINARY) if build_has_any(BUILD_DEFAULT);
443 # -nc with -S implies no dependency checks
444 $checkbuilddep = 0 if build_is(BUILD_SOURCE);
447 if ($call_target_as_root and @call_target == 0) {
448 error(g_('option %s is only meaningful with option %s'),
449 '--as-root', '--rules-target');
452 if ($check_command and not find_command($check_command)) {
453 error(g_("check-command '%s' not found"), $check_command);
456 if ($signcommand and not find_command($signcommand)) {
457 error(g_("sign-command '%s' not found"), $signcommand);
460 # Default to auto if none of parallel=N, -J or -j have been specified.
461 if (not defined $parallel and not $build_opts->has('parallel')) {
462 $parallel = 'auto';
466 # Prepare the environment.
469 run_hook('preinit');
471 if (defined $parallel) {
472 if ($parallel eq 'auto') {
473 # Most Unices.
474 $parallel = qx(getconf _NPROCESSORS_ONLN 2>/dev/null);
475 # Fallback for at least Irix.
476 $parallel = qx(getconf _NPROC_ONLN 2>/dev/null) if $?;
477 # Fallback to serial execution if cannot infer the number of online
478 # processors.
479 $parallel = '1' if $?;
480 chomp $parallel;
482 if ($parallel_force) {
483 $ENV{MAKEFLAGS} //= '';
484 $ENV{MAKEFLAGS} .= " -j$parallel";
486 $build_opts->set('parallel', $parallel);
487 $build_opts->export();
490 if ($build_opts->has('terse')) {
491 $ENV{MAKEFLAGS} //= '';
492 $ENV{MAKEFLAGS} .= ' --no-print-directory';
495 set_build_profiles(@build_profiles) if @build_profiles;
497 # Handle specified source trees.
498 if (defined $source) {
499 if (-d $source) {
500 chdir $source
501 or syserr(g_('cannot change directory to %s'), $source);
502 } elsif (-f $source) {
503 require Dpkg::Source::Package;
505 if (build_has_any(BUILD_SOURCE)) {
506 error(g_('building source package would overwrite input source %s'),
507 $source);
510 if ($source =~ m{/}) {
511 error(g_('source package %s is expected in the current directory'),
512 $source);
515 my $srcpkg = Dpkg::Source::Package->new(
516 filename => $source,
517 options => {
518 no_check => 0,
519 no_overwrite_dir => 1,
520 require_valid_signature => 0,
521 require_strong_checksums => 0,
524 $srcdir = $srcpkg->get_basedirname();
526 if (-e $srcdir) {
527 error(g_('source directory %s exists already, aborting'), $srcdir);
530 info(g_('extracting source package %s'), $source);
532 run_cmd('dpkg-source', @source_opts, '--extract', $source);
534 chdir $srcdir
535 or syserr(g_('cannot change directory to %s'), $srcdir);
537 # Track whether we extracted the source from a specified .dsc.
538 $source_from_dsc = 1;
542 my $changelog = changelog_parse();
543 my $ctrl = Dpkg::Control::Info->new();
545 my $pkg = mustsetvar($changelog->{source}, g_('source package'));
546 my $version = mustsetvar($changelog->{version}, g_('source version'));
547 my $v = Dpkg::Version->new($version);
548 my ($ok, $error) = version_check($v);
549 error($error) unless $ok;
551 my $sversion = $v->as_string(omit_epoch => 1);
552 my $uversion = $v->version();
554 my $distribution = mustsetvar($changelog->{distribution}, g_('source distribution'));
556 my $maintainer;
557 if ($changedby) {
558 $maintainer = $changedby;
559 } elsif ($maint) {
560 $maintainer = $maint;
561 } else {
562 $maintainer = mustsetvar($changelog->{maintainer}, g_('source changed by'));
565 # <https://reproducible-builds.org/specs/source-date-epoch/>
566 $ENV{SOURCE_DATE_EPOCH} ||= $changelog->{timestamp} || time;
568 my @arch_opts;
569 push @arch_opts, ('--host-arch', $host_arch) if $host_arch;
570 push @arch_opts, ('--host-type', $host_type) if $host_type;
571 push @arch_opts, ('--target-arch', $target_arch) if $target_arch;
572 push @arch_opts, ('--target-type', $target_type) if $target_type;
574 open my $arch_env, '-|', 'dpkg-architecture', '-f', @arch_opts
575 or subprocerr('dpkg-architecture');
576 while (<$arch_env>) {
577 chomp;
578 my ($key, $value) = split /=/, $_, 2;
579 $ENV{$key} = $value;
581 close $arch_env or subprocerr('dpkg-architecture');
583 my $arch;
584 if (build_has_any(BUILD_ARCH_DEP)) {
585 $arch = mustsetvar($ENV{DEB_HOST_ARCH}, g_('host architecture'));
586 } elsif (build_has_any(BUILD_ARCH_INDEP)) {
587 $arch = 'all';
588 } elsif (build_has_any(BUILD_SOURCE)) {
589 $arch = 'source';
592 my $pv = "${pkg}_$sversion";
593 my $pva = "${pkg}_${sversion}_$arch";
595 my $signkeytype;
596 my $signkeyhandle;
597 if (defined $signkeyfile) {
598 $signkeytype = 'keyfile';
599 $signkeyhandle = bsd_glob($signkeyfile, GLOB_TILDE | GLOB_NOCHECK);
600 } elsif (defined $signkeyid) {
601 $signkeytype = 'autoid';
602 $signkeyhandle = $signkeyid;
603 } else {
604 $signkeytype = 'userid';
605 $signkeyhandle = $maintainer;
607 my $signkey = Dpkg::OpenPGP::KeyHandle->new(
608 type => $signkeytype,
609 handle => $signkeyhandle,
611 signkey_validate();
613 my $openpgp = Dpkg::OpenPGP->new(
614 backend => $signbackend // 'auto',
615 cmd => $signcommand // 'auto',
616 needs => {
617 keystore => $signkey->needs_keystore(),
621 if (not $openpgp->can_use_secrets($signkey)) {
622 $signsource = 0;
623 $signbuildinfo = 0;
624 $signchanges = 0;
625 } elsif ($signforce) {
626 $signsource = 1;
627 $signbuildinfo = 1;
628 $signchanges = 1;
629 } elsif (($signsource or $signbuildinfo or $signchanges) and
630 $distribution eq 'UNRELEASED') {
631 $signreleased = 0;
632 $signsource = 0;
633 $signbuildinfo = 0;
634 $signchanges = 0;
637 if ($signsource && build_has_none(BUILD_SOURCE)) {
638 $signsource = 0;
641 # Sanitize build environment.
642 if ($sanitize_env) {
643 run_vendor_hook('sanitize-environment');
646 my $build_driver = Dpkg::BuildDriver->new(
647 ctrl => $ctrl,
648 debian_rules => \@debian_rules,
649 root_cmd => \@rootcommand,
650 as_root => $call_target_as_root,
651 rrr_override => $rrr_override,
655 # Preparation of environment stops here
658 run_hook('init');
660 $build_driver->pre_check();
662 if (scalar @call_target == 0) {
663 run_cmd('dpkg-source', @source_opts, '--before-build', '.');
666 if ($checkbuilddep) {
667 my @checkbuilddep_opts;
669 push @checkbuilddep_opts, '-A' if build_has_none(BUILD_ARCH_DEP);
670 push @checkbuilddep_opts, '-B' if build_has_none(BUILD_ARCH_INDEP);
671 push @checkbuilddep_opts, '-I' if not $check_builtin_builddep;
672 push @checkbuilddep_opts, "--admindir=$admindir" if $admindir;
674 system('dpkg-checkbuilddeps', @checkbuilddep_opts);
675 if (not WIFEXITED($?)) {
676 subprocerr('dpkg-checkbuilddeps');
677 } elsif (WEXITSTATUS($?)) {
678 warning(g_('build dependencies/conflicts unsatisfied; aborting'));
679 warning(g_('(Use -d flag to override.)'));
680 exit 3;
684 foreach my $call_target (@call_target) {
685 $build_driver->run_task($call_target);
687 exit 0 if scalar @call_target;
689 run_hook('preclean', {
690 enabled => $preclean,
693 if ($preclean) {
694 $build_driver->run_task('clean');
697 run_hook('source', {
698 enabled => build_has_any(BUILD_SOURCE),
699 env => {
700 DPKG_BUILDPACKAGE_HOOK_SOURCE_OPTIONS => join(' ', @source_opts),
704 if (build_has_any(BUILD_SOURCE)) {
705 warning(g_('building a source package without cleaning up as you asked; ' .
706 'it might contain undesired files')) if not $preclean;
707 run_cmd('dpkg-source', @source_opts, '-b', '.');
710 my $build_types = get_build_options_from_type();
712 my $need_buildtask = $build_driver->need_build_task($buildtarget, $binarytarget);
714 run_hook('build', {
715 enabled => build_has_any(BUILD_BINARY) && $need_buildtask,
716 env => {
717 DPKG_BUILDPACKAGE_HOOK_BUILD_TARGET => $buildtarget,
721 # If we are building rootless, there is no need to call the build target
722 # independently as non-root.
723 if (build_has_any(BUILD_BINARY) && $need_buildtask) {
724 $build_driver->run_build_task($buildtarget, $binarytarget);
727 if (build_has_any(BUILD_BINARY)) {
728 run_hook('binary', {
729 env => {
730 DPKG_BUILDPACKAGE_HOOK_BINARY_TARGET => $binarytarget,
733 $build_driver->run_task($binarytarget);
736 $buildinfo_file //= "../$pva.buildinfo";
738 if (build_has_none(BUILD_DEFAULT) || $source_from_dsc) {
739 my $buildinfo_buildtypes = $build_types;
741 # We can now let dpkg-genbuildinfo know that we can include the .dsc
742 # in the .buildinfo file as we handled it ourselves, and what we are
743 # building matches either the source we built or extracted it from.
744 $buildinfo_buildtypes .= ',source' if $source_from_dsc;
746 push @buildinfo_opts, "--build=$buildinfo_buildtypes";
748 push @buildinfo_opts, "--admindir=$admindir" if $admindir;
749 push @buildinfo_opts, "-O$buildinfo_file" if $buildinfo_file;
751 run_hook('buildinfo', {
752 env => {
753 DPKG_BUILDPACKAGE_HOOK_BUILDINFO_OPTIONS => join(' ', @buildinfo_opts),
756 run_cmd('dpkg-genbuildinfo', @buildinfo_opts);
758 $changes_file //= "../$pva.changes";
760 push @changes_opts, "--build=$build_types" if build_has_none(BUILD_DEFAULT);
761 push @changes_opts, "-m$maint" if defined $maint;
762 push @changes_opts, "-e$changedby" if defined $changedby;
763 push @changes_opts, "-v$since" if defined $since;
764 push @changes_opts, "-C$desc" if defined $desc;
765 push @changes_opts, "-O$changes_file";
767 my $changes = Dpkg::Control->new(type => CTRL_FILE_CHANGES);
769 run_hook('changes', {
770 env => {
771 DPKG_BUILDPACKAGE_HOOK_CHANGES_OPTIONS => join(' ', @changes_opts),
774 run_cmd('dpkg-genchanges', @changes_opts);
775 $changes->load($changes_file);
777 run_hook('postclean', {
778 enabled => $postclean,
781 if ($postclean) {
782 $build_driver->run_task('clean');
785 run_cmd('dpkg-source', @source_opts, '--after-build', '.');
787 info(describe_build($changes->{'Files'}));
789 run_hook('check', {
790 enabled => $check_command,
791 env => {
792 DPKG_BUILDPACKAGE_HOOK_CHECK_OPTIONS => join(' ', @check_opts),
796 if ($check_command) {
797 run_cmd($check_command, @check_opts, $changes_file);
800 if ($signpause && ($signsource || $signbuildinfo || $signchanges)) {
801 print g_("Press <enter> to start the signing process.\n");
802 getc();
805 run_hook('sign', {
806 enabled => $signsource || $signbuildinfo || $signchanges,
809 if ($signsource) {
810 signfile("$pv.dsc");
812 # Recompute the checksums as the .dsc has changed now.
813 my $buildinfo = Dpkg::Control->new(type => CTRL_FILE_BUILDINFO);
814 $buildinfo->load($buildinfo_file);
815 my $checksums = Dpkg::Checksums->new();
816 $checksums->add_from_control($buildinfo);
817 $checksums->add_from_file("../$pv.dsc", update => 1, key => "$pv.dsc");
818 $checksums->export_to_control($buildinfo);
819 $buildinfo->save($buildinfo_file);
821 if ($signbuildinfo) {
822 signfile("$pva.buildinfo");
824 if ($signsource or $signbuildinfo) {
825 # Recompute the checksums as the .dsc and/or .buildinfo have changed.
826 my $checksums = Dpkg::Checksums->new();
827 $checksums->add_from_control($changes);
828 $checksums->add_from_file("../$pv.dsc", update => 1, key => "$pv.dsc")
829 if $signsource;
830 $checksums->add_from_file($buildinfo_file, update => 1, key => "$pva.buildinfo");
831 $checksums->export_to_control($changes);
832 delete $changes->{'Checksums-Md5'};
833 update_files_field($changes, $checksums, "$pv.dsc")
834 if $signsource;
835 update_files_field($changes, $checksums, "$pva.buildinfo");
836 $changes->save($changes_file);
838 if ($signchanges) {
839 signfile("$pva.changes");
842 if (not $signreleased) {
843 warning(g_('not signing UNRELEASED build; use --force-sign to override'));
846 if ($source_from_dsc) {
847 info(g_('removing extracted source directory %s'), $srcdir);
848 chdir '..'
849 or syserr(g_('cannot change directory to %s'), '..');
850 remove_tree($srcdir);
853 run_hook('done');
855 sub mustsetvar {
856 my ($var, $text) = @_;
858 error(g_('unable to determine %s'), $text)
859 unless defined($var);
861 info("$text $var");
862 return $var;
865 sub run_cmd {
866 my @cmd = @_;
868 printcmd(@cmd);
869 system @cmd and subprocerr("@cmd");
872 sub run_hook {
873 my ($name, $opts) = @_;
874 my $cmd = $hook{$name};
875 $opts->{enabled} //= 1;
877 return if not $cmd;
879 info("running hook $name");
881 my %hook_vars = (
882 '%' => '%',
883 'a' => $opts->{enabled} ? 1 : 0,
884 'p' => $pkg // q{},
885 'v' => $version // q{},
886 's' => $sversion // q{},
887 'u' => $uversion // q{},
890 my $subst_hook_var = sub {
891 my $var = shift;
893 if (exists $hook_vars{$var}) {
894 return $hook_vars{$var};
895 } else {
896 warning(g_('unknown %% substitution in hook: %%%s'), $var);
897 return "\%$var";
901 $cmd =~ s/\%(.)/$subst_hook_var->($1)/eg;
903 $opts->{env}{DPKG_BUILDPACKAGE_HOOK_NAME} = $name;
905 # Set any environment variables for this hook invocation.
906 local @ENV{keys %{$opts->{env}}} = values %{$opts->{env}};
908 run_cmd($cmd);
911 sub update_files_field {
912 my ($ctrl, $checksums, $filename) = @_;
914 my $md5sum_regex = checksums_get_property('md5', 'regex');
915 my $md5sum = $checksums->get_checksum($filename, 'md5');
916 my $size = $checksums->get_size($filename);
917 my $file_regex = qr/$md5sum_regex\s+\d+\s+(\S+\s+\S+\s+\Q$filename\E)/;
919 $ctrl->{'Files'} =~ s/^$file_regex$/$md5sum $size $1/m;
922 sub signkey_validate {
923 return unless $signkey->type eq 'keyid';
925 if (length $signkey->handle <= 8) {
926 error(g_('short OpenPGP key IDs are broken; ' .
927 'please use key fingerprints in %s or %s instead'),
928 '-k', 'DEB_SIGN_KEYID');
929 } elsif (length $signkey->handle <= 16) {
930 warning(g_('long OpenPGP key IDs are strongly discouraged; ' .
931 'please use key fingerprints in %s or %s instead'),
932 '-k', 'DEB_SIGN_KEYID');
936 sub signfile {
937 my $file = shift;
938 my $signfile = "../$file";
940 printcmd("signfile $file");
942 my $status = $openpgp->inline_sign($signfile, "$signfile.asc", $signkey);
943 if ($status == OPENPGP_OK) {
944 move("$signfile.asc", $signfile)
945 or syserror(g_('cannot move %s to %s'), "$signfile.asc", $signfile);
946 } else {
947 error(g_('failed to sign %s file: %s'), $signfile,
948 openpgp_errorcode_to_string($status));
951 return $status
954 sub fileomitted {
955 my ($files, $regex) = @_;
957 return $files !~ m/$regex$/m
960 sub describe_build {
961 my $files = shift;
962 my $ext = compression_get_file_extension_regex();
964 if (fileomitted($files, qr/\.deb/)) {
965 # source-only upload
966 if (fileomitted($files, qr/\.diff\.$ext/) and
967 fileomitted($files, qr/\.debian\.tar\.$ext/)) {
968 return g_('source-only upload: Debian-native package');
969 } elsif (fileomitted($files, qr/\.orig\.tar\.$ext/)) {
970 return g_('source-only, diff-only upload (original source NOT included)');
971 } else {
972 return g_('source-only upload (original source is included)');
974 } elsif (fileomitted($files, qr/\.dsc/)) {
975 return g_('binary-only upload (no source included)');
976 } elsif (fileomitted($files, qr/\.diff\.$ext/) and
977 fileomitted($files, qr/\.debian\.tar\.$ext/)) {
978 return g_('full upload; Debian-native package (full source is included)');
979 } elsif (fileomitted($files, qr/\.orig\.tar\.$ext/)) {
980 return g_('binary and diff upload (original source NOT included)');
981 } else {
982 return g_('full upload (original source is included)');