dpkg-buildpackage.pl 12 KB

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