dpkg-buildpackage.pl 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  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::BuildOptions;
  9. push (@INC, $dpkglibdir);
  10. require 'controllib.pl';
  11. our $warnable_error;
  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 if it
  30. exists).
  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. } only passed
  53. -s[sAkurKUR] see dpkg-source for explanation. } to dpkg-source
  54. -nc do not clean source tree (implies -b).
  55. -tc clean source tree when finished.
  56. -ap add pause before starting signature process.
  57. -W turn certain errors into warnings. } passed to
  58. -E when -W is turned on, -E turns it off. } dpkg-source
  59. -i[<regex>] ignore diffs of files matching regex. } only passed
  60. -I[<pattern>] filter out files when building tarballs. } to dpkg-source
  61. --admindir=<directory>
  62. change the administrative directory.
  63. -h, --help show this help message.
  64. --version show the version.
  65. "), $progname;
  66. }
  67. sub testcommand {
  68. my ($cmd) = @_;
  69. my $fullcmd = `which $cmd`;
  70. chomp $fullcmd;
  71. return $fullcmd && -x $fullcmd;
  72. }
  73. my $rootcommand = '';
  74. if (testcommand('fakeroot')) {
  75. $rootcommand = 'fakeroot';
  76. }
  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 (/^-i.*$/) {
  132. $diffignore = $_;
  133. } elsif (/^-I.*$/) {
  134. push @tarignore, $_;
  135. } elsif (/^-tc$/) {
  136. $cleansource = 1;
  137. } elsif (/^-t(.*)$/) {
  138. $targetgnusystem = $1; # Order DOES matter!
  139. } elsif (/^-nc$/) {
  140. $noclean = 1;
  141. if ($sourceonly) {
  142. usageerr(sprintf(_g("cannot combine %s and %s"), '-nc', '-S'));
  143. }
  144. unless ($binaryonly) {
  145. $binaryonly = '-b';
  146. }
  147. } elsif (/^-b$/) {
  148. $binaryonly = '-b';
  149. @checkbuilddep_args = ();
  150. $binarytarget = 'binary';
  151. if ($sourceonly) {
  152. usageerr(sprintf(_g("cannot combine %s and %s"), '-b', '-S'));
  153. }
  154. } elsif (/^-B$/) {
  155. $binaryonly = '-B';
  156. @checkbuilddep_args = ('-B');
  157. $binarytarget = 'binary-arch';
  158. if ($sourceonly) {
  159. usageerr(sprintf(_g("cannot combine %s and %s"), '-B', '-S'));
  160. }
  161. } elsif (/^-S$/) {
  162. $sourceonly = '-S';
  163. $checkbuilddep = 0;
  164. if ($binaryonly) {
  165. usageerr(sprintf(_g("cannot combine %s and %s"), $binaryonly, '-S'));
  166. }
  167. } elsif (/^-v(.*)$/) {
  168. $since = $1;
  169. } elsif (/^-m(.*)$/) {
  170. $maint = $1;
  171. } elsif (/^-e(.*)$/) {
  172. $changedby = $1;
  173. } elsif (/^-C(.*)$/) {
  174. $desc = $1;
  175. } elsif (/^-W$/) {
  176. $warnable_error = 1;
  177. push @passopts, '-W';
  178. } elsif (/^-E$/) {
  179. $warnable_error = 0;
  180. push @passopts, '-E';
  181. } else {
  182. usageerr(sprintf(_g("unknown option or argument %s"), $_));
  183. }
  184. }
  185. unless ($signcommand) {
  186. $signsource = 0;
  187. $signchanges = 0;
  188. }
  189. my $signinterface;
  190. if ($forcesigninterface) {
  191. $signinterface = $forcesigninterface;
  192. } else {
  193. $signinterface = $signcommand;
  194. }
  195. if ($signcommand && ($signinterface !~ /^(gpg|pgp)$/)) {
  196. warning(_g("unknown sign command, assuming pgp style interface"));
  197. }
  198. if ($parallel || $ENV{DEB_BUILD_OPTIONS}) {
  199. my $build_opts = Dpkg::BuildOptions::parse();
  200. $parallel ||= $build_opts->{parallel};
  201. if (defined $parallel) {
  202. $ENV{MAKEFLAGS} ||= '';
  203. if ($parallel eq '-1') {
  204. $ENV{MAKEFLAGS} .= " -j";
  205. } else {
  206. $ENV{MAKEFLAGS} .= " -j$parallel";
  207. }
  208. }
  209. $build_opts->{parallel} = $parallel;
  210. Dpkg::BuildOptions::set($build_opts);
  211. }
  212. my $cwd = cwd();
  213. my $dir = basename($cwd);
  214. my %changes;
  215. open CHANGELOG, '-|', 'dpkg-parsechangelog' or subprocerr('dpkg-parsechangelog');
  216. # until we have a better parsecdata function this
  217. # should suffice
  218. while ($_ = <CHANGELOG>) {
  219. chomp;
  220. /^(\S+):\s*(.*)$/ && do {
  221. $changes{lc $1} = $2;
  222. };
  223. }
  224. close CHANGELOG or subprocerr('dpkg-parsechangelog');
  225. sub mustsetvar {
  226. my ($var, $text) = @_;
  227. error(sprintf(_g("unable to determine %s", $text)))
  228. unless defined($var);
  229. print "$progname: $text $var\n";
  230. return $var;
  231. }
  232. my $pkg = mustsetvar($changes{source}, _g('source package'));
  233. my $version = mustsetvar($changes{version}, _g('source version'));
  234. checkversion($version);
  235. my $maintainer;
  236. if ($changedby) {
  237. $maintainer = $changedby;
  238. } elsif ($maint) {
  239. $maintainer = $maint;
  240. } else {
  241. $maintainer = mustsetvar($changes{maintainer}, _g('source changed by'));
  242. }
  243. open my $arch_env, '-|', 'dpkg-architecture', "-a$targetarch",
  244. "-t$targetgnusystem", '-s', '-f' or subprocerr('dpkg-architecture');
  245. while ($_ = <$arch_env>) {
  246. chomp;
  247. my @cmds = split /\s*;\s*/;
  248. foreach (@cmds) {
  249. /^\s*(\w+)=([\w-]*)\s*$/ && do {
  250. $ENV{$1} = $2;
  251. };
  252. }
  253. }
  254. close $arch_env or subprocerr('dpkg-architecture');
  255. my $arch;
  256. unless ($sourceonly) {
  257. $arch = mustsetvar($ENV{'DEB_HOST_ARCH'}, _g('host architecture'));
  258. } else {
  259. $arch = 'source';
  260. }
  261. (my $sversion = $version) =~ s/^\d+://;
  262. my $pv = "${pkg}_$sversion";
  263. my $pva = "${pkg}_${sversion}_$arch";
  264. sub signfile {
  265. my ($file) = @_;
  266. print STDERR " signfile $file\n";
  267. my $qfile = quotemeta($file);
  268. if ($signinterface eq 'gpg') {
  269. system("(cat ../$qfile ; echo '') | ".
  270. "$signcommand --utf8-strings --local-user "
  271. .quotemeta($signkey||$maintainer).
  272. " --clearsign --armor --textmode > ../$qfile.asc");
  273. } else {
  274. system("$signcommand -u ".quotemeta($signkey||$maintainer).
  275. " +clearsig=on -fast <../$qfile >../$qfile.asc");
  276. }
  277. my $status = $?;
  278. unless ($status) {
  279. system('mv', '--', "../$file.asc", "../$file")
  280. and subprocerr('mv');
  281. } else {
  282. system('rm', '-f', "../$file.asc")
  283. and subprocerr('rm -f');
  284. }
  285. print "\n";
  286. return $status
  287. }
  288. sub withecho {
  289. shift while !$_[0];
  290. print STDERR " @_\n";
  291. system(@_)
  292. and subprocerr("@_");
  293. }
  294. if ($checkbuilddep) {
  295. if ($admindir) {
  296. push @checkbuilddep_args, "--admindir=$admindir";
  297. }
  298. if (system('dpkg-checkbuilddeps', @checkbuilddep_args)) {
  299. warning(_g("Build dependencies/conflicts unsatisfied; aborting."));
  300. warning(_g("(Use -d flag to override.)"));
  301. exit 3;
  302. }
  303. }
  304. unless ($noclean) {
  305. withecho($rootcommand, 'debian/rules', 'clean');
  306. }
  307. unless ($binaryonly) {
  308. chdir('..') or failure('chdir ..');
  309. my @opts = @passopts;
  310. if ($diffignore) { push @opts, $diffignore }
  311. push @opts, @tarignore;
  312. withecho('dpkg-source', @opts, '-b', $dir);
  313. chdir($dir) or failure("chdir $dir");
  314. }
  315. unless ($sourceonly) {
  316. withecho('debian/rules', 'build');
  317. withecho($rootcommand, 'debian/rules', $binarytarget);
  318. }
  319. if ($usepause &&
  320. ($signchanges || ( !$binaryonly && $signsource )) ) {
  321. print _g("Press the return key to start signing process\n");
  322. getc();
  323. }
  324. my $signerrors;
  325. unless ($binaryonly) {
  326. if ($signsource && signfile("$pv.dsc")) {
  327. $signerrors = _g("Failed to sign .dsc and .changes file");
  328. $signchanges = 0;
  329. }
  330. }
  331. my @change_opts;
  332. if ($binaryonly) { push @change_opts, $binaryonly }
  333. if ($sourceonly) { push @change_opts, $sourceonly }
  334. if ($sourcestyle) { push @change_opts, $sourcestyle }
  335. if ($maint) { push @change_opts, "-m$maint" }
  336. if ($changedby) { push @change_opts, "-e$changedby" }
  337. if ($since) { push @change_opts, "-v$since" }
  338. if ($desc) { push @change_opts, "-C$desc" }
  339. my $chg = "../$pva.changes";
  340. print STDERR " dpkg-genchanges @change_opts >$chg\n";
  341. open CHANGES, '-|', 'dpkg-genchanges', @change_opts
  342. or subprocerr('dpkg-genchanges');
  343. open OUT, '>', $chg or syserr(_g('write changes file'));
  344. my $infiles = my $files = '';
  345. while ($_ = <CHANGES>) {
  346. print OUT $_ or syserr(_g('write changes file'));
  347. chomp;
  348. if (/^Files:/i) {
  349. $infiles = 1;
  350. } elsif ($infiles && /^\s+(.*)$/) {
  351. $files .= " $1 ";
  352. } elsif ($infiles && /^\S/) {
  353. $infiles = 0;
  354. }
  355. }
  356. close CHANGES or subprocerr(_g('dpkg-genchanges'));
  357. close OUT or syserr(_g('write changes file'));
  358. sub fileomitted {
  359. my ($regex) = @_;
  360. return $files !~ /$regex/;
  361. }
  362. my $srcmsg;
  363. if (fileomitted '\.deb') {
  364. # source only upload
  365. if (fileomitted '\.diff\.gz') {
  366. $srcmsg = _g('source only upload: Debian-native package');
  367. } elsif (fileomitted '\.orig\.tar\.gz') {
  368. $srcmsg = _g('source only, diff-only upload (original source NOT included)');
  369. } else {
  370. $srcmsg = _g('source only upload (original source is included)');
  371. }
  372. } else {
  373. $srcmsg = _g('full upload (original source is included)');
  374. if (fileomitted '\.dsc') {
  375. $srcmsg = _g('binary only upload (no source included)');
  376. } elsif (fileomitted '\.diff\.gz') {
  377. $srcmsg = _g('full upload; Debian-native package (full source is included)');
  378. } elsif (fileomitted '\.orig\.tar\.gz') {
  379. $srcmsg = _g('binary and diff upload (original source NOT included)');
  380. } else {
  381. $srcmsg = _g('full upload (original source is included)');
  382. }
  383. }
  384. if ($signchanges && signfile("$pva.changes")) {
  385. $signerrors = _g("Failed to sign .changes file");
  386. }
  387. if ($cleansource) {
  388. withecho($rootcommand, 'debian/rules', 'clean');
  389. }
  390. print "$progname: $srcmsg\n";
  391. if ($signerrors) {
  392. warning($signerrors);
  393. exit 1;
  394. }