dpkg-buildpackage.pl 16 KB

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