dpkg-buildpackage.pl 14 KB

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