dpkg-buildpackage.pl 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  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 || '';
  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 (defined $parallel) {
  240. $parallel = $build_opts->{parallel} if exists $build_opts->{parallel};
  241. $ENV{MAKEFLAGS} ||= '';
  242. $ENV{MAKEFLAGS} .= " -j$parallel";
  243. $build_opts->{parallel} = $parallel;
  244. Dpkg::BuildOptions::set($build_opts);
  245. }
  246. my $default_flags = exists $build_opts->{noopt} ? "-g -O0" : "-g -O2";
  247. my %flags = ( CPPFLAGS => '',
  248. CFLAGS => $default_flags,
  249. CXXFLAGS => $default_flags,
  250. FFLAGS => $default_flags,
  251. LDFLAGS => '',
  252. );
  253. foreach my $flag (keys %flags) {
  254. if ($ENV{$flag}) {
  255. printf(_g("%s: use %s from environment: %s\n"), $progname,
  256. $flag, $ENV{$flag});
  257. } else {
  258. $ENV{$flag} = $flags{$flag};
  259. printf(_g("%s: set %s to default value: %s\n"), $progname,
  260. $flag, $ENV{$flag});
  261. }
  262. if ($ENV{"${flag}_APPEND"}) {
  263. $ENV{$flag} .= " ".$ENV{"${flag}_APPEND"};
  264. }
  265. }
  266. my $cwd = cwd();
  267. my $dir = basename($cwd);
  268. my $changelog = changelog_parse();
  269. my $pkg = mustsetvar($changelog->{source}, _g('source package'));
  270. my $version = mustsetvar($changelog->{version}, _g('source version'));
  271. my ($ok, $error) = version_check($version);
  272. error($error) unless $ok;
  273. my $maintainer;
  274. if ($changedby) {
  275. $maintainer = $changedby;
  276. } elsif ($maint) {
  277. $maintainer = $maint;
  278. } else {
  279. $maintainer = mustsetvar($changelog->{maintainer}, _g('source changed by'));
  280. }
  281. open my $arch_env, '-|', 'dpkg-architecture', "-a$targetarch",
  282. "-t$targetgnusystem", '-s', '-f' or subprocerr('dpkg-architecture');
  283. while ($_ = <$arch_env>) {
  284. chomp;
  285. my @cmds = split /\s*;\s*/;
  286. foreach (@cmds) {
  287. /^\s*(\w+)=([\w-]*)\s*$/ && do {
  288. $ENV{$1} = $2;
  289. };
  290. }
  291. }
  292. close $arch_env or subprocerr('dpkg-architecture');
  293. # In case of cross-compilation, give sensible default search path
  294. # for some widely used tools
  295. $targetgnusystem = debarch_to_gnutriplet($targetarch) if $targetarch;
  296. if ($targetgnusystem and
  297. ($targetgnusystem ne debarch_to_gnutriplet(get_build_arch())))
  298. {
  299. $ENV{PKG_CONFIG_LIBDIR} ||= "/usr/$targetgnusystem/lib/pkgconfig/:" .
  300. "/usr/share/pkgconfig/";
  301. }
  302. my $arch;
  303. unless ($sourceonly) {
  304. $arch = mustsetvar($ENV{'DEB_HOST_ARCH'}, _g('host architecture'));
  305. } else {
  306. $arch = 'source';
  307. }
  308. # Preparation of environment stops here
  309. (my $sversion = $version) =~ s/^\d+://;
  310. my $pv = "${pkg}_$sversion";
  311. my $pva = "${pkg}_${sversion}_$arch";
  312. if (not -x "debian/rules") {
  313. warning(_g("debian/rules is not executable: fixing that."));
  314. chmod(0755, "debian/rules"); # No checks of failures, non fatal
  315. }
  316. if ($checkbuilddep) {
  317. if ($admindir) {
  318. push @checkbuilddep_args, "--admindir=$admindir";
  319. }
  320. system('dpkg-checkbuilddeps', @checkbuilddep_args);
  321. if (not WIFEXITED($?)) {
  322. subprocerr('dpkg-checkbuilddeps');
  323. } elsif (WEXITSTATUS($?)) {
  324. warning(_g("Build dependencies/conflicts unsatisfied; aborting."));
  325. warning(_g("(Use -d flag to override.)"));
  326. if ($sourceonly) {
  327. warning(_g("This is currently a non-fatal warning with -S, but"));
  328. warning(_g("will probably become fatal in the future."));
  329. } else {
  330. exit 3;
  331. }
  332. }
  333. }
  334. if ($call_target) {
  335. if ($call_target_as_root or
  336. $call_target =~ /^(clean|binary(|-arch|-indep))$/)
  337. {
  338. withecho(@rootcommand, @debian_rules, $call_target);
  339. } else {
  340. withecho(@debian_rules, $call_target);
  341. }
  342. exit 0;
  343. }
  344. unless ($noclean) {
  345. withecho(@rootcommand, @debian_rules, 'clean');
  346. }
  347. unless ($binaryonly) {
  348. warning(_g("it is a bad idea to generate a source package " .
  349. "without cleaning up first, it might contain undesired " .
  350. "files.")) if $noclean;
  351. chdir('..') or syserr('chdir ..');
  352. my @opts = @passopts;
  353. if ($diffignore) { push @opts, $diffignore }
  354. push @opts, @tarignore;
  355. withecho('dpkg-source', @opts, '-b', $dir);
  356. chdir($dir) or syserr("chdir $dir");
  357. }
  358. unless ($sourceonly) {
  359. withecho(@debian_rules, 'build');
  360. withecho(@rootcommand, @debian_rules, $binarytarget);
  361. }
  362. if ($usepause &&
  363. ($signchanges || ( !$binaryonly && $signsource )) ) {
  364. print _g("Press the return key to start signing process\n");
  365. getc();
  366. }
  367. my $signerrors;
  368. unless ($binaryonly) {
  369. if ($signsource && signfile("$pv.dsc")) {
  370. $signerrors = _g("Failed to sign .dsc and .changes file");
  371. $signchanges = 0;
  372. }
  373. }
  374. my @change_opts;
  375. if ($binaryonly) { push @change_opts, $binaryonly }
  376. if ($sourceonly) { push @change_opts, $sourceonly }
  377. if ($sourcestyle) { push @change_opts, $sourcestyle }
  378. if (defined($maint)) { push @change_opts, "-m$maint" }
  379. if (defined($changedby)) { push @change_opts, "-e$changedby" }
  380. if (defined($since)) { push @change_opts, "-v$since" }
  381. if (defined($desc)) { push @change_opts, "-C$desc" }
  382. my $chg = "../$pva.changes";
  383. print STDERR " dpkg-genchanges @change_opts >$chg\n";
  384. open CHANGES, '-|', 'dpkg-genchanges', @change_opts
  385. or subprocerr('dpkg-genchanges');
  386. open OUT, '>', $chg or syserr(_g('write changes file'));
  387. my $infiles = my $files = '';
  388. while ($_ = <CHANGES>) {
  389. print OUT $_ or syserr(_g('write changes file'));
  390. chomp;
  391. if (/^Files:/i) {
  392. $infiles = 1;
  393. } elsif ($infiles && /^\s+(.*)$/) {
  394. $files .= " $1 ";
  395. } elsif ($infiles && /^\S/) {
  396. $infiles = 0;
  397. }
  398. }
  399. close CHANGES or subprocerr(_g('dpkg-genchanges'));
  400. close OUT or syserr(_g('write changes file'));
  401. my $srcmsg;
  402. sub fileomitted($) { return $files !~ /$_[0]/ }
  403. my $ext = $compression_re_file_ext;
  404. if (fileomitted '\.deb') {
  405. # source only upload
  406. if (fileomitted "\.diff\.$ext" and fileomitted "\.debian\.tar\.$ext") {
  407. $srcmsg = _g('source only upload: Debian-native package');
  408. } elsif (fileomitted "\.orig\.tar\.$ext") {
  409. $srcmsg = _g('source only, diff-only upload (original source NOT included)');
  410. } else {
  411. $srcmsg = _g('source only upload (original source is included)');
  412. }
  413. } else {
  414. $srcmsg = _g('full upload (original source is included)');
  415. if (fileomitted '\.dsc') {
  416. $srcmsg = _g('binary only upload (no source included)');
  417. } elsif (fileomitted "\.diff\.$ext" and fileomitted "\.debian\.tar\.$ext") {
  418. $srcmsg = _g('full upload; Debian-native package (full source is included)');
  419. } elsif (fileomitted "\.orig\.tar\.$ext") {
  420. $srcmsg = _g('binary and diff upload (original source NOT included)');
  421. } else {
  422. $srcmsg = _g('full upload (original source is included)');
  423. }
  424. }
  425. if ($signchanges && signfile("$pva.changes")) {
  426. $signerrors = _g("Failed to sign .changes file");
  427. }
  428. if ($cleansource) {
  429. withecho(@rootcommand, @debian_rules, 'clean');
  430. }
  431. print "$progname: $srcmsg\n";
  432. if ($signerrors) {
  433. warning($signerrors);
  434. exit 1;
  435. }
  436. sub testcommand {
  437. my ($cmd) = @_;
  438. my $fullcmd = `which $cmd`;
  439. chomp $fullcmd;
  440. return $fullcmd && -x $fullcmd;
  441. }
  442. sub mustsetvar {
  443. my ($var, $text) = @_;
  444. error(_g("unable to determine %s"), $text)
  445. unless defined($var);
  446. print "$progname: $text $var\n";
  447. return $var;
  448. }
  449. sub withecho {
  450. shift while !$_[0];
  451. print STDERR " @_\n";
  452. system(@_)
  453. and subprocerr("@_");
  454. }
  455. sub signfile {
  456. my ($file) = @_;
  457. print STDERR " signfile $file\n";
  458. my $qfile = quotemeta($file);
  459. if ($signinterface eq 'gpg') {
  460. system("(cat ../$qfile ; echo '') | ".
  461. "$signcommand --utf8-strings --local-user "
  462. .quotemeta($signkey||$maintainer).
  463. " --clearsign --armor --textmode > ../$qfile.asc");
  464. } else {
  465. system("$signcommand -u ".quotemeta($signkey||$maintainer).
  466. " +clearsig=on -fast <../$qfile >../$qfile.asc");
  467. }
  468. my $status = $?;
  469. unless ($status) {
  470. system('mv', '--', "../$file.asc", "../$file")
  471. and subprocerr('mv');
  472. } else {
  473. system('rm', '-f', "../$file.asc")
  474. and subprocerr('rm -f');
  475. }
  476. print "\n";
  477. return $status
  478. }