dpkg-buildpackage.pl 15 KB

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