dpkg-buildpackage.pl 24 KB

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