dpkg-buildpackage.pl 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  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. $ENV{$flag} = $build_flags->get($flag);
  285. printf(_g("%s: export %s from dpkg-buildflags (origin: %s): %s\n"),
  286. $progname, $flag, $build_flags->get_origin($flag), $ENV{$flag});
  287. }
  288. my $cwd = cwd();
  289. my $dir = basename($cwd);
  290. my $changelog = changelog_parse();
  291. my $pkg = mustsetvar($changelog->{source}, _g('source package'));
  292. my $version = mustsetvar($changelog->{version}, _g('source version'));
  293. my ($ok, $error) = version_check($version);
  294. error($error) unless $ok;
  295. my $maintainer;
  296. if ($changedby) {
  297. $maintainer = $changedby;
  298. } elsif ($maint) {
  299. $maintainer = $maint;
  300. } else {
  301. $maintainer = mustsetvar($changelog->{maintainer}, _g('source changed by'));
  302. }
  303. open my $arch_env, '-|', 'dpkg-architecture', "-a$targetarch",
  304. "-t$targetgnusystem", '-s', '-f' or subprocerr('dpkg-architecture');
  305. while ($_ = <$arch_env>) {
  306. chomp;
  307. my @cmds = split /\s*;\s*/;
  308. foreach (@cmds) {
  309. /^\s*(\w+)=([\w-]*)\s*$/ && do {
  310. $ENV{$1} = $2;
  311. };
  312. }
  313. }
  314. close $arch_env or subprocerr('dpkg-architecture');
  315. my $arch;
  316. unless (build_sourceonly) {
  317. $arch = mustsetvar($ENV{'DEB_HOST_ARCH'}, _g('host architecture'));
  318. } else {
  319. $arch = 'source';
  320. }
  321. # Preparation of environment stops here
  322. (my $sversion = $version) =~ s/^\d+://;
  323. my $pv = "${pkg}_$sversion";
  324. my $pva = "${pkg}_${sversion}_$arch";
  325. if (not -x "debian/rules") {
  326. warning(_g("debian/rules is not executable: fixing that."));
  327. chmod(0755, "debian/rules"); # No checks of failures, non fatal
  328. }
  329. unless ($call_target) {
  330. chdir('..') or syserr('chdir ..');
  331. withecho('dpkg-source', @source_opts, '--before-build', $dir);
  332. chdir($dir) or syserr("chdir $dir");
  333. }
  334. if ($checkbuilddep) {
  335. if ($admindir) {
  336. push @checkbuilddep_opts, "--admindir=$admindir";
  337. }
  338. system('dpkg-checkbuilddeps', @checkbuilddep_opts);
  339. if (not WIFEXITED($?)) {
  340. subprocerr('dpkg-checkbuilddeps');
  341. } elsif (WEXITSTATUS($?)) {
  342. warning(_g("Build dependencies/conflicts unsatisfied; aborting."));
  343. warning(_g("(Use -d flag to override.)"));
  344. if (build_sourceonly) {
  345. warning(_g("This is currently a non-fatal warning with -S, but"));
  346. warning(_g("will probably become fatal in the future."));
  347. } else {
  348. exit 3;
  349. }
  350. }
  351. }
  352. if ($call_target) {
  353. if ($call_target_as_root or
  354. $call_target =~ /^(clean|binary(|-arch|-indep))$/)
  355. {
  356. withecho(@rootcommand, @debian_rules, $call_target);
  357. } else {
  358. withecho(@debian_rules, $call_target);
  359. }
  360. exit 0;
  361. }
  362. unless ($noclean) {
  363. withecho(@rootcommand, @debian_rules, 'clean');
  364. }
  365. unless (build_binaryonly) {
  366. warning(_g("it is a bad idea to generate a source package " .
  367. "without cleaning up first, it might contain undesired " .
  368. "files.")) if $noclean;
  369. chdir('..') or syserr('chdir ..');
  370. withecho('dpkg-source', @source_opts, '-b', $dir);
  371. chdir($dir) or syserr("chdir $dir");
  372. }
  373. unless (build_sourceonly) {
  374. withecho(@debian_rules, $buildtarget);
  375. withecho(@rootcommand, @debian_rules, $binarytarget);
  376. }
  377. if ($usepause &&
  378. ($signchanges || (!build_binaryonly && $signsource))) {
  379. print _g("Press the return key to start signing process\n");
  380. getc();
  381. }
  382. my $signerrors;
  383. unless (build_binaryonly) {
  384. if ($signsource && signfile("$pv.dsc")) {
  385. $signerrors = _g("Failed to sign .dsc and .changes file");
  386. $signchanges = 0;
  387. }
  388. }
  389. if (defined($maint)) { push @changes_opts, "-m$maint" }
  390. if (defined($changedby)) { push @changes_opts, "-e$changedby" }
  391. if (defined($since)) { push @changes_opts, "-v$since" }
  392. if (defined($desc)) { push @changes_opts, "-C$desc" }
  393. my $chg = "../$pva.changes";
  394. print STDERR " dpkg-genchanges @changes_opts >$chg\n";
  395. open CHANGES, '-|', 'dpkg-genchanges', @changes_opts
  396. or subprocerr('dpkg-genchanges');
  397. open OUT, '>', $chg or syserr(_g('write changes file'));
  398. my $infiles = my $files = '';
  399. while ($_ = <CHANGES>) {
  400. print OUT $_ or syserr(_g('write changes file'));
  401. chomp;
  402. if (/^Files:/i) {
  403. $infiles = 1;
  404. } elsif ($infiles && /^\s+(.*)$/) {
  405. $files .= " $1 ";
  406. } elsif ($infiles && /^\S/) {
  407. $infiles = 0;
  408. }
  409. }
  410. close CHANGES or subprocerr(_g('dpkg-genchanges'));
  411. close OUT or syserr(_g('write changes file'));
  412. my $srcmsg;
  413. sub fileomitted($) { return $files !~ /$_[0]/ }
  414. my $ext = $compression_re_file_ext;
  415. if (fileomitted '\.deb') {
  416. # source only upload
  417. if (fileomitted "\.diff\.$ext" and fileomitted "\.debian\.tar\.$ext") {
  418. $srcmsg = _g('source only upload: Debian-native package');
  419. } elsif (fileomitted "\.orig\.tar\.$ext") {
  420. $srcmsg = _g('source only, diff-only upload (original source NOT included)');
  421. } else {
  422. $srcmsg = _g('source only upload (original source is included)');
  423. }
  424. } else {
  425. $srcmsg = _g('full upload (original source is included)');
  426. if (fileomitted '\.dsc') {
  427. $srcmsg = _g('binary only upload (no source included)');
  428. } elsif (fileomitted "\.diff\.$ext" and fileomitted "\.debian\.tar\.$ext") {
  429. $srcmsg = _g('full upload; Debian-native package (full source is included)');
  430. } elsif (fileomitted "\.orig\.tar\.$ext") {
  431. $srcmsg = _g('binary and diff upload (original source NOT included)');
  432. } else {
  433. $srcmsg = _g('full upload (original source is included)');
  434. }
  435. }
  436. if ($signchanges && signfile("$pva.changes")) {
  437. $signerrors = _g("Failed to sign .changes file");
  438. }
  439. if ($cleansource) {
  440. withecho(@rootcommand, @debian_rules, 'clean');
  441. }
  442. chdir('..') or syserr('chdir ..');
  443. withecho('dpkg-source', @source_opts, '--after-build', $dir);
  444. chdir($dir) or syserr("chdir $dir");
  445. print "$progname: $srcmsg\n";
  446. if ($signerrors) {
  447. warning($signerrors);
  448. exit 1;
  449. }
  450. sub mustsetvar {
  451. my ($var, $text) = @_;
  452. error(_g("unable to determine %s"), $text)
  453. unless defined($var);
  454. print "$progname: $text $var\n";
  455. return $var;
  456. }
  457. sub withecho {
  458. shift while !$_[0];
  459. print STDERR " @_\n";
  460. system(@_)
  461. and subprocerr("@_");
  462. }
  463. sub signfile {
  464. my ($file) = @_;
  465. print STDERR " signfile $file\n";
  466. my $qfile = quotemeta($file);
  467. if ($signinterface eq 'gpg') {
  468. system("(cat ../$qfile ; echo '') | ".
  469. "$signcommand --utf8-strings --local-user "
  470. .quotemeta($signkey||$maintainer).
  471. " --clearsign --armor --textmode > ../$qfile.asc");
  472. } else {
  473. system("$signcommand -u ".quotemeta($signkey||$maintainer).
  474. " +clearsig=on -fast <../$qfile >../$qfile.asc");
  475. }
  476. my $status = $?;
  477. unless ($status) {
  478. system('mv', '--', "../$file.asc", "../$file")
  479. and subprocerr('mv');
  480. } else {
  481. system('rm', '-f', "../$file.asc")
  482. and subprocerr('rm -f');
  483. }
  484. print "\n";
  485. return $status
  486. }