dpkg-buildpackage.pl 16 KB

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