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/>.
26 use File
::Temp
qw(tempdir);
29 use File
::Glob
qw(bsd_glob GLOB_TILDE GLOB_NOCHECK);
30 use POSIX
qw(:sys_wait_h);
34 use Dpkg
::ErrorHandling
;
36 use Dpkg
::BuildOptions
;
37 use Dpkg
::BuildProfiles
qw(set_build_profiles);
39 use Dpkg
::Compression
;
44 use Dpkg
::Control
::Info
;
45 use Dpkg
::Changelog
::Parse
;
47 use Dpkg
::OpenPGP
::ErrorCodes
;
48 use Dpkg
::OpenPGP
::KeyHandle
;
49 use Dpkg
::Path
qw(find_command);
51 use Dpkg
::Vendor
qw(run_vendor_hook);
53 textdomain
('dpkg-dev');
56 printf g_
("Debian %s version %s.\n"), $Dpkg::PROGNAME
, $Dpkg::PROGVERSION
;
59 This is free software; see the GNU General Public License version 2 or
60 later for copying conditions. There is NO warranty.
66 'Usage: %s [<option>...]')
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]
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
115 -p, --sign-command=<command>
116 command to sign .dsc and/or .changes files
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.')
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.')
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.')
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.
164 my @debian_rules = ('debian/rules');
165 my @rootcommand = ();
170 my $sanitize_env = 0;
172 my $parallel_force = 0;
173 my $checkbuilddep = 1;
174 my $check_builtin_builddep = 1;
176 my $check_command = $ENV{DEB_CHECK_COMMAND
};
179 my $signkeyfile = $ENV{DEB_SIGN_KEYFILE
};
180 my $signkeyid = $ENV{DEB_SIGN_KEYID
};
182 my $signreleased = 1;
184 my $signbuildinfo = 1;
186 my $buildtarget = 'build';
187 my $binarytarget = 'binary';
190 my $target_arch = '';
191 my $target_type = '';
192 my @build_profiles = ();
194 my @call_target = ();
195 my $call_target_as_root = 0;
204 my %target_legacy_root = map { $_ => 1 } qw(
210 my %target_official = map { $_ => 1 } qw(
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;
254 if (/^(?:--help|-\?)$/) {
257 } elsif (/^--version$/) {
260 } elsif (/^--admindir$/) {
261 $admindir = shift @ARGV;
262 } elsif (/^--admindir=(.*)$/) {
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;
276 push @buildinfo_opts, $buildinfo_opt;
278 } elsif (/^--changes-file=(.*)$/) {
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');
288 push @changes_opts, $changes_opt;
290 } elsif (/^--jobs(?:-try)?$/) {
293 } elsif (/^(?:-[jJ]|--jobs(?:-try)?=)(\d*|auto)$/) {
294 $parallel = $1 || '';
296 } elsif (/^--jobs-force(?:=(\d*|auto))?$/) {
297 $parallel = $1 || '';
299 } elsif (/^(?:-r|--root-command=)(.*)$/) {
301 @rootcommand = split ' ', $arg;
302 } elsif (/^--check-command=(.*)$/) {
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)=.*$/) {
315 warning
(g_
('%s is deprecated; it is without effect'), $1);
316 } elsif (/^--sign-backend=(.*)$/) {
318 } elsif (/^(?:-p|--sign-command=)(.*)$/) {
320 } elsif (/^--sign-keyfile=(.*)$/) {
322 } elsif (/^(?:-k|--sign-keyid=|--sign-key=)(.*)$/) {
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)$/) {
332 warning
(g_
('-s%s is deprecated; always using gpg style interface'), $1);
333 } elsif (/^--force-sign$/) {
335 } elsif (/^--no-sign$/) {
340 } elsif (/^-us$/ or /^--unsigned-source$/) {
342 } elsif (/^-ui$/ or /^--unsigned-buildinfo$/) {
344 } elsif (/^-uc$/ or /^--unsigned-changes$/) {
347 } elsif (/^-ap$/ or /^--sign-pausa$/) {
349 } elsif (/^-a$/ or /^--host-arch$/) {
351 } elsif (/^-a(.*)$/ or /^--host-arch=(.*)$/) {
353 } elsif (/^-P(.*)$/ or /^--build-profiles=(.*)$/) {
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$/) {
366 } elsif (/^--no-post-clean$/) {
368 } elsif (/^--sanitize-env$/) {
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=(.*)$/) {
378 } elsif (/^--target-type$/) {
379 $target_type = shift;
380 } elsif (/^--target-type=(.*)$/) {
382 } elsif (/^(?:--target|--rules-target|-T)$/) {
383 push @call_target, split /,/, shift @ARGV;
384 } elsif (/^(?:--target=|--rules-target=|-T)(.+)$/) {
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$/) {
393 } elsif (/^-nc$/ or /^--no-pre-clean$/) {
395 } elsif (/^--build=(.*)$/) {
396 set_build_type_from_options
($1, $_);
398 set_build_type
(BUILD_BINARY
, $_);
400 set_build_type
(BUILD_ARCH_DEP
, $_);
402 set_build_type
(BUILD_ARCH_INDEP
, $_);
404 set_build_type
(BUILD_SOURCE
, $_);
406 set_build_type
(BUILD_SOURCE
| BUILD_ARCH_DEP
, $_);
408 set_build_type
(BUILD_SOURCE
| BUILD_ARCH_INDEP
, $_);
410 set_build_type
(BUILD_FULL
, $_);
411 } elsif (/^-v(.*)$/) {
413 } elsif (/^-m(.*)$/ or /^--(?:source|build)-by=(.*)$/) {
415 } elsif (/^-e(.*)$/ or /^--(?:changed|release)-by=(.*)$/) {
417 } elsif (/^-C(.*)$/) {
419 } elsif (m/^-[EW]$/) {
421 warning
(g_
('%s is deprecated; it is without effect'), $_);
422 } elsif (/^-R(.*)$/ or /^--rules-file=(.*)$/) {
424 @debian_rules = split ' ', $arg;
426 usageerr
(g_
('unknown option or argument %s'), $_);
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';
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')) {
472 # Prepare the environment.
475 run_hook
('preinit', 1);
477 if (defined $parallel) {
478 if ($parallel eq 'auto') {
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
485 $parallel = '1' if $?
;
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
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'));
523 $maintainer = $changedby;
525 $maintainer = $maint;
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;
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>) {
543 my ($key, $value) = split /=/, $_, 2;
546 close $arch_env or subprocerr
('dpkg-architecture');
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
)) {
553 } elsif (build_has_any
(BUILD_SOURCE
)) {
557 my $pv = "${pkg}_$sversion";
558 my $pva = "${pkg}_${sversion}_$arch";
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;
569 $signkeytype = 'userid';
570 $signkeyhandle = $maintainer;
572 my $signkey = Dpkg
::OpenPGP
::KeyHandle
->new(
573 type
=> $signkeytype,
574 handle
=> $signkeyhandle,
578 my $openpgp = Dpkg
::OpenPGP
->new(
579 backend
=> $signbackend // 'auto',
580 cmd
=> $signcommand // 'auto',
582 keystore
=> $signkey->needs_keystore(),
586 if (not $openpgp->can_use_secrets($signkey)) {
590 } elsif ($signforce) {
594 } elsif (($signsource or $signbuildinfo or $signchanges) and
595 $distribution eq 'UNRELEASED') {
602 if ($signsource && build_has_none
(BUILD_SOURCE
)) {
606 # Sanitize build environment.
608 run_vendor_hook
('sanitize-environment');
612 # Preparation of environment stops here
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.)'));
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);
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
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);
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");
727 run_hook
('sign', $signsource || $signbuildinfo || $signchanges);
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")
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")
755 update_files_field
($changes, $checksums, "$pva.buildinfo");
756 $changes->save($changes_file);
759 signfile
("$pva.changes");
762 if (not $signreleased) {
763 warning
(g_
('not signing UNRELEASED build; use --force-sign to override'));
769 my ($var, $text) = @_;
771 error
(g_
('unable to determine %s'), $text)
772 unless defined($var);
778 sub setup_rootcommand
{
780 warning
(g_
('using a gain-root-command while being root')) if @rootcommand;
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'));
791 error
(g_
("gain-root-command '%s' not found"), $rootcommand[0]);
796 sub parse_rules_requires_root
{
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);
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);
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}) {
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;
851 # We should not provide the variable otherwise.
852 delete $ENV{DEB_GAIN_ROOT_CMD
};
862 system @cmd and subprocerr
("@cmd");
865 sub rules_requires_root
{
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};
874 sub run_rules_cond_root
{
878 push @cmd, @rootcommand if rules_requires_root
($target);
879 push @cmd, @debian_rules, $target;
885 my ($name, $enabled) = @_;
886 my $cmd = $hook{$name};
890 info
("running hook $name");
894 'a' => $enabled ?
1 : 0,
901 my $subst_hook_var = sub {
904 if (exists $hook_vars{$var}) {
905 return $hook_vars{$var};
907 warning
(g_
('unknown %% substitution in hook: %%%s'), $var);
912 $cmd =~ s/\%(.)/$subst_hook_var->($1)/eg;
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');
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");
961 error
(g_
('failed to sign %s file: %s'), $file,
962 openpgp_errorcode_to_string
($status));
969 my ($files, $regex) = @_;
971 return $files !~ m/$regex$/m
976 my $ext = compression_get_file_extension_regex
();
978 if (fileomitted
($files, qr/\.deb/)) {
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)');
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)');
996 return g_
('full upload (original source is included)');
1000 sub build_target_fallback
{
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.
1014 foreach my $bin ($ctrl->get_packages()) {
1015 if ($bin->{Architecture
} eq 'all') {
1016 $pkg_arch |= BUILD_ARCH_INDEP
;
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
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';