dpkg-buildpackage.pl 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  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::Compression;
  12. use Dpkg::Version qw(check_version);
  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) {
  222. my $build_opts = Dpkg::BuildOptions::parse();
  223. $parallel = $build_opts->{parallel} if (defined $build_opts->{parallel});
  224. $ENV{MAKEFLAGS} ||= '';
  225. if ($parallel eq '-1') {
  226. $ENV{MAKEFLAGS} .= " -j";
  227. } else {
  228. $ENV{MAKEFLAGS} .= " -j$parallel";
  229. }
  230. $build_opts->{parallel} = $parallel;
  231. Dpkg::BuildOptions::set($build_opts);
  232. }
  233. my $cwd = cwd();
  234. my $dir = basename($cwd);
  235. my %changes;
  236. open CHANGELOG, '-|', 'dpkg-parsechangelog' or subprocerr('dpkg-parsechangelog');
  237. # until we have a better parsecdata function this
  238. # should suffice
  239. while ($_ = <CHANGELOG>) {
  240. chomp;
  241. /^(\S+):\s*(.*)$/ && do {
  242. $changes{lc $1} = $2;
  243. };
  244. }
  245. close CHANGELOG or subprocerr('dpkg-parsechangelog');
  246. sub mustsetvar {
  247. my ($var, $text) = @_;
  248. error(_g("unable to determine %s"), $text)
  249. unless defined($var);
  250. print "$progname: $text $var\n";
  251. return $var;
  252. }
  253. my $pkg = mustsetvar($changes{source}, _g('source package'));
  254. my $version = mustsetvar($changes{version}, _g('source version'));
  255. check_version($version);
  256. my $maintainer;
  257. if ($changedby) {
  258. $maintainer = $changedby;
  259. } elsif ($maint) {
  260. $maintainer = $maint;
  261. } else {
  262. $maintainer = mustsetvar($changes{maintainer}, _g('source changed by'));
  263. }
  264. open my $arch_env, '-|', 'dpkg-architecture', "-a$targetarch",
  265. "-t$targetgnusystem", '-s', '-f' or subprocerr('dpkg-architecture');
  266. while ($_ = <$arch_env>) {
  267. chomp;
  268. my @cmds = split /\s*;\s*/;
  269. foreach (@cmds) {
  270. /^\s*(\w+)=([\w-]*)\s*$/ && do {
  271. $ENV{$1} = $2;
  272. };
  273. }
  274. }
  275. close $arch_env or subprocerr('dpkg-architecture');
  276. my $arch;
  277. unless ($sourceonly) {
  278. $arch = mustsetvar($ENV{'DEB_HOST_ARCH'}, _g('host architecture'));
  279. } else {
  280. $arch = 'source';
  281. }
  282. (my $sversion = $version) =~ s/^\d+://;
  283. my $pv = "${pkg}_$sversion";
  284. my $pva = "${pkg}_${sversion}_$arch";
  285. sub signfile {
  286. my ($file) = @_;
  287. print STDERR " signfile $file\n";
  288. my $qfile = quotemeta($file);
  289. if ($signinterface eq 'gpg') {
  290. system("(cat ../$qfile ; echo '') | ".
  291. "$signcommand --utf8-strings --local-user "
  292. .quotemeta($signkey||$maintainer).
  293. " --clearsign --armor --textmode > ../$qfile.asc");
  294. } else {
  295. system("$signcommand -u ".quotemeta($signkey||$maintainer).
  296. " +clearsig=on -fast <../$qfile >../$qfile.asc");
  297. }
  298. my $status = $?;
  299. unless ($status) {
  300. system('mv', '--', "../$file.asc", "../$file")
  301. and subprocerr('mv');
  302. } else {
  303. system('rm', '-f', "../$file.asc")
  304. and subprocerr('rm -f');
  305. }
  306. print "\n";
  307. return $status
  308. }
  309. sub withecho {
  310. shift while !$_[0];
  311. print STDERR " @_\n";
  312. system(@_)
  313. and subprocerr("@_");
  314. }
  315. if ($checkbuilddep) {
  316. if ($admindir) {
  317. push @checkbuilddep_args, "--admindir=$admindir";
  318. }
  319. if (system('dpkg-checkbuilddeps', @checkbuilddep_args)) {
  320. warning(_g("Build dependencies/conflicts unsatisfied; aborting."));
  321. warning(_g("(Use -d flag to override.)"));
  322. exit 3;
  323. }
  324. }
  325. unless ($noclean) {
  326. withecho($rootcommand, 'debian/rules', 'clean');
  327. }
  328. unless ($binaryonly) {
  329. chdir('..') or failure('chdir ..');
  330. my @opts = @passopts;
  331. if ($diffignore) { push @opts, $diffignore }
  332. push @opts, @tarignore;
  333. withecho('dpkg-source', @opts, '-b', $dir);
  334. chdir($dir) or failure("chdir $dir");
  335. }
  336. unless ($sourceonly) {
  337. withecho('debian/rules', 'build');
  338. withecho($rootcommand, 'debian/rules', $binarytarget);
  339. }
  340. if ($usepause &&
  341. ($signchanges || ( !$binaryonly && $signsource )) ) {
  342. print _g("Press the return key to start signing process\n");
  343. getc();
  344. }
  345. my $signerrors;
  346. unless ($binaryonly) {
  347. if ($signsource && signfile("$pv.dsc")) {
  348. $signerrors = _g("Failed to sign .dsc and .changes file");
  349. $signchanges = 0;
  350. }
  351. }
  352. my @change_opts;
  353. if ($binaryonly) { push @change_opts, $binaryonly }
  354. if ($sourceonly) { push @change_opts, $sourceonly }
  355. if ($sourcestyle) { push @change_opts, $sourcestyle }
  356. if ($maint) { push @change_opts, "-m$maint" }
  357. if ($changedby) { push @change_opts, "-e$changedby" }
  358. if ($since) { push @change_opts, "-v$since" }
  359. if ($desc) { push @change_opts, "-C$desc" }
  360. my $chg = "../$pva.changes";
  361. print STDERR " dpkg-genchanges @change_opts >$chg\n";
  362. open CHANGES, '-|', 'dpkg-genchanges', @change_opts
  363. or subprocerr('dpkg-genchanges');
  364. open OUT, '>', $chg or syserr(_g('write changes file'));
  365. my $infiles = my $files = '';
  366. while ($_ = <CHANGES>) {
  367. print OUT $_ or syserr(_g('write changes file'));
  368. chomp;
  369. if (/^Files:/i) {
  370. $infiles = 1;
  371. } elsif ($infiles && /^\s+(.*)$/) {
  372. $files .= " $1 ";
  373. } elsif ($infiles && /^\S/) {
  374. $infiles = 0;
  375. }
  376. }
  377. close CHANGES or subprocerr(_g('dpkg-genchanges'));
  378. close OUT or syserr(_g('write changes file'));
  379. sub fileomitted {
  380. my ($regex) = @_;
  381. return $files !~ /$regex/;
  382. }
  383. my $srcmsg;
  384. if (fileomitted '\.deb') {
  385. # source only upload
  386. if (fileomitted "\.diff\.$comp_regex") {
  387. $srcmsg = _g('source only upload: Debian-native package');
  388. } elsif (fileomitted "\.orig\.tar\.$comp_regex") {
  389. $srcmsg = _g('source only, diff-only upload (original source NOT included)');
  390. } else {
  391. $srcmsg = _g('source only upload (original source is included)');
  392. }
  393. } else {
  394. $srcmsg = _g('full upload (original source is included)');
  395. if (fileomitted '\.dsc') {
  396. $srcmsg = _g('binary only upload (no source included)');
  397. } elsif (fileomitted "\.diff\.$comp_regex") {
  398. $srcmsg = _g('full upload; Debian-native package (full source is included)');
  399. } elsif (fileomitted "\.orig\.tar\.$comp_regex") {
  400. $srcmsg = _g('binary and diff upload (original source NOT included)');
  401. } else {
  402. $srcmsg = _g('full upload (original source is included)');
  403. }
  404. }
  405. if ($signchanges && signfile("$pva.changes")) {
  406. $signerrors = _g("Failed to sign .changes file");
  407. }
  408. if ($cleansource) {
  409. withecho($rootcommand, 'debian/rules', 'clean');
  410. }
  411. print "$progname: $srcmsg\n";
  412. if ($signerrors) {
  413. warning($signerrors);
  414. exit 1;
  415. }