dpkg-buildpackage.pl 21 KB

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