dpkg-buildpackage.pl 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  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 qw(:DEFAULT $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. -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. } elsif (/^-B$/) {
  158. $binaryonly = '-B';
  159. @checkbuilddep_args = ('-B');
  160. $binarytarget = 'binary-arch';
  161. } elsif (/^-A$/) {
  162. $binaryonly = '-A';
  163. @checkbuilddep_args = ();
  164. $binarytarget = 'binary-indep';
  165. } elsif (/^-S$/) {
  166. $sourceonly = '-S';
  167. @checkbuilddep_args = ('-B');
  168. } elsif (/^-v(.*)$/) {
  169. $since = $1;
  170. } elsif (/^-m(.*)$/) {
  171. $maint = $1;
  172. } elsif (/^-e(.*)$/) {
  173. $changedby = $1;
  174. } elsif (/^-C(.*)$/) {
  175. $desc = $1;
  176. } elsif (/^-W$/) {
  177. $warnable_error = 1;
  178. push @passopts, '-W';
  179. } elsif (/^-E$/) {
  180. $warnable_error = 0;
  181. push @passopts, '-E';
  182. } elsif (/^-R(.*)$/) {
  183. @debian_rules = split /\s+/, $1;
  184. } else {
  185. usageerr(_g("unknown option or argument %s"), $_);
  186. }
  187. }
  188. if ($binaryonly and $sourceonly) {
  189. usageerr(_g("cannot combine %s and %s"), $binaryonly, $sourceonly);
  190. }
  191. if ($noclean) {
  192. # -nc without -b/-B/-A/-S implies -b
  193. $binaryonly = '-b' unless ($binaryonly or $sourceonly);
  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, 1);
  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 $arch;
  296. unless ($sourceonly) {
  297. $arch = mustsetvar($ENV{'DEB_HOST_ARCH'}, _g('host architecture'));
  298. } else {
  299. $arch = 'source';
  300. }
  301. # Preparation of environment stops here
  302. (my $sversion = $version) =~ s/^\d+://;
  303. my $pv = "${pkg}_$sversion";
  304. my $pva = "${pkg}_${sversion}_$arch";
  305. if (not -x "debian/rules") {
  306. warning(_g("debian/rules is not executable: fixing that."));
  307. chmod(0755, "debian/rules"); # No checks of failures, non fatal
  308. }
  309. if ($checkbuilddep) {
  310. if ($admindir) {
  311. push @checkbuilddep_args, "--admindir=$admindir";
  312. }
  313. system('dpkg-checkbuilddeps', @checkbuilddep_args);
  314. if (not WIFEXITED($?)) {
  315. subprocerr('dpkg-checkbuilddeps');
  316. } elsif (WEXITSTATUS($?)) {
  317. warning(_g("Build dependencies/conflicts unsatisfied; aborting."));
  318. warning(_g("(Use -d flag to override.)"));
  319. if ($sourceonly) {
  320. warning(_g("This is currently a non-fatal warning with -S, but"));
  321. warning(_g("will probably become fatal in the future."));
  322. } else {
  323. exit 3;
  324. }
  325. }
  326. }
  327. if ($call_target) {
  328. if ($call_target_as_root or
  329. $call_target =~ /^(clean|binary(|-arch|-indep))$/)
  330. {
  331. withecho(@rootcommand, @debian_rules, $call_target);
  332. } else {
  333. withecho(@debian_rules, $call_target);
  334. }
  335. exit 0;
  336. }
  337. unless ($noclean) {
  338. withecho(@rootcommand, @debian_rules, 'clean');
  339. }
  340. unless ($binaryonly) {
  341. warning(_g("it is a bad idea to generate a source package " .
  342. "without cleaning up first, it might contain undesired " .
  343. "files.")) if $noclean;
  344. chdir('..') or syserr('chdir ..');
  345. my @opts = @passopts;
  346. if ($diffignore) { push @opts, $diffignore }
  347. push @opts, @tarignore;
  348. withecho('dpkg-source', @opts, '-b', $dir);
  349. chdir($dir) or syserr("chdir $dir");
  350. }
  351. unless ($sourceonly) {
  352. withecho(@debian_rules, 'build');
  353. withecho(@rootcommand, @debian_rules, $binarytarget);
  354. }
  355. if ($usepause &&
  356. ($signchanges || ( !$binaryonly && $signsource )) ) {
  357. print _g("Press the return key to start signing process\n");
  358. getc();
  359. }
  360. my $signerrors;
  361. unless ($binaryonly) {
  362. if ($signsource && signfile("$pv.dsc")) {
  363. $signerrors = _g("Failed to sign .dsc and .changes file");
  364. $signchanges = 0;
  365. }
  366. }
  367. my @change_opts;
  368. if ($binaryonly) { push @change_opts, $binaryonly }
  369. if ($sourceonly) { push @change_opts, $sourceonly }
  370. if ($sourcestyle) { push @change_opts, $sourcestyle }
  371. if (defined($maint)) { push @change_opts, "-m$maint" }
  372. if (defined($changedby)) { push @change_opts, "-e$changedby" }
  373. if (defined($since)) { push @change_opts, "-v$since" }
  374. if (defined($desc)) { push @change_opts, "-C$desc" }
  375. my $chg = "../$pva.changes";
  376. print STDERR " dpkg-genchanges @change_opts >$chg\n";
  377. open CHANGES, '-|', 'dpkg-genchanges', @change_opts
  378. or subprocerr('dpkg-genchanges');
  379. open OUT, '>', $chg or syserr(_g('write changes file'));
  380. my $infiles = my $files = '';
  381. while ($_ = <CHANGES>) {
  382. print OUT $_ or syserr(_g('write changes file'));
  383. chomp;
  384. if (/^Files:/i) {
  385. $infiles = 1;
  386. } elsif ($infiles && /^\s+(.*)$/) {
  387. $files .= " $1 ";
  388. } elsif ($infiles && /^\S/) {
  389. $infiles = 0;
  390. }
  391. }
  392. close CHANGES or subprocerr(_g('dpkg-genchanges'));
  393. close OUT or syserr(_g('write changes file'));
  394. my $srcmsg;
  395. sub fileomitted($) { return $files !~ /$_[0]/ }
  396. if (fileomitted '\.deb') {
  397. # source only upload
  398. if (fileomitted "\.diff\.$comp_regex" and fileomitted "\.debian\.tar\.$comp_regex") {
  399. $srcmsg = _g('source only upload: Debian-native package');
  400. } elsif (fileomitted "\.orig\.tar\.$comp_regex") {
  401. $srcmsg = _g('source only, diff-only upload (original source NOT included)');
  402. } else {
  403. $srcmsg = _g('source only upload (original source is included)');
  404. }
  405. } else {
  406. $srcmsg = _g('full upload (original source is included)');
  407. if (fileomitted '\.dsc') {
  408. $srcmsg = _g('binary only upload (no source included)');
  409. } elsif (fileomitted "\.diff\.$comp_regex" and fileomitted "\.debian\.tar\.$comp_regex") {
  410. $srcmsg = _g('full upload; Debian-native package (full source is included)');
  411. } elsif (fileomitted "\.orig\.tar\.$comp_regex") {
  412. $srcmsg = _g('binary and diff upload (original source NOT included)');
  413. } else {
  414. $srcmsg = _g('full upload (original source is included)');
  415. }
  416. }
  417. if ($signchanges && signfile("$pva.changes")) {
  418. $signerrors = _g("Failed to sign .changes file");
  419. }
  420. if ($cleansource) {
  421. withecho(@rootcommand, @debian_rules, 'clean');
  422. }
  423. print "$progname: $srcmsg\n";
  424. if ($signerrors) {
  425. warning($signerrors);
  426. exit 1;
  427. }
  428. sub testcommand {
  429. my ($cmd) = @_;
  430. my $fullcmd = `which $cmd`;
  431. chomp $fullcmd;
  432. return $fullcmd && -x $fullcmd;
  433. }
  434. sub mustsetvar {
  435. my ($var, $text) = @_;
  436. error(_g("unable to determine %s"), $text)
  437. unless defined($var);
  438. print "$progname: $text $var\n";
  439. return $var;
  440. }
  441. sub withecho {
  442. shift while !$_[0];
  443. print STDERR " @_\n";
  444. system(@_)
  445. and subprocerr("@_");
  446. }
  447. sub signfile {
  448. my ($file) = @_;
  449. print STDERR " signfile $file\n";
  450. my $qfile = quotemeta($file);
  451. if ($signinterface eq 'gpg') {
  452. system("(cat ../$qfile ; echo '') | ".
  453. "$signcommand --utf8-strings --local-user "
  454. .quotemeta($signkey||$maintainer).
  455. " --clearsign --armor --textmode > ../$qfile.asc");
  456. } else {
  457. system("$signcommand -u ".quotemeta($signkey||$maintainer).
  458. " +clearsig=on -fast <../$qfile >../$qfile.asc");
  459. }
  460. my $status = $?;
  461. unless ($status) {
  462. system('mv', '--', "../$file.asc", "../$file")
  463. and subprocerr('mv');
  464. } else {
  465. system('rm', '-f', "../$file.asc")
  466. and subprocerr('rm -f');
  467. }
  468. print "\n";
  469. return $status
  470. }