dpkg-buildpackage: Fix R³ namespace check to only refuse unknown dpkg keywords
[dpkg.git] / scripts / dpkg-buildpackage.pl
blobf119f11f510831c4b24fcecae4da055ec953173f
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');
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');
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', {
650 enabled => $preclean,
653 if ($preclean) {
654 run_rules_cond_root('clean');
657 run_hook('source', {
658 enabled => build_has_any(BUILD_SOURCE),
659 env => {
660 DPKG_BUILDPACKAGE_HOOK_SOURCE_OPTIONS => join(' ', @source_opts),
664 if (build_has_any(BUILD_SOURCE)) {
665 warning(g_('building a source package without cleaning up as you asked; ' .
666 'it might contain undesired files')) if not $preclean;
667 run_cmd('dpkg-source', @source_opts, '-b', '.');
670 my $build_types = get_build_options_from_type();
672 if (build_has_any(BUILD_BINARY)) {
673 # XXX Use some heuristics to decide whether to use build-{arch,indep}
674 # targets. This is a temporary measure to not break too many packages
675 # on a flag day.
676 build_target_fallback($ctrl);
679 # If we are building rootless, there is no need to call the build target
680 # independently as non-root.
681 if (build_has_any(BUILD_BINARY) && rules_requires_root($binarytarget)) {
682 run_hook('build', {
683 env => {
684 DPKG_BUILDPACKAGE_HOOK_BUILD_TARGET => $buildtarget,
687 run_cmd(@debian_rules, $buildtarget);
688 } else {
689 run_hook('build', {
690 enabled => 0,
694 if (build_has_any(BUILD_BINARY)) {
695 run_hook('binary', {
696 env => {
697 DPKG_BUILDPACKAGE_HOOK_BINARY_TARGET => $binarytarget,
700 run_rules_cond_root($binarytarget);
703 $buildinfo_file //= "../$pva.buildinfo";
705 push @buildinfo_opts, "--build=$build_types" if build_has_none(BUILD_DEFAULT);
706 push @buildinfo_opts, "--admindir=$admindir" if $admindir;
707 push @buildinfo_opts, "-O$buildinfo_file" if $buildinfo_file;
709 run_hook('buildinfo', {
710 env => {
711 DPKG_BUILDPACKAGE_HOOK_BUILDINFO_OPTIONS => join(' ', @buildinfo_opts),
714 run_cmd('dpkg-genbuildinfo', @buildinfo_opts);
716 $changes_file //= "../$pva.changes";
718 push @changes_opts, "--build=$build_types" if build_has_none(BUILD_DEFAULT);
719 push @changes_opts, "-m$maint" if defined $maint;
720 push @changes_opts, "-e$changedby" if defined $changedby;
721 push @changes_opts, "-v$since" if defined $since;
722 push @changes_opts, "-C$desc" if defined $desc;
723 push @changes_opts, "-O$changes_file";
725 my $changes = Dpkg::Control->new(type => CTRL_FILE_CHANGES);
727 run_hook('changes', {
728 env => {
729 DPKG_BUILDPACKAGE_HOOK_CHANGES_OPTIONS => join(' ', @changes_opts),
732 run_cmd('dpkg-genchanges', @changes_opts);
733 $changes->load($changes_file);
735 run_hook('postclean', {
736 enabled => $postclean,
739 if ($postclean) {
740 run_rules_cond_root('clean');
743 run_cmd('dpkg-source', @source_opts, '--after-build', '.');
745 info(describe_build($changes->{'Files'}));
747 run_hook('check', {
748 enabled => $check_command,
749 env => {
750 DPKG_BUILDPACKAGE_HOOK_CHECK_OPTIONS => join(' ', @check_opts),
754 if ($check_command) {
755 run_cmd($check_command, @check_opts, $changes_file);
758 if ($signpause && ($signsource || $signbuildinfo || $signchanges)) {
759 print g_("Press <enter> to start the signing process.\n");
760 getc();
763 run_hook('sign', {
764 enabled => $signsource || $signbuildinfo || $signchanges,
767 if ($signsource) {
768 signfile("$pv.dsc");
770 # Recompute the checksums as the .dsc has changed now.
771 my $buildinfo = Dpkg::Control->new(type => CTRL_FILE_BUILDINFO);
772 $buildinfo->load($buildinfo_file);
773 my $checksums = Dpkg::Checksums->new();
774 $checksums->add_from_control($buildinfo);
775 $checksums->add_from_file("../$pv.dsc", update => 1, key => "$pv.dsc");
776 $checksums->export_to_control($buildinfo);
777 $buildinfo->save($buildinfo_file);
779 if ($signbuildinfo) {
780 signfile("$pva.buildinfo");
782 if ($signsource or $signbuildinfo) {
783 # Recompute the checksums as the .dsc and/or .buildinfo have changed.
784 my $checksums = Dpkg::Checksums->new();
785 $checksums->add_from_control($changes);
786 $checksums->add_from_file("../$pv.dsc", update => 1, key => "$pv.dsc")
787 if $signsource;
788 $checksums->add_from_file($buildinfo_file, update => 1, key => "$pva.buildinfo");
789 $checksums->export_to_control($changes);
790 delete $changes->{'Checksums-Md5'};
791 update_files_field($changes, $checksums, "$pv.dsc")
792 if $signsource;
793 update_files_field($changes, $checksums, "$pva.buildinfo");
794 $changes->save($changes_file);
796 if ($signchanges) {
797 signfile("$pva.changes");
800 if (not $signreleased) {
801 warning(g_('not signing UNRELEASED build; use --force-sign to override'));
804 run_hook('done');
806 sub mustsetvar {
807 my ($var, $text) = @_;
809 error(g_('unable to determine %s'), $text)
810 unless defined($var);
812 info("$text $var");
813 return $var;
816 sub setup_rootcommand {
817 if ($< == 0) {
818 warning(g_('using a gain-root-command while being root')) if @rootcommand;
819 } else {
820 push @rootcommand, 'fakeroot' unless @rootcommand;
823 if (@rootcommand and not find_command($rootcommand[0])) {
824 if ($rootcommand[0] eq 'fakeroot' and $< != 0) {
825 error(g_("fakeroot not found, either install the fakeroot\n" .
826 'package, specify a command with the -r option, ' .
827 'or run this as root'));
828 } else {
829 error(g_("gain-root-command '%s' not found"), $rootcommand[0]);
834 sub parse_rules_requires_root {
835 my $ctrl = shift;
837 my %rrr;
838 my $rrr;
839 my $keywords_base;
840 my $keywords_impl;
842 $rrr = $rrr_override // $ctrl->{'Rules-Requires-Root'} // 'binary-targets';
844 foreach my $keyword (split ' ', $rrr) {
845 if ($keyword =~ m{/}) {
846 if ($keyword =~ m{^dpkg/target/(.*)$}p and $target_official{$1}) {
847 error(g_('disallowed target in %s field keyword "%s"'),
848 'Rules-Requires-Root', $keyword);
849 } elsif ($keyword =~ m{^dpkg/(.*)$} and $1 ne 'target-subcommand') {
850 error(g_('%s field keyword "%s" is unknown in dpkg namespace'),
851 'Rules-Requires-Root', $keyword);
853 $keywords_impl++;
854 } else {
855 if ($keyword ne lc $keyword and
856 (lc $keyword eq 'no' or lc $keyword eq 'binary-targets')) {
857 error(g_('%s field keyword "%s" is uppercase; use "%s" instead'),
858 'Rules-Requires-Root', $keyword, lc $keyword);
859 } elsif (lc $keyword eq 'yes') {
860 error(g_('%s field keyword "%s" is invalid; use "%s" instead'),
861 'Rules-Requires-Root', $keyword, 'binary-targets');
862 } elsif ($keyword ne 'no' and $keyword ne 'binary-targets') {
863 warning(g_('%s field keyword "%s" is unknown'),
864 'Rules-Requires-Root', $keyword);
866 $keywords_base++;
869 if ($rrr{$keyword}++) {
870 error(g_('field %s contains duplicate keyword "%s"'),
871 'Rules-Requires-Root', $keyword);
875 if ($call_target_as_root or not exists $rrr{no}) {
876 setup_rootcommand();
879 # Notify the children we do support R³.
880 $ENV{DEB_RULES_REQUIRES_ROOT} = join ' ', sort keys %rrr;
882 if ($keywords_base > 1 or $keywords_base and $keywords_impl) {
883 error(g_('%s field contains both global and implementation specific keywords'),
884 'Rules-Requires-Root');
885 } elsif ($keywords_impl) {
886 # Set only on <implementations-keywords>.
887 $ENV{DEB_GAIN_ROOT_CMD} = join ' ', @rootcommand;
888 } else {
889 # We should not provide the variable otherwise.
890 delete $ENV{DEB_GAIN_ROOT_CMD};
893 return %rrr;
896 sub run_cmd {
897 my @cmd = @_;
899 printcmd(@cmd);
900 system @cmd and subprocerr("@cmd");
903 sub rules_requires_root {
904 my $target = shift;
906 return 1 if $call_target_as_root;
907 return 1 if $rules_requires_root{"dpkg/target/$target"};
908 return 1 if $rules_requires_root{'binary-targets'} and $target_legacy_root{$target};
909 return 0;
912 sub run_rules_cond_root {
913 my $target = shift;
915 my @cmd;
916 push @cmd, @rootcommand if rules_requires_root($target);
917 push @cmd, @debian_rules, $target;
919 run_cmd(@cmd);
922 sub run_hook {
923 my ($name, $opts) = @_;
924 my $cmd = $hook{$name};
925 $opts->{enabled} //= 1;
927 return if not $cmd;
929 info("running hook $name");
931 my %hook_vars = (
932 '%' => '%',
933 'a' => $opts->{enabled} ? 1 : 0,
934 'p' => $pkg // q{},
935 'v' => $version // q{},
936 's' => $sversion // q{},
937 'u' => $uversion // q{},
940 my $subst_hook_var = sub {
941 my $var = shift;
943 if (exists $hook_vars{$var}) {
944 return $hook_vars{$var};
945 } else {
946 warning(g_('unknown %% substitution in hook: %%%s'), $var);
947 return "\%$var";
951 $cmd =~ s/\%(.)/$subst_hook_var->($1)/eg;
953 $opts->{env}{DPKG_BUILDPACKAGE_HOOK_NAME} = $name;
955 # Set any environment variables for this hook invocation.
956 local @ENV{keys %{$opts->{env}}} = values %{$opts->{env}};
958 run_cmd($cmd);
961 sub update_files_field {
962 my ($ctrl, $checksums, $filename) = @_;
964 my $md5sum_regex = checksums_get_property('md5', 'regex');
965 my $md5sum = $checksums->get_checksum($filename, 'md5');
966 my $size = $checksums->get_size($filename);
967 my $file_regex = qr/$md5sum_regex\s+\d+\s+(\S+\s+\S+\s+\Q$filename\E)/;
969 $ctrl->{'Files'} =~ s/^$file_regex$/$md5sum $size $1/m;
972 sub signkey_validate {
973 return unless $signkey->type eq 'keyid';
975 if (length $signkey->handle <= 8) {
976 error(g_('short OpenPGP key IDs are broken; ' .
977 'please use key fingerprints in %s or %s instead'),
978 '-k', 'DEB_SIGN_KEYID');
979 } elsif (length $signkey->handle <= 16) {
980 warning(g_('long OpenPGP key IDs are strongly discouraged; ' .
981 'please use key fingerprints in %s or %s instead'),
982 '-k', 'DEB_SIGN_KEYID');
986 sub signfile {
987 my $file = shift;
989 printcmd("signfile $file");
991 my $signdir = tempdir('dpkg-sign.XXXXXXXX', CLEANUP => 1);
992 my $signfile = "$signdir/$file";
994 # Make sure the file to sign ends with a newline.
995 copy("../$file", $signfile);
996 open my $signfh, '>>', $signfile or syserr(g_('cannot open %s'), $signfile);
997 print { $signfh } "\n";
998 close $signfh or syserr(g_('cannot close %s'), $signfile);
1000 my $status = $openpgp->inline_sign($signfile, "$signfile.asc", $signkey);
1001 if ($status == OPENPGP_OK) {
1002 move("$signfile.asc", "../$file")
1003 or syserror(g_('cannot move %s to %s'), "$signfile.asc", "../$file");
1004 } else {
1005 error(g_('failed to sign %s file: %s'), $file,
1006 openpgp_errorcode_to_string($status));
1009 return $status
1012 sub fileomitted {
1013 my ($files, $regex) = @_;
1015 return $files !~ m/$regex$/m
1018 sub describe_build {
1019 my $files = shift;
1020 my $ext = compression_get_file_extension_regex();
1022 if (fileomitted($files, qr/\.deb/)) {
1023 # source-only upload
1024 if (fileomitted($files, qr/\.diff\.$ext/) and
1025 fileomitted($files, qr/\.debian\.tar\.$ext/)) {
1026 return g_('source-only upload: Debian-native package');
1027 } elsif (fileomitted($files, qr/\.orig\.tar\.$ext/)) {
1028 return g_('source-only, diff-only upload (original source NOT included)');
1029 } else {
1030 return g_('source-only upload (original source is included)');
1032 } elsif (fileomitted($files, qr/\.dsc/)) {
1033 return g_('binary-only upload (no source included)');
1034 } elsif (fileomitted($files, qr/\.diff\.$ext/) and
1035 fileomitted($files, qr/\.debian\.tar\.$ext/)) {
1036 return g_('full upload; Debian-native package (full source is included)');
1037 } elsif (fileomitted($files, qr/\.orig\.tar\.$ext/)) {
1038 return g_('binary and diff upload (original source NOT included)');
1039 } else {
1040 return g_('full upload (original source is included)');
1044 sub build_target_fallback {
1045 my $ctrl = shift;
1047 # If we are building rootless, there is no need to call the build target
1048 # independently as non-root.
1049 return if not rules_requires_root($binarytarget);
1051 return if $buildtarget eq 'build';
1052 return if scalar @debian_rules != 1;
1054 # Check if we are building both arch:all and arch:any packages, in which
1055 # case we now require working build-indep and build-arch targets.
1056 my $pkg_arch = 0;
1058 foreach my $bin ($ctrl->get_packages()) {
1059 if ($bin->{Architecture} eq 'all') {
1060 $pkg_arch |= BUILD_ARCH_INDEP;
1061 } else {
1062 $pkg_arch |= BUILD_ARCH_DEP;
1066 return if $pkg_arch == BUILD_BINARY;
1068 # Check if the build-{arch,indep} targets are supported. If not, fallback
1069 # to build.
1070 my $pid = spawn(exec => [ $Dpkg::PROGMAKE, '-f', @debian_rules, '-qn', $buildtarget ],
1071 from_file => '/dev/null', to_file => '/dev/null',
1072 error_to_file => '/dev/null');
1073 my $cmdline = "make -f @debian_rules -qn $buildtarget";
1074 wait_child($pid, nocheck => 1, cmdline => $cmdline);
1075 my $exitcode = WEXITSTATUS($?);
1076 subprocerr($cmdline) unless WIFEXITED($?);
1077 if ($exitcode == 2) {
1078 warning(g_("%s must be updated to support the 'build-arch' and " .
1079 "'build-indep' targets (at least '%s' seems to be " .
1080 'missing)'), "@debian_rules", $buildtarget);
1081 $buildtarget = 'build';