dpkg-buildpackage.pl 17 KB

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