dpkg-buildpackage.pl 25 KB

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