dpkg-buildpackage.pl 22 KB

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