doc: Move Perl version baseline as the first perl coding style subsection
[dpkg.git] / scripts / dpkg-buildpackage.pl
blob1532d801908d529f3f8cde8d2d7e872e1801e0b5
1 #!/usr/bin/perl
3 # dpkg-buildpackage
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
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::Temp qw(tempdir);
27 use File::Basename;
28 use File::Copy;
29 use File::Glob qw(bsd_glob GLOB_TILDE GLOB_NOCHECK);
30 use POSIX qw(:sys_wait_h);
32 use Dpkg ();
33 use Dpkg::Gettext;
34 use Dpkg::ErrorHandling;
35 use Dpkg::BuildTypes;
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::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>...]')
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_opts;
176 my $check_command = $ENV{DEB_CHECK_COMMAND};
177 my @check_opts;
178 my $signpause;
179 my $signkeyfile = $ENV{DEB_SIGN_KEYFILE};
180 my $signkeyid = $ENV{DEB_SIGN_KEYID};
181 my $signforce = 0;
182 my $signreleased = 1;
183 my $signsource = 1;
184 my $signbuildinfo = 1;
185 my $signchanges = 1;
186 my $buildtarget = 'build';
187 my $binarytarget = 'binary';
188 my $host_arch = '';
189 my $host_type = '';
190 my $target_arch = '';
191 my $target_type = '';
192 my @build_profiles = ();
193 my $rrr_override;
194 my @call_target = ();
195 my $call_target_as_root = 0;
196 my $since;
197 my $maint;
198 my $changedby;
199 my $desc;
200 my $buildinfo_file;
201 my @buildinfo_opts;
202 my $changes_file;
203 my @changes_opts;
204 my %target_legacy_root = map { $_ => 1 } qw(
205 clean
206 binary
207 binary-arch
208 binary-indep
210 my %target_official = map { $_ => 1 } qw(
211 clean
212 build
213 build-arch
214 build-indep
215 binary
216 binary-arch
217 binary-indep
219 my @hook_names = qw(
220 preinit
221 init
222 preclean
223 source
224 build
225 binary
226 buildinfo
227 changes
228 postclean
229 check
230 sign
231 done
233 my %hook;
234 $hook{$_} = undef foreach @hook_names;
237 my $conf = Dpkg::Conf->new();
238 $conf->load_config('buildpackage.conf');
240 # Inject config options for command-line parser.
241 unshift @ARGV, @{$conf};
243 my $build_opts = Dpkg::BuildOptions->new();
245 if ($build_opts->has('nocheck')) {
246 $check_command = undef;
247 } elsif (not find_command($check_command)) {
248 $check_command = undef;
251 while (@ARGV) {
252 $_ = shift @ARGV;
254 if (/^(?:--help|-\?)$/) {
255 usage;
256 exit 0;
257 } elsif (/^--version$/) {
258 showversion;
259 exit 0;
260 } elsif (/^--admindir$/) {
261 $admindir = shift @ARGV;
262 } elsif (/^--admindir=(.*)$/) {
263 $admindir = $1;
264 } elsif (/^--source-option=(.*)$/) {
265 push @source_opts, $1;
266 } elsif (/^--buildinfo-file=(.*)$/) {
267 $buildinfo_file = $1;
268 usageerr(g_('missing .buildinfo filename')) if not length $buildinfo_file;
269 } elsif (/^--buildinfo-option=(.*)$/) {
270 my $buildinfo_opt = $1;
271 if ($buildinfo_opt =~ m/^-O(.*)/) {
272 warning(g_('passing %s via %s is not supported; please use %s instead'),
273 '-O', '--buildinfo-option', '--buildinfo-file');
274 $buildinfo_file = $1;
275 } else {
276 push @buildinfo_opts, $buildinfo_opt;
278 } elsif (/^--changes-file=(.*)$/) {
279 $changes_file = $1;
280 usageerr(g_('missing .changes filename')) if not length $changes_file;
281 } elsif (/^--changes-option=(.*)$/) {
282 my $changes_opt = $1;
283 if ($changes_opt =~ m/^-O(.*)/) {
284 warning(g_('passing %s via %s is not supported; please use %s instead'),
285 '-O', '--changes-option', '--changes-file');
286 $changes_file = $1;
287 } else {
288 push @changes_opts, $changes_opt;
290 } elsif (/^--jobs(?:-try)?$/) {
291 $parallel = '';
292 $parallel_force = 0;
293 } elsif (/^(?:-[jJ]|--jobs(?:-try)?=)(\d*|auto)$/) {
294 $parallel = $1 || '';
295 $parallel_force = 0;
296 } elsif (/^--jobs-force(?:=(\d*|auto))?$/) {
297 $parallel = $1 || '';
298 $parallel_force = 1;
299 } elsif (/^(?:-r|--root-command=)(.*)$/) {
300 my $arg = $1;
301 @rootcommand = split ' ', $arg;
302 } elsif (/^--check-command=(.*)$/) {
303 $check_command = $1;
304 } elsif (/^--check-option=(.*)$/) {
305 push @check_opts, $1;
306 } elsif (/^--hook-([^=]+)=(.*)$/) {
307 my ($hook_name, $hook_cmd) = ($1, $2);
308 usageerr(g_('unknown hook name %s'), $hook_name)
309 if not exists $hook{$hook_name};
310 usageerr(g_('missing hook %s command'), $hook_name)
311 if not defined $hook_cmd;
312 $hook{$hook_name} = $hook_cmd;
313 } elsif (/^(--buildinfo-id)=.*$/) {
314 # Deprecated option
315 warning(g_('%s is deprecated; it is without effect'), $1);
316 } elsif (/^--sign-backend=(.*)$/) {
317 $signbackend = $1;
318 } elsif (/^(?:-p|--sign-command=)(.*)$/) {
319 $signcommand = $1;
320 } elsif (/^--sign-keyfile=(.*)$/) {
321 $signkeyfile = $1;
322 } elsif (/^(?:-k|--sign-keyid=|--sign-key=)(.*)$/) {
323 $signkeyid = $1;
324 } elsif (/^--(no-)?check-builddeps$/) {
325 $checkbuilddep = !(defined $1 and $1 eq 'no-');
326 } elsif (/^-([dD])$/) {
327 $checkbuilddep = ($1 eq 'D');
328 } elsif (/^--ignore-builtin-builddeps$/) {
329 $check_builtin_builddep = 0;
330 } elsif (/^-s(gpg|pgp)$/) {
331 # Deprecated option
332 warning(g_('-s%s is deprecated; always using gpg style interface'), $1);
333 } elsif (/^--force-sign$/) {
334 $signforce = 1;
335 } elsif (/^--no-sign$/) {
336 $signforce = 0;
337 $signsource = 0;
338 $signbuildinfo = 0;
339 $signchanges = 0;
340 } elsif (/^-us$/ or /^--unsigned-source$/) {
341 $signsource = 0;
342 } elsif (/^-ui$/ or /^--unsigned-buildinfo$/) {
343 $signbuildinfo = 0;
344 } elsif (/^-uc$/ or /^--unsigned-changes$/) {
345 $signbuildinfo = 0;
346 $signchanges = 0;
347 } elsif (/^-ap$/ or /^--sign-pausa$/) {
348 $signpause = 1;
349 } elsif (/^-a$/ or /^--host-arch$/) {
350 $host_arch = shift;
351 } elsif (/^-a(.*)$/ or /^--host-arch=(.*)$/) {
352 $host_arch = $1;
353 } elsif (/^-P(.*)$/ or /^--build-profiles=(.*)$/) {
354 my $arg = $1;
355 @build_profiles = split /,/, $arg;
356 } elsif (/^-s[iad]$/) {
357 push @changes_opts, $_;
358 } elsif (/^--(?:compression-level|compression)=.+$/) {
359 push @source_opts, $_;
360 } elsif (/^--(?:diff-ignore|tar-ignore)(?:=.+)?$/) {
361 push @source_opts, $_;
362 } elsif (/^-(?:s[nsAkurKUR]|[zZ].*|i.*|I.*)$/) {
363 push @source_opts, $_; # passed to dpkg-source
364 } elsif (/^-tc$/ or /^--post-clean$/) {
365 $postclean = 1;
366 } elsif (/^--no-post-clean$/) {
367 $postclean = 0;
368 } elsif (/^--sanitize-env$/) {
369 $sanitize_env = 1;
370 } elsif (/^-t$/ or /^--host-type$/) {
371 $host_type = shift; # Order DOES matter!
372 } elsif (/^-t(.*)$/ or /^--host-type=(.*)$/) {
373 $host_type = $1; # Order DOES matter!
374 } elsif (/^--target-arch$/) {
375 $target_arch = shift;
376 } elsif (/^--target-arch=(.*)$/) {
377 $target_arch = $1;
378 } elsif (/^--target-type$/) {
379 $target_type = shift;
380 } elsif (/^--target-type=(.*)$/) {
381 $target_type = $1;
382 } elsif (/^(?:--target|--rules-target|-T)$/) {
383 push @call_target, split /,/, shift @ARGV;
384 } elsif (/^(?:--target=|--rules-target=|-T)(.+)$/) {
385 my $arg = $1;
386 push @call_target, split /,/, $arg;
387 } elsif (/^--rules-requires-root$/) {
388 $rrr_override = 'binary-targets';
389 } elsif (/^--as-root$/) {
390 $call_target_as_root = 1;
391 } elsif (/^--pre-clean$/) {
392 $preclean = 1;
393 } elsif (/^-nc$/ or /^--no-pre-clean$/) {
394 $preclean = 0;
395 } elsif (/^--build=(.*)$/) {
396 set_build_type_from_options($1, $_);
397 } elsif (/^-b$/) {
398 set_build_type(BUILD_BINARY, $_);
399 } elsif (/^-B$/) {
400 set_build_type(BUILD_ARCH_DEP, $_);
401 } elsif (/^-A$/) {
402 set_build_type(BUILD_ARCH_INDEP, $_);
403 } elsif (/^-S$/) {
404 set_build_type(BUILD_SOURCE, $_);
405 } elsif (/^-G$/) {
406 set_build_type(BUILD_SOURCE | BUILD_ARCH_DEP, $_);
407 } elsif (/^-g$/) {
408 set_build_type(BUILD_SOURCE | BUILD_ARCH_INDEP, $_);
409 } elsif (/^-F$/) {
410 set_build_type(BUILD_FULL, $_);
411 } elsif (/^-v(.*)$/) {
412 $since = $1;
413 } elsif (/^-m(.*)$/ or /^--(?:source|build)-by=(.*)$/) {
414 $maint = $1;
415 } elsif (/^-e(.*)$/ or /^--(?:changed|release)-by=(.*)$/) {
416 $changedby = $1;
417 } elsif (/^-C(.*)$/) {
418 $desc = $1;
419 } elsif (m/^-[EW]$/) {
420 # Deprecated option
421 warning(g_('%s is deprecated; it is without effect'), $_);
422 } elsif (/^-R(.*)$/ or /^--rules-file=(.*)$/) {
423 my $arg = $1;
424 @debian_rules = split ' ', $arg;
425 } else {
426 usageerr(g_('unknown option or argument %s'), $_);
430 if (@call_target) {
431 my $targets = join ',', @call_target;
432 set_build_type_from_targets($targets, '--rules-target', nocheck => 1);
435 if (build_has_all(BUILD_BINARY)) {
436 $buildtarget = 'build';
437 $binarytarget = 'binary';
438 } elsif (build_has_any(BUILD_ARCH_DEP)) {
439 $buildtarget = 'build-arch';
440 $binarytarget = 'binary-arch';
441 } elsif (build_has_any(BUILD_ARCH_INDEP)) {
442 $buildtarget = 'build-indep';
443 $binarytarget = 'binary-indep';
446 if (not $preclean) {
447 # -nc without -b/-B/-A/-S/-F implies -b
448 set_build_type(BUILD_BINARY) if build_has_any(BUILD_DEFAULT);
449 # -nc with -S implies no dependency checks
450 $checkbuilddep = 0 if build_is(BUILD_SOURCE);
453 if ($call_target_as_root and @call_target == 0) {
454 error(g_('option %s is only meaningful with option %s'),
455 '--as-root', '--rules-target');
458 if ($check_command and not find_command($check_command)) {
459 error(g_("check-command '%s' not found"), $check_command);
462 if ($signcommand and not find_command($signcommand)) {
463 error(g_("sign-command '%s' not found"), $signcommand);
466 # Default to auto if none of parallel=N, -J or -j have been specified.
467 if (not defined $parallel and not $build_opts->has('parallel')) {
468 $parallel = 'auto';
472 # Prepare the environment.
475 run_hook('preinit', 1);
477 if (defined $parallel) {
478 if ($parallel eq 'auto') {
479 # Most Unices.
480 $parallel = qx(getconf _NPROCESSORS_ONLN 2>/dev/null);
481 # Fallback for at least Irix.
482 $parallel = qx(getconf _NPROC_ONLN 2>/dev/null) if $?;
483 # Fallback to serial execution if cannot infer the number of online
484 # processors.
485 $parallel = '1' if $?;
486 chomp $parallel;
488 if ($parallel_force) {
489 $ENV{MAKEFLAGS} //= '';
490 $ENV{MAKEFLAGS} .= " -j$parallel";
492 $build_opts->set('parallel', $parallel);
493 $build_opts->export();
496 if ($build_opts->has('terse')) {
497 $ENV{MAKEFLAGS} //= '';
498 $ENV{MAKEFLAGS} .= ' --no-print-directory';
501 set_build_profiles(@build_profiles) if @build_profiles;
503 my $changelog = changelog_parse();
504 my $ctrl = Dpkg::Control::Info->new();
506 # Check whether we are doing some kind of rootless build, and sanity check
507 # the fields values.
508 my %rules_requires_root = parse_rules_requires_root($ctrl->get_source());
510 my $pkg = mustsetvar($changelog->{source}, g_('source package'));
511 my $version = mustsetvar($changelog->{version}, g_('source version'));
512 my $v = Dpkg::Version->new($version);
513 my ($ok, $error) = version_check($v);
514 error($error) unless $ok;
516 my $sversion = $v->as_string(omit_epoch => 1);
517 my $uversion = $v->version();
519 my $distribution = mustsetvar($changelog->{distribution}, g_('source distribution'));
521 my $maintainer;
522 if ($changedby) {
523 $maintainer = $changedby;
524 } elsif ($maint) {
525 $maintainer = $maint;
526 } else {
527 $maintainer = mustsetvar($changelog->{maintainer}, g_('source changed by'));
530 # <https://reproducible-builds.org/specs/source-date-epoch/>
531 $ENV{SOURCE_DATE_EPOCH} ||= $changelog->{timestamp} || time;
533 my @arch_opts;
534 push @arch_opts, ('--host-arch', $host_arch) if $host_arch;
535 push @arch_opts, ('--host-type', $host_type) if $host_type;
536 push @arch_opts, ('--target-arch', $target_arch) if $target_arch;
537 push @arch_opts, ('--target-type', $target_type) if $target_type;
539 open my $arch_env, '-|', 'dpkg-architecture', '-f', @arch_opts
540 or subprocerr('dpkg-architecture');
541 while (<$arch_env>) {
542 chomp;
543 my ($key, $value) = split /=/, $_, 2;
544 $ENV{$key} = $value;
546 close $arch_env or subprocerr('dpkg-architecture');
548 my $arch;
549 if (build_has_any(BUILD_ARCH_DEP)) {
550 $arch = mustsetvar($ENV{DEB_HOST_ARCH}, g_('host architecture'));
551 } elsif (build_has_any(BUILD_ARCH_INDEP)) {
552 $arch = 'all';
553 } elsif (build_has_any(BUILD_SOURCE)) {
554 $arch = 'source';
557 my $pv = "${pkg}_$sversion";
558 my $pva = "${pkg}_${sversion}_$arch";
560 my $signkeytype;
561 my $signkeyhandle;
562 if (defined $signkeyfile) {
563 $signkeytype = 'keyfile';
564 $signkeyhandle = bsd_glob($signkeyfile, GLOB_TILDE | GLOB_NOCHECK);
565 } elsif (defined $signkeyid) {
566 $signkeytype = 'autoid';
567 $signkeyhandle = $signkeyid;
568 } else {
569 $signkeytype = 'userid';
570 $signkeyhandle = $maintainer;
572 my $signkey = Dpkg::OpenPGP::KeyHandle->new(
573 type => $signkeytype,
574 handle => $signkeyhandle,
576 signkey_validate();
578 my $openpgp = Dpkg::OpenPGP->new(
579 backend => $signbackend // 'auto',
580 cmd => $signcommand // 'auto',
581 needs => {
582 keystore => $signkey->needs_keystore(),
586 if (not $openpgp->can_use_secrets($signkey)) {
587 $signsource = 0;
588 $signbuildinfo = 0;
589 $signchanges = 0;
590 } elsif ($signforce) {
591 $signsource = 1;
592 $signbuildinfo = 1;
593 $signchanges = 1;
594 } elsif (($signsource or $signbuildinfo or $signchanges) and
595 $distribution eq 'UNRELEASED') {
596 $signreleased = 0;
597 $signsource = 0;
598 $signbuildinfo = 0;
599 $signchanges = 0;
602 if ($signsource && build_has_none(BUILD_SOURCE)) {
603 $signsource = 0;
606 # Sanitize build environment.
607 if ($sanitize_env) {
608 run_vendor_hook('sanitize-environment');
612 # Preparation of environment stops here
615 run_hook('init', 1);
617 if (not -x 'debian/rules') {
618 warning(g_('debian/rules is not executable; fixing that'));
619 chmod(0755, 'debian/rules'); # No checks of failures, non fatal
622 if (scalar @call_target == 0) {
623 run_cmd('dpkg-source', @source_opts, '--before-build', '.');
626 if ($checkbuilddep) {
627 my @checkbuilddep_opts;
629 push @checkbuilddep_opts, '-A' if build_has_none(BUILD_ARCH_DEP);
630 push @checkbuilddep_opts, '-B' if build_has_none(BUILD_ARCH_INDEP);
631 push @checkbuilddep_opts, '-I' if not $check_builtin_builddep;
632 push @checkbuilddep_opts, "--admindir=$admindir" if $admindir;
634 system('dpkg-checkbuilddeps', @checkbuilddep_opts);
635 if (not WIFEXITED($?)) {
636 subprocerr('dpkg-checkbuilddeps');
637 } elsif (WEXITSTATUS($?)) {
638 warning(g_('build dependencies/conflicts unsatisfied; aborting'));
639 warning(g_('(Use -d flag to override.)'));
640 exit 3;
644 foreach my $call_target (@call_target) {
645 run_rules_cond_root($call_target);
647 exit 0 if scalar @call_target;
649 run_hook('preclean', $preclean);
651 if ($preclean) {
652 run_rules_cond_root('clean');
655 run_hook('source', build_has_any(BUILD_SOURCE));
657 if (build_has_any(BUILD_SOURCE)) {
658 warning(g_('building a source package without cleaning up as you asked; ' .
659 'it might contain undesired files')) if not $preclean;
660 run_cmd('dpkg-source', @source_opts, '-b', '.');
663 run_hook('build', build_has_any(BUILD_BINARY));
665 my $build_types = get_build_options_from_type();
667 if (build_has_any(BUILD_BINARY)) {
668 # XXX Use some heuristics to decide whether to use build-{arch,indep}
669 # targets. This is a temporary measure to not break too many packages
670 # on a flag day.
671 build_target_fallback($ctrl);
673 # If we are building rootless, there is no need to call the build target
674 # independently as non-root.
675 run_cmd(@debian_rules, $buildtarget) if rules_requires_root($binarytarget);
676 run_hook('binary', 1);
677 run_rules_cond_root($binarytarget);
680 run_hook('buildinfo', 1);
682 $buildinfo_file //= "../$pva.buildinfo";
684 push @buildinfo_opts, "--build=$build_types" if build_has_none(BUILD_DEFAULT);
685 push @buildinfo_opts, "--admindir=$admindir" if $admindir;
686 push @buildinfo_opts, "-O$buildinfo_file" if $buildinfo_file;
688 run_cmd('dpkg-genbuildinfo', @buildinfo_opts);
690 run_hook('changes', 1);
692 $changes_file //= "../$pva.changes";
694 push @changes_opts, "--build=$build_types" if build_has_none(BUILD_DEFAULT);
695 push @changes_opts, "-m$maint" if defined $maint;
696 push @changes_opts, "-e$changedby" if defined $changedby;
697 push @changes_opts, "-v$since" if defined $since;
698 push @changes_opts, "-C$desc" if defined $desc;
699 push @changes_opts, "-O$changes_file";
701 my $changes = Dpkg::Control->new(type => CTRL_FILE_CHANGES);
703 run_cmd('dpkg-genchanges', @changes_opts);
704 $changes->load($changes_file);
706 run_hook('postclean', $postclean);
708 if ($postclean) {
709 run_rules_cond_root('clean');
712 run_cmd('dpkg-source', @source_opts, '--after-build', '.');
714 info(describe_build($changes->{'Files'}));
716 run_hook('check', $check_command);
718 if ($check_command) {
719 run_cmd($check_command, @check_opts, $changes_file);
722 if ($signpause && ($signsource || $signbuildinfo || $signchanges)) {
723 print g_("Press <enter> to start the signing process.\n");
724 getc();
727 run_hook('sign', $signsource || $signbuildinfo || $signchanges);
729 if ($signsource) {
730 signfile("$pv.dsc");
732 # Recompute the checksums as the .dsc has changed now.
733 my $buildinfo = Dpkg::Control->new(type => CTRL_FILE_BUILDINFO);
734 $buildinfo->load($buildinfo_file);
735 my $checksums = Dpkg::Checksums->new();
736 $checksums->add_from_control($buildinfo);
737 $checksums->add_from_file("../$pv.dsc", update => 1, key => "$pv.dsc");
738 $checksums->export_to_control($buildinfo);
739 $buildinfo->save($buildinfo_file);
741 if ($signbuildinfo) {
742 signfile("$pva.buildinfo");
744 if ($signsource or $signbuildinfo) {
745 # Recompute the checksums as the .dsc and/or .buildinfo have changed.
746 my $checksums = Dpkg::Checksums->new();
747 $checksums->add_from_control($changes);
748 $checksums->add_from_file("../$pv.dsc", update => 1, key => "$pv.dsc")
749 if $signsource;
750 $checksums->add_from_file($buildinfo_file, update => 1, key => "$pva.buildinfo");
751 $checksums->export_to_control($changes);
752 delete $changes->{'Checksums-Md5'};
753 update_files_field($changes, $checksums, "$pv.dsc")
754 if $signsource;
755 update_files_field($changes, $checksums, "$pva.buildinfo");
756 $changes->save($changes_file);
758 if ($signchanges) {
759 signfile("$pva.changes");
762 if (not $signreleased) {
763 warning(g_('not signing UNRELEASED build; use --force-sign to override'));
766 run_hook('done', 1);
768 sub mustsetvar {
769 my ($var, $text) = @_;
771 error(g_('unable to determine %s'), $text)
772 unless defined($var);
774 info("$text $var");
775 return $var;
778 sub setup_rootcommand {
779 if ($< == 0) {
780 warning(g_('using a gain-root-command while being root')) if @rootcommand;
781 } else {
782 push @rootcommand, 'fakeroot' unless @rootcommand;
785 if (@rootcommand and not find_command($rootcommand[0])) {
786 if ($rootcommand[0] eq 'fakeroot' and $< != 0) {
787 error(g_("fakeroot not found, either install the fakeroot\n" .
788 'package, specify a command with the -r option, ' .
789 'or run this as root'));
790 } else {
791 error(g_("gain-root-command '%s' not found"), $rootcommand[0]);
796 sub parse_rules_requires_root {
797 my $ctrl = shift;
799 my %rrr;
800 my $rrr;
801 my $keywords_base;
802 my $keywords_impl;
804 $rrr = $rrr_override // $ctrl->{'Rules-Requires-Root'} // 'binary-targets';
806 foreach my $keyword (split ' ', $rrr) {
807 if ($keyword =~ m{/}) {
808 if ($keyword =~ m{^dpkg/target/(.*)$}p and $target_official{$1}) {
809 error(g_('disallowed target in %s field keyword "%s"'),
810 'Rules-Requires-Root', $keyword);
811 } elsif ($keyword ne 'dpkg/target-subcommand') {
812 error(g_('%s field keyword "%s" is unknown in dpkg namespace'),
813 'Rules-Requires-Root', $keyword);
815 $keywords_impl++;
816 } else {
817 if ($keyword ne lc $keyword and
818 (lc $keyword eq 'no' or lc $keyword eq 'binary-targets')) {
819 error(g_('%s field keyword "%s" is uppercase; use "%s" instead'),
820 'Rules-Requires-Root', $keyword, lc $keyword);
821 } elsif (lc $keyword eq 'yes') {
822 error(g_('%s field keyword "%s" is invalid; use "%s" instead'),
823 'Rules-Requires-Root', $keyword, 'binary-targets');
824 } elsif ($keyword ne 'no' and $keyword ne 'binary-targets') {
825 warning(g_('%s field keyword "%s" is unknown'),
826 'Rules-Requires-Root', $keyword);
828 $keywords_base++;
831 if ($rrr{$keyword}++) {
832 error(g_('field %s contains duplicate keyword "%s"'),
833 'Rules-Requires-Root', $keyword);
837 if ($call_target_as_root or not exists $rrr{no}) {
838 setup_rootcommand();
841 # Notify the children we do support R³.
842 $ENV{DEB_RULES_REQUIRES_ROOT} = join ' ', sort keys %rrr;
844 if ($keywords_base > 1 or $keywords_base and $keywords_impl) {
845 error(g_('%s field contains both global and implementation specific keywords'),
846 'Rules-Requires-Root');
847 } elsif ($keywords_impl) {
848 # Set only on <implementations-keywords>.
849 $ENV{DEB_GAIN_ROOT_CMD} = join ' ', @rootcommand;
850 } else {
851 # We should not provide the variable otherwise.
852 delete $ENV{DEB_GAIN_ROOT_CMD};
855 return %rrr;
858 sub run_cmd {
859 my @cmd = @_;
861 printcmd(@cmd);
862 system @cmd and subprocerr("@cmd");
865 sub rules_requires_root {
866 my $target = shift;
868 return 1 if $call_target_as_root;
869 return 1 if $rules_requires_root{"dpkg/target/$target"};
870 return 1 if $rules_requires_root{'binary-targets'} and $target_legacy_root{$target};
871 return 0;
874 sub run_rules_cond_root {
875 my $target = shift;
877 my @cmd;
878 push @cmd, @rootcommand if rules_requires_root($target);
879 push @cmd, @debian_rules, $target;
881 run_cmd(@cmd);
884 sub run_hook {
885 my ($name, $enabled) = @_;
886 my $cmd = $hook{$name};
888 return if not $cmd;
890 info("running hook $name");
892 my %hook_vars = (
893 '%' => '%',
894 'a' => $enabled ? 1 : 0,
895 'p' => $pkg,
896 'v' => $version,
897 's' => $sversion,
898 'u' => $uversion,
901 my $subst_hook_var = sub {
902 my $var = shift;
904 if (exists $hook_vars{$var}) {
905 return $hook_vars{$var};
906 } else {
907 warning(g_('unknown %% substitution in hook: %%%s'), $var);
908 return "\%$var";
912 $cmd =~ s/\%(.)/$subst_hook_var->($1)/eg;
914 run_cmd($cmd);
917 sub update_files_field {
918 my ($ctrl, $checksums, $filename) = @_;
920 my $md5sum_regex = checksums_get_property('md5', 'regex');
921 my $md5sum = $checksums->get_checksum($filename, 'md5');
922 my $size = $checksums->get_size($filename);
923 my $file_regex = qr/$md5sum_regex\s+\d+\s+(\S+\s+\S+\s+\Q$filename\E)/;
925 $ctrl->{'Files'} =~ s/^$file_regex$/$md5sum $size $1/m;
928 sub signkey_validate {
929 return unless $signkey->type eq 'keyid';
931 if (length $signkey->handle <= 8) {
932 error(g_('short OpenPGP key IDs are broken; ' .
933 'please use key fingerprints in %s or %s instead'),
934 '-k', 'DEB_SIGN_KEYID');
935 } elsif (length $signkey->handle <= 16) {
936 warning(g_('long OpenPGP key IDs are strongly discouraged; ' .
937 'please use key fingerprints in %s or %s instead'),
938 '-k', 'DEB_SIGN_KEYID');
942 sub signfile {
943 my $file = shift;
945 printcmd("signfile $file");
947 my $signdir = tempdir('dpkg-sign.XXXXXXXX', CLEANUP => 1);
948 my $signfile = "$signdir/$file";
950 # Make sure the file to sign ends with a newline.
951 copy("../$file", $signfile);
952 open my $signfh, '>>', $signfile or syserr(g_('cannot open %s'), $signfile);
953 print { $signfh } "\n";
954 close $signfh or syserr(g_('cannot close %s'), $signfile);
956 my $status = $openpgp->inline_sign($signfile, "$signfile.asc", $signkey);
957 if ($status == OPENPGP_OK) {
958 move("$signfile.asc", "../$file")
959 or syserror(g_('cannot move %s to %s'), "$signfile.asc", "../$file");
960 } else {
961 error(g_('failed to sign %s file: %s'), $file,
962 openpgp_errorcode_to_string($status));
965 return $status
968 sub fileomitted {
969 my ($files, $regex) = @_;
971 return $files !~ m/$regex$/m
974 sub describe_build {
975 my $files = shift;
976 my $ext = compression_get_file_extension_regex();
978 if (fileomitted($files, qr/\.deb/)) {
979 # source-only upload
980 if (fileomitted($files, qr/\.diff\.$ext/) and
981 fileomitted($files, qr/\.debian\.tar\.$ext/)) {
982 return g_('source-only upload: Debian-native package');
983 } elsif (fileomitted($files, qr/\.orig\.tar\.$ext/)) {
984 return g_('source-only, diff-only upload (original source NOT included)');
985 } else {
986 return g_('source-only upload (original source is included)');
988 } elsif (fileomitted($files, qr/\.dsc/)) {
989 return g_('binary-only upload (no source included)');
990 } elsif (fileomitted($files, qr/\.diff\.$ext/) and
991 fileomitted($files, qr/\.debian\.tar\.$ext/)) {
992 return g_('full upload; Debian-native package (full source is included)');
993 } elsif (fileomitted($files, qr/\.orig\.tar\.$ext/)) {
994 return g_('binary and diff upload (original source NOT included)');
995 } else {
996 return g_('full upload (original source is included)');
1000 sub build_target_fallback {
1001 my $ctrl = shift;
1003 # If we are building rootless, there is no need to call the build target
1004 # independently as non-root.
1005 return if not rules_requires_root($binarytarget);
1007 return if $buildtarget eq 'build';
1008 return if scalar @debian_rules != 1;
1010 # Check if we are building both arch:all and arch:any packages, in which
1011 # case we now require working build-indep and build-arch targets.
1012 my $pkg_arch = 0;
1014 foreach my $bin ($ctrl->get_packages()) {
1015 if ($bin->{Architecture} eq 'all') {
1016 $pkg_arch |= BUILD_ARCH_INDEP;
1017 } else {
1018 $pkg_arch |= BUILD_ARCH_DEP;
1022 return if $pkg_arch == BUILD_BINARY;
1024 # Check if the build-{arch,indep} targets are supported. If not, fallback
1025 # to build.
1026 my $pid = spawn(exec => [ $Dpkg::PROGMAKE, '-f', @debian_rules, '-qn', $buildtarget ],
1027 from_file => '/dev/null', to_file => '/dev/null',
1028 error_to_file => '/dev/null');
1029 my $cmdline = "make -f @debian_rules -qn $buildtarget";
1030 wait_child($pid, nocheck => 1, cmdline => $cmdline);
1031 my $exitcode = WEXITSTATUS($?);
1032 subprocerr($cmdline) unless WIFEXITED($?);
1033 if ($exitcode == 2) {
1034 warning(g_("%s must be updated to support the 'build-arch' and " .
1035 "'build-indep' targets (at least '%s' seems to be " .
1036 'missing)'), "@debian_rules", $buildtarget);
1037 $buildtarget = 'build';