dpkg-buildpackage.pl 17 KB

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