dpkg-buildpackage.pl 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562
  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, $usepause, $noclean,
  110. $cleansource, $since, $maint,
  111. $changedby, $desc, $parallel);
  112. my $checkbuilddep = 1;
  113. my $signkey = defined $ENV{DEB_SIGN_KEYID} ? $ENV{DEB_SIGN_KEYID} : undef;
  114. my $signforce = 0;
  115. my $signreleased = 1;
  116. my $signsource = 1;
  117. my $signchanges = 1;
  118. my $buildtarget = 'build';
  119. my $binarytarget = 'binary';
  120. my $targetarch = my $targetgnusystem = '';
  121. my @build_profiles = ();
  122. my $call_target = '';
  123. my $call_target_as_root = 0;
  124. my (@checkbuilddep_opts, @changes_opts, @source_opts);
  125. use constant BUILD_DEFAULT => 1;
  126. use constant BUILD_SOURCE => 2;
  127. use constant BUILD_ARCH_DEP => 4;
  128. use constant BUILD_ARCH_INDEP => 8;
  129. use constant BUILD_BINARY => BUILD_ARCH_DEP | BUILD_ARCH_INDEP;
  130. use constant BUILD_ALL => BUILD_BINARY | BUILD_SOURCE;
  131. my $include = BUILD_ALL | BUILD_DEFAULT;
  132. sub build_normal() { return ($include & BUILD_ALL) == BUILD_ALL; }
  133. sub build_sourceonly() { return $include == BUILD_SOURCE; }
  134. sub build_binaryonly() { return !($include & BUILD_SOURCE); }
  135. sub build_binaryindep() { return ($include == BUILD_ARCH_INDEP); }
  136. sub build_opt() {
  137. return (($include == BUILD_BINARY) ? '-b' :
  138. (($include == BUILD_ARCH_DEP) ? '-B' :
  139. (($include == BUILD_ARCH_INDEP) ? '-A' :
  140. (($include == BUILD_SOURCE) ? '-S' :
  141. internerr("build_opt called with include=$include")))));
  142. }
  143. while (@ARGV) {
  144. $_ = shift @ARGV;
  145. if (/^(--help|-\?)$/) {
  146. usage;
  147. exit 0;
  148. } elsif (/^--version$/) {
  149. showversion;
  150. exit 0;
  151. } elsif (/^--admindir$/) {
  152. $admindir = shift @ARGV;
  153. } elsif (/^--admindir=(.*)$/) {
  154. $admindir = $1;
  155. } elsif (/^--source-option=(.*)$/) {
  156. push @source_opts, $1;
  157. } elsif (/^--changes-option=(.*)$/) {
  158. push @changes_opts, $1;
  159. } elsif (/^-j(\d*)$/) {
  160. $parallel = $1 || '';
  161. } elsif (/^-r(.*)$/) {
  162. @rootcommand = split /\s+/, $1;
  163. } elsif (/^-p(.*)$/) {
  164. $signcommand = $1;
  165. } elsif (/^-k(.*)$/) {
  166. $signkey = $1;
  167. } elsif (/^-([dD])$/) {
  168. $checkbuilddep = ($1 eq 'D');
  169. } elsif (/^-s(gpg|pgp)$/) {
  170. # Deprecated option
  171. warning(_g('-s%s is deprecated; always using gpg style interface'), $1);
  172. } elsif (/^--force-sign$/) {
  173. $signforce = 1;
  174. } elsif (/^-us$/) {
  175. $signsource = 0;
  176. } elsif (/^-uc$/) {
  177. $signchanges = 0;
  178. } elsif (/^-ap$/) {
  179. $usepause = 1;
  180. } elsif (/^-a(.*)$/) {
  181. $targetarch = $1;
  182. } elsif (/^-P(.*)$/) {
  183. @build_profiles = split /,/, $1;
  184. } elsif (/^-s[iad]$/) {
  185. push @changes_opts, $_;
  186. } elsif (/^-(?:s[insAkurKUR]|[zZ].*|i.*|I.*)$/) {
  187. push @source_opts, $_; # passed to dpkg-source
  188. } elsif (/^-tc$/) {
  189. $cleansource = 1;
  190. } elsif (/^-t(.*)$/) {
  191. $targetgnusystem = $1; # Order DOES matter!
  192. } elsif (/^(--target|-T)$/) {
  193. $call_target = shift @ARGV;
  194. } elsif (/^(--target=|-T)(.+)$/) {
  195. $call_target = $2;
  196. } elsif (/^--as-root$/) {
  197. $call_target_as_root = 1;
  198. } elsif (/^-nc$/) {
  199. $noclean = 1;
  200. } elsif (/^-b$/) {
  201. usageerr(_g('cannot combine %s and %s'), $_, '-S') if build_sourceonly;
  202. $include = BUILD_BINARY;
  203. push @changes_opts, '-b';
  204. @checkbuilddep_opts = ();
  205. $buildtarget = 'build';
  206. $binarytarget = 'binary';
  207. } elsif (/^-B$/) {
  208. usageerr(_g('cannot combine %s and %s'), $_, '-S') if build_sourceonly;
  209. $include = BUILD_ARCH_DEP;
  210. push @changes_opts, '-B';
  211. @checkbuilddep_opts = qw(-B);
  212. $buildtarget = 'build-arch';
  213. $binarytarget = 'binary-arch';
  214. } elsif (/^-A$/) {
  215. usageerr(_g('cannot combine %s and %s'), $_, '-S') if build_sourceonly;
  216. $include = BUILD_ARCH_INDEP;
  217. push @changes_opts, '-A';
  218. @checkbuilddep_opts = qw(-A);
  219. $buildtarget = 'build-indep';
  220. $binarytarget = 'binary-indep';
  221. } elsif (/^-S$/) {
  222. usageerr(_g('cannot combine %s and %s'), build_opt, '-S')
  223. if build_binaryonly;
  224. $include = BUILD_SOURCE;
  225. push @changes_opts, '-S';
  226. @checkbuilddep_opts = qw(-A -B);
  227. } elsif (/^-F$/) {
  228. usageerr(_g('cannot combine %s and %s'), $_, build_opt)
  229. if not build_normal;
  230. $include = BUILD_ALL;
  231. @checkbuilddep_opts = ();
  232. } elsif (/^-v(.*)$/) {
  233. $since = $1;
  234. } elsif (/^-m(.*)$/) {
  235. $maint = $1;
  236. } elsif (/^-e(.*)$/) {
  237. $changedby = $1;
  238. } elsif (/^-C(.*)$/) {
  239. $desc = $1;
  240. } elsif (m/^-[EW]$/) {
  241. # Deprecated option
  242. warning(_g('-E and -W are deprecated, they are without effect'));
  243. } elsif (/^-R(.*)$/) {
  244. @debian_rules = split /\s+/, $1;
  245. } else {
  246. usageerr(_g('unknown option or argument %s'), $_);
  247. }
  248. }
  249. if ($noclean) {
  250. # -nc without -b/-B/-A/-S/-F implies -b
  251. $include = BUILD_BINARY if ($include & BUILD_DEFAULT);
  252. }
  253. if ($< == 0) {
  254. warning(_g('using a gain-root-command while being root')) if (@rootcommand);
  255. } else {
  256. push @rootcommand, 'fakeroot' unless @rootcommand;
  257. if (!find_command($rootcommand[0])) {
  258. if ($rootcommand[0] eq 'fakeroot') {
  259. error(_g("fakeroot not found, either install the fakeroot\n" .
  260. 'package, specify a command with the -r option, ' .
  261. 'or run this as root'));
  262. } else {
  263. error(_g("gain-root-commmand '%s' not found"), $rootcommand[0]);
  264. }
  265. }
  266. }
  267. my $build_opts = Dpkg::BuildOptions->new();
  268. if (defined $parallel) {
  269. $parallel = $build_opts->get('parallel') if $build_opts->has('parallel');
  270. $ENV{MAKEFLAGS} ||= '';
  271. $ENV{MAKEFLAGS} .= " -j$parallel";
  272. $build_opts->set('parallel', $parallel);
  273. $build_opts->export();
  274. }
  275. set_build_profiles(@build_profiles) if @build_profiles;
  276. my $cwd = cwd();
  277. my $dir = basename($cwd);
  278. my $changelog = changelog_parse();
  279. my $pkg = mustsetvar($changelog->{source}, _g('source package'));
  280. my $version = mustsetvar($changelog->{version}, _g('source version'));
  281. my ($ok, $error) = version_check($version);
  282. error($error) unless $ok;
  283. my $distribution = mustsetvar($changelog->{distribution}, _g('source distribution'));
  284. my $maintainer;
  285. if ($changedby) {
  286. $maintainer = $changedby;
  287. } elsif ($maint) {
  288. $maintainer = $maint;
  289. } else {
  290. $maintainer = mustsetvar($changelog->{maintainer}, _g('source changed by'));
  291. }
  292. open my $arch_env, '-|', 'dpkg-architecture', "-a$targetarch",
  293. "-t$targetgnusystem", '-f' or subprocerr('dpkg-architecture');
  294. while ($_ = <$arch_env>) {
  295. chomp;
  296. my ($key, $value) = split /=/, $_, 2;
  297. $ENV{$key} = $value;
  298. }
  299. close $arch_env or subprocerr('dpkg-architecture');
  300. my $arch;
  301. if (build_sourceonly) {
  302. $arch = 'source';
  303. } elsif (build_binaryindep) {
  304. $arch = 'all';
  305. } else {
  306. $arch = mustsetvar($ENV{DEB_HOST_ARCH}, _g('host architecture'));
  307. }
  308. if (!defined $signcommand &&
  309. (($ENV{GNUPGHOME} && -e $ENV{GNUPGHOME}) ||
  310. ($ENV{HOME} && -e "$ENV{HOME}/.gnupg"))) {
  311. if (find_command('gpg2')) {
  312. $signcommand = 'gpg2';
  313. } elsif (find_command('gpg')) {
  314. $signcommand = 'gpg';
  315. }
  316. }
  317. if (not $signcommand) {
  318. $signsource = 0;
  319. $signchanges = 0;
  320. } elsif ($signforce) {
  321. $signsource = 1;
  322. $signchanges = 1;
  323. } elsif (($signsource or $signchanges) and $distribution eq 'UNRELEASED') {
  324. $signreleased = 0;
  325. $signsource = 0;
  326. $signchanges = 0;
  327. }
  328. # Preparation of environment stops here
  329. (my $sversion = $version) =~ s/^\d+://;
  330. my $pv = "${pkg}_$sversion";
  331. my $pva = "${pkg}_${sversion}_$arch";
  332. if (not -x 'debian/rules') {
  333. warning(_g('debian/rules is not executable; fixing that'));
  334. chmod(0755, 'debian/rules'); # No checks of failures, non fatal
  335. }
  336. unless ($call_target) {
  337. chdir('..') or syserr('chdir ..');
  338. withecho('dpkg-source', @source_opts, '--before-build', $dir);
  339. chdir($dir) or syserr("chdir $dir");
  340. }
  341. if ($checkbuilddep) {
  342. if ($admindir) {
  343. push @checkbuilddep_opts, "--admindir=$admindir";
  344. }
  345. system('dpkg-checkbuilddeps', @checkbuilddep_opts);
  346. if (not WIFEXITED($?)) {
  347. subprocerr('dpkg-checkbuilddeps');
  348. } elsif (WEXITSTATUS($?)) {
  349. warning(_g('build dependencies/conflicts unsatisfied; aborting'));
  350. warning(_g('(Use -d flag to override.)'));
  351. if (build_sourceonly) {
  352. warning(_g('this is currently a non-fatal warning with -S, but ' .
  353. 'will probably become fatal in the future'));
  354. } else {
  355. exit 3;
  356. }
  357. }
  358. }
  359. if ($call_target) {
  360. if ($call_target_as_root or
  361. $call_target =~ /^(clean|binary(|-arch|-indep))$/)
  362. {
  363. withecho(@rootcommand, @debian_rules, $call_target);
  364. } else {
  365. withecho(@debian_rules, $call_target);
  366. }
  367. exit 0;
  368. }
  369. unless ($noclean) {
  370. withecho(@rootcommand, @debian_rules, 'clean');
  371. }
  372. unless (build_binaryonly) {
  373. warning(_g('building a source package without cleaning up as you asked; ' .
  374. 'it might contain undesired files')) if $noclean;
  375. chdir('..') or syserr('chdir ..');
  376. withecho('dpkg-source', @source_opts, '-b', $dir);
  377. chdir($dir) or syserr("chdir $dir");
  378. }
  379. if ($buildtarget ne 'build' and scalar(@debian_rules) == 1) {
  380. # Verify that build-{arch,indep} are supported. If not, fallback to build.
  381. # This is a temporary measure to not break too many packages on a flag day.
  382. my $pid = spawn(exec => [ 'make', '-f', @debian_rules, '-qn', $buildtarget ],
  383. from_file => '/dev/null', to_file => '/dev/null',
  384. error_to_file => '/dev/null');
  385. my $cmdline = "make -f @debian_rules -qn $buildtarget";
  386. wait_child($pid, nocheck => 1, cmdline => $cmdline);
  387. my $exitcode = WEXITSTATUS($?);
  388. subprocerr($cmdline) unless WIFEXITED($?);
  389. if ($exitcode == 2) {
  390. warning(_g("%s must be updated to support the 'build-arch' and " .
  391. "'build-indep' targets (at least '%s' seems to be " .
  392. 'missing)'), "@debian_rules", $buildtarget);
  393. $buildtarget = 'build';
  394. }
  395. }
  396. unless (build_sourceonly) {
  397. withecho(@debian_rules, $buildtarget);
  398. withecho(@rootcommand, @debian_rules, $binarytarget);
  399. }
  400. if ($usepause &&
  401. ($signchanges || (!build_binaryonly && $signsource))) {
  402. print _g("Press the return key to start signing process\n");
  403. getc();
  404. }
  405. my $signerrors;
  406. unless (build_binaryonly) {
  407. if ($signsource && signfile("$pv.dsc")) {
  408. $signerrors = _g('failed to sign .dsc and .changes file');
  409. $signchanges = 0;
  410. }
  411. }
  412. if (defined($maint)) { push @changes_opts, "-m$maint" }
  413. if (defined($changedby)) { push @changes_opts, "-e$changedby" }
  414. if (defined($since)) { push @changes_opts, "-v$since" }
  415. if (defined($desc)) { push @changes_opts, "-C$desc" }
  416. my $chg = "../$pva.changes";
  417. print { *STDERR } " dpkg-genchanges @changes_opts >$chg\n";
  418. open my $changes_fh, '-|', 'dpkg-genchanges', @changes_opts
  419. or subprocerr('dpkg-genchanges');
  420. open my $out_fh, '>', $chg or syserr(_g('write changes file'));
  421. my $infiles = my $files = '';
  422. while ($_ = <$changes_fh>) {
  423. print { $out_fh } $_ or syserr(_g('write changes file'));
  424. chomp;
  425. if (/^Files:/i) {
  426. $infiles = 1;
  427. } elsif ($infiles && /^\s+(.*)$/) {
  428. $files .= " $1 ";
  429. } elsif ($infiles && /^\S/) {
  430. $infiles = 0;
  431. }
  432. }
  433. close $changes_fh or subprocerr(_g('dpkg-genchanges'));
  434. close $out_fh or syserr(_g('write changes file'));
  435. my $srcmsg;
  436. sub fileomitted($) { return $files !~ /$_[0]/ }
  437. my $ext = compression_get_file_extension_regex();
  438. if (fileomitted '\.deb') {
  439. # source only upload
  440. if (fileomitted "\.diff\.$ext" and fileomitted "\.debian\.tar\.$ext") {
  441. $srcmsg = _g('source only upload: Debian-native package');
  442. } elsif (fileomitted "\.orig\.tar\.$ext") {
  443. $srcmsg = _g('source only, diff-only upload (original source NOT included)');
  444. } else {
  445. $srcmsg = _g('source only upload (original source is included)');
  446. }
  447. } else {
  448. $srcmsg = _g('full upload (original source is included)');
  449. if (fileomitted '\.dsc') {
  450. $srcmsg = _g('binary only upload (no source included)');
  451. } elsif (fileomitted "\.diff\.$ext" and fileomitted "\.debian\.tar\.$ext") {
  452. $srcmsg = _g('full upload; Debian-native package (full source is included)');
  453. } elsif (fileomitted "\.orig\.tar\.$ext") {
  454. $srcmsg = _g('binary and diff upload (original source NOT included)');
  455. } else {
  456. $srcmsg = _g('full upload (original source is included)');
  457. }
  458. }
  459. if ($signchanges && signfile("$pva.changes")) {
  460. $signerrors = _g('failed to sign .changes file');
  461. }
  462. if ($cleansource) {
  463. withecho(@rootcommand, @debian_rules, 'clean');
  464. }
  465. chdir('..') or syserr('chdir ..');
  466. withecho('dpkg-source', @source_opts, '--after-build', $dir);
  467. chdir($dir) or syserr("chdir $dir");
  468. if (not $signreleased) {
  469. warning(_g('not signing UNRELEASED build; use --force-sign to override'));
  470. }
  471. print "$Dpkg::PROGNAME: $srcmsg\n";
  472. if ($signerrors) {
  473. warning($signerrors);
  474. exit 1;
  475. }
  476. sub mustsetvar {
  477. my ($var, $text) = @_;
  478. error(_g('unable to determine %s'), $text)
  479. unless defined($var);
  480. print "$Dpkg::PROGNAME: $text $var\n";
  481. return $var;
  482. }
  483. sub withecho {
  484. print { *STDERR } " @_\n";
  485. system(@_)
  486. and subprocerr("@_");
  487. }
  488. sub signfile {
  489. my ($file) = @_;
  490. print { *STDERR } " signfile $file\n";
  491. my $qfile = quotemeta($file);
  492. system("(cat ../$qfile ; echo '') | " .
  493. "$signcommand --utf8-strings --local-user " .
  494. quotemeta($signkey || $maintainer) .
  495. " --clearsign --armor --textmode > ../$qfile.asc");
  496. my $status = $?;
  497. unless ($status) {
  498. system('mv', '--', "../$file.asc", "../$file")
  499. and subprocerr('mv');
  500. } else {
  501. system('rm', '-f', "../$file.asc")
  502. and subprocerr('rm -f');
  503. }
  504. print "\n";
  505. return $status
  506. }