dpkg-buildpackage.pl 14 KB

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