dpkg-buildpackage.pl 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4. use Cwd;
  5. use File::Basename;
  6. use Dpkg;
  7. use Dpkg::Gettext;
  8. use Dpkg::ErrorHandling qw(warning error failure syserr subprocerr usageerr
  9. $warnable_error);
  10. use Dpkg::BuildOptions;
  11. push (@INC, $dpkglibdir);
  12. require 'controllib.pl';
  13. textdomain("dpkg-dev");
  14. sub showversion {
  15. printf _g("Debian %s version %s.\n"), $progname, $version;
  16. print _g("
  17. Copyright (C) 1996 Ian Jackson.
  18. Copyright (C) 2000 Wichert Akkerman
  19. Copyright (C) 2007 Frank Lichtenheld");
  20. print _g("
  21. This is free software; see the GNU General Public Licence version 2 or
  22. later for copying conditions. There is NO warranty.
  23. ");
  24. }
  25. sub usage {
  26. printf _g("
  27. Usage: %s [<options> ...]
  28. Options:
  29. -r<gain-root-command>
  30. command to gain root privileges (default is fakeroot).
  31. -p<sign-command>
  32. -d do not check build dependencies and conflicts.
  33. -D check build dependencies and conflicts.
  34. -j[<number>] specify jobs to run simultaneously } passed to debian/rules
  35. -k<keyid> the key to use for signing.
  36. -sgpg the sign-command is called like GPG.
  37. -spgp the sign-command is called like PGP.
  38. -us unsigned source.
  39. -uc unsigned changes.
  40. -a<arch> Debian architecture we build for (implies -d).
  41. -b binary-only, do not build source. } also passed to
  42. -B binary-only, no arch-indep files. } dpkg-genchanges
  43. -S source only, no binary files. }
  44. -t<system> set GNU system type. } passed to dpkg-architecture
  45. -v<version> changes since version <version>. }
  46. -m<maint> maintainer for package is <maint>. }
  47. -e<maint> maintainer for release is <maint>. } only passed
  48. -C<descfile> changes are described in <descfile>. } to dpkg-genchanges
  49. -si (default) src includes orig for rev. 0 or 1. }
  50. -sa uploaded src always includes orig. }
  51. -sd uploaded src is diff and .dsc only. }
  52. -sn force Debian native source format. }
  53. -s[sAkurKUR] see dpkg-source for explanation. } only passed
  54. -z<level> compression level of source } to dpkg-source
  55. -Z(gz|bz2|lzma) compression to use for source }
  56. -nc do not clean source tree (implies -b).
  57. -tc clean source tree when finished.
  58. -ap add pause before starting signature process.
  59. -E turn certain warnings into errors. } passed to
  60. -W when -E is turned on, -W turns it off. } dpkg-source
  61. -i[<regex>] ignore diffs of files matching regex. } only passed
  62. -I[<pattern>] filter out files when building tarballs. } to dpkg-source
  63. --admindir=<directory>
  64. change the administrative directory.
  65. -h, --help show this help message.
  66. --version show the version.
  67. "), $progname;
  68. }
  69. sub testcommand {
  70. my ($cmd) = @_;
  71. my $fullcmd = `which $cmd`;
  72. chomp $fullcmd;
  73. return $fullcmd && -x $fullcmd;
  74. }
  75. my $rootcommand = '';
  76. my $signcommand = '';
  77. if ( ( ($ENV{GNUPGHOME} && -e $ENV{GNUPGHOME})
  78. || ($ENV{HOME} && -e "$ENV{HOME}/.gnupg") )
  79. && testcommand('gpg')) {
  80. $signcommand = 'gpg';
  81. } elsif (testcommand('pgp')) {
  82. $signcommand = 'pgp'
  83. }
  84. my ($admindir, $signkey, $forcesigninterface, $usepause, $noclean,
  85. $sourcestyle, $cleansource,
  86. $binaryonly, $sourceonly, $since, $maint,
  87. $changedby, $desc, $parallel);
  88. my (@checkbuilddep_args, @passopts, @tarignore);
  89. my $checkbuilddep = 1;
  90. my $signsource = 1;
  91. my $signchanges = 1;
  92. my $diffignore = '';
  93. my $binarytarget = 'binary';
  94. my $targetarch = my $targetgnusystem = '';
  95. while (@ARGV) {
  96. $_ = shift @ARGV;
  97. if (/^(--help|-h)$/) {
  98. usage;
  99. exit 0;
  100. } elsif (/^--version$/) {
  101. showversion;
  102. exit 0;
  103. } elsif (/^--admindir=(.*)$/) {
  104. $admindir = $1;
  105. } elsif (/^-j(\d*)$/) {
  106. $parallel = $1 || '-1';
  107. } elsif (/^-r(.*)$/) {
  108. $rootcommand = $1;
  109. } elsif (/^-p(.*)$/) {
  110. $signcommand = $1;
  111. } elsif (/^-k(.*)$/) {
  112. $signkey = $1;
  113. } elsif (/^-([dD])$/) {
  114. $checkbuilddep = ($1 eq 'D');
  115. } elsif (/^-s(gpg|pgp)$/) {
  116. $forcesigninterface = $1;
  117. } elsif (/^-us$/) {
  118. $signsource = 0;
  119. } elsif (/^-uc$/) {
  120. $signchanges = 0;
  121. } elsif (/^-ap$/) {
  122. $usepause = 1;
  123. } elsif (/^-a(.*)$/) {
  124. $targetarch = $1;
  125. $checkbuilddep = 0;
  126. } elsif (/^-s[iad]$/) {
  127. $sourcestyle = $_;
  128. } elsif (/^-s[nsAkurKUR]$/) {
  129. push @passopts, $_; # passed to dpkg-source
  130. } elsif (/^-[zZ]/) {
  131. push @passopts, $_; # passed to dpkg-source
  132. } elsif (/^-i.*$/) {
  133. $diffignore = $_;
  134. } elsif (/^-I.*$/) {
  135. push @tarignore, $_;
  136. } elsif (/^-tc$/) {
  137. $cleansource = 1;
  138. } elsif (/^-t(.*)$/) {
  139. $targetgnusystem = $1; # Order DOES matter!
  140. } elsif (/^-nc$/) {
  141. $noclean = 1;
  142. if ($sourceonly) {
  143. usageerr(sprintf(_g("cannot combine %s and %s"), '-nc', '-S'));
  144. }
  145. unless ($binaryonly) {
  146. $binaryonly = '-b';
  147. }
  148. } elsif (/^-b$/) {
  149. $binaryonly = '-b';
  150. @checkbuilddep_args = ();
  151. $binarytarget = 'binary';
  152. if ($sourceonly) {
  153. usageerr(sprintf(_g("cannot combine %s and %s"), '-b', '-S'));
  154. }
  155. } elsif (/^-B$/) {
  156. $binaryonly = '-B';
  157. @checkbuilddep_args = ('-B');
  158. $binarytarget = 'binary-arch';
  159. if ($sourceonly) {
  160. usageerr(sprintf(_g("cannot combine %s and %s"), '-B', '-S'));
  161. }
  162. } elsif (/^-S$/) {
  163. $sourceonly = '-S';
  164. $checkbuilddep = 0;
  165. if ($binaryonly) {
  166. usageerr(sprintf(_g("cannot combine %s and %s"), $binaryonly, '-S'));
  167. }
  168. } elsif (/^-v(.*)$/) {
  169. $since = $1;
  170. } elsif (/^-m(.*)$/) {
  171. $maint = $1;
  172. } elsif (/^-e(.*)$/) {
  173. $changedby = $1;
  174. } elsif (/^-C(.*)$/) {
  175. $desc = $1;
  176. } elsif (/^-W$/) {
  177. $warnable_error = 1;
  178. push @passopts, '-W';
  179. } elsif (/^-E$/) {
  180. $warnable_error = 0;
  181. push @passopts, '-E';
  182. } else {
  183. usageerr(sprintf(_g("unknown option or argument %s"), $_));
  184. }
  185. }
  186. if ($< == 0) {
  187. warning(_g("using a gain-root-command while being root")) if ($rootcommand);
  188. } else {
  189. $rootcommand ||= 'fakeroot';
  190. if (!testcommand($rootcommand)) {
  191. if ($rootcommand eq 'fakeroot') {
  192. error(_g("fakeroot not found, either install the fakeroot\n" .
  193. "package, specify a command with the -r option, " .
  194. "or run this as root"));
  195. } else {
  196. error(sprintf(_g("gain-root-commmand '%s' not found"), $rootcommand));
  197. }
  198. }
  199. }
  200. unless ($signcommand) {
  201. $signsource = 0;
  202. $signchanges = 0;
  203. }
  204. my $signinterface;
  205. if ($forcesigninterface) {
  206. $signinterface = $forcesigninterface;
  207. } else {
  208. $signinterface = $signcommand;
  209. }
  210. if ($signcommand && ($signinterface !~ /^(gpg|pgp)$/)) {
  211. warning(_g("unknown sign command, assuming pgp style interface"));
  212. }
  213. if ($parallel || $ENV{DEB_BUILD_OPTIONS}) {
  214. my $build_opts = Dpkg::BuildOptions::parse();
  215. $parallel ||= $build_opts->{parallel};
  216. if (defined $parallel) {
  217. $ENV{MAKEFLAGS} ||= '';
  218. if ($parallel eq '-1') {
  219. $ENV{MAKEFLAGS} .= " -j";
  220. } else {
  221. $ENV{MAKEFLAGS} .= " -j$parallel";
  222. }
  223. }
  224. $build_opts->{parallel} = $parallel;
  225. Dpkg::BuildOptions::set($build_opts);
  226. }
  227. my $cwd = cwd();
  228. my $dir = basename($cwd);
  229. my %changes;
  230. open CHANGELOG, '-|', 'dpkg-parsechangelog' or subprocerr('dpkg-parsechangelog');
  231. # until we have a better parsecdata function this
  232. # should suffice
  233. while ($_ = <CHANGELOG>) {
  234. chomp;
  235. /^(\S+):\s*(.*)$/ && do {
  236. $changes{lc $1} = $2;
  237. };
  238. }
  239. close CHANGELOG or subprocerr('dpkg-parsechangelog');
  240. sub mustsetvar {
  241. my ($var, $text) = @_;
  242. error(sprintf(_g("unable to determine %s", $text)))
  243. unless defined($var);
  244. print "$progname: $text $var\n";
  245. return $var;
  246. }
  247. my $pkg = mustsetvar($changes{source}, _g('source package'));
  248. my $version = mustsetvar($changes{version}, _g('source version'));
  249. checkversion($version);
  250. my $maintainer;
  251. if ($changedby) {
  252. $maintainer = $changedby;
  253. } elsif ($maint) {
  254. $maintainer = $maint;
  255. } else {
  256. $maintainer = mustsetvar($changes{maintainer}, _g('source changed by'));
  257. }
  258. open my $arch_env, '-|', 'dpkg-architecture', "-a$targetarch",
  259. "-t$targetgnusystem", '-s', '-f' or subprocerr('dpkg-architecture');
  260. while ($_ = <$arch_env>) {
  261. chomp;
  262. my @cmds = split /\s*;\s*/;
  263. foreach (@cmds) {
  264. /^\s*(\w+)=([\w-]*)\s*$/ && do {
  265. $ENV{$1} = $2;
  266. };
  267. }
  268. }
  269. close $arch_env or subprocerr('dpkg-architecture');
  270. my $arch;
  271. unless ($sourceonly) {
  272. $arch = mustsetvar($ENV{'DEB_HOST_ARCH'}, _g('host architecture'));
  273. } else {
  274. $arch = 'source';
  275. }
  276. (my $sversion = $version) =~ s/^\d+://;
  277. my $pv = "${pkg}_$sversion";
  278. my $pva = "${pkg}_${sversion}_$arch";
  279. sub signfile {
  280. my ($file) = @_;
  281. print STDERR " signfile $file\n";
  282. my $qfile = quotemeta($file);
  283. if ($signinterface eq 'gpg') {
  284. system("(cat ../$qfile ; echo '') | ".
  285. "$signcommand --utf8-strings --local-user "
  286. .quotemeta($signkey||$maintainer).
  287. " --clearsign --armor --textmode > ../$qfile.asc");
  288. } else {
  289. system("$signcommand -u ".quotemeta($signkey||$maintainer).
  290. " +clearsig=on -fast <../$qfile >../$qfile.asc");
  291. }
  292. my $status = $?;
  293. unless ($status) {
  294. system('mv', '--', "../$file.asc", "../$file")
  295. and subprocerr('mv');
  296. } else {
  297. system('rm', '-f', "../$file.asc")
  298. and subprocerr('rm -f');
  299. }
  300. print "\n";
  301. return $status
  302. }
  303. sub withecho {
  304. shift while !$_[0];
  305. print STDERR " @_\n";
  306. system(@_)
  307. and subprocerr("@_");
  308. }
  309. if ($checkbuilddep) {
  310. if ($admindir) {
  311. push @checkbuilddep_args, "--admindir=$admindir";
  312. }
  313. if (system('dpkg-checkbuilddeps', @checkbuilddep_args)) {
  314. warning(_g("Build dependencies/conflicts unsatisfied; aborting."));
  315. warning(_g("(Use -d flag to override.)"));
  316. exit 3;
  317. }
  318. }
  319. unless ($noclean) {
  320. withecho($rootcommand, 'debian/rules', 'clean');
  321. }
  322. unless ($binaryonly) {
  323. chdir('..') or failure('chdir ..');
  324. my @opts = @passopts;
  325. if ($diffignore) { push @opts, $diffignore }
  326. push @opts, @tarignore;
  327. withecho('dpkg-source', @opts, '-b', $dir);
  328. chdir($dir) or failure("chdir $dir");
  329. }
  330. unless ($sourceonly) {
  331. withecho('debian/rules', 'build');
  332. withecho($rootcommand, 'debian/rules', $binarytarget);
  333. }
  334. if ($usepause &&
  335. ($signchanges || ( !$binaryonly && $signsource )) ) {
  336. print _g("Press the return key to start signing process\n");
  337. getc();
  338. }
  339. my $signerrors;
  340. unless ($binaryonly) {
  341. if ($signsource && signfile("$pv.dsc")) {
  342. $signerrors = _g("Failed to sign .dsc and .changes file");
  343. $signchanges = 0;
  344. }
  345. }
  346. my @change_opts;
  347. if ($binaryonly) { push @change_opts, $binaryonly }
  348. if ($sourceonly) { push @change_opts, $sourceonly }
  349. if ($sourcestyle) { push @change_opts, $sourcestyle }
  350. if ($maint) { push @change_opts, "-m$maint" }
  351. if ($changedby) { push @change_opts, "-e$changedby" }
  352. if ($since) { push @change_opts, "-v$since" }
  353. if ($desc) { push @change_opts, "-C$desc" }
  354. my $chg = "../$pva.changes";
  355. print STDERR " dpkg-genchanges @change_opts >$chg\n";
  356. open CHANGES, '-|', 'dpkg-genchanges', @change_opts
  357. or subprocerr('dpkg-genchanges');
  358. open OUT, '>', $chg or syserr(_g('write changes file'));
  359. my $infiles = my $files = '';
  360. while ($_ = <CHANGES>) {
  361. print OUT $_ or syserr(_g('write changes file'));
  362. chomp;
  363. if (/^Files:/i) {
  364. $infiles = 1;
  365. } elsif ($infiles && /^\s+(.*)$/) {
  366. $files .= " $1 ";
  367. } elsif ($infiles && /^\S/) {
  368. $infiles = 0;
  369. }
  370. }
  371. close CHANGES or subprocerr(_g('dpkg-genchanges'));
  372. close OUT or syserr(_g('write changes file'));
  373. sub fileomitted {
  374. my ($regex) = @_;
  375. return $files !~ /$regex/;
  376. }
  377. my $srcmsg;
  378. if (fileomitted '\.deb') {
  379. # source only upload
  380. if (fileomitted '\.diff\.gz') {
  381. $srcmsg = _g('source only upload: Debian-native package');
  382. } elsif (fileomitted '\.orig\.tar\.gz') {
  383. $srcmsg = _g('source only, diff-only upload (original source NOT included)');
  384. } else {
  385. $srcmsg = _g('source only upload (original source is included)');
  386. }
  387. } else {
  388. $srcmsg = _g('full upload (original source is included)');
  389. if (fileomitted '\.dsc') {
  390. $srcmsg = _g('binary only upload (no source included)');
  391. } elsif (fileomitted '\.diff\.gz') {
  392. $srcmsg = _g('full upload; Debian-native package (full source is included)');
  393. } elsif (fileomitted '\.orig\.tar\.gz') {
  394. $srcmsg = _g('binary and diff upload (original source NOT included)');
  395. } else {
  396. $srcmsg = _g('full upload (original source is included)');
  397. }
  398. }
  399. if ($signchanges && signfile("$pva.changes")) {
  400. $signerrors = _g("Failed to sign .changes file");
  401. }
  402. if ($cleansource) {
  403. withecho($rootcommand, 'debian/rules', 'clean');
  404. }
  405. print "$progname: $srcmsg\n";
  406. if ($signerrors) {
  407. warning($signerrors);
  408. exit 1;
  409. }