dpkg-buildpackage.pl 15 KB

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