dpkg-buildpackage.pl 16 KB

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