dpkg-buildpackage.pl 22 KB

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