dpkg-buildpackage.pl 26 KB

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