dpkg-buildpackage.pl 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4. use Cwd;
  5. use File::Basename;
  6. use Dpkg;
  7. use Dpkg::Gettext;
  8. use Dpkg::ErrorHandling qw(:DEFAULT $warnable_error);
  9. use Dpkg::BuildOptions;
  10. use Dpkg::Compression;
  11. use Dpkg::Version qw(check_version);
  12. use Dpkg::Changelog qw(parse_changelog);
  13. use Dpkg::Arch qw(get_build_arch debarch_to_gnutriplet);
  14. use Dpkg::Vendor qw(get_current_vendor);
  15. textdomain("dpkg-dev");
  16. sub showversion {
  17. printf _g("Debian %s version %s.\n"), $progname, $version;
  18. print _g("
  19. Copyright (C) 1996 Ian Jackson.
  20. Copyright (C) 2000 Wichert Akkerman
  21. Copyright (C) 2007 Frank Lichtenheld");
  22. print _g("
  23. This is free software; see the GNU General Public Licence version 2 or
  24. later for copying conditions. There is NO warranty.
  25. ");
  26. }
  27. sub usage {
  28. printf _g("
  29. Usage: %s [<options> ...]
  30. Options:
  31. -r<gain-root-command>
  32. command to gain root privileges (default is fakeroot).
  33. -R<rules> rules file to execute (default is debian/rules).
  34. -p<sign-command>
  35. -d do not check build dependencies and conflicts.
  36. -D check build dependencies and conflicts.
  37. -T<target> call debian/rules <target> with the proper environment
  38. --as-root ensure -T calls the target with root rights
  39. -j[<number>] specify jobs to run simultaneously } passed to debian/rules
  40. -k<keyid> the key to use for signing.
  41. -sgpg the sign-command is called like GPG.
  42. -spgp the sign-command is called like PGP.
  43. -us unsigned source.
  44. -uc unsigned changes.
  45. -a<arch> Debian architecture we build for (implies -d).
  46. -b binary-only, do not build source. } also passed to
  47. -B binary-only, no arch-indep files. } dpkg-genchanges
  48. -A binary-only, only arch-indep files. }
  49. -S source only, no binary files. }
  50. -t<system> set GNU system type. } passed to dpkg-architecture
  51. -v<version> changes since version <version>. }
  52. -m<maint> maintainer for package is <maint>. }
  53. -e<maint> maintainer for release is <maint>. } only passed
  54. -C<descfile> changes are described in <descfile>. } to dpkg-genchanges
  55. -si (default) src includes orig if new upstream. }
  56. -sa uploaded src always includes orig. }
  57. -sd uploaded src is diff and .dsc only. }
  58. -sn force Debian native source format. }
  59. -s[sAkurKUR] see dpkg-source for explanation. } only passed
  60. -z<level> compression level of source } to dpkg-source
  61. -Z(gz|bz2|lzma) compression to use for source }
  62. -nc do not clean source tree (implies -b).
  63. -tc clean source tree when finished.
  64. -ap add pause before starting signature process.
  65. -E turn certain warnings into errors. } passed to
  66. -W when -E is turned on, -W turns it off. } dpkg-source
  67. -i[<regex>] ignore diffs of files matching regex. } only passed
  68. -I[<pattern>] filter out files when building tarballs. } to dpkg-source
  69. --admindir=<directory>
  70. change the administrative directory.
  71. -h, --help show this help message.
  72. --version show the version.
  73. "), $progname;
  74. }
  75. my @debian_rules = ("debian/rules");
  76. my @rootcommand = ();
  77. my $signcommand = '';
  78. if ( ( ($ENV{GNUPGHOME} && -e $ENV{GNUPGHOME})
  79. || ($ENV{HOME} && -e "$ENV{HOME}/.gnupg") )
  80. && testcommand('gpg')) {
  81. $signcommand = 'gpg';
  82. } elsif (testcommand('pgp')) {
  83. $signcommand = 'pgp'
  84. }
  85. my ($admindir, $signkey, $forcesigninterface, $usepause, $noclean,
  86. $sourcestyle, $cleansource,
  87. $binaryonly, $sourceonly, $since, $maint,
  88. $changedby, $desc, $parallel);
  89. my (@checkbuilddep_args, @passopts, @tarignore);
  90. my $checkbuilddep = 1;
  91. my $signsource = 1;
  92. my $signchanges = 1;
  93. my $diffignore = '';
  94. my $binarytarget = 'binary';
  95. my $targetarch = my $targetgnusystem = '';
  96. my $call_target = '';
  97. my $call_target_as_root = 0;
  98. while (@ARGV) {
  99. $_ = shift @ARGV;
  100. if (/^(--help|-h)$/) {
  101. usage;
  102. exit 0;
  103. } elsif (/^--version$/) {
  104. showversion;
  105. exit 0;
  106. } elsif (/^--admindir$/) {
  107. $admindir = shift @ARGV;
  108. } elsif (/^--admindir=(.*)$/) {
  109. $admindir = $1;
  110. } elsif (/^-j(\d*)$/) {
  111. $parallel = $1 || '-1';
  112. } elsif (/^-r(.*)$/) {
  113. @rootcommand = split /\s+/, $1;
  114. } elsif (/^-p(.*)$/) {
  115. $signcommand = $1;
  116. } elsif (/^-k(.*)$/) {
  117. $signkey = $1;
  118. } elsif (/^-([dD])$/) {
  119. $checkbuilddep = ($1 eq 'D');
  120. } elsif (/^-s(gpg|pgp)$/) {
  121. $forcesigninterface = $1;
  122. } elsif (/^-us$/) {
  123. $signsource = 0;
  124. } elsif (/^-uc$/) {
  125. $signchanges = 0;
  126. } elsif (/^-ap$/) {
  127. $usepause = 1;
  128. } elsif (/^-a(.*)$/) {
  129. $targetarch = $1;
  130. $checkbuilddep = 0;
  131. } elsif (/^-s[iad]$/) {
  132. $sourcestyle = $_;
  133. } elsif (/^-s[nsAkurKUR]$/) {
  134. push @passopts, $_; # passed to dpkg-source
  135. } elsif (/^-[zZ]/) {
  136. push @passopts, $_; # passed to dpkg-source
  137. } elsif (/^-i.*$/) {
  138. $diffignore = $_;
  139. } elsif (/^-I.*$/) {
  140. push @tarignore, $_;
  141. } elsif (/^-tc$/) {
  142. $cleansource = 1;
  143. } elsif (/^-t(.*)$/) {
  144. $targetgnusystem = $1; # Order DOES matter!
  145. } elsif (/^(--target|-T)$/) {
  146. $call_target = shift @ARGV;
  147. } elsif (/^(--target=|-T)(.+)$/) {
  148. $call_target = $2;
  149. } elsif (/^--as-root$/) {
  150. $call_target_as_root = 1;
  151. } elsif (/^-nc$/) {
  152. $noclean = 1;
  153. if ($sourceonly) {
  154. usageerr(_g("cannot combine %s and %s"), '-nc', '-S');
  155. }
  156. unless ($binaryonly) {
  157. $binaryonly = '-b';
  158. }
  159. } elsif (/^-b$/) {
  160. $binaryonly = '-b';
  161. @checkbuilddep_args = ();
  162. $binarytarget = 'binary';
  163. if ($sourceonly) {
  164. usageerr(_g("cannot combine %s and %s"), '-b', '-S');
  165. }
  166. } elsif (/^-B$/) {
  167. $binaryonly = '-B';
  168. @checkbuilddep_args = ('-B');
  169. $binarytarget = 'binary-arch';
  170. if ($sourceonly) {
  171. usageerr(_g("cannot combine %s and %s"), '-B', '-S');
  172. }
  173. } elsif (/^-A$/) {
  174. $binaryonly = '-A';
  175. @checkbuilddep_args = ();
  176. $binarytarget = 'binary-indep';
  177. if ($sourceonly) {
  178. usageerr(_g("cannot combine %s and %s"), '-A', '-S');
  179. }
  180. } elsif (/^-S$/) {
  181. $sourceonly = '-S';
  182. @checkbuilddep_args = ('-B');
  183. if ($binaryonly) {
  184. usageerr(_g("cannot combine %s and %s"), $binaryonly, '-S');
  185. }
  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 (/^-W$/) {
  195. $warnable_error = 1;
  196. push @passopts, '-W';
  197. } elsif (/^-E$/) {
  198. $warnable_error = 0;
  199. push @passopts, '-E';
  200. } elsif (/^-R(.*)$/) {
  201. @debian_rules = split /\s+/, $1;
  202. } else {
  203. usageerr(_g("unknown option or argument %s"), $_);
  204. }
  205. }
  206. if ($< == 0) {
  207. warning(_g("using a gain-root-command while being root")) if (@rootcommand);
  208. } else {
  209. push @rootcommand, "fakeroot" unless @rootcommand;
  210. if (!testcommand($rootcommand[0])) {
  211. if ($rootcommand[0] eq 'fakeroot') {
  212. error(_g("fakeroot not found, either install the fakeroot\n" .
  213. "package, specify a command with the -r option, " .
  214. "or run this as root"));
  215. } else {
  216. error(_g("gain-root-commmand '%s' not found"), $rootcommand[0]);
  217. }
  218. }
  219. }
  220. unless ($signcommand) {
  221. $signsource = 0;
  222. $signchanges = 0;
  223. }
  224. my $signinterface;
  225. if ($forcesigninterface) {
  226. $signinterface = $forcesigninterface;
  227. } else {
  228. $signinterface = $signcommand;
  229. }
  230. if ($signcommand) {
  231. if ($signinterface !~ /^(gpg|pgp)$/) {
  232. warning(_g("unknown sign command, assuming pgp style interface"));
  233. } elsif ($signinterface eq 'pgp') {
  234. if ($signsource or $signchanges) {
  235. warning(_g("PGP support is deprecated (see README.feature-removal-schedule)"));
  236. }
  237. }
  238. }
  239. my $build_opts = Dpkg::BuildOptions::parse();
  240. if ($parallel) {
  241. $parallel = $build_opts->{parallel} if (defined $build_opts->{parallel});
  242. $ENV{MAKEFLAGS} ||= '';
  243. if ($parallel eq '-1') {
  244. $ENV{MAKEFLAGS} .= " -j";
  245. } else {
  246. $ENV{MAKEFLAGS} .= " -j$parallel";
  247. }
  248. $build_opts->{parallel} = $parallel;
  249. Dpkg::BuildOptions::set($build_opts);
  250. }
  251. my $default_flags = defined $build_opts->{noopt} ? "-g -O0" : "-g -O2";
  252. my %flags = ( CPPFLAGS => '',
  253. CFLAGS => $default_flags,
  254. CXXFLAGS => $default_flags,
  255. FFLAGS => $default_flags,
  256. LDFLAGS => '',
  257. );
  258. foreach my $flag (keys %flags) {
  259. if ($ENV{$flag}) {
  260. printf(_g("%s: use %s from environment: %s\n"), $progname,
  261. $flag, $ENV{$flag});
  262. } else {
  263. $ENV{$flag} = $flags{$flag};
  264. printf(_g("%s: set %s to default value: %s\n"), $progname,
  265. $flag, $ENV{$flag});
  266. }
  267. if ($ENV{"${flag}_APPEND"}) {
  268. $ENV{$flag} .= " ".$ENV{"${flag}_APPEND"};
  269. }
  270. }
  271. my $cwd = cwd();
  272. my $dir = basename($cwd);
  273. my $changelog = parse_changelog();
  274. my $pkg = mustsetvar($changelog->{source}, _g('source package'));
  275. my $version = mustsetvar($changelog->{version}, _g('source version'));
  276. check_version($version);
  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 $vendor = get_current_vendor();
  307. if (defined $vendor) {
  308. $ENV{'DEB_VENDOR'} = $vendor;
  309. } else {
  310. delete $ENV{'DEB_VENDOR'};
  311. }
  312. my $arch;
  313. unless ($sourceonly) {
  314. $arch = mustsetvar($ENV{'DEB_HOST_ARCH'}, _g('host architecture'));
  315. } else {
  316. $arch = 'source';
  317. }
  318. # Preparation of environment stops here
  319. (my $sversion = $version) =~ s/^\d+://;
  320. my $pv = "${pkg}_$sversion";
  321. my $pva = "${pkg}_${sversion}_$arch";
  322. if ($checkbuilddep) {
  323. if ($admindir) {
  324. push @checkbuilddep_args, "--admindir=$admindir";
  325. }
  326. if (system('dpkg-checkbuilddeps', @checkbuilddep_args)) {
  327. warning(_g("Build dependencies/conflicts unsatisfied; aborting."));
  328. warning(_g("(Use -d flag to override.)"));
  329. if ($sourceonly) {
  330. warning(_g("This is currently a non-fatal warning with -S, but"));
  331. warning(_g("will probably become fatal in the future."));
  332. } else {
  333. exit 3;
  334. }
  335. }
  336. }
  337. if ($call_target) {
  338. if ($call_target_as_root or
  339. $call_target =~ /^(clean|binary(|-arch|-indep))$/)
  340. {
  341. withecho(@rootcommand, @debian_rules, $call_target);
  342. } else {
  343. withecho(@debian_rules, $call_target);
  344. }
  345. exit 0;
  346. }
  347. unless ($noclean) {
  348. withecho(@rootcommand, @debian_rules, 'clean');
  349. }
  350. unless ($binaryonly) {
  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 ($maint) { push @change_opts, "-m$maint" }
  379. if ($changedby) { push @change_opts, "-e$changedby" }
  380. if ($since) { push @change_opts, "-v$since" }
  381. if ($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. if (fileomitted '\.deb') {
  404. # source only upload
  405. if (fileomitted "\.diff\.$comp_regex" and fileomitted "\.debian\.tar\.$comp_regex") {
  406. $srcmsg = _g('source only upload: Debian-native package');
  407. } elsif (fileomitted "\.orig\.tar\.$comp_regex") {
  408. $srcmsg = _g('source only, diff-only upload (original source NOT included)');
  409. } else {
  410. $srcmsg = _g('source only upload (original source is included)');
  411. }
  412. } else {
  413. $srcmsg = _g('full upload (original source is included)');
  414. if (fileomitted '\.dsc') {
  415. $srcmsg = _g('binary only upload (no source included)');
  416. } elsif (fileomitted "\.diff\.$comp_regex" and fileomitted "\.debian\.tar\.$comp_regex") {
  417. $srcmsg = _g('full upload; Debian-native package (full source is included)');
  418. } elsif (fileomitted "\.orig\.tar\.$comp_regex") {
  419. $srcmsg = _g('binary and diff upload (original source NOT included)');
  420. } else {
  421. $srcmsg = _g('full upload (original source is included)');
  422. }
  423. }
  424. if ($signchanges && signfile("$pva.changes")) {
  425. $signerrors = _g("Failed to sign .changes file");
  426. }
  427. if ($cleansource) {
  428. withecho(@rootcommand, @debian_rules, 'clean');
  429. }
  430. print "$progname: $srcmsg\n";
  431. if ($signerrors) {
  432. warning($signerrors);
  433. exit 1;
  434. }
  435. sub testcommand {
  436. my ($cmd) = @_;
  437. my $fullcmd = `which $cmd`;
  438. chomp $fullcmd;
  439. return $fullcmd && -x $fullcmd;
  440. }
  441. sub mustsetvar {
  442. my ($var, $text) = @_;
  443. error(_g("unable to determine %s"), $text)
  444. unless defined($var);
  445. print "$progname: $text $var\n";
  446. return $var;
  447. }
  448. sub withecho {
  449. shift while !$_[0];
  450. print STDERR " @_\n";
  451. system(@_)
  452. and subprocerr("@_");
  453. }
  454. sub signfile {
  455. my ($file) = @_;
  456. print STDERR " signfile $file\n";
  457. my $qfile = quotemeta($file);
  458. if ($signinterface eq 'gpg') {
  459. system("(cat ../$qfile ; echo '') | ".
  460. "$signcommand --utf8-strings --local-user "
  461. .quotemeta($signkey||$maintainer).
  462. " --clearsign --armor --textmode > ../$qfile.asc");
  463. } else {
  464. system("$signcommand -u ".quotemeta($signkey||$maintainer).
  465. " +clearsig=on -fast <../$qfile >../$qfile.asc");
  466. }
  467. my $status = $?;
  468. unless ($status) {
  469. system('mv', '--', "../$file.asc", "../$file")
  470. and subprocerr('mv');
  471. } else {
  472. system('rm', '-f', "../$file.asc")
  473. and subprocerr('rm -f');
  474. }
  475. print "\n";
  476. return $status
  477. }