dpkg-buildpackage.pl 17 KB

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