dpkg-buildpackage.pl 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  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 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. -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 = parse_changelog();
  258. my $pkg = mustsetvar($changelog->{source}, _g('source package'));
  259. my $version = mustsetvar($changelog->{version}, _g('source version'));
  260. check_version($version, 1);
  261. my $maintainer;
  262. if ($changedby) {
  263. $maintainer = $changedby;
  264. } elsif ($maint) {
  265. $maintainer = $maint;
  266. } else {
  267. $maintainer = mustsetvar($changelog->{maintainer}, _g('source changed by'));
  268. }
  269. open my $arch_env, '-|', 'dpkg-architecture', "-a$targetarch",
  270. "-t$targetgnusystem", '-s', '-f' or subprocerr('dpkg-architecture');
  271. while ($_ = <$arch_env>) {
  272. chomp;
  273. my @cmds = split /\s*;\s*/;
  274. foreach (@cmds) {
  275. /^\s*(\w+)=([\w-]*)\s*$/ && do {
  276. $ENV{$1} = $2;
  277. };
  278. }
  279. }
  280. close $arch_env or subprocerr('dpkg-architecture');
  281. # In case of cross-compilation, give sensible default search path
  282. # for some widely used tools
  283. $targetgnusystem = debarch_to_gnutriplet($targetarch) if $targetarch;
  284. if ($targetgnusystem and
  285. ($targetgnusystem ne debarch_to_gnutriplet(get_build_arch())))
  286. {
  287. $ENV{PKG_CONFIG_LIBDIR} ||= "/usr/$targetgnusystem/lib/pkgconfig/:" .
  288. "/usr/share/pkgconfig/";
  289. }
  290. my $arch;
  291. unless ($sourceonly) {
  292. $arch = mustsetvar($ENV{'DEB_HOST_ARCH'}, _g('host architecture'));
  293. } else {
  294. $arch = 'source';
  295. }
  296. # Preparation of environment stops here
  297. (my $sversion = $version) =~ s/^\d+://;
  298. my $pv = "${pkg}_$sversion";
  299. my $pva = "${pkg}_${sversion}_$arch";
  300. if (not -x "debian/rules") {
  301. warning(_g("debian/rules is not executable: fixing that."));
  302. chmod(0755, "debian/rules"); # No checks of failures, non fatal
  303. }
  304. if ($checkbuilddep) {
  305. if ($admindir) {
  306. push @checkbuilddep_args, "--admindir=$admindir";
  307. }
  308. system('dpkg-checkbuilddeps', @checkbuilddep_args);
  309. if (not WIFEXITED($?)) {
  310. subprocerr('dpkg-checkbuilddeps');
  311. } elsif (WEXITSTATUS($?)) {
  312. warning(_g("Build dependencies/conflicts unsatisfied; aborting."));
  313. warning(_g("(Use -d flag to override.)"));
  314. if ($sourceonly) {
  315. warning(_g("This is currently a non-fatal warning with -S, but"));
  316. warning(_g("will probably become fatal in the future."));
  317. } else {
  318. exit 3;
  319. }
  320. }
  321. }
  322. if ($call_target) {
  323. if ($call_target_as_root or
  324. $call_target =~ /^(clean|binary(|-arch|-indep))$/)
  325. {
  326. withecho(@rootcommand, @debian_rules, $call_target);
  327. } else {
  328. withecho(@debian_rules, $call_target);
  329. }
  330. exit 0;
  331. }
  332. unless ($noclean) {
  333. withecho(@rootcommand, @debian_rules, 'clean');
  334. }
  335. unless ($binaryonly) {
  336. warning(_g("it is a bad idea to generate a source package " .
  337. "without cleaning up first, it might contain undesired " .
  338. "files.")) if $noclean;
  339. chdir('..') or syserr('chdir ..');
  340. my @opts = @passopts;
  341. if ($diffignore) { push @opts, $diffignore }
  342. push @opts, @tarignore;
  343. withecho('dpkg-source', @opts, '-b', $dir);
  344. chdir($dir) or syserr("chdir $dir");
  345. }
  346. unless ($sourceonly) {
  347. withecho(@debian_rules, 'build');
  348. withecho(@rootcommand, @debian_rules, $binarytarget);
  349. }
  350. if ($usepause &&
  351. ($signchanges || ( !$binaryonly && $signsource )) ) {
  352. print _g("Press the return key to start signing process\n");
  353. getc();
  354. }
  355. my $signerrors;
  356. unless ($binaryonly) {
  357. if ($signsource && signfile("$pv.dsc")) {
  358. $signerrors = _g("Failed to sign .dsc and .changes file");
  359. $signchanges = 0;
  360. }
  361. }
  362. my @change_opts;
  363. if ($binaryonly) { push @change_opts, $binaryonly }
  364. if ($sourceonly) { push @change_opts, $sourceonly }
  365. if ($sourcestyle) { push @change_opts, $sourcestyle }
  366. if (defined($maint)) { push @change_opts, "-m$maint" }
  367. if (defined($changedby)) { push @change_opts, "-e$changedby" }
  368. if (defined($since)) { push @change_opts, "-v$since" }
  369. if (defined($desc)) { push @change_opts, "-C$desc" }
  370. my $chg = "../$pva.changes";
  371. print STDERR " dpkg-genchanges @change_opts >$chg\n";
  372. open CHANGES, '-|', 'dpkg-genchanges', @change_opts
  373. or subprocerr('dpkg-genchanges');
  374. open OUT, '>', $chg or syserr(_g('write changes file'));
  375. my $infiles = my $files = '';
  376. while ($_ = <CHANGES>) {
  377. print OUT $_ or syserr(_g('write changes file'));
  378. chomp;
  379. if (/^Files:/i) {
  380. $infiles = 1;
  381. } elsif ($infiles && /^\s+(.*)$/) {
  382. $files .= " $1 ";
  383. } elsif ($infiles && /^\S/) {
  384. $infiles = 0;
  385. }
  386. }
  387. close CHANGES or subprocerr(_g('dpkg-genchanges'));
  388. close OUT or syserr(_g('write changes file'));
  389. my $srcmsg;
  390. sub fileomitted($) { return $files !~ /$_[0]/ }
  391. if (fileomitted '\.deb') {
  392. # source only upload
  393. if (fileomitted "\.diff\.$comp_regex" and fileomitted "\.debian\.tar\.$comp_regex") {
  394. $srcmsg = _g('source only upload: Debian-native package');
  395. } elsif (fileomitted "\.orig\.tar\.$comp_regex") {
  396. $srcmsg = _g('source only, diff-only upload (original source NOT included)');
  397. } else {
  398. $srcmsg = _g('source only upload (original source is included)');
  399. }
  400. } else {
  401. $srcmsg = _g('full upload (original source is included)');
  402. if (fileomitted '\.dsc') {
  403. $srcmsg = _g('binary only upload (no source included)');
  404. } elsif (fileomitted "\.diff\.$comp_regex" and fileomitted "\.debian\.tar\.$comp_regex") {
  405. $srcmsg = _g('full upload; Debian-native package (full source is included)');
  406. } elsif (fileomitted "\.orig\.tar\.$comp_regex") {
  407. $srcmsg = _g('binary and diff upload (original source NOT included)');
  408. } else {
  409. $srcmsg = _g('full upload (original source is included)');
  410. }
  411. }
  412. if ($signchanges && signfile("$pva.changes")) {
  413. $signerrors = _g("Failed to sign .changes file");
  414. }
  415. if ($cleansource) {
  416. withecho(@rootcommand, @debian_rules, 'clean');
  417. }
  418. print "$progname: $srcmsg\n";
  419. if ($signerrors) {
  420. warning($signerrors);
  421. exit 1;
  422. }
  423. sub testcommand {
  424. my ($cmd) = @_;
  425. my $fullcmd = `which $cmd`;
  426. chomp $fullcmd;
  427. return $fullcmd && -x $fullcmd;
  428. }
  429. sub mustsetvar {
  430. my ($var, $text) = @_;
  431. error(_g("unable to determine %s"), $text)
  432. unless defined($var);
  433. print "$progname: $text $var\n";
  434. return $var;
  435. }
  436. sub withecho {
  437. shift while !$_[0];
  438. print STDERR " @_\n";
  439. system(@_)
  440. and subprocerr("@_");
  441. }
  442. sub signfile {
  443. my ($file) = @_;
  444. print STDERR " signfile $file\n";
  445. my $qfile = quotemeta($file);
  446. if ($signinterface eq 'gpg') {
  447. system("(cat ../$qfile ; echo '') | ".
  448. "$signcommand --utf8-strings --local-user "
  449. .quotemeta($signkey||$maintainer).
  450. " --clearsign --armor --textmode > ../$qfile.asc");
  451. } else {
  452. system("$signcommand -u ".quotemeta($signkey||$maintainer).
  453. " +clearsig=on -fast <../$qfile >../$qfile.asc");
  454. }
  455. my $status = $?;
  456. unless ($status) {
  457. system('mv', '--', "../$file.asc", "../$file")
  458. and subprocerr('mv');
  459. } else {
  460. system('rm', '-f', "../$file.asc")
  461. and subprocerr('rm -f');
  462. }
  463. print "\n";
  464. return $status
  465. }