dpkg-buildpackage.pl 24 KB

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