dpkg-buildpackage.pl 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  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. --source-option=<opt>
  83. pass option <opt> to dpkg-source
  84. --changes-option=<opt>
  85. pass option <opt> to dpkg-genchanges
  86. --admindir=<directory>
  87. change the administrative directory.
  88. -h, --help show this help message.
  89. --version show the version.
  90. "), $progname;
  91. }
  92. my @debian_rules = ("debian/rules");
  93. my @rootcommand = ();
  94. my $signcommand = '';
  95. if ( ( ($ENV{GNUPGHOME} && -e $ENV{GNUPGHOME})
  96. || ($ENV{HOME} && -e "$ENV{HOME}/.gnupg") )
  97. && testcommand('gpg')) {
  98. $signcommand = 'gpg';
  99. } elsif (testcommand('pgp')) {
  100. $signcommand = 'pgp'
  101. }
  102. my ($admindir, $signkey, $forcesigninterface, $usepause, $noclean,
  103. $cleansource, $binaryonly, $sourceonly, $since, $maint,
  104. $changedby, $desc, $parallel);
  105. my $checkbuilddep = 1;
  106. my $signsource = 1;
  107. my $signchanges = 1;
  108. my $binarytarget = 'binary';
  109. my $targetarch = my $targetgnusystem = '';
  110. my $call_target = '';
  111. my $call_target_as_root = 0;
  112. my (@checkbuilddep_opts, @changes_opts, @source_opts);
  113. while (@ARGV) {
  114. $_ = shift @ARGV;
  115. if (/^(--help|-h)$/) {
  116. usage;
  117. exit 0;
  118. } elsif (/^--version$/) {
  119. showversion;
  120. exit 0;
  121. } elsif (/^--admindir$/) {
  122. $admindir = shift @ARGV;
  123. } elsif (/^--admindir=(.*)$/) {
  124. $admindir = $1;
  125. } elsif (/^--source-option=(.*)$/) {
  126. push @source_opts, $1;
  127. } elsif (/^--changes-option=(.*)$/) {
  128. push @changes_opts, $1;
  129. } elsif (/^-j(\d*)$/) {
  130. $parallel = $1 || '';
  131. } elsif (/^-r(.*)$/) {
  132. @rootcommand = split /\s+/, $1;
  133. } elsif (/^-p(.*)$/) {
  134. $signcommand = $1;
  135. } elsif (/^-k(.*)$/) {
  136. $signkey = $1;
  137. } elsif (/^-([dD])$/) {
  138. $checkbuilddep = ($1 eq 'D');
  139. } elsif (/^-s(gpg|pgp)$/) {
  140. $forcesigninterface = $1;
  141. } elsif (/^-us$/) {
  142. $signsource = 0;
  143. } elsif (/^-uc$/) {
  144. $signchanges = 0;
  145. } elsif (/^-ap$/) {
  146. $usepause = 1;
  147. } elsif (/^-a(.*)$/) {
  148. $targetarch = $1;
  149. $checkbuilddep = 0;
  150. } elsif (/^-s[iad]$/) {
  151. push @changes_opts, $_;
  152. } elsif (/^-(?:s[insAkurKUR]|[zZ].*|i.*|I.*)$/) {
  153. push @source_opts, $_; # passed to dpkg-source
  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. push @changes_opts, '-b';
  169. @checkbuilddep_opts = ();
  170. $binarytarget = 'binary';
  171. } elsif (/^-B$/) {
  172. $binaryonly = '-B';
  173. push @changes_opts, '-B';
  174. @checkbuilddep_opts = ('-B');
  175. $binarytarget = 'binary-arch';
  176. } elsif (/^-A$/) {
  177. $binaryonly = '-A';
  178. push @changes_opts, '-A';
  179. @checkbuilddep_opts = ();
  180. $binarytarget = 'binary-indep';
  181. } elsif (/^-S$/) {
  182. $sourceonly = '-S';
  183. push @changes_opts, '-S';
  184. @checkbuilddep_opts = ('-B');
  185. } elsif (/^-v(.*)$/) {
  186. $since = $1;
  187. } elsif (/^-m(.*)$/) {
  188. $maint = $1;
  189. } elsif (/^-e(.*)$/) {
  190. $changedby = $1;
  191. } elsif (/^-C(.*)$/) {
  192. $desc = $1;
  193. } elsif (m/^-[EW]$/) {
  194. # Deprecated option
  195. warning(_g("-E and -W are deprecated, they are without effect"));
  196. } elsif (/^-R(.*)$/) {
  197. @debian_rules = split /\s+/, $1;
  198. } else {
  199. usageerr(_g("unknown option or argument %s"), $_);
  200. }
  201. }
  202. if ($binaryonly and $sourceonly) {
  203. usageerr(_g("cannot combine %s and %s"), $binaryonly, $sourceonly);
  204. }
  205. if ($noclean) {
  206. # -nc without -b/-B/-A/-S implies -b
  207. $binaryonly = '-b' unless ($binaryonly or $sourceonly);
  208. }
  209. if ($< == 0) {
  210. warning(_g("using a gain-root-command while being root")) if (@rootcommand);
  211. } else {
  212. push @rootcommand, "fakeroot" unless @rootcommand;
  213. if (!testcommand($rootcommand[0])) {
  214. if ($rootcommand[0] eq 'fakeroot') {
  215. error(_g("fakeroot not found, either install the fakeroot\n" .
  216. "package, specify a command with the -r option, " .
  217. "or run this as root"));
  218. } else {
  219. error(_g("gain-root-commmand '%s' not found"), $rootcommand[0]);
  220. }
  221. }
  222. }
  223. unless ($signcommand) {
  224. $signsource = 0;
  225. $signchanges = 0;
  226. }
  227. my $signinterface;
  228. if ($forcesigninterface) {
  229. $signinterface = $forcesigninterface;
  230. } else {
  231. $signinterface = $signcommand;
  232. }
  233. if ($signcommand) {
  234. if ($signinterface !~ /^(gpg|pgp)$/) {
  235. warning(_g("unknown sign command, assuming pgp style interface"));
  236. } elsif ($signinterface eq 'pgp') {
  237. if ($signsource or $signchanges) {
  238. warning(_g("PGP support is deprecated (see README.feature-removal-schedule)"));
  239. }
  240. }
  241. }
  242. my $build_opts = Dpkg::BuildOptions->new();
  243. if (defined $parallel) {
  244. $parallel = $build_opts->get("parallel") if $build_opts->has("parallel");
  245. $ENV{MAKEFLAGS} ||= '';
  246. $ENV{MAKEFLAGS} .= " -j$parallel";
  247. $build_opts->set("parallel", $parallel);
  248. $build_opts->export();
  249. }
  250. my $default_flags = $build_opts->has("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_opts, "--admindir=$admindir";
  323. }
  324. system('dpkg-checkbuilddeps', @checkbuilddep_opts);
  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. withecho('dpkg-source', @source_opts, '-b', $dir);
  357. chdir($dir) or syserr("chdir $dir");
  358. }
  359. unless ($sourceonly) {
  360. withecho(@debian_rules, 'build');
  361. withecho(@rootcommand, @debian_rules, $binarytarget);
  362. }
  363. if ($usepause &&
  364. ($signchanges || ( !$binaryonly && $signsource )) ) {
  365. print _g("Press the return key to start signing process\n");
  366. getc();
  367. }
  368. my $signerrors;
  369. unless ($binaryonly) {
  370. if ($signsource && signfile("$pv.dsc")) {
  371. $signerrors = _g("Failed to sign .dsc and .changes file");
  372. $signchanges = 0;
  373. }
  374. }
  375. if (defined($maint)) { push @changes_opts, "-m$maint" }
  376. if (defined($changedby)) { push @changes_opts, "-e$changedby" }
  377. if (defined($since)) { push @changes_opts, "-v$since" }
  378. if (defined($desc)) { push @changes_opts, "-C$desc" }
  379. my $chg = "../$pva.changes";
  380. print STDERR " dpkg-genchanges @changes_opts >$chg\n";
  381. open CHANGES, '-|', 'dpkg-genchanges', @changes_opts
  382. or subprocerr('dpkg-genchanges');
  383. open OUT, '>', $chg or syserr(_g('write changes file'));
  384. my $infiles = my $files = '';
  385. while ($_ = <CHANGES>) {
  386. print OUT $_ or syserr(_g('write changes file'));
  387. chomp;
  388. if (/^Files:/i) {
  389. $infiles = 1;
  390. } elsif ($infiles && /^\s+(.*)$/) {
  391. $files .= " $1 ";
  392. } elsif ($infiles && /^\S/) {
  393. $infiles = 0;
  394. }
  395. }
  396. close CHANGES or subprocerr(_g('dpkg-genchanges'));
  397. close OUT or syserr(_g('write changes file'));
  398. my $srcmsg;
  399. sub fileomitted($) { return $files !~ /$_[0]/ }
  400. my $ext = $compression_re_file_ext;
  401. if (fileomitted '\.deb') {
  402. # source only upload
  403. if (fileomitted "\.diff\.$ext" and fileomitted "\.debian\.tar\.$ext") {
  404. $srcmsg = _g('source only upload: Debian-native package');
  405. } elsif (fileomitted "\.orig\.tar\.$ext") {
  406. $srcmsg = _g('source only, diff-only upload (original source NOT included)');
  407. } else {
  408. $srcmsg = _g('source only upload (original source is included)');
  409. }
  410. } else {
  411. $srcmsg = _g('full upload (original source is included)');
  412. if (fileomitted '\.dsc') {
  413. $srcmsg = _g('binary only upload (no source included)');
  414. } elsif (fileomitted "\.diff\.$ext" and fileomitted "\.debian\.tar\.$ext") {
  415. $srcmsg = _g('full upload; Debian-native package (full source is included)');
  416. } elsif (fileomitted "\.orig\.tar\.$ext") {
  417. $srcmsg = _g('binary and diff upload (original source NOT included)');
  418. } else {
  419. $srcmsg = _g('full upload (original source is included)');
  420. }
  421. }
  422. if ($signchanges && signfile("$pva.changes")) {
  423. $signerrors = _g("Failed to sign .changes file");
  424. }
  425. if ($cleansource) {
  426. withecho(@rootcommand, @debian_rules, 'clean');
  427. }
  428. print "$progname: $srcmsg\n";
  429. if ($signerrors) {
  430. warning($signerrors);
  431. exit 1;
  432. }
  433. sub testcommand {
  434. my ($cmd) = @_;
  435. my $fullcmd = `which $cmd`;
  436. chomp $fullcmd;
  437. return $fullcmd && -x $fullcmd;
  438. }
  439. sub mustsetvar {
  440. my ($var, $text) = @_;
  441. error(_g("unable to determine %s"), $text)
  442. unless defined($var);
  443. print "$progname: $text $var\n";
  444. return $var;
  445. }
  446. sub withecho {
  447. shift while !$_[0];
  448. print STDERR " @_\n";
  449. system(@_)
  450. and subprocerr("@_");
  451. }
  452. sub signfile {
  453. my ($file) = @_;
  454. print STDERR " signfile $file\n";
  455. my $qfile = quotemeta($file);
  456. if ($signinterface eq 'gpg') {
  457. system("(cat ../$qfile ; echo '') | ".
  458. "$signcommand --utf8-strings --local-user "
  459. .quotemeta($signkey||$maintainer).
  460. " --clearsign --armor --textmode > ../$qfile.asc");
  461. } else {
  462. system("$signcommand -u ".quotemeta($signkey||$maintainer).
  463. " +clearsig=on -fast <../$qfile >../$qfile.asc");
  464. }
  465. my $status = $?;
  466. unless ($status) {
  467. system('mv', '--', "../$file.asc", "../$file")
  468. and subprocerr('mv');
  469. } else {
  470. system('rm', '-f', "../$file.asc")
  471. and subprocerr('rm -f');
  472. }
  473. print "\n";
  474. return $status
  475. }