dpkg-buildpackage.pl 13 KB

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