dpkg-buildpackage.pl 21 KB

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