dpkg-buildpackage.pl 17 KB

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