dpkg-buildpackage.pl 15 KB

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