dpkg-buildpackage.pl 22 KB

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