dpkg-buildpackage.pl 12 KB

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