dpkg-buildpackage.pl 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572
  1. #!/usr/bin/perl
  2. #
  3. # dpkg-buildpackage
  4. #
  5. # Copyright © 1996 Ian Jackson
  6. # Copyright © 2000 Wichert Akkerman
  7. # Copyright © 2006-2010,2012-2013 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::Basename;
  26. use POSIX qw(:sys_wait_h);
  27. use Dpkg ();
  28. use Dpkg::Gettext;
  29. use Dpkg::ErrorHandling;
  30. use Dpkg::BuildOptions;
  31. use Dpkg::BuildProfiles qw(set_build_profiles);
  32. use Dpkg::Compression;
  33. use Dpkg::Version;
  34. use Dpkg::Changelog::Parse;
  35. use Dpkg::Path qw(find_command);
  36. use Dpkg::IPC;
  37. textdomain('dpkg-dev');
  38. sub showversion {
  39. printf _g("Debian %s version %s.\n"), $Dpkg::PROGNAME, $Dpkg::PROGVERSION;
  40. print _g('
  41. This is free software; see the GNU General Public License version 2 or
  42. later for copying conditions. There is NO warranty.
  43. ');
  44. }
  45. sub usage {
  46. printf _g(
  47. 'Usage: %s [<option>...]')
  48. . "\n\n" . _g(
  49. 'Options:
  50. -F (default) normal full build (binaries and sources).
  51. -b binary-only, do not build source.
  52. -B binary-only, no arch-indep files.
  53. -A binary-only, only arch-indep files.
  54. -S source only, no binary files.
  55. -nc do not clean source tree (implies -b).
  56. -tc clean source tree when finished.
  57. -D (default) check build dependencies and conflicts.
  58. -d do not check build dependencies and conflicts.
  59. -P<profiles> assume given build profiles as active (comma-separated list).
  60. -R<rules> rules file to execute (default is debian/rules).
  61. -T<target> call debian/rules <target> with the proper environment.
  62. --as-root ensure -T calls the target with root rights.
  63. -j[<number>] specify jobs to run simultaneously (passed to <rules>).
  64. -r<gain-root-command>
  65. command to gain root privileges (default is fakeroot).
  66. -p<sign-command>
  67. command to sign .dsc and/or .changes files
  68. (default is gpg2 or gpg).
  69. -k<keyid> the key to use for signing.
  70. -ap add pause before starting signature process.
  71. -us unsigned source package.
  72. -uc unsigned .changes file.
  73. --force-sign
  74. force signing the resulting files.
  75. --admindir=<directory>
  76. change the administrative directory.
  77. -?, --help show this help message.
  78. --version show the version.')
  79. . "\n\n" . _g(
  80. 'Options passed to dpkg-architecture:
  81. -a<arch> Debian architecture we build for.
  82. -t<system> set GNU system type.')
  83. . "\n\n" . _g(
  84. 'Options passed to dpkg-genchanges:
  85. -si (default) source includes orig if new upstream.
  86. -sa uploaded source always includes orig.
  87. -sd uploaded source is diff and .dsc only.
  88. -v<version> changes since version <version>.
  89. -m<maint> maintainer for package is <maint>.
  90. -e<maint> maintainer for release is <maint>.
  91. -C<descfile> changes are described in <descfile>.
  92. --changes-option=<opt>
  93. pass option <opt> to dpkg-genchanges.')
  94. . "\n\n" . _g(
  95. 'Options passed to dpkg-source:
  96. -sn force Debian native source format.
  97. -s[sAkurKUR] see dpkg-source for explanation.
  98. -z<level> compression level to use for source.
  99. -Z<compressor> compression to use for source (gz|xz|bzip2|lzma).
  100. -i[<regex>] ignore diffs of files matching regex.
  101. -I[<pattern>] filter out files when building tarballs.
  102. --source-option=<opt>
  103. pass option <opt> to dpkg-source.
  104. '), $Dpkg::PROGNAME;
  105. }
  106. my @debian_rules = ('debian/rules');
  107. my @rootcommand = ();
  108. my $signcommand;
  109. my ($admindir, $noclean,
  110. $cleansource, $since, $maint,
  111. $changedby, $desc, $parallel);
  112. my $checkbuilddep = 1;
  113. my $signpause;
  114. my $signkey = defined $ENV{DEB_SIGN_KEYID} ? $ENV{DEB_SIGN_KEYID} : undef;
  115. my $signforce = 0;
  116. my $signreleased = 1;
  117. my $signsource = 1;
  118. my $signchanges = 1;
  119. my $buildtarget = 'build';
  120. my $binarytarget = 'binary';
  121. my $targetarch = my $targetgnusystem = '';
  122. my @build_profiles = ();
  123. my $call_target = '';
  124. my $call_target_as_root = 0;
  125. my (@checkbuilddep_opts, @changes_opts, @source_opts);
  126. use constant BUILD_DEFAULT => 1;
  127. use constant BUILD_SOURCE => 2;
  128. use constant BUILD_ARCH_DEP => 4;
  129. use constant BUILD_ARCH_INDEP => 8;
  130. use constant BUILD_BINARY => BUILD_ARCH_DEP | BUILD_ARCH_INDEP;
  131. use constant BUILD_ALL => BUILD_BINARY | BUILD_SOURCE;
  132. my $include = BUILD_ALL | BUILD_DEFAULT;
  133. sub build_normal() { return ($include & BUILD_ALL) == BUILD_ALL; }
  134. sub build_sourceonly() { return $include == BUILD_SOURCE; }
  135. sub build_binaryonly() { return !($include & BUILD_SOURCE); }
  136. sub build_binaryindep() { return ($include == BUILD_ARCH_INDEP); }
  137. sub build_opt {
  138. if ($include == BUILD_BINARY) {
  139. return '-b';
  140. } elsif ($include == BUILD_ARCH_DEP) {
  141. return '-B';
  142. } elsif ($include == BUILD_ARCH_INDEP) {
  143. return '-A';
  144. } elsif ($include == BUILD_SOURCE) {
  145. return '-S';
  146. } else {
  147. internerr("build_opt called with include=$include");
  148. }
  149. }
  150. while (@ARGV) {
  151. $_ = shift @ARGV;
  152. if (/^(--help|-\?)$/) {
  153. usage;
  154. exit 0;
  155. } elsif (/^--version$/) {
  156. showversion;
  157. exit 0;
  158. } elsif (/^--admindir$/) {
  159. $admindir = shift @ARGV;
  160. } elsif (/^--admindir=(.*)$/) {
  161. $admindir = $1;
  162. } elsif (/^--source-option=(.*)$/) {
  163. push @source_opts, $1;
  164. } elsif (/^--changes-option=(.*)$/) {
  165. push @changes_opts, $1;
  166. } elsif (/^-j(\d*)$/) {
  167. $parallel = $1 || '';
  168. } elsif (/^-r(.*)$/) {
  169. @rootcommand = split /\s+/, $1;
  170. } elsif (/^-p(.*)$/) {
  171. $signcommand = $1;
  172. } elsif (/^-k(.*)$/) {
  173. $signkey = $1;
  174. } elsif (/^-([dD])$/) {
  175. $checkbuilddep = ($1 eq 'D');
  176. } elsif (/^-s(gpg|pgp)$/) {
  177. # Deprecated option
  178. warning(_g('-s%s is deprecated; always using gpg style interface'), $1);
  179. } elsif (/^--force-sign$/) {
  180. $signforce = 1;
  181. } elsif (/^-us$/) {
  182. $signsource = 0;
  183. } elsif (/^-uc$/) {
  184. $signchanges = 0;
  185. } elsif (/^-ap$/) {
  186. $signpause = 1;
  187. } elsif (/^-a(.*)$/) {
  188. $targetarch = $1;
  189. } elsif (/^-P(.*)$/) {
  190. @build_profiles = split /,/, $1;
  191. } elsif (/^-s[iad]$/) {
  192. push @changes_opts, $_;
  193. } elsif (/^-(?:s[insAkurKUR]|[zZ].*|i.*|I.*)$/) {
  194. push @source_opts, $_; # passed to dpkg-source
  195. } elsif (/^-tc$/) {
  196. $cleansource = 1;
  197. } elsif (/^-t(.*)$/) {
  198. $targetgnusystem = $1; # Order DOES matter!
  199. } elsif (/^(--target|-T)$/) {
  200. $call_target = shift @ARGV;
  201. } elsif (/^(--target=|-T)(.+)$/) {
  202. $call_target = $2;
  203. } elsif (/^--as-root$/) {
  204. $call_target_as_root = 1;
  205. } elsif (/^-nc$/) {
  206. $noclean = 1;
  207. } elsif (/^-b$/) {
  208. usageerr(_g('cannot combine %s and %s'), build_opt(), $_)
  209. if build_sourceonly;
  210. $include = BUILD_BINARY;
  211. push @changes_opts, '-b';
  212. @checkbuilddep_opts = ();
  213. $buildtarget = 'build';
  214. $binarytarget = 'binary';
  215. } elsif (/^-B$/) {
  216. usageerr(_g('cannot combine %s and %s'), build_opt(), $_)
  217. if build_sourceonly;
  218. $include = BUILD_ARCH_DEP;
  219. push @changes_opts, '-B';
  220. @checkbuilddep_opts = qw(-B);
  221. $buildtarget = 'build-arch';
  222. $binarytarget = 'binary-arch';
  223. } elsif (/^-A$/) {
  224. usageerr(_g('cannot combine %s and %s'), build_opt(), $_)
  225. if build_sourceonly;
  226. $include = BUILD_ARCH_INDEP;
  227. push @changes_opts, '-A';
  228. @checkbuilddep_opts = qw(-A);
  229. $buildtarget = 'build-indep';
  230. $binarytarget = 'binary-indep';
  231. } elsif (/^-S$/) {
  232. usageerr(_g('cannot combine %s and %s'), build_opt(), $_)
  233. if build_binaryonly;
  234. $include = BUILD_SOURCE;
  235. push @changes_opts, '-S';
  236. @checkbuilddep_opts = qw(-A -B);
  237. } elsif (/^-F$/) {
  238. usageerr(_g('cannot combine %s and %s'), build_opt(), $_)
  239. if not build_normal;
  240. $include = BUILD_ALL;
  241. @checkbuilddep_opts = ();
  242. } elsif (/^-v(.*)$/) {
  243. $since = $1;
  244. } elsif (/^-m(.*)$/) {
  245. $maint = $1;
  246. } elsif (/^-e(.*)$/) {
  247. $changedby = $1;
  248. } elsif (/^-C(.*)$/) {
  249. $desc = $1;
  250. } elsif (m/^-[EW]$/) {
  251. # Deprecated option
  252. warning(_g('-E and -W are deprecated, they are without effect'));
  253. } elsif (/^-R(.*)$/) {
  254. @debian_rules = split /\s+/, $1;
  255. } else {
  256. usageerr(_g('unknown option or argument %s'), $_);
  257. }
  258. }
  259. if ($noclean) {
  260. # -nc without -b/-B/-A/-S/-F implies -b
  261. $include = BUILD_BINARY if ($include & BUILD_DEFAULT);
  262. }
  263. if ($< == 0) {
  264. warning(_g('using a gain-root-command while being root')) if (@rootcommand);
  265. } else {
  266. push @rootcommand, 'fakeroot' unless @rootcommand;
  267. if (!find_command($rootcommand[0])) {
  268. if ($rootcommand[0] eq 'fakeroot') {
  269. error(_g("fakeroot not found, either install the fakeroot\n" .
  270. 'package, specify a command with the -r option, ' .
  271. 'or run this as root'));
  272. } else {
  273. error(_g("gain-root-commmand '%s' not found"), $rootcommand[0]);
  274. }
  275. }
  276. }
  277. my $build_opts = Dpkg::BuildOptions->new();
  278. if (defined $parallel) {
  279. $parallel = $build_opts->get('parallel') if $build_opts->has('parallel');
  280. $ENV{MAKEFLAGS} ||= '';
  281. $ENV{MAKEFLAGS} .= " -j$parallel";
  282. $build_opts->set('parallel', $parallel);
  283. $build_opts->export();
  284. }
  285. set_build_profiles(@build_profiles) if @build_profiles;
  286. my $cwd = cwd();
  287. my $dir = basename($cwd);
  288. my $changelog = changelog_parse();
  289. my $pkg = mustsetvar($changelog->{source}, _g('source package'));
  290. my $version = mustsetvar($changelog->{version}, _g('source version'));
  291. my ($ok, $error) = version_check($version);
  292. error($error) unless $ok;
  293. my $distribution = mustsetvar($changelog->{distribution}, _g('source distribution'));
  294. my $maintainer;
  295. if ($changedby) {
  296. $maintainer = $changedby;
  297. } elsif ($maint) {
  298. $maintainer = $maint;
  299. } else {
  300. $maintainer = mustsetvar($changelog->{maintainer}, _g('source changed by'));
  301. }
  302. open my $arch_env, '-|', 'dpkg-architecture', "-a$targetarch",
  303. "-t$targetgnusystem", '-f' or subprocerr('dpkg-architecture');
  304. while ($_ = <$arch_env>) {
  305. chomp;
  306. my ($key, $value) = split /=/, $_, 2;
  307. $ENV{$key} = $value;
  308. }
  309. close $arch_env or subprocerr('dpkg-architecture');
  310. my $arch;
  311. if (build_sourceonly) {
  312. $arch = 'source';
  313. } elsif (build_binaryindep) {
  314. $arch = 'all';
  315. } else {
  316. $arch = mustsetvar($ENV{DEB_HOST_ARCH}, _g('host architecture'));
  317. }
  318. if (!defined $signcommand &&
  319. (($ENV{GNUPGHOME} && -e $ENV{GNUPGHOME}) ||
  320. ($ENV{HOME} && -e "$ENV{HOME}/.gnupg"))) {
  321. if (find_command('gpg2')) {
  322. $signcommand = 'gpg2';
  323. } elsif (find_command('gpg')) {
  324. $signcommand = 'gpg';
  325. }
  326. }
  327. if (not $signcommand) {
  328. $signsource = 0;
  329. $signchanges = 0;
  330. } elsif ($signforce) {
  331. $signsource = 1;
  332. $signchanges = 1;
  333. } elsif (($signsource or $signchanges) and $distribution eq 'UNRELEASED') {
  334. $signreleased = 0;
  335. $signsource = 0;
  336. $signchanges = 0;
  337. }
  338. # Preparation of environment stops here
  339. (my $sversion = $version) =~ s/^\d+://;
  340. my $pv = "${pkg}_$sversion";
  341. my $pva = "${pkg}_${sversion}_$arch";
  342. if (not -x 'debian/rules') {
  343. warning(_g('debian/rules is not executable; fixing that'));
  344. chmod(0755, 'debian/rules'); # No checks of failures, non fatal
  345. }
  346. unless ($call_target) {
  347. chdir('..') or syserr('chdir ..');
  348. withecho('dpkg-source', @source_opts, '--before-build', $dir);
  349. chdir($dir) or syserr("chdir $dir");
  350. }
  351. if ($checkbuilddep) {
  352. if ($admindir) {
  353. push @checkbuilddep_opts, "--admindir=$admindir";
  354. }
  355. system('dpkg-checkbuilddeps', @checkbuilddep_opts);
  356. if (not WIFEXITED($?)) {
  357. subprocerr('dpkg-checkbuilddeps');
  358. } elsif (WEXITSTATUS($?)) {
  359. warning(_g('build dependencies/conflicts unsatisfied; aborting'));
  360. warning(_g('(Use -d flag to override.)'));
  361. if (build_sourceonly) {
  362. warning(_g('this is currently a non-fatal warning with -S, but ' .
  363. 'will probably become fatal in the future'));
  364. } else {
  365. exit 3;
  366. }
  367. }
  368. }
  369. if ($call_target) {
  370. if ($call_target_as_root or
  371. $call_target =~ /^(clean|binary(|-arch|-indep))$/)
  372. {
  373. withecho(@rootcommand, @debian_rules, $call_target);
  374. } else {
  375. withecho(@debian_rules, $call_target);
  376. }
  377. exit 0;
  378. }
  379. unless ($noclean) {
  380. withecho(@rootcommand, @debian_rules, 'clean');
  381. }
  382. if ($include & BUILD_SOURCE) {
  383. warning(_g('building a source package without cleaning up as you asked; ' .
  384. 'it might contain undesired files')) if $noclean;
  385. chdir('..') or syserr('chdir ..');
  386. withecho('dpkg-source', @source_opts, '-b', $dir);
  387. chdir($dir) or syserr("chdir $dir");
  388. }
  389. if ($buildtarget ne 'build' and scalar(@debian_rules) == 1) {
  390. # Verify that build-{arch,indep} are supported. If not, fallback to build.
  391. # This is a temporary measure to not break too many packages on a flag day.
  392. my $pid = spawn(exec => [ 'make', '-f', @debian_rules, '-qn', $buildtarget ],
  393. from_file => '/dev/null', to_file => '/dev/null',
  394. error_to_file => '/dev/null');
  395. my $cmdline = "make -f @debian_rules -qn $buildtarget";
  396. wait_child($pid, nocheck => 1, cmdline => $cmdline);
  397. my $exitcode = WEXITSTATUS($?);
  398. subprocerr($cmdline) unless WIFEXITED($?);
  399. if ($exitcode == 2) {
  400. warning(_g("%s must be updated to support the 'build-arch' and " .
  401. "'build-indep' targets (at least '%s' seems to be " .
  402. 'missing)'), "@debian_rules", $buildtarget);
  403. $buildtarget = 'build';
  404. }
  405. }
  406. if ($include & BUILD_BINARY) {
  407. withecho(@debian_rules, $buildtarget);
  408. withecho(@rootcommand, @debian_rules, $binarytarget);
  409. }
  410. if ($signpause &&
  411. ($signchanges || (($include & BUILD_SOURCE) && $signsource))) {
  412. print _g("Press the return key to start signing process\n");
  413. getc();
  414. }
  415. my $signerrors;
  416. if ($include & BUILD_SOURCE) {
  417. if ($signsource && signfile("$pv.dsc")) {
  418. $signerrors = _g('failed to sign .dsc and .changes file');
  419. $signchanges = 0;
  420. }
  421. }
  422. if (defined($maint)) { push @changes_opts, "-m$maint" }
  423. if (defined($changedby)) { push @changes_opts, "-e$changedby" }
  424. if (defined($since)) { push @changes_opts, "-v$since" }
  425. if (defined($desc)) { push @changes_opts, "-C$desc" }
  426. my $chg = "../$pva.changes";
  427. print { *STDERR } " dpkg-genchanges @changes_opts >$chg\n";
  428. open my $changes_fh, '-|', 'dpkg-genchanges', @changes_opts
  429. or subprocerr('dpkg-genchanges');
  430. open my $out_fh, '>', $chg or syserr(_g('write changes file'));
  431. my $infiles = my $files = '';
  432. while ($_ = <$changes_fh>) {
  433. print { $out_fh } $_ or syserr(_g('write changes file'));
  434. chomp;
  435. if (/^Files:/i) {
  436. $infiles = 1;
  437. } elsif ($infiles && /^\s+(.*)$/) {
  438. $files .= " $1 ";
  439. } elsif ($infiles && /^\S/) {
  440. $infiles = 0;
  441. }
  442. }
  443. close $changes_fh or subprocerr(_g('dpkg-genchanges'));
  444. close $out_fh or syserr(_g('write changes file'));
  445. my $srcmsg;
  446. sub fileomitted($) { return $files !~ /$_[0]/ }
  447. my $ext = compression_get_file_extension_regex();
  448. if (fileomitted '\.deb') {
  449. # source only upload
  450. if (fileomitted "\.diff\.$ext" and fileomitted "\.debian\.tar\.$ext") {
  451. $srcmsg = _g('source only upload: Debian-native package');
  452. } elsif (fileomitted "\.orig\.tar\.$ext") {
  453. $srcmsg = _g('source only, diff-only upload (original source NOT included)');
  454. } else {
  455. $srcmsg = _g('source only upload (original source is included)');
  456. }
  457. } else {
  458. $srcmsg = _g('full upload (original source is included)');
  459. if (fileomitted '\.dsc') {
  460. $srcmsg = _g('binary only upload (no source included)');
  461. } elsif (fileomitted "\.diff\.$ext" and fileomitted "\.debian\.tar\.$ext") {
  462. $srcmsg = _g('full upload; Debian-native package (full source is included)');
  463. } elsif (fileomitted "\.orig\.tar\.$ext") {
  464. $srcmsg = _g('binary and diff upload (original source NOT included)');
  465. } else {
  466. $srcmsg = _g('full upload (original source is included)');
  467. }
  468. }
  469. if ($signchanges && signfile("$pva.changes")) {
  470. $signerrors = _g('failed to sign .changes file');
  471. }
  472. if ($cleansource) {
  473. withecho(@rootcommand, @debian_rules, 'clean');
  474. }
  475. chdir('..') or syserr('chdir ..');
  476. withecho('dpkg-source', @source_opts, '--after-build', $dir);
  477. chdir($dir) or syserr("chdir $dir");
  478. if (not $signreleased) {
  479. warning(_g('not signing UNRELEASED build; use --force-sign to override'));
  480. }
  481. print "$Dpkg::PROGNAME: $srcmsg\n";
  482. if ($signerrors) {
  483. warning($signerrors);
  484. exit 1;
  485. }
  486. sub mustsetvar {
  487. my ($var, $text) = @_;
  488. error(_g('unable to determine %s'), $text)
  489. unless defined($var);
  490. print "$Dpkg::PROGNAME: $text $var\n";
  491. return $var;
  492. }
  493. sub withecho {
  494. print { *STDERR } " @_\n";
  495. system(@_)
  496. and subprocerr("@_");
  497. }
  498. sub signfile {
  499. my ($file) = @_;
  500. print { *STDERR } " signfile $file\n";
  501. my $qfile = quotemeta($file);
  502. system("(cat ../$qfile ; echo '') | " .
  503. "$signcommand --utf8-strings --local-user " .
  504. quotemeta($signkey || $maintainer) .
  505. " --clearsign --armor --textmode > ../$qfile.asc");
  506. my $status = $?;
  507. unless ($status) {
  508. system('mv', '--', "../$file.asc", "../$file")
  509. and subprocerr('mv');
  510. } else {
  511. system('rm', '-f', "../$file.asc")
  512. and subprocerr('rm -f');
  513. }
  514. print "\n";
  515. return $status
  516. }