dpkg-buildpackage.pl 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  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. -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, $binaryonly, $sourceonly, $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. while (@ARGV) {
  115. $_ = shift @ARGV;
  116. if (/^(--help|-h)$/) {
  117. usage;
  118. exit 0;
  119. } elsif (/^--version$/) {
  120. showversion;
  121. exit 0;
  122. } elsif (/^--admindir$/) {
  123. $admindir = shift @ARGV;
  124. } elsif (/^--admindir=(.*)$/) {
  125. $admindir = $1;
  126. } elsif (/^--source-option=(.*)$/) {
  127. push @source_opts, $1;
  128. } elsif (/^--changes-option=(.*)$/) {
  129. push @changes_opts, $1;
  130. } elsif (/^-j(\d*)$/) {
  131. $parallel = $1 || '';
  132. } elsif (/^-r(.*)$/) {
  133. @rootcommand = split /\s+/, $1;
  134. } elsif (/^-p(.*)$/) {
  135. $signcommand = $1;
  136. } elsif (/^-k(.*)$/) {
  137. $signkey = $1;
  138. } elsif (/^-([dD])$/) {
  139. $checkbuilddep = ($1 eq 'D');
  140. } elsif (/^-s(gpg|pgp)$/) {
  141. $forcesigninterface = $1;
  142. } elsif (/^-us$/) {
  143. $signsource = 0;
  144. } elsif (/^-uc$/) {
  145. $signchanges = 0;
  146. } elsif (/^-ap$/) {
  147. $usepause = 1;
  148. } elsif (/^-a(.*)$/) {
  149. $targetarch = $1;
  150. $checkbuilddep = 0;
  151. } elsif (/^-s[iad]$/) {
  152. push @changes_opts, $_;
  153. } elsif (/^-(?:s[insAkurKUR]|[zZ].*|i.*|I.*)$/) {
  154. push @source_opts, $_; # passed to dpkg-source
  155. } elsif (/^-tc$/) {
  156. $cleansource = 1;
  157. } elsif (/^-t(.*)$/) {
  158. $targetgnusystem = $1; # Order DOES matter!
  159. } elsif (/^(--target|-T)$/) {
  160. $call_target = shift @ARGV;
  161. } elsif (/^(--target=|-T)(.+)$/) {
  162. $call_target = $2;
  163. } elsif (/^--as-root$/) {
  164. $call_target_as_root = 1;
  165. } elsif (/^-nc$/) {
  166. $noclean = 1;
  167. } elsif (/^-b$/) {
  168. $binaryonly = '-b';
  169. push @changes_opts, '-b';
  170. @checkbuilddep_opts = ();
  171. $binarytarget = 'binary';
  172. } elsif (/^-B$/) {
  173. $binaryonly = '-B';
  174. push @changes_opts, '-B';
  175. @checkbuilddep_opts = ('-B');
  176. $binarytarget = 'binary-arch';
  177. } elsif (/^-A$/) {
  178. $binaryonly = '-A';
  179. push @changes_opts, '-A';
  180. @checkbuilddep_opts = ();
  181. $binarytarget = 'binary-indep';
  182. } elsif (/^-S$/) {
  183. $sourceonly = '-S';
  184. push @changes_opts, '-S';
  185. @checkbuilddep_opts = ('-B');
  186. } elsif (/^-v(.*)$/) {
  187. $since = $1;
  188. } elsif (/^-m(.*)$/) {
  189. $maint = $1;
  190. } elsif (/^-e(.*)$/) {
  191. $changedby = $1;
  192. } elsif (/^-C(.*)$/) {
  193. $desc = $1;
  194. } elsif (m/^-[EW]$/) {
  195. # Deprecated option
  196. warning(_g("-E and -W are deprecated, they are without effect"));
  197. } elsif (/^-R(.*)$/) {
  198. @debian_rules = split /\s+/, $1;
  199. } else {
  200. usageerr(_g("unknown option or argument %s"), $_);
  201. }
  202. }
  203. if ($binaryonly and $sourceonly) {
  204. usageerr(_g("cannot combine %s and %s"), $binaryonly, $sourceonly);
  205. }
  206. if ($noclean) {
  207. # -nc without -b/-B/-A/-S implies -b
  208. $binaryonly = '-b' unless ($binaryonly or $sourceonly);
  209. }
  210. if ($< == 0) {
  211. warning(_g("using a gain-root-command while being root")) if (@rootcommand);
  212. } else {
  213. push @rootcommand, "fakeroot" unless @rootcommand;
  214. if (!find_command($rootcommand[0])) {
  215. if ($rootcommand[0] eq 'fakeroot') {
  216. error(_g("fakeroot not found, either install the fakeroot\n" .
  217. "package, specify a command with the -r option, " .
  218. "or run this as root"));
  219. } else {
  220. error(_g("gain-root-commmand '%s' not found"), $rootcommand[0]);
  221. }
  222. }
  223. }
  224. unless ($signcommand) {
  225. $signsource = 0;
  226. $signchanges = 0;
  227. }
  228. my $signinterface;
  229. if ($forcesigninterface) {
  230. $signinterface = $forcesigninterface;
  231. } else {
  232. $signinterface = $signcommand;
  233. }
  234. if ($signcommand) {
  235. if ($signinterface !~ /^(gpg|pgp)$/) {
  236. warning(_g("unknown sign command, assuming pgp style interface"));
  237. } elsif ($signinterface eq 'pgp') {
  238. if ($signsource or $signchanges) {
  239. warning(_g("PGP support is deprecated (see README.feature-removal-schedule)"));
  240. }
  241. }
  242. }
  243. my $build_opts = Dpkg::BuildOptions->new();
  244. if (defined $parallel) {
  245. $parallel = $build_opts->get("parallel") if $build_opts->has("parallel");
  246. $ENV{MAKEFLAGS} ||= '';
  247. $ENV{MAKEFLAGS} .= " -j$parallel";
  248. $build_opts->set("parallel", $parallel);
  249. $build_opts->export();
  250. }
  251. my $build_flags = Dpkg::BuildFlags->new();
  252. $build_flags->load_config();
  253. foreach my $flag ($build_flags->list()) {
  254. $ENV{$flag} = $build_flags->get($flag);
  255. printf(_g("%s: export %s from dpkg-buildflags (origin: %s): %s\n"),
  256. $progname, $flag, $build_flags->get_origin($flag), $ENV{$flag});
  257. }
  258. my $cwd = cwd();
  259. my $dir = basename($cwd);
  260. my $changelog = changelog_parse();
  261. my $pkg = mustsetvar($changelog->{source}, _g('source package'));
  262. my $version = mustsetvar($changelog->{version}, _g('source version'));
  263. my ($ok, $error) = version_check($version);
  264. error($error) unless $ok;
  265. my $maintainer;
  266. if ($changedby) {
  267. $maintainer = $changedby;
  268. } elsif ($maint) {
  269. $maintainer = $maint;
  270. } else {
  271. $maintainer = mustsetvar($changelog->{maintainer}, _g('source changed by'));
  272. }
  273. open my $arch_env, '-|', 'dpkg-architecture', "-a$targetarch",
  274. "-t$targetgnusystem", '-s', '-f' or subprocerr('dpkg-architecture');
  275. while ($_ = <$arch_env>) {
  276. chomp;
  277. my @cmds = split /\s*;\s*/;
  278. foreach (@cmds) {
  279. /^\s*(\w+)=([\w-]*)\s*$/ && do {
  280. $ENV{$1} = $2;
  281. };
  282. }
  283. }
  284. close $arch_env or subprocerr('dpkg-architecture');
  285. my $arch;
  286. unless ($sourceonly) {
  287. $arch = mustsetvar($ENV{'DEB_HOST_ARCH'}, _g('host architecture'));
  288. } else {
  289. $arch = 'source';
  290. }
  291. # Preparation of environment stops here
  292. (my $sversion = $version) =~ s/^\d+://;
  293. my $pv = "${pkg}_$sversion";
  294. my $pva = "${pkg}_${sversion}_$arch";
  295. if (not -x "debian/rules") {
  296. warning(_g("debian/rules is not executable: fixing that."));
  297. chmod(0755, "debian/rules"); # No checks of failures, non fatal
  298. }
  299. unless ($call_target) {
  300. chdir('..') or syserr('chdir ..');
  301. withecho('dpkg-source', @source_opts, '--before-build', $dir);
  302. chdir($dir) or syserr("chdir $dir");
  303. }
  304. if ($checkbuilddep) {
  305. if ($admindir) {
  306. push @checkbuilddep_opts, "--admindir=$admindir";
  307. }
  308. system('dpkg-checkbuilddeps', @checkbuilddep_opts);
  309. if (not WIFEXITED($?)) {
  310. subprocerr('dpkg-checkbuilddeps');
  311. } elsif (WEXITSTATUS($?)) {
  312. warning(_g("Build dependencies/conflicts unsatisfied; aborting."));
  313. warning(_g("(Use -d flag to override.)"));
  314. if ($sourceonly) {
  315. warning(_g("This is currently a non-fatal warning with -S, but"));
  316. warning(_g("will probably become fatal in the future."));
  317. } else {
  318. exit 3;
  319. }
  320. }
  321. }
  322. if ($call_target) {
  323. if ($call_target_as_root or
  324. $call_target =~ /^(clean|binary(|-arch|-indep))$/)
  325. {
  326. withecho(@rootcommand, @debian_rules, $call_target);
  327. } else {
  328. withecho(@debian_rules, $call_target);
  329. }
  330. exit 0;
  331. }
  332. unless ($noclean) {
  333. withecho(@rootcommand, @debian_rules, 'clean');
  334. }
  335. unless ($binaryonly) {
  336. warning(_g("it is a bad idea to generate a source package " .
  337. "without cleaning up first, it might contain undesired " .
  338. "files.")) if $noclean;
  339. chdir('..') or syserr('chdir ..');
  340. withecho('dpkg-source', @source_opts, '-b', $dir);
  341. chdir($dir) or syserr("chdir $dir");
  342. }
  343. unless ($sourceonly) {
  344. withecho(@debian_rules, 'build');
  345. withecho(@rootcommand, @debian_rules, $binarytarget);
  346. }
  347. if ($usepause &&
  348. ($signchanges || ( !$binaryonly && $signsource )) ) {
  349. print _g("Press the return key to start signing process\n");
  350. getc();
  351. }
  352. my $signerrors;
  353. unless ($binaryonly) {
  354. if ($signsource && signfile("$pv.dsc")) {
  355. $signerrors = _g("Failed to sign .dsc and .changes file");
  356. $signchanges = 0;
  357. }
  358. }
  359. if (defined($maint)) { push @changes_opts, "-m$maint" }
  360. if (defined($changedby)) { push @changes_opts, "-e$changedby" }
  361. if (defined($since)) { push @changes_opts, "-v$since" }
  362. if (defined($desc)) { push @changes_opts, "-C$desc" }
  363. my $chg = "../$pva.changes";
  364. print STDERR " dpkg-genchanges @changes_opts >$chg\n";
  365. open CHANGES, '-|', 'dpkg-genchanges', @changes_opts
  366. or subprocerr('dpkg-genchanges');
  367. open OUT, '>', $chg or syserr(_g('write changes file'));
  368. my $infiles = my $files = '';
  369. while ($_ = <CHANGES>) {
  370. print OUT $_ or syserr(_g('write changes file'));
  371. chomp;
  372. if (/^Files:/i) {
  373. $infiles = 1;
  374. } elsif ($infiles && /^\s+(.*)$/) {
  375. $files .= " $1 ";
  376. } elsif ($infiles && /^\S/) {
  377. $infiles = 0;
  378. }
  379. }
  380. close CHANGES or subprocerr(_g('dpkg-genchanges'));
  381. close OUT or syserr(_g('write changes file'));
  382. my $srcmsg;
  383. sub fileomitted($) { return $files !~ /$_[0]/ }
  384. my $ext = $compression_re_file_ext;
  385. if (fileomitted '\.deb') {
  386. # source only upload
  387. if (fileomitted "\.diff\.$ext" and fileomitted "\.debian\.tar\.$ext") {
  388. $srcmsg = _g('source only upload: Debian-native package');
  389. } elsif (fileomitted "\.orig\.tar\.$ext") {
  390. $srcmsg = _g('source only, diff-only upload (original source NOT included)');
  391. } else {
  392. $srcmsg = _g('source only upload (original source is included)');
  393. }
  394. } else {
  395. $srcmsg = _g('full upload (original source is included)');
  396. if (fileomitted '\.dsc') {
  397. $srcmsg = _g('binary only upload (no source included)');
  398. } elsif (fileomitted "\.diff\.$ext" and fileomitted "\.debian\.tar\.$ext") {
  399. $srcmsg = _g('full upload; Debian-native package (full source is included)');
  400. } elsif (fileomitted "\.orig\.tar\.$ext") {
  401. $srcmsg = _g('binary and diff upload (original source NOT included)');
  402. } else {
  403. $srcmsg = _g('full upload (original source is included)');
  404. }
  405. }
  406. if ($signchanges && signfile("$pva.changes")) {
  407. $signerrors = _g("Failed to sign .changes file");
  408. }
  409. if ($cleansource) {
  410. withecho(@rootcommand, @debian_rules, 'clean');
  411. }
  412. chdir('..') or syserr('chdir ..');
  413. withecho('dpkg-source', @source_opts, '--after-build', $dir);
  414. chdir($dir) or syserr("chdir $dir");
  415. print "$progname: $srcmsg\n";
  416. if ($signerrors) {
  417. warning($signerrors);
  418. exit 1;
  419. }
  420. sub mustsetvar {
  421. my ($var, $text) = @_;
  422. error(_g("unable to determine %s"), $text)
  423. unless defined($var);
  424. print "$progname: $text $var\n";
  425. return $var;
  426. }
  427. sub withecho {
  428. shift while !$_[0];
  429. print STDERR " @_\n";
  430. system(@_)
  431. and subprocerr("@_");
  432. }
  433. sub signfile {
  434. my ($file) = @_;
  435. print STDERR " signfile $file\n";
  436. my $qfile = quotemeta($file);
  437. if ($signinterface eq 'gpg') {
  438. system("(cat ../$qfile ; echo '') | ".
  439. "$signcommand --utf8-strings --local-user "
  440. .quotemeta($signkey||$maintainer).
  441. " --clearsign --armor --textmode > ../$qfile.asc");
  442. } else {
  443. system("$signcommand -u ".quotemeta($signkey||$maintainer).
  444. " +clearsig=on -fast <../$qfile >../$qfile.asc");
  445. }
  446. my $status = $?;
  447. unless ($status) {
  448. system('mv', '--', "../$file.asc", "../$file")
  449. and subprocerr('mv');
  450. } else {
  451. system('rm', '-f', "../$file.asc")
  452. and subprocerr('rm -f');
  453. }
  454. print "\n";
  455. return $status
  456. }