dpkg-buildpackage.pl 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721
  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. my $build_opts = Dpkg::BuildOptions->new();
  184. if ($build_opts->has('nocheck')) {
  185. $check_command = undef;
  186. } elsif (not find_command($check_command)) {
  187. $check_command = undef;
  188. }
  189. while (@ARGV) {
  190. $_ = shift @ARGV;
  191. if (/^(?:--help|-\?)$/) {
  192. usage;
  193. exit 0;
  194. } elsif (/^--version$/) {
  195. showversion;
  196. exit 0;
  197. } elsif (/^--admindir$/) {
  198. $admindir = shift @ARGV;
  199. } elsif (/^--admindir=(.*)$/) {
  200. $admindir = $1;
  201. } elsif (/^--source-option=(.*)$/) {
  202. push @source_opts, $1;
  203. } elsif (/^--changes-option=(.*)$/) {
  204. push @changes_opts, $1;
  205. } elsif (/^-j(\d*|auto)$/) {
  206. $parallel = $1 || '';
  207. } elsif (/^-r(.*)$/) {
  208. my $arg = $1;
  209. @rootcommand = split /\s+/, $arg;
  210. } elsif (/^--check-command=(.*)$/) {
  211. $check_command = $1;
  212. } elsif (/^--check-option=(.*)$/) {
  213. push @check_opts, $1;
  214. } elsif (/^--hook-(.+)=(.*)$/) {
  215. my ($hook_name, $hook_cmd) = ($1, $2);
  216. usageerr(_g('unknown hook name %s'), $hook_name)
  217. if not exists $hook{$hook_name};
  218. usageerr(_g('missing hook %s command'), $hook_name)
  219. if not defined $hook_cmd;
  220. $hook{$hook_name} = $hook_cmd;
  221. } elsif (/^-p(.*)$/) {
  222. $signcommand = $1;
  223. } elsif (/^-k(.*)$/) {
  224. $signkey = $1;
  225. } elsif (/^-([dD])$/) {
  226. $checkbuilddep = ($1 eq 'D');
  227. } elsif (/^-s(gpg|pgp)$/) {
  228. # Deprecated option
  229. warning(_g('-s%s is deprecated; always using gpg style interface'), $1);
  230. } elsif (/^--force-sign$/) {
  231. $signforce = 1;
  232. } elsif (/^-us$/) {
  233. $signsource = 0;
  234. } elsif (/^-uc$/) {
  235. $signchanges = 0;
  236. } elsif (/^-ap$/) {
  237. $signpause = 1;
  238. } elsif (/^-a(.*)$/) {
  239. $targetarch = $1;
  240. } elsif (/^-P(.*)$/) {
  241. my $arg = $1;
  242. @build_profiles = split /,/, $arg;
  243. } elsif (/^-s[iad]$/) {
  244. push @changes_opts, $_;
  245. } elsif (/^-(?:s[insAkurKUR]|[zZ].*|i.*|I.*)$/) {
  246. push @source_opts, $_; # passed to dpkg-source
  247. } elsif (/^-tc$/) {
  248. $cleansource = 1;
  249. } elsif (/^-t(.*)$/) {
  250. $targetgnusystem = $1; # Order DOES matter!
  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 (/^-b$/) {
  260. usageerr(_g('cannot combine %s and %s'), build_opt(), $_)
  261. if not build_is_default;
  262. $include = BUILD_BINARY;
  263. push @changes_opts, '-b';
  264. } elsif (/^-B$/) {
  265. usageerr(_g('cannot combine %s and %s'), build_opt(), $_)
  266. if not build_is_default;
  267. $include = BUILD_ARCH_DEP;
  268. push @changes_opts, '-B';
  269. } elsif (/^-A$/) {
  270. usageerr(_g('cannot combine %s and %s'), build_opt(), $_)
  271. if not build_is_default;
  272. $include = BUILD_ARCH_INDEP;
  273. push @changes_opts, '-A';
  274. } elsif (/^-S$/) {
  275. usageerr(_g('cannot combine %s and %s'), build_opt(), $_)
  276. if not build_is_default;
  277. $include = BUILD_SOURCE;
  278. push @changes_opts, '-S';
  279. } elsif (/^-G$/) {
  280. usageerr(_g('cannot combine %s and %s'), build_opt(), $_)
  281. if not build_is_default;
  282. $include = BUILD_SOURCE_DEP;
  283. push @changes_opts, '-G';
  284. } elsif (/^-g$/) {
  285. usageerr(_g('cannot combine %s and %s'), build_opt(), $_)
  286. if not build_is_default;
  287. $include = BUILD_SOURCE_INDEP;
  288. push @changes_opts, '-g';
  289. } elsif (/^-F$/) {
  290. usageerr(_g('cannot combine %s and %s'), build_opt(), $_)
  291. if not build_is_default;
  292. $include = BUILD_ALL;
  293. } elsif (/^-v(.*)$/) {
  294. $since = $1;
  295. } elsif (/^-m(.*)$/) {
  296. $maint = $1;
  297. } elsif (/^-e(.*)$/) {
  298. $changedby = $1;
  299. } elsif (/^-C(.*)$/) {
  300. $desc = $1;
  301. } elsif (m/^-[EW]$/) {
  302. # Deprecated option
  303. warning(_g('-E and -W are deprecated, they are without effect'));
  304. } elsif (/^-R(.*)$/) {
  305. my $arg = $1;
  306. @debian_rules = split /\s+/, $arg;
  307. } else {
  308. usageerr(_g('unknown option or argument %s'), $_);
  309. }
  310. }
  311. if (($include & BUILD_BINARY) == BUILD_BINARY) {
  312. $buildtarget = 'build';
  313. $binarytarget = 'binary';
  314. } elsif ($include & BUILD_ARCH_DEP) {
  315. $buildtarget = 'build-arch';
  316. $binarytarget = 'binary-arch';
  317. } elsif ($include & BUILD_ARCH_INDEP) {
  318. $buildtarget = 'build-indep';
  319. $binarytarget = 'binary-indep';
  320. }
  321. if ($noclean) {
  322. # -nc without -b/-B/-A/-S/-F implies -b
  323. $include = BUILD_BINARY if build_is_default;
  324. }
  325. if ($< == 0) {
  326. warning(_g('using a gain-root-command while being root')) if (@rootcommand);
  327. } else {
  328. push @rootcommand, 'fakeroot' unless @rootcommand;
  329. }
  330. if (@rootcommand and not find_command($rootcommand[0])) {
  331. if ($rootcommand[0] eq 'fakeroot' and $< != 0) {
  332. error(_g("fakeroot not found, either install the fakeroot\n" .
  333. 'package, specify a command with the -r option, ' .
  334. 'or run this as root'));
  335. } else {
  336. error(_g("gain-root-commmand '%s' not found"), $rootcommand[0]);
  337. }
  338. }
  339. if ($check_command and not find_command($check_command)) {
  340. error(_g("check-commmand '%s' not found"), $check_command);
  341. }
  342. if ($signcommand) {
  343. if (!find_command($signcommand)) {
  344. error(_g("sign-commmand '%s' not found"), $signcommand);
  345. }
  346. } elsif (($ENV{GNUPGHOME} && -e $ENV{GNUPGHOME}) ||
  347. ($ENV{HOME} && -e "$ENV{HOME}/.gnupg")) {
  348. if (find_command('gpg2')) {
  349. $signcommand = 'gpg2';
  350. } elsif (find_command('gpg')) {
  351. $signcommand = 'gpg';
  352. }
  353. }
  354. if (defined $parallel) {
  355. if ($parallel eq 'auto') {
  356. # Most Unices.
  357. $parallel = qx(getconf _NPROCESSORS_ONLN 2>/dev/null);
  358. # Fallback for at least Irix.
  359. $parallel = qx(getconf _NPROC_ONLN 2>/dev/null) if $?;
  360. chomp $parallel;
  361. }
  362. $parallel = $build_opts->get('parallel') if $build_opts->has('parallel');
  363. $ENV{MAKEFLAGS} //= '';
  364. $ENV{MAKEFLAGS} .= " -j$parallel";
  365. $build_opts->set('parallel', $parallel);
  366. $build_opts->export();
  367. }
  368. set_build_profiles(@build_profiles) if @build_profiles;
  369. my $cwd = cwd();
  370. my $dir = basename($cwd);
  371. my $changelog = changelog_parse();
  372. my $pkg = mustsetvar($changelog->{source}, _g('source package'));
  373. my $version = mustsetvar($changelog->{version}, _g('source version'));
  374. my $v = Dpkg::Version->new($version);
  375. my ($ok, $error) = version_check($v);
  376. error($error) unless $ok;
  377. my $sversion = $v->as_string(omit_epoch => 1);
  378. my $uversion = $v->version();
  379. my $distribution = mustsetvar($changelog->{distribution}, _g('source distribution'));
  380. my $maintainer;
  381. if ($changedby) {
  382. $maintainer = $changedby;
  383. } elsif ($maint) {
  384. $maintainer = $maint;
  385. } else {
  386. $maintainer = mustsetvar($changelog->{maintainer}, _g('source changed by'));
  387. }
  388. open my $arch_env, '-|', 'dpkg-architecture', "-a$targetarch",
  389. "-t$targetgnusystem", '-f' or subprocerr('dpkg-architecture');
  390. while ($_ = <$arch_env>) {
  391. chomp;
  392. my ($key, $value) = split /=/, $_, 2;
  393. $ENV{$key} = $value;
  394. }
  395. close $arch_env or subprocerr('dpkg-architecture');
  396. my $arch;
  397. if (build_sourceonly) {
  398. $arch = 'source';
  399. } elsif (build_binaryindep) {
  400. $arch = 'all';
  401. } else {
  402. $arch = mustsetvar($ENV{DEB_HOST_ARCH}, _g('host architecture'));
  403. }
  404. my $pv = "${pkg}_$sversion";
  405. my $pva = "${pkg}_${sversion}_$arch";
  406. if (not $signcommand) {
  407. $signsource = 0;
  408. $signchanges = 0;
  409. } elsif ($signforce) {
  410. $signsource = 1;
  411. $signchanges = 1;
  412. } elsif (($signsource or $signchanges) and $distribution eq 'UNRELEASED') {
  413. $signreleased = 0;
  414. $signsource = 0;
  415. $signchanges = 0;
  416. }
  417. if ($signsource && build_binaryonly) {
  418. $signsource = 0;
  419. }
  420. #
  421. # Preparation of environment stops here
  422. #
  423. run_hook('init', 1);
  424. if (not -x 'debian/rules') {
  425. warning(_g('debian/rules is not executable; fixing that'));
  426. chmod(0755, 'debian/rules'); # No checks of failures, non fatal
  427. }
  428. unless ($call_target) {
  429. chdir('..') or syserr('chdir ..');
  430. withecho('dpkg-source', @source_opts, '--before-build', $dir);
  431. chdir($dir) or syserr("chdir $dir");
  432. }
  433. if ($checkbuilddep) {
  434. my @checkbuilddep_opts;
  435. push @checkbuilddep_opts, '-A' if ($include & BUILD_ARCH_DEP) == 0;
  436. push @checkbuilddep_opts, '-B' if ($include & BUILD_ARCH_INDEP) == 0;
  437. push @checkbuilddep_opts, "--admindir=$admindir" if $admindir;
  438. system('dpkg-checkbuilddeps', @checkbuilddep_opts);
  439. if (not WIFEXITED($?)) {
  440. subprocerr('dpkg-checkbuilddeps');
  441. } elsif (WEXITSTATUS($?)) {
  442. warning(_g('build dependencies/conflicts unsatisfied; aborting'));
  443. warning(_g('(Use -d flag to override.)'));
  444. if (build_sourceonly) {
  445. warning(_g('this is currently a non-fatal warning with -S, but ' .
  446. 'will probably become fatal in the future'));
  447. } else {
  448. exit 3;
  449. }
  450. }
  451. }
  452. if ($call_target) {
  453. if ($call_target_as_root or
  454. $call_target =~ /^(clean|binary(|-arch|-indep))$/)
  455. {
  456. withecho(@rootcommand, @debian_rules, $call_target);
  457. } else {
  458. withecho(@debian_rules, $call_target);
  459. }
  460. exit 0;
  461. }
  462. run_hook('preclean', ! $noclean);
  463. unless ($noclean) {
  464. withecho(@rootcommand, @debian_rules, 'clean');
  465. }
  466. run_hook('source', $include & BUILD_SOURCE);
  467. if ($include & BUILD_SOURCE) {
  468. warning(_g('building a source package without cleaning up as you asked; ' .
  469. 'it might contain undesired files')) if $noclean;
  470. chdir('..') or syserr('chdir ..');
  471. withecho('dpkg-source', @source_opts, '-b', $dir);
  472. chdir($dir) or syserr("chdir $dir");
  473. }
  474. run_hook('build', $include & BUILD_BINARY);
  475. if ($buildtarget ne 'build' and scalar(@debian_rules) == 1) {
  476. # Verify that build-{arch,indep} are supported. If not, fallback to build.
  477. # This is a temporary measure to not break too many packages on a flag day.
  478. my $pid = spawn(exec => [ 'make', '-f', @debian_rules, '-qn', $buildtarget ],
  479. from_file => '/dev/null', to_file => '/dev/null',
  480. error_to_file => '/dev/null');
  481. my $cmdline = "make -f @debian_rules -qn $buildtarget";
  482. wait_child($pid, nocheck => 1, cmdline => $cmdline);
  483. my $exitcode = WEXITSTATUS($?);
  484. subprocerr($cmdline) unless WIFEXITED($?);
  485. if ($exitcode == 2) {
  486. warning(_g("%s must be updated to support the 'build-arch' and " .
  487. "'build-indep' targets (at least '%s' seems to be " .
  488. 'missing)'), "@debian_rules", $buildtarget);
  489. $buildtarget = 'build';
  490. }
  491. }
  492. if ($include & BUILD_BINARY) {
  493. withecho(@debian_rules, $buildtarget);
  494. run_hook('binary', 1);
  495. withecho(@rootcommand, @debian_rules, $binarytarget);
  496. }
  497. run_hook('changes', 1);
  498. push @changes_opts, "-m$maint" if defined $maint;
  499. push @changes_opts, "-e$changedby" if defined $changedby;
  500. push @changes_opts, "-v$since" if defined $since;
  501. push @changes_opts, "-C$desc" if defined $desc;
  502. my $chg = "../$pva.changes";
  503. my $changes = Dpkg::Control->new(type => CTRL_FILE_CHANGES);
  504. print { *STDERR } " dpkg-genchanges @changes_opts >$chg\n";
  505. open my $changes_fh, '-|', 'dpkg-genchanges', @changes_opts
  506. or subprocerr('dpkg-genchanges');
  507. $changes->parse($changes_fh, _g('parse changes file'));
  508. $changes->save($chg);
  509. close $changes_fh or subprocerr(_g('dpkg-genchanges'));
  510. run_hook('postclean', $cleansource);
  511. if ($cleansource) {
  512. withecho(@rootcommand, @debian_rules, 'clean');
  513. }
  514. chdir('..') or syserr('chdir ..');
  515. withecho('dpkg-source', @source_opts, '--after-build', $dir);
  516. chdir($dir) or syserr("chdir $dir");
  517. printf "$Dpkg::PROGNAME: %s\n", describe_build($changes->{'Files'});
  518. run_hook('check', $check_command);
  519. if ($check_command) {
  520. withecho($check_command, @check_opts, $chg);
  521. }
  522. if ($signpause && ($signchanges || $signsource)) {
  523. print _g("Press the return key to start signing process\n");
  524. getc();
  525. }
  526. run_hook('sign', $signsource || $signchanges);
  527. if ($signsource) {
  528. if (signfile("$pv.dsc")) {
  529. error(_g('failed to sign .dsc and .changes file'));
  530. }
  531. # Recompute the checksums as the .dsc has changed now.
  532. my $checksums = Dpkg::Checksums->new();
  533. $checksums->add_from_control($changes);
  534. $checksums->add_from_file("../$pv.dsc", update => 1, key => "$pv.dsc");
  535. $checksums->export_to_control($changes);
  536. delete $changes->{'Checksums-Md5'};
  537. my $md5sum_regex = checksums_get_property('md5', 'regex');
  538. my $dsc_md5sum = $checksums->get_checksum("$pv.dsc", 'md5');
  539. my $dsc_size = $checksums->get_size("$pv.dsc");
  540. my $dsc_files_regex = qr/$md5sum_regex\s+\d+\s+(\S+\s+\S+\s+\Q$pv\E\.dsc)/;
  541. $changes->{'Files'} =~ s/^$dsc_files_regex$/$dsc_md5sum $dsc_size $1/m;
  542. $changes->save($chg);
  543. }
  544. if ($signchanges && signfile("$pva.changes")) {
  545. error(_g('failed to sign .changes file'));
  546. }
  547. if (not $signreleased) {
  548. warning(_g('not signing UNRELEASED build; use --force-sign to override'));
  549. }
  550. run_hook('done', 1);
  551. sub mustsetvar {
  552. my ($var, $text) = @_;
  553. error(_g('unable to determine %s'), $text)
  554. unless defined($var);
  555. print "$Dpkg::PROGNAME: $text $var\n";
  556. return $var;
  557. }
  558. sub withecho {
  559. print { *STDERR } " @_\n";
  560. system(@_)
  561. and subprocerr("@_");
  562. }
  563. sub run_hook {
  564. my ($name, $enabled) = @_;
  565. my $cmd = $hook{$name};
  566. return if not $cmd;
  567. print { *STDERR } "$Dpkg::PROGNAME: running hook $name\n";
  568. my %hook_vars = (
  569. '%' => '%',
  570. 'a' => $enabled ? 1 : 0,
  571. 'p' => $pkg,
  572. 'v' => $version,
  573. 's' => $sversion,
  574. 'u' => $uversion,
  575. );
  576. my $subst_hook_var = sub {
  577. my ($var) = @_;
  578. if (exists $hook_vars{$var}) {
  579. return $hook_vars{$var};
  580. } else {
  581. warning(_g('unknown %% substitution in hook: %%%s'), $var);
  582. return "\%$var";
  583. }
  584. };
  585. $cmd =~ s/\%(.)/&$subst_hook_var($1)/eg;
  586. withecho($cmd);
  587. }
  588. sub signfile {
  589. my ($file) = @_;
  590. print { *STDERR } " signfile $file\n";
  591. my $qfile = quotemeta($file);
  592. system("(cat ../$qfile ; echo '') | " .
  593. "$signcommand --utf8-strings --local-user " .
  594. quotemeta($signkey || $maintainer) .
  595. " --clearsign --armor --textmode > ../$qfile.asc");
  596. my $status = $?;
  597. unless ($status) {
  598. system('mv', '--', "../$file.asc", "../$file")
  599. and subprocerr('mv');
  600. } else {
  601. system('rm', '-f', "../$file.asc")
  602. and subprocerr('rm -f');
  603. }
  604. print "\n";
  605. return $status
  606. }
  607. sub fileomitted {
  608. my ($files, $regex) = @_;
  609. return $files !~ /$regex/
  610. }
  611. sub describe_build {
  612. my ($files) = @_;
  613. my $ext = compression_get_file_extension_regex();
  614. if (fileomitted($files, qr/\.deb/)) {
  615. # source-only upload
  616. if (fileomitted($files, qr/\.diff\.$ext/) and
  617. fileomitted($files, qr/\.debian\.tar\.$ext/)) {
  618. return _g('source-only upload: Debian-native package');
  619. } elsif (fileomitted($files, qr/\.orig\.tar\.$ext/)) {
  620. return _g('source-only, diff-only upload (original source NOT included)');
  621. } else {
  622. return _g('source-only upload (original source is included)');
  623. }
  624. } elsif (fileomitted($files, qr/\.dsc/)) {
  625. return _g('binary-only upload (no source included)');
  626. } elsif (fileomitted($files, qr/\.diff\.$ext/) and
  627. fileomitted($files, qr/\.debian\.tar\.$ext/)) {
  628. return _g('full upload; Debian-native package (full source is included)');
  629. } elsif (fileomitted($files, qr/\.orig\.tar\.$ext/)) {
  630. return _g('binary and diff upload (original source NOT included)');
  631. } else {
  632. return _g('full upload (original source is included)');
  633. }
  634. }