dpkg-buildpackage.pl 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  1. #!/usr/bin/perl
  2. #
  3. # dpkg-buildpackage
  4. #
  5. # This program is free software; you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation; either version 2 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. use strict;
  18. use warnings;
  19. use Cwd;
  20. use File::Basename;
  21. use POSIX;
  22. use Dpkg;
  23. use Dpkg::Gettext;
  24. use Dpkg::ErrorHandling;
  25. use Dpkg::BuildFlags;
  26. use Dpkg::BuildOptions;
  27. use Dpkg::BuildFeatures;
  28. use Dpkg::Compression;
  29. use Dpkg::Version;
  30. use Dpkg::Changelog::Parse;
  31. use Dpkg::Path qw(find_command);
  32. textdomain("dpkg-dev");
  33. sub showversion {
  34. printf _g("Debian %s version %s.\n"), $progname, $version;
  35. print _g("
  36. Copyright (C) 1996 Ian Jackson.
  37. Copyright (C) 2000 Wichert Akkerman
  38. Copyright (C) 2007 Frank Lichtenheld");
  39. print _g("
  40. This is free software; see the GNU General Public License version 2 or
  41. later for copying conditions. There is NO warranty.
  42. ");
  43. }
  44. sub usage {
  45. printf _g("
  46. Usage: %s [<options> ...]
  47. Options:
  48. -r<gain-root-command>
  49. command to gain root privileges (default is fakeroot).
  50. -R<rules> rules file to execute (default is debian/rules).
  51. -p<sign-command>
  52. -d do not check build dependencies and conflicts.
  53. -D check build dependencies and conflicts.
  54. -T<target> call debian/rules <target> with the proper environment
  55. --as-root ensure -T calls the target with root rights
  56. -j[<number>] specify jobs to run simultaneously } passed to debian/rules
  57. -k<keyid> the key to use for signing.
  58. -sgpg the sign-command is called like GPG.
  59. -spgp the sign-command is called like PGP.
  60. -us unsigned source.
  61. -uc unsigned changes.
  62. -a<arch> Debian architecture we build for (implies -d).
  63. -b binary-only, do not build source. } also passed to
  64. -B binary-only, no arch-indep files. } dpkg-genchanges
  65. -A binary-only, only arch-indep files. }
  66. -S source only, no binary files. }
  67. -F normal full build (binaries and sources).
  68. -t<system> set GNU system type. } passed to dpkg-architecture
  69. -v<version> changes since version <version>. }
  70. -m<maint> maintainer for package is <maint>. }
  71. -e<maint> maintainer for release is <maint>. } only passed
  72. -C<descfile> changes are described in <descfile>. } to dpkg-genchanges
  73. -si (default) src includes orig if new upstream. }
  74. -sa uploaded src always includes orig. }
  75. -sd uploaded src is diff and .dsc only. }
  76. -sn force Debian native source format. }
  77. -s[sAkurKUR] see dpkg-source for explanation. } only passed
  78. -z<level> compression level of source } to dpkg-source
  79. -Z<compressor> compression to use for source }
  80. -nc do not clean source tree (implies -b).
  81. -tc clean source tree when finished.
  82. -ap add pause before starting signature process.
  83. -i[<regex>] ignore diffs of files matching regex. } only passed
  84. -I[<pattern>] filter out files when building tarballs. } to dpkg-source
  85. --source-option=<opt>
  86. pass option <opt> to dpkg-source
  87. --changes-option=<opt>
  88. pass option <opt> to dpkg-genchanges
  89. --admindir=<directory>
  90. change the administrative directory.
  91. -h, --help show this help message.
  92. --version show the version.
  93. "), $progname;
  94. }
  95. my @debian_rules = ("debian/rules");
  96. my @rootcommand = ();
  97. my $signcommand = '';
  98. if ( ( ($ENV{GNUPGHOME} && -e $ENV{GNUPGHOME})
  99. || ($ENV{HOME} && -e "$ENV{HOME}/.gnupg") )
  100. && find_command('gpg')) {
  101. $signcommand = 'gpg';
  102. } elsif (find_command('pgp')) {
  103. $signcommand = 'pgp'
  104. }
  105. my ($admindir, $signkey, $forcesigninterface, $usepause, $noclean,
  106. $cleansource, $since, $maint,
  107. $changedby, $desc, $parallel);
  108. my $checkbuilddep = 1;
  109. my $signsource = 1;
  110. my $signchanges = 1;
  111. my $binarytarget = 'binary';
  112. my $buildtarget = 'build';
  113. my $targetarch = my $targetgnusystem = '';
  114. my $call_target = '';
  115. my $call_target_as_root = 0;
  116. my (@checkbuilddep_opts, @changes_opts, @source_opts);
  117. use constant BUILD_DEFAULT => 1;
  118. use constant BUILD_SOURCE => 2;
  119. use constant BUILD_ARCH_DEP => 4;
  120. use constant BUILD_ARCH_INDEP => 8;
  121. use constant BUILD_BINARY => BUILD_ARCH_DEP | BUILD_ARCH_INDEP;
  122. use constant BUILD_ALL => BUILD_BINARY | BUILD_SOURCE;
  123. my $include = BUILD_ALL | BUILD_DEFAULT;
  124. sub build_normal() { return ($include & BUILD_ALL) == BUILD_ALL; }
  125. sub build_sourceonly() { return $include == BUILD_SOURCE; }
  126. sub build_binaryonly() { return !($include & BUILD_SOURCE); }
  127. sub build_opt() {
  128. return (($include == BUILD_BINARY) ? '-b' :
  129. (($include == BUILD_ARCH_DEP) ? '-B' :
  130. (($include == BUILD_ARCH_INDEP) ? '-A' :
  131. (($include == BUILD_SOURCE) ? '-S' :
  132. internerr("build_opt called with include=$include")))));
  133. }
  134. while (@ARGV) {
  135. $_ = shift @ARGV;
  136. if (/^(--help|-h)$/) {
  137. usage;
  138. exit 0;
  139. } elsif (/^--version$/) {
  140. showversion;
  141. exit 0;
  142. } elsif (/^--admindir$/) {
  143. $admindir = shift @ARGV;
  144. } elsif (/^--admindir=(.*)$/) {
  145. $admindir = $1;
  146. } elsif (/^--source-option=(.*)$/) {
  147. push @source_opts, $1;
  148. } elsif (/^--changes-option=(.*)$/) {
  149. push @changes_opts, $1;
  150. } elsif (/^-j(\d*)$/) {
  151. $parallel = $1 || '';
  152. } elsif (/^-r(.*)$/) {
  153. @rootcommand = split /\s+/, $1;
  154. } elsif (/^-p(.*)$/) {
  155. $signcommand = $1;
  156. } elsif (/^-k(.*)$/) {
  157. $signkey = $1;
  158. } elsif (/^-([dD])$/) {
  159. $checkbuilddep = ($1 eq 'D');
  160. } elsif (/^-s(gpg|pgp)$/) {
  161. $forcesigninterface = $1;
  162. } elsif (/^-us$/) {
  163. $signsource = 0;
  164. } elsif (/^-uc$/) {
  165. $signchanges = 0;
  166. } elsif (/^-ap$/) {
  167. $usepause = 1;
  168. } elsif (/^-a(.*)$/) {
  169. $targetarch = $1;
  170. $checkbuilddep = 0;
  171. } elsif (/^-s[iad]$/) {
  172. push @changes_opts, $_;
  173. } elsif (/^-(?:s[insAkurKUR]|[zZ].*|i.*|I.*)$/) {
  174. push @source_opts, $_; # passed to dpkg-source
  175. } elsif (/^-tc$/) {
  176. $cleansource = 1;
  177. } elsif (/^-t(.*)$/) {
  178. $targetgnusystem = $1; # Order DOES matter!
  179. } elsif (/^(--target|-T)$/) {
  180. $call_target = shift @ARGV;
  181. } elsif (/^(--target=|-T)(.+)$/) {
  182. $call_target = $2;
  183. } elsif (/^--as-root$/) {
  184. $call_target_as_root = 1;
  185. } elsif (/^-nc$/) {
  186. $noclean = 1;
  187. } elsif (/^-b$/) {
  188. build_sourceonly && usageerr(_g("cannot combine %s and %s"), $_, "-S");
  189. $include = BUILD_BINARY;
  190. push @changes_opts, '-b';
  191. @checkbuilddep_opts = ();
  192. $buildtarget = 'build';
  193. $binarytarget = 'binary';
  194. } elsif (/^-B$/) {
  195. build_sourceonly && usageerr(_g("cannot combine %s and %s"), $_, "-S");
  196. $include = BUILD_ARCH_DEP;
  197. push @changes_opts, '-B';
  198. @checkbuilddep_opts = ('-B');
  199. $buildtarget = 'build-arch';
  200. $binarytarget = 'binary-arch';
  201. } elsif (/^-A$/) {
  202. build_sourceonly && usageerr(_g("cannot combine %s and %s"), $_, "-S");
  203. $include = BUILD_ARCH_INDEP;
  204. push @changes_opts, '-A';
  205. @checkbuilddep_opts = ();
  206. $buildtarget = 'build-indep';
  207. $binarytarget = 'binary-indep';
  208. } elsif (/^-S$/) {
  209. build_binaryonly && usageerr(_g("cannot combine %s and %s"), build_opt, "-S");
  210. $include = BUILD_SOURCE;
  211. push @changes_opts, '-S';
  212. @checkbuilddep_opts = ('-B');
  213. } elsif (/^-F$/) {
  214. !build_normal && usageerr(_g("cannot combine %s and %s"), $_, build_opt);
  215. $include = BUILD_ALL;
  216. @checkbuilddep_opts = ();
  217. } elsif (/^-v(.*)$/) {
  218. $since = $1;
  219. } elsif (/^-m(.*)$/) {
  220. $maint = $1;
  221. } elsif (/^-e(.*)$/) {
  222. $changedby = $1;
  223. } elsif (/^-C(.*)$/) {
  224. $desc = $1;
  225. } elsif (m/^-[EW]$/) {
  226. # Deprecated option
  227. warning(_g("-E and -W are deprecated, they are without effect"));
  228. } elsif (/^-R(.*)$/) {
  229. @debian_rules = split /\s+/, $1;
  230. } else {
  231. usageerr(_g("unknown option or argument %s"), $_);
  232. }
  233. }
  234. if ($noclean) {
  235. # -nc without -b/-B/-A/-S/-F implies -b
  236. $include = BUILD_BINARY if ($include & BUILD_DEFAULT);
  237. }
  238. my $buildfeats = Dpkg::BuildFeatures->new();
  239. $buildtarget = 'build' unless $buildfeats->has('build-arch');
  240. if ($< == 0) {
  241. warning(_g("using a gain-root-command while being root")) if (@rootcommand);
  242. } else {
  243. push @rootcommand, "fakeroot" unless @rootcommand;
  244. if (!find_command($rootcommand[0])) {
  245. if ($rootcommand[0] eq 'fakeroot') {
  246. error(_g("fakeroot not found, either install the fakeroot\n" .
  247. "package, specify a command with the -r option, " .
  248. "or run this as root"));
  249. } else {
  250. error(_g("gain-root-commmand '%s' not found"), $rootcommand[0]);
  251. }
  252. }
  253. }
  254. unless ($signcommand) {
  255. $signsource = 0;
  256. $signchanges = 0;
  257. }
  258. my $signinterface;
  259. if ($forcesigninterface) {
  260. $signinterface = $forcesigninterface;
  261. } else {
  262. $signinterface = $signcommand;
  263. }
  264. if ($signcommand) {
  265. if ($signinterface !~ /^(gpg|pgp)$/) {
  266. warning(_g("unknown sign command, assuming pgp style interface"));
  267. } elsif ($signinterface eq 'pgp') {
  268. if ($signsource or $signchanges) {
  269. warning(_g("PGP support is deprecated (see README.feature-removal-schedule)"));
  270. }
  271. }
  272. }
  273. my $build_opts = Dpkg::BuildOptions->new();
  274. if (defined $parallel) {
  275. $parallel = $build_opts->get("parallel") if $build_opts->has("parallel");
  276. $ENV{MAKEFLAGS} ||= '';
  277. $ENV{MAKEFLAGS} .= " -j$parallel";
  278. $build_opts->set("parallel", $parallel);
  279. $build_opts->export();
  280. }
  281. my $build_flags = Dpkg::BuildFlags->new();
  282. $build_flags->load_config();
  283. foreach my $flag ($build_flags->list()) {
  284. printf(_g("%s: dpkg-buildflags (origin: %s): %s = %s\n"), $progname,
  285. $build_flags->get_origin($flag), $flag, $build_flags->get($flag));
  286. }
  287. my $cwd = cwd();
  288. my $dir = basename($cwd);
  289. my $changelog = changelog_parse();
  290. my $pkg = mustsetvar($changelog->{source}, _g('source package'));
  291. my $version = mustsetvar($changelog->{version}, _g('source version'));
  292. my ($ok, $error) = version_check($version);
  293. error($error) unless $ok;
  294. my $maintainer;
  295. if ($changedby) {
  296. $maintainer = $changedby;
  297. } elsif ($maint) {
  298. $maintainer = $maint;
  299. } else {
  300. $maintainer = mustsetvar($changelog->{maintainer}, _g('source changed by'));
  301. }
  302. open my $arch_env, '-|', 'dpkg-architecture', "-a$targetarch",
  303. "-t$targetgnusystem", '-s', '-f' or subprocerr('dpkg-architecture');
  304. while ($_ = <$arch_env>) {
  305. chomp;
  306. my @cmds = split /\s*;\s*/;
  307. foreach (@cmds) {
  308. /^\s*(\w+)=([\w-]*)\s*$/ && do {
  309. $ENV{$1} = $2;
  310. };
  311. }
  312. }
  313. close $arch_env or subprocerr('dpkg-architecture');
  314. my $arch;
  315. unless (build_sourceonly) {
  316. $arch = mustsetvar($ENV{'DEB_HOST_ARCH'}, _g('host architecture'));
  317. } else {
  318. $arch = 'source';
  319. }
  320. # Preparation of environment stops here
  321. (my $sversion = $version) =~ s/^\d+://;
  322. my $pv = "${pkg}_$sversion";
  323. my $pva = "${pkg}_${sversion}_$arch";
  324. if (not -x "debian/rules") {
  325. warning(_g("debian/rules is not executable: fixing that."));
  326. chmod(0755, "debian/rules"); # No checks of failures, non fatal
  327. }
  328. unless ($call_target) {
  329. chdir('..') or syserr('chdir ..');
  330. withecho('dpkg-source', @source_opts, '--before-build', $dir);
  331. chdir($dir) or syserr("chdir $dir");
  332. }
  333. if ($checkbuilddep) {
  334. if ($admindir) {
  335. push @checkbuilddep_opts, "--admindir=$admindir";
  336. }
  337. system('dpkg-checkbuilddeps', @checkbuilddep_opts);
  338. if (not WIFEXITED($?)) {
  339. subprocerr('dpkg-checkbuilddeps');
  340. } elsif (WEXITSTATUS($?)) {
  341. warning(_g("Build dependencies/conflicts unsatisfied; aborting."));
  342. warning(_g("(Use -d flag to override.)"));
  343. if (build_sourceonly) {
  344. warning(_g("This is currently a non-fatal warning with -S, but"));
  345. warning(_g("will probably become fatal in the future."));
  346. } else {
  347. exit 3;
  348. }
  349. }
  350. }
  351. if ($call_target) {
  352. if ($call_target_as_root or
  353. $call_target =~ /^(clean|binary(|-arch|-indep))$/)
  354. {
  355. withecho(@rootcommand, @debian_rules, $call_target);
  356. } else {
  357. withecho(@debian_rules, $call_target);
  358. }
  359. exit 0;
  360. }
  361. unless ($noclean) {
  362. withecho(@rootcommand, @debian_rules, 'clean');
  363. }
  364. unless (build_binaryonly) {
  365. warning(_g("it is a bad idea to generate a source package " .
  366. "without cleaning up first, it might contain undesired " .
  367. "files.")) if $noclean;
  368. chdir('..') or syserr('chdir ..');
  369. withecho('dpkg-source', @source_opts, '-b', $dir);
  370. chdir($dir) or syserr("chdir $dir");
  371. }
  372. unless (build_sourceonly) {
  373. withecho(@debian_rules, $buildtarget);
  374. withecho(@rootcommand, @debian_rules, $binarytarget);
  375. }
  376. if ($usepause &&
  377. ($signchanges || (!build_binaryonly && $signsource))) {
  378. print _g("Press the return key to start signing process\n");
  379. getc();
  380. }
  381. my $signerrors;
  382. unless (build_binaryonly) {
  383. if ($signsource && signfile("$pv.dsc")) {
  384. $signerrors = _g("Failed to sign .dsc and .changes file");
  385. $signchanges = 0;
  386. }
  387. }
  388. if (defined($maint)) { push @changes_opts, "-m$maint" }
  389. if (defined($changedby)) { push @changes_opts, "-e$changedby" }
  390. if (defined($since)) { push @changes_opts, "-v$since" }
  391. if (defined($desc)) { push @changes_opts, "-C$desc" }
  392. my $chg = "../$pva.changes";
  393. print STDERR " dpkg-genchanges @changes_opts >$chg\n";
  394. open CHANGES, '-|', 'dpkg-genchanges', @changes_opts
  395. or subprocerr('dpkg-genchanges');
  396. open OUT, '>', $chg or syserr(_g('write changes file'));
  397. my $infiles = my $files = '';
  398. while ($_ = <CHANGES>) {
  399. print OUT $_ or syserr(_g('write changes file'));
  400. chomp;
  401. if (/^Files:/i) {
  402. $infiles = 1;
  403. } elsif ($infiles && /^\s+(.*)$/) {
  404. $files .= " $1 ";
  405. } elsif ($infiles && /^\S/) {
  406. $infiles = 0;
  407. }
  408. }
  409. close CHANGES or subprocerr(_g('dpkg-genchanges'));
  410. close OUT or syserr(_g('write changes file'));
  411. my $srcmsg;
  412. sub fileomitted($) { return $files !~ /$_[0]/ }
  413. my $ext = $compression_re_file_ext;
  414. if (fileomitted '\.deb') {
  415. # source only upload
  416. if (fileomitted "\.diff\.$ext" and fileomitted "\.debian\.tar\.$ext") {
  417. $srcmsg = _g('source only upload: Debian-native package');
  418. } elsif (fileomitted "\.orig\.tar\.$ext") {
  419. $srcmsg = _g('source only, diff-only upload (original source NOT included)');
  420. } else {
  421. $srcmsg = _g('source only upload (original source is included)');
  422. }
  423. } else {
  424. $srcmsg = _g('full upload (original source is included)');
  425. if (fileomitted '\.dsc') {
  426. $srcmsg = _g('binary only upload (no source included)');
  427. } elsif (fileomitted "\.diff\.$ext" and fileomitted "\.debian\.tar\.$ext") {
  428. $srcmsg = _g('full upload; Debian-native package (full source is included)');
  429. } elsif (fileomitted "\.orig\.tar\.$ext") {
  430. $srcmsg = _g('binary and diff upload (original source NOT included)');
  431. } else {
  432. $srcmsg = _g('full upload (original source is included)');
  433. }
  434. }
  435. if ($signchanges && signfile("$pva.changes")) {
  436. $signerrors = _g("Failed to sign .changes file");
  437. }
  438. if ($cleansource) {
  439. withecho(@rootcommand, @debian_rules, 'clean');
  440. }
  441. chdir('..') or syserr('chdir ..');
  442. withecho('dpkg-source', @source_opts, '--after-build', $dir);
  443. chdir($dir) or syserr("chdir $dir");
  444. print "$progname: $srcmsg\n";
  445. if ($signerrors) {
  446. warning($signerrors);
  447. exit 1;
  448. }
  449. sub mustsetvar {
  450. my ($var, $text) = @_;
  451. error(_g("unable to determine %s"), $text)
  452. unless defined($var);
  453. print "$progname: $text $var\n";
  454. return $var;
  455. }
  456. sub withecho {
  457. shift while !$_[0];
  458. print STDERR " @_\n";
  459. system(@_)
  460. and subprocerr("@_");
  461. }
  462. sub signfile {
  463. my ($file) = @_;
  464. print STDERR " signfile $file\n";
  465. my $qfile = quotemeta($file);
  466. if ($signinterface eq 'gpg') {
  467. system("(cat ../$qfile ; echo '') | ".
  468. "$signcommand --utf8-strings --local-user "
  469. .quotemeta($signkey||$maintainer).
  470. " --clearsign --armor --textmode > ../$qfile.asc");
  471. } else {
  472. system("$signcommand -u ".quotemeta($signkey||$maintainer).
  473. " +clearsig=on -fast <../$qfile >../$qfile.asc");
  474. }
  475. my $status = $?;
  476. unless ($status) {
  477. system('mv', '--', "../$file.asc", "../$file")
  478. and subprocerr('mv');
  479. } else {
  480. system('rm', '-f', "../$file.asc")
  481. and subprocerr('rm -f');
  482. }
  483. print "\n";
  484. return $status
  485. }