dpkg-buildpackage.pl 16 KB

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