dpkg-buildpackage.pl 16 KB

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