dpkg-buildpackage.pl 21 KB

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