dpkg-buildpackage.pl 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  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. -R<rules> rules file to execute (default is debian/rules).
  33. -p<sign-command>
  34. -d do not check build dependencies and conflicts.
  35. -D check build dependencies and conflicts.
  36. -j[<number>] specify jobs to run simultaneously } passed to debian/rules
  37. -k<keyid> the key to use for signing.
  38. -sgpg the sign-command is called like GPG.
  39. -spgp the sign-command is called like PGP.
  40. -us unsigned source.
  41. -uc unsigned changes.
  42. -a<arch> Debian architecture we build for (implies -d).
  43. -b binary-only, do not build source. } also passed to
  44. -B binary-only, no arch-indep files. } dpkg-genchanges
  45. -A binary-only, only arch-indep files. }
  46. -S source only, no binary files. }
  47. -t<system> set GNU system type. } passed to dpkg-architecture
  48. -v<version> changes since version <version>. }
  49. -m<maint> maintainer for package is <maint>. }
  50. -e<maint> maintainer for release is <maint>. } only passed
  51. -C<descfile> changes are described in <descfile>. } to dpkg-genchanges
  52. -si (default) src includes orig for rev. 0 or 1. }
  53. -sa uploaded src always includes orig. }
  54. -sd uploaded src is diff and .dsc only. }
  55. -sn force Debian native source format. }
  56. -s[sAkurKUR] see dpkg-source for explanation. } only passed
  57. -z<level> compression level of source } to dpkg-source
  58. -Z(gz|bz2|lzma) compression to use for source }
  59. -nc do not clean source tree (implies -b).
  60. -tc clean source tree when finished.
  61. -ap add pause before starting signature process.
  62. -E turn certain warnings into errors. } passed to
  63. -W when -E is turned on, -W turns it off. } dpkg-source
  64. -i[<regex>] ignore diffs of files matching regex. } only passed
  65. -I[<pattern>] filter out files when building tarballs. } to dpkg-source
  66. --admindir=<directory>
  67. change the administrative directory.
  68. -h, --help show this help message.
  69. --version show the version.
  70. "), $progname;
  71. }
  72. my @debian_rules = ("debian/rules");
  73. my @rootcommand = ();
  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. $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 = split /\s+/, $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 (/^-[zZ]/) {
  129. push @passopts, $_; # passed to dpkg-source
  130. } elsif (/^-i.*$/) {
  131. $diffignore = $_;
  132. } elsif (/^-I.*$/) {
  133. push @tarignore, $_;
  134. } elsif (/^-tc$/) {
  135. $cleansource = 1;
  136. } elsif (/^-t(.*)$/) {
  137. $targetgnusystem = $1; # Order DOES matter!
  138. } elsif (/^-nc$/) {
  139. $noclean = 1;
  140. if ($sourceonly) {
  141. usageerr(_g("cannot combine %s and %s"), '-nc', '-S');
  142. }
  143. unless ($binaryonly) {
  144. $binaryonly = '-b';
  145. }
  146. } elsif (/^-b$/) {
  147. $binaryonly = '-b';
  148. @checkbuilddep_args = ();
  149. $binarytarget = 'binary';
  150. if ($sourceonly) {
  151. usageerr(_g("cannot combine %s and %s"), '-b', '-S');
  152. }
  153. } elsif (/^-B$/) {
  154. $binaryonly = '-B';
  155. @checkbuilddep_args = ('-B');
  156. $binarytarget = 'binary-arch';
  157. if ($sourceonly) {
  158. usageerr(_g("cannot combine %s and %s"), '-B', '-S');
  159. }
  160. } elsif (/^-A$/) {
  161. $binaryonly = '-A';
  162. @checkbuilddep_args = ();
  163. $binarytarget = 'binary-indep';
  164. if ($sourceonly) {
  165. usageerr(_g("cannot combine %s and %s"), '-A', '-S');
  166. }
  167. } elsif (/^-S$/) {
  168. $sourceonly = '-S';
  169. @checkbuilddep_args = ('-B');
  170. if ($binaryonly) {
  171. usageerr(_g("cannot combine %s and %s"), $binaryonly, '-S');
  172. }
  173. } elsif (/^-v(.*)$/) {
  174. $since = $1;
  175. } elsif (/^-m(.*)$/) {
  176. $maint = $1;
  177. } elsif (/^-e(.*)$/) {
  178. $changedby = $1;
  179. } elsif (/^-C(.*)$/) {
  180. $desc = $1;
  181. } elsif (/^-W$/) {
  182. $warnable_error = 1;
  183. push @passopts, '-W';
  184. } elsif (/^-E$/) {
  185. $warnable_error = 0;
  186. push @passopts, '-E';
  187. } elsif (/^-R(.*)$/) {
  188. @debian_rules = split /\s+/, $1;
  189. } else {
  190. usageerr(_g("unknown option or argument %s"), $_);
  191. }
  192. }
  193. if ($< == 0) {
  194. warning(_g("using a gain-root-command while being root")) if (@rootcommand);
  195. } else {
  196. push @rootcommand, "fakeroot" unless @rootcommand;
  197. if (!testcommand($rootcommand[0])) {
  198. if ($rootcommand[0] eq 'fakeroot') {
  199. error(_g("fakeroot not found, either install the fakeroot\n" .
  200. "package, specify a command with the -r option, " .
  201. "or run this as root"));
  202. } else {
  203. error(_g("gain-root-commmand '%s' not found"), $rootcommand[0]);
  204. }
  205. }
  206. }
  207. unless ($signcommand) {
  208. $signsource = 0;
  209. $signchanges = 0;
  210. }
  211. my $signinterface;
  212. if ($forcesigninterface) {
  213. $signinterface = $forcesigninterface;
  214. } else {
  215. $signinterface = $signcommand;
  216. }
  217. if ($signcommand) {
  218. if ($signinterface !~ /^(gpg|pgp)$/) {
  219. warning(_g("unknown sign command, assuming pgp style interface"));
  220. } elsif ($signinterface eq 'pgp') {
  221. if ($signsource or $signchanges) {
  222. warning(_g("PGP support is deprecated (see README.feature-removal-schedule)"));
  223. }
  224. }
  225. }
  226. my $build_opts = Dpkg::BuildOptions::parse();
  227. if ($parallel) {
  228. $parallel = $build_opts->{parallel} if (defined $build_opts->{parallel});
  229. $ENV{MAKEFLAGS} ||= '';
  230. if ($parallel eq '-1') {
  231. $ENV{MAKEFLAGS} .= " -j";
  232. } else {
  233. $ENV{MAKEFLAGS} .= " -j$parallel";
  234. }
  235. $build_opts->{parallel} = $parallel;
  236. Dpkg::BuildOptions::set($build_opts);
  237. }
  238. my $default_flags = defined $build_opts->{noopt} ? "-g -O0" : "-g -O2";
  239. my %flags = ( CPPFLAGS => '',
  240. CFLAGS => $default_flags,
  241. CXXFLAGS => $default_flags,
  242. FFLAGS => $default_flags,
  243. LDFLAGS => "-Wl,-Bsymbolic-functions",
  244. );
  245. foreach my $flag (keys %flags) {
  246. if ($ENV{$flag}) {
  247. printf(_g("%s: use %s from environment: %s\n"), $progname,
  248. $flag, $ENV{$flag});
  249. } else {
  250. $ENV{$flag} = $flags{$flag};
  251. printf(_g("%s: set %s to default value: %s\n"), $progname,
  252. $flag, $ENV{$flag});
  253. }
  254. if ($ENV{"${flag}_APPEND"}) {
  255. $ENV{$flag} .= " ".$ENV{"${flag}_APPEND"};
  256. }
  257. }
  258. my $cwd = cwd();
  259. my $dir = basename($cwd);
  260. my $changelog = parse_changelog();
  261. my $pkg = mustsetvar($changelog->{source}, _g('source package'));
  262. my $version = mustsetvar($changelog->{version}, _g('source version'));
  263. check_version($version);
  264. my $maintainer;
  265. if ($changedby) {
  266. $maintainer = $changedby;
  267. } elsif ($maint) {
  268. $maintainer = $maint;
  269. } else {
  270. $maintainer = mustsetvar($changelog->{maintainer}, _g('source changed by'));
  271. }
  272. open my $arch_env, '-|', 'dpkg-architecture', "-a$targetarch",
  273. "-t$targetgnusystem", '-s', '-f' or subprocerr('dpkg-architecture');
  274. while ($_ = <$arch_env>) {
  275. chomp;
  276. my @cmds = split /\s*;\s*/;
  277. foreach (@cmds) {
  278. /^\s*(\w+)=([\w-]*)\s*$/ && do {
  279. $ENV{$1} = $2;
  280. };
  281. }
  282. }
  283. close $arch_env or subprocerr('dpkg-architecture');
  284. my $arch;
  285. unless ($sourceonly) {
  286. $arch = mustsetvar($ENV{'DEB_HOST_ARCH'}, _g('host architecture'));
  287. } else {
  288. $arch = 'source';
  289. }
  290. (my $sversion = $version) =~ s/^\d+://;
  291. my $pv = "${pkg}_$sversion";
  292. my $pva = "${pkg}_${sversion}_$arch";
  293. if ($checkbuilddep) {
  294. if ($admindir) {
  295. push @checkbuilddep_args, "--admindir=$admindir";
  296. }
  297. if (system('dpkg-checkbuilddeps', @checkbuilddep_args)) {
  298. warning(_g("Build dependencies/conflicts unsatisfied; aborting."));
  299. warning(_g("(Use -d flag to override.)"));
  300. if ($sourceonly) {
  301. warning(_g("This is currently a non-fatal warning with -S, but"));
  302. warning(_g("will probably become fatal in the future."));
  303. } else {
  304. exit 3;
  305. }
  306. }
  307. }
  308. unless ($noclean) {
  309. withecho(@rootcommand, @debian_rules, 'clean');
  310. }
  311. unless ($binaryonly) {
  312. chdir('..') or failure('chdir ..');
  313. my @opts = @passopts;
  314. if ($diffignore) { push @opts, $diffignore }
  315. push @opts, @tarignore;
  316. withecho('dpkg-source', @opts, '-b', $dir);
  317. chdir($dir) or failure("chdir $dir");
  318. }
  319. unless ($sourceonly) {
  320. withecho(@debian_rules, 'build');
  321. withecho(@rootcommand, @debian_rules, $binarytarget);
  322. }
  323. if ($usepause &&
  324. ($signchanges || ( !$binaryonly && $signsource )) ) {
  325. print _g("Press the return key to start signing process\n");
  326. getc();
  327. }
  328. my $signerrors;
  329. unless ($binaryonly) {
  330. if ($signsource && signfile("$pv.dsc")) {
  331. $signerrors = _g("Failed to sign .dsc and .changes file");
  332. $signchanges = 0;
  333. }
  334. }
  335. my @change_opts;
  336. if ($binaryonly) { push @change_opts, $binaryonly }
  337. if ($sourceonly) { push @change_opts, $sourceonly }
  338. if ($sourcestyle) { push @change_opts, $sourcestyle }
  339. if ($maint) { push @change_opts, "-m$maint" }
  340. if ($changedby) { push @change_opts, "-e$changedby" }
  341. if ($since) { push @change_opts, "-v$since" }
  342. if ($desc) { push @change_opts, "-C$desc" }
  343. my $chg = "../$pva.changes";
  344. print STDERR " dpkg-genchanges @change_opts >$chg\n";
  345. open CHANGES, '-|', 'dpkg-genchanges', @change_opts
  346. or subprocerr('dpkg-genchanges');
  347. open OUT, '>', $chg or syserr(_g('write changes file'));
  348. my $infiles = my $files = '';
  349. while ($_ = <CHANGES>) {
  350. print OUT $_ or syserr(_g('write changes file'));
  351. chomp;
  352. if (/^Files:/i) {
  353. $infiles = 1;
  354. } elsif ($infiles && /^\s+(.*)$/) {
  355. $files .= " $1 ";
  356. } elsif ($infiles && /^\S/) {
  357. $infiles = 0;
  358. }
  359. }
  360. close CHANGES or subprocerr(_g('dpkg-genchanges'));
  361. close OUT or syserr(_g('write changes file'));
  362. my $srcmsg;
  363. sub fileomitted { return $files !~ /$_[0]/ }
  364. if (fileomitted '\.deb') {
  365. # source only upload
  366. if (fileomitted "\.diff\.$comp_regex") {
  367. $srcmsg = _g('source only upload: Debian-native package');
  368. } elsif (fileomitted "\.orig\.tar\.$comp_regex") {
  369. $srcmsg = _g('source only, diff-only upload (original source NOT included)');
  370. } else {
  371. $srcmsg = _g('source only upload (original source is included)');
  372. }
  373. } else {
  374. $srcmsg = _g('full upload (original source is included)');
  375. if (fileomitted '\.dsc') {
  376. $srcmsg = _g('binary only upload (no source included)');
  377. } elsif (fileomitted "\.diff\.$comp_regex") {
  378. $srcmsg = _g('full upload; Debian-native package (full source is included)');
  379. } elsif (fileomitted "\.orig\.tar\.$comp_regex") {
  380. $srcmsg = _g('binary and diff upload (original source NOT included)');
  381. } else {
  382. $srcmsg = _g('full upload (original source is included)');
  383. }
  384. }
  385. if ($signchanges && signfile("$pva.changes")) {
  386. $signerrors = _g("Failed to sign .changes file");
  387. }
  388. if ($cleansource) {
  389. withecho(@rootcommand, @debian_rules, 'clean');
  390. }
  391. print "$progname: $srcmsg\n";
  392. if ($signerrors) {
  393. warning($signerrors);
  394. exit 1;
  395. }
  396. sub testcommand {
  397. my ($cmd) = @_;
  398. my $fullcmd = `which $cmd`;
  399. chomp $fullcmd;
  400. return $fullcmd && -x $fullcmd;
  401. }
  402. sub mustsetvar {
  403. my ($var, $text) = @_;
  404. error(_g("unable to determine %s"), $text)
  405. unless defined($var);
  406. print "$progname: $text $var\n";
  407. return $var;
  408. }
  409. sub withecho {
  410. shift while !$_[0];
  411. print STDERR " @_\n";
  412. system(@_)
  413. and subprocerr("@_");
  414. }
  415. sub signfile {
  416. my ($file) = @_;
  417. print STDERR " signfile $file\n";
  418. my $qfile = quotemeta($file);
  419. if ($signinterface eq 'gpg') {
  420. system("(cat ../$qfile ; echo '') | ".
  421. "$signcommand --utf8-strings --local-user "
  422. .quotemeta($signkey||$maintainer).
  423. " --clearsign --armor --textmode > ../$qfile.asc");
  424. } else {
  425. system("$signcommand -u ".quotemeta($signkey||$maintainer).
  426. " +clearsig=on -fast <../$qfile >../$qfile.asc");
  427. }
  428. my $status = $?;
  429. unless ($status) {
  430. system('mv', '--', "../$file.asc", "../$file")
  431. and subprocerr('mv');
  432. } else {
  433. system('rm', '-f', "../$file.asc")
  434. and subprocerr('rm -f');
  435. }
  436. print "\n";
  437. return $status
  438. }