dpkg-buildpackage.pl 13 KB

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