dpkg-buildpackage.pl 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  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. textdomain("dpkg-dev");
  13. sub showversion {
  14. printf _g("Debian %s version %s.\n"), $progname, $version;
  15. print _g("
  16. Copyright (C) 1996 Ian Jackson.
  17. Copyright (C) 2000 Wichert Akkerman
  18. Copyright (C) 2007 Frank Lichtenheld");
  19. print _g("
  20. This is free software; see the GNU General Public Licence version 2 or
  21. later for copying conditions. There is NO warranty.
  22. ");
  23. }
  24. sub usage {
  25. printf _g("
  26. Usage: %s [<options> ...]
  27. Options:
  28. -r<gain-root-command>
  29. command to gain root privileges (default is fakeroot).
  30. -p<sign-command>
  31. -d do not check build dependencies and conflicts.
  32. -D check build dependencies and conflicts.
  33. -j[<number>] specify jobs to run simultaneously } passed to debian/rules
  34. -k<keyid> the key to use for signing.
  35. -sgpg the sign-command is called like GPG.
  36. -spgp the sign-command is called like PGP.
  37. -us unsigned source.
  38. -uc unsigned changes.
  39. -a<arch> Debian architecture we build for (implies -d).
  40. -b binary-only, do not build source. } also passed to
  41. -B binary-only, no arch-indep files. } dpkg-genchanges
  42. -A binary-only, only arch-indep files. }
  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(_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(_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(_g("cannot combine %s and %s"), '-B', '-S');
  161. }
  162. } elsif (/^-A$/) {
  163. $binaryonly = '-A';
  164. @checkbuilddep_args = ();
  165. $binarytarget = 'binary-indep';
  166. if ($sourceonly) {
  167. usageerr(_g("cannot combine %s and %s"), '-A', '-S');
  168. }
  169. } elsif (/^-S$/) {
  170. $sourceonly = '-S';
  171. $checkbuilddep = 0;
  172. if ($binaryonly) {
  173. usageerr(_g("cannot combine %s and %s"), $binaryonly, '-S');
  174. }
  175. } elsif (/^-v(.*)$/) {
  176. $since = $1;
  177. } elsif (/^-m(.*)$/) {
  178. $maint = $1;
  179. } elsif (/^-e(.*)$/) {
  180. $changedby = $1;
  181. } elsif (/^-C(.*)$/) {
  182. $desc = $1;
  183. } elsif (/^-W$/) {
  184. $warnable_error = 1;
  185. push @passopts, '-W';
  186. } elsif (/^-E$/) {
  187. $warnable_error = 0;
  188. push @passopts, '-E';
  189. } else {
  190. usageerr(_g("unknown option or argument %s"), $_);
  191. }
  192. }
  193. if ($< == 0) {
  194. warning(_g("using a gain-root-command while being root")) if ($rootcommand);
  195. } else {
  196. $rootcommand ||= 'fakeroot';
  197. if (!testcommand($rootcommand)) {
  198. if ($rootcommand eq 'fakeroot') {
  199. error(_g("fakeroot not found, either install the fakeroot\n" .
  200. "package, specify a command with the -r option, " .
  201. "or run this as root"));
  202. } else {
  203. error(_g("gain-root-commmand '%s' not found"), $rootcommand);
  204. }
  205. }
  206. }
  207. unless ($signcommand) {
  208. $signsource = 0;
  209. $signchanges = 0;
  210. }
  211. my $signinterface;
  212. if ($forcesigninterface) {
  213. $signinterface = $forcesigninterface;
  214. } else {
  215. $signinterface = $signcommand;
  216. }
  217. if ($signcommand && ($signinterface !~ /^(gpg|pgp)$/)) {
  218. warning(_g("unknown sign command, assuming pgp style interface"));
  219. }
  220. if ($parallel || $ENV{DEB_BUILD_OPTIONS}) {
  221. my $build_opts = Dpkg::BuildOptions::parse();
  222. $parallel ||= $build_opts->{parallel};
  223. if (defined $parallel) {
  224. $ENV{MAKEFLAGS} ||= '';
  225. if ($parallel eq '-1') {
  226. $ENV{MAKEFLAGS} .= " -j";
  227. } else {
  228. $ENV{MAKEFLAGS} .= " -j$parallel";
  229. }
  230. }
  231. $build_opts->{parallel} = $parallel;
  232. Dpkg::BuildOptions::set($build_opts);
  233. }
  234. my $cwd = cwd();
  235. my $dir = basename($cwd);
  236. my %changes;
  237. open CHANGELOG, '-|', 'dpkg-parsechangelog' or subprocerr('dpkg-parsechangelog');
  238. # until we have a better parsecdata function this
  239. # should suffice
  240. while ($_ = <CHANGELOG>) {
  241. chomp;
  242. /^(\S+):\s*(.*)$/ && do {
  243. $changes{lc $1} = $2;
  244. };
  245. }
  246. close CHANGELOG or subprocerr('dpkg-parsechangelog');
  247. sub mustsetvar {
  248. my ($var, $text) = @_;
  249. error(_g("unable to determine %s"), $text)
  250. unless defined($var);
  251. print "$progname: $text $var\n";
  252. return $var;
  253. }
  254. my $pkg = mustsetvar($changes{source}, _g('source package'));
  255. my $version = mustsetvar($changes{version}, _g('source version'));
  256. check_version($version);
  257. my $maintainer;
  258. if ($changedby) {
  259. $maintainer = $changedby;
  260. } elsif ($maint) {
  261. $maintainer = $maint;
  262. } else {
  263. $maintainer = mustsetvar($changes{maintainer}, _g('source changed by'));
  264. }
  265. open my $arch_env, '-|', 'dpkg-architecture', "-a$targetarch",
  266. "-t$targetgnusystem", '-s', '-f' or subprocerr('dpkg-architecture');
  267. while ($_ = <$arch_env>) {
  268. chomp;
  269. my @cmds = split /\s*;\s*/;
  270. foreach (@cmds) {
  271. /^\s*(\w+)=([\w-]*)\s*$/ && do {
  272. $ENV{$1} = $2;
  273. };
  274. }
  275. }
  276. close $arch_env or subprocerr('dpkg-architecture');
  277. my $arch;
  278. unless ($sourceonly) {
  279. $arch = mustsetvar($ENV{'DEB_HOST_ARCH'}, _g('host architecture'));
  280. } else {
  281. $arch = 'source';
  282. }
  283. (my $sversion = $version) =~ s/^\d+://;
  284. my $pv = "${pkg}_$sversion";
  285. my $pva = "${pkg}_${sversion}_$arch";
  286. sub signfile {
  287. my ($file) = @_;
  288. print STDERR " signfile $file\n";
  289. my $qfile = quotemeta($file);
  290. if ($signinterface eq 'gpg') {
  291. system("(cat ../$qfile ; echo '') | ".
  292. "$signcommand --utf8-strings --local-user "
  293. .quotemeta($signkey||$maintainer).
  294. " --clearsign --armor --textmode > ../$qfile.asc");
  295. } else {
  296. system("$signcommand -u ".quotemeta($signkey||$maintainer).
  297. " +clearsig=on -fast <../$qfile >../$qfile.asc");
  298. }
  299. my $status = $?;
  300. unless ($status) {
  301. system('mv', '--', "../$file.asc", "../$file")
  302. and subprocerr('mv');
  303. } else {
  304. system('rm', '-f', "../$file.asc")
  305. and subprocerr('rm -f');
  306. }
  307. print "\n";
  308. return $status
  309. }
  310. sub withecho {
  311. shift while !$_[0];
  312. print STDERR " @_\n";
  313. system(@_)
  314. and subprocerr("@_");
  315. }
  316. if ($checkbuilddep) {
  317. if ($admindir) {
  318. push @checkbuilddep_args, "--admindir=$admindir";
  319. }
  320. if (system('dpkg-checkbuilddeps', @checkbuilddep_args)) {
  321. warning(_g("Build dependencies/conflicts unsatisfied; aborting."));
  322. warning(_g("(Use -d flag to override.)"));
  323. exit 3;
  324. }
  325. }
  326. unless ($noclean) {
  327. withecho($rootcommand, 'debian/rules', 'clean');
  328. }
  329. unless ($binaryonly) {
  330. chdir('..') or failure('chdir ..');
  331. my @opts = @passopts;
  332. if ($diffignore) { push @opts, $diffignore }
  333. push @opts, @tarignore;
  334. withecho('dpkg-source', @opts, '-b', $dir);
  335. chdir($dir) or failure("chdir $dir");
  336. }
  337. unless ($sourceonly) {
  338. withecho('debian/rules', 'build');
  339. withecho($rootcommand, 'debian/rules', $binarytarget);
  340. }
  341. if ($usepause &&
  342. ($signchanges || ( !$binaryonly && $signsource )) ) {
  343. print _g("Press the return key to start signing process\n");
  344. getc();
  345. }
  346. my $signerrors;
  347. unless ($binaryonly) {
  348. if ($signsource && signfile("$pv.dsc")) {
  349. $signerrors = _g("Failed to sign .dsc and .changes file");
  350. $signchanges = 0;
  351. }
  352. }
  353. my @change_opts;
  354. if ($binaryonly) { push @change_opts, $binaryonly }
  355. if ($sourceonly) { push @change_opts, $sourceonly }
  356. if ($sourcestyle) { push @change_opts, $sourcestyle }
  357. if ($maint) { push @change_opts, "-m$maint" }
  358. if ($changedby) { push @change_opts, "-e$changedby" }
  359. if ($since) { push @change_opts, "-v$since" }
  360. if ($desc) { push @change_opts, "-C$desc" }
  361. my $chg = "../$pva.changes";
  362. print STDERR " dpkg-genchanges @change_opts >$chg\n";
  363. open CHANGES, '-|', 'dpkg-genchanges', @change_opts
  364. or subprocerr('dpkg-genchanges');
  365. open OUT, '>', $chg or syserr(_g('write changes file'));
  366. my $infiles = my $files = '';
  367. while ($_ = <CHANGES>) {
  368. print OUT $_ or syserr(_g('write changes file'));
  369. chomp;
  370. if (/^Files:/i) {
  371. $infiles = 1;
  372. } elsif ($infiles && /^\s+(.*)$/) {
  373. $files .= " $1 ";
  374. } elsif ($infiles && /^\S/) {
  375. $infiles = 0;
  376. }
  377. }
  378. close CHANGES or subprocerr(_g('dpkg-genchanges'));
  379. close OUT or syserr(_g('write changes file'));
  380. sub fileomitted {
  381. my ($regex) = @_;
  382. return $files !~ /$regex/;
  383. }
  384. my $srcmsg;
  385. if (fileomitted '\.deb') {
  386. # source only upload
  387. if (fileomitted '\.diff\.gz') {
  388. $srcmsg = _g('source only upload: Debian-native package');
  389. } elsif (fileomitted '\.orig\.tar\.gz') {
  390. $srcmsg = _g('source only, diff-only upload (original source NOT included)');
  391. } else {
  392. $srcmsg = _g('source only upload (original source is included)');
  393. }
  394. } else {
  395. $srcmsg = _g('full upload (original source is included)');
  396. if (fileomitted '\.dsc') {
  397. $srcmsg = _g('binary only upload (no source included)');
  398. } elsif (fileomitted '\.diff\.gz') {
  399. $srcmsg = _g('full upload; Debian-native package (full source is included)');
  400. } elsif (fileomitted '\.orig\.tar\.gz') {
  401. $srcmsg = _g('binary and diff upload (original source NOT included)');
  402. } else {
  403. $srcmsg = _g('full upload (original source is included)');
  404. }
  405. }
  406. if ($signchanges && signfile("$pva.changes")) {
  407. $signerrors = _g("Failed to sign .changes file");
  408. }
  409. if ($cleansource) {
  410. withecho($rootcommand, 'debian/rules', 'clean');
  411. }
  412. print "$progname: $srcmsg\n";
  413. if ($signerrors) {
  414. warning($signerrors);
  415. exit 1;
  416. }