dpkg-buildpackage.pl 12 KB

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