dpkg-buildpackage.pl 12 KB

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