dpkg-buildpackage.pl 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  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. } elsif (/^-b$/) {
  154. $binaryonly = '-b';
  155. @checkbuilddep_args = ();
  156. $binarytarget = 'binary';
  157. if ($sourceonly) {
  158. usageerr(_g("cannot combine %s and %s"), '-b', '-S');
  159. }
  160. } elsif (/^-B$/) {
  161. $binaryonly = '-B';
  162. @checkbuilddep_args = ('-B');
  163. $binarytarget = 'binary-arch';
  164. if ($sourceonly) {
  165. usageerr(_g("cannot combine %s and %s"), '-B', '-S');
  166. }
  167. } elsif (/^-A$/) {
  168. $binaryonly = '-A';
  169. @checkbuilddep_args = ();
  170. $binarytarget = 'binary-indep';
  171. if ($sourceonly) {
  172. usageerr(_g("cannot combine %s and %s"), '-A', '-S');
  173. }
  174. } elsif (/^-S$/) {
  175. $sourceonly = '-S';
  176. @checkbuilddep_args = ('-B');
  177. if ($binaryonly) {
  178. usageerr(_g("cannot combine %s and %s"), $binaryonly, '-S');
  179. }
  180. } elsif (/^-v(.*)$/) {
  181. $since = $1;
  182. } elsif (/^-m(.*)$/) {
  183. $maint = $1;
  184. } elsif (/^-e(.*)$/) {
  185. $changedby = $1;
  186. } elsif (/^-C(.*)$/) {
  187. $desc = $1;
  188. } elsif (/^-W$/) {
  189. $warnable_error = 1;
  190. push @passopts, '-W';
  191. } elsif (/^-E$/) {
  192. $warnable_error = 0;
  193. push @passopts, '-E';
  194. } elsif (/^-R(.*)$/) {
  195. @debian_rules = split /\s+/, $1;
  196. } else {
  197. usageerr(_g("unknown option or argument %s"), $_);
  198. }
  199. }
  200. if ($noclean) {
  201. # -nc without -b/-B/-A/-S implies -b
  202. $binaryonly = '-b' unless ($binaryonly or $sourceonly);
  203. }
  204. if ($< == 0) {
  205. warning(_g("using a gain-root-command while being root")) if (@rootcommand);
  206. } else {
  207. push @rootcommand, "fakeroot" unless @rootcommand;
  208. if (!testcommand($rootcommand[0])) {
  209. if ($rootcommand[0] eq 'fakeroot') {
  210. error(_g("fakeroot not found, either install the fakeroot\n" .
  211. "package, specify a command with the -r option, " .
  212. "or run this as root"));
  213. } else {
  214. error(_g("gain-root-commmand '%s' not found"), $rootcommand[0]);
  215. }
  216. }
  217. }
  218. unless ($signcommand) {
  219. $signsource = 0;
  220. $signchanges = 0;
  221. }
  222. my $signinterface;
  223. if ($forcesigninterface) {
  224. $signinterface = $forcesigninterface;
  225. } else {
  226. $signinterface = $signcommand;
  227. }
  228. if ($signcommand) {
  229. if ($signinterface !~ /^(gpg|pgp)$/) {
  230. warning(_g("unknown sign command, assuming pgp style interface"));
  231. } elsif ($signinterface eq 'pgp') {
  232. if ($signsource or $signchanges) {
  233. warning(_g("PGP support is deprecated (see README.feature-removal-schedule)"));
  234. }
  235. }
  236. }
  237. my $build_opts = Dpkg::BuildOptions::parse();
  238. if ($parallel) {
  239. $parallel = $build_opts->{parallel} if (defined $build_opts->{parallel});
  240. $ENV{MAKEFLAGS} ||= '';
  241. if ($parallel eq '-1') {
  242. $ENV{MAKEFLAGS} .= " -j";
  243. } else {
  244. $ENV{MAKEFLAGS} .= " -j$parallel";
  245. }
  246. $build_opts->{parallel} = $parallel;
  247. Dpkg::BuildOptions::set($build_opts);
  248. }
  249. my $default_flags = defined $build_opts->{noopt} ? "-g -O0" : "-g -O2";
  250. my %flags = ( CPPFLAGS => '',
  251. CFLAGS => $default_flags,
  252. CXXFLAGS => $default_flags,
  253. FFLAGS => $default_flags,
  254. LDFLAGS => '',
  255. );
  256. foreach my $flag (keys %flags) {
  257. if ($ENV{$flag}) {
  258. printf(_g("%s: use %s from environment: %s\n"), $progname,
  259. $flag, $ENV{$flag});
  260. } else {
  261. $ENV{$flag} = $flags{$flag};
  262. printf(_g("%s: set %s to default value: %s\n"), $progname,
  263. $flag, $ENV{$flag});
  264. }
  265. if ($ENV{"${flag}_APPEND"}) {
  266. $ENV{$flag} .= " ".$ENV{"${flag}_APPEND"};
  267. }
  268. }
  269. my $cwd = cwd();
  270. my $dir = basename($cwd);
  271. my $changelog = parse_changelog();
  272. my $pkg = mustsetvar($changelog->{source}, _g('source package'));
  273. my $version = mustsetvar($changelog->{version}, _g('source version'));
  274. check_version($version, 1);
  275. my $maintainer;
  276. if ($changedby) {
  277. $maintainer = $changedby;
  278. } elsif ($maint) {
  279. $maintainer = $maint;
  280. } else {
  281. $maintainer = mustsetvar($changelog->{maintainer}, _g('source changed by'));
  282. }
  283. open my $arch_env, '-|', 'dpkg-architecture', "-a$targetarch",
  284. "-t$targetgnusystem", '-s', '-f' or subprocerr('dpkg-architecture');
  285. while ($_ = <$arch_env>) {
  286. chomp;
  287. my @cmds = split /\s*;\s*/;
  288. foreach (@cmds) {
  289. /^\s*(\w+)=([\w-]*)\s*$/ && do {
  290. $ENV{$1} = $2;
  291. };
  292. }
  293. }
  294. close $arch_env or subprocerr('dpkg-architecture');
  295. # In case of cross-compilation, give sensible default search path
  296. # for some widely used tools
  297. $targetgnusystem = debarch_to_gnutriplet($targetarch) if $targetarch;
  298. if ($targetgnusystem and
  299. ($targetgnusystem ne debarch_to_gnutriplet(get_build_arch())))
  300. {
  301. $ENV{PKG_CONFIG_LIBDIR} ||= "/usr/$targetgnusystem/lib/pkgconfig/:" .
  302. "/usr/share/pkgconfig/";
  303. }
  304. my $vendor = get_current_vendor();
  305. if (defined $vendor) {
  306. $ENV{'DEB_VENDOR'} = $vendor;
  307. } else {
  308. delete $ENV{'DEB_VENDOR'};
  309. }
  310. my $arch;
  311. unless ($sourceonly) {
  312. $arch = mustsetvar($ENV{'DEB_HOST_ARCH'}, _g('host architecture'));
  313. } else {
  314. $arch = 'source';
  315. }
  316. # Preparation of environment stops here
  317. (my $sversion = $version) =~ s/^\d+://;
  318. my $pv = "${pkg}_$sversion";
  319. my $pva = "${pkg}_${sversion}_$arch";
  320. if (not -x "debian/rules") {
  321. warning(_g("debian/rules is not executable: fixing that."));
  322. chmod(0755, "debian/rules"); # No checks of failures, non fatal
  323. }
  324. if ($checkbuilddep) {
  325. if ($admindir) {
  326. push @checkbuilddep_args, "--admindir=$admindir";
  327. }
  328. if (system('dpkg-checkbuilddeps', @checkbuilddep_args)) {
  329. warning(_g("Build dependencies/conflicts unsatisfied; aborting."));
  330. warning(_g("(Use -d flag to override.)"));
  331. if ($sourceonly) {
  332. warning(_g("This is currently a non-fatal warning with -S, but"));
  333. warning(_g("will probably become fatal in the future."));
  334. } else {
  335. exit 3;
  336. }
  337. }
  338. }
  339. if ($call_target) {
  340. if ($call_target_as_root or
  341. $call_target =~ /^(clean|binary(|-arch|-indep))$/)
  342. {
  343. withecho(@rootcommand, @debian_rules, $call_target);
  344. } else {
  345. withecho(@debian_rules, $call_target);
  346. }
  347. exit 0;
  348. }
  349. unless ($noclean) {
  350. withecho(@rootcommand, @debian_rules, 'clean');
  351. }
  352. unless ($binaryonly) {
  353. warning(_g("it is a bad idea to generate a source package " .
  354. "without cleaning up first, it might contain undesired " .
  355. "files.")) if $noclean;
  356. chdir('..') or syserr('chdir ..');
  357. my @opts = @passopts;
  358. if ($diffignore) { push @opts, $diffignore }
  359. push @opts, @tarignore;
  360. withecho('dpkg-source', @opts, '-b', $dir);
  361. chdir($dir) or syserr("chdir $dir");
  362. }
  363. unless ($sourceonly) {
  364. withecho(@debian_rules, 'build');
  365. withecho(@rootcommand, @debian_rules, $binarytarget);
  366. }
  367. if ($usepause &&
  368. ($signchanges || ( !$binaryonly && $signsource )) ) {
  369. print _g("Press the return key to start signing process\n");
  370. getc();
  371. }
  372. my $signerrors;
  373. unless ($binaryonly) {
  374. if ($signsource && signfile("$pv.dsc")) {
  375. $signerrors = _g("Failed to sign .dsc and .changes file");
  376. $signchanges = 0;
  377. }
  378. }
  379. my @change_opts;
  380. if ($binaryonly) { push @change_opts, $binaryonly }
  381. if ($sourceonly) { push @change_opts, $sourceonly }
  382. if ($sourcestyle) { push @change_opts, $sourcestyle }
  383. if ($maint) { push @change_opts, "-m$maint" }
  384. if ($changedby) { push @change_opts, "-e$changedby" }
  385. if ($since) { push @change_opts, "-v$since" }
  386. if ($desc) { push @change_opts, "-C$desc" }
  387. my $chg = "../$pva.changes";
  388. print STDERR " dpkg-genchanges @change_opts >$chg\n";
  389. open CHANGES, '-|', 'dpkg-genchanges', @change_opts
  390. or subprocerr('dpkg-genchanges');
  391. open OUT, '>', $chg or syserr(_g('write changes file'));
  392. my $infiles = my $files = '';
  393. while ($_ = <CHANGES>) {
  394. print OUT $_ or syserr(_g('write changes file'));
  395. chomp;
  396. if (/^Files:/i) {
  397. $infiles = 1;
  398. } elsif ($infiles && /^\s+(.*)$/) {
  399. $files .= " $1 ";
  400. } elsif ($infiles && /^\S/) {
  401. $infiles = 0;
  402. }
  403. }
  404. close CHANGES or subprocerr(_g('dpkg-genchanges'));
  405. close OUT or syserr(_g('write changes file'));
  406. my $srcmsg;
  407. sub fileomitted($) { return $files !~ /$_[0]/ }
  408. if (fileomitted '\.deb') {
  409. # source only upload
  410. if (fileomitted "\.diff\.$comp_regex" and fileomitted "\.debian\.tar\.$comp_regex") {
  411. $srcmsg = _g('source only upload: Debian-native package');
  412. } elsif (fileomitted "\.orig\.tar\.$comp_regex") {
  413. $srcmsg = _g('source only, diff-only upload (original source NOT included)');
  414. } else {
  415. $srcmsg = _g('source only upload (original source is included)');
  416. }
  417. } else {
  418. $srcmsg = _g('full upload (original source is included)');
  419. if (fileomitted '\.dsc') {
  420. $srcmsg = _g('binary only upload (no source included)');
  421. } elsif (fileomitted "\.diff\.$comp_regex" and fileomitted "\.debian\.tar\.$comp_regex") {
  422. $srcmsg = _g('full upload; Debian-native package (full source is included)');
  423. } elsif (fileomitted "\.orig\.tar\.$comp_regex") {
  424. $srcmsg = _g('binary and diff upload (original source NOT included)');
  425. } else {
  426. $srcmsg = _g('full upload (original source is included)');
  427. }
  428. }
  429. if ($signchanges && signfile("$pva.changes")) {
  430. $signerrors = _g("Failed to sign .changes file");
  431. }
  432. if ($cleansource) {
  433. withecho(@rootcommand, @debian_rules, 'clean');
  434. }
  435. print "$progname: $srcmsg\n";
  436. if ($signerrors) {
  437. warning($signerrors);
  438. exit 1;
  439. }
  440. sub testcommand {
  441. my ($cmd) = @_;
  442. my $fullcmd = `which $cmd`;
  443. chomp $fullcmd;
  444. return $fullcmd && -x $fullcmd;
  445. }
  446. sub mustsetvar {
  447. my ($var, $text) = @_;
  448. error(_g("unable to determine %s"), $text)
  449. unless defined($var);
  450. print "$progname: $text $var\n";
  451. return $var;
  452. }
  453. sub withecho {
  454. shift while !$_[0];
  455. print STDERR " @_\n";
  456. system(@_)
  457. and subprocerr("@_");
  458. }
  459. sub signfile {
  460. my ($file) = @_;
  461. print STDERR " signfile $file\n";
  462. my $qfile = quotemeta($file);
  463. if ($signinterface eq 'gpg') {
  464. system("(cat ../$qfile ; echo '') | ".
  465. "$signcommand --utf8-strings --local-user "
  466. .quotemeta($signkey||$maintainer).
  467. " --clearsign --armor --textmode > ../$qfile.asc");
  468. } else {
  469. system("$signcommand -u ".quotemeta($signkey||$maintainer).
  470. " +clearsig=on -fast <../$qfile >../$qfile.asc");
  471. }
  472. my $status = $?;
  473. unless ($status) {
  474. system('mv', '--', "../$file.asc", "../$file")
  475. and subprocerr('mv');
  476. } else {
  477. system('rm', '-f', "../$file.asc")
  478. and subprocerr('rm -f');
  479. }
  480. print "\n";
  481. return $status
  482. }