dpkg-genbuildinfo.pl 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  1. #!/usr/bin/perl
  2. #
  3. # dpkg-genbuildinfo
  4. #
  5. # Copyright © 1996 Ian Jackson
  6. # Copyright © 2000,2001 Wichert Akkerman
  7. # Copyright © 2003-2013 Yann Dirson <dirson@debian.org>
  8. # Copyright © 2006-2016 Guillem Jover <guillem@debian.org>
  9. # Copyright © 2014 Niko Tyni <ntyni@debian.org>
  10. # Copyright © 2014-2015 Jérémy Bobbio <lunar@debian.org>
  11. #
  12. # This program is free software; you can redistribute it and/or modify
  13. # it under the terms of the GNU General Public License as published by
  14. # the Free Software Foundation; either version 2 of the License, or
  15. # (at your option) any later version.
  16. #
  17. # This program is distributed in the hope that it will be useful,
  18. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. # GNU General Public License for more details.
  21. #
  22. # You should have received a copy of the GNU General Public License
  23. # along with this program. If not, see <https://www.gnu.org/licenses/>.
  24. use strict;
  25. use warnings;
  26. use Cwd;
  27. use File::Basename;
  28. use POSIX qw(:fcntl_h :locale_h strftime);
  29. use Dpkg ();
  30. use Dpkg::Gettext;
  31. use Dpkg::Checksums;
  32. use Dpkg::ErrorHandling;
  33. use Dpkg::Arch qw(get_build_arch get_host_arch);
  34. use Dpkg::Build::Types;
  35. use Dpkg::Build::Info qw(get_build_env_whitelist);
  36. use Dpkg::BuildOptions;
  37. use Dpkg::BuildFlags;
  38. use Dpkg::BuildProfiles qw(get_build_profiles);
  39. use Dpkg::Control::Info;
  40. use Dpkg::Control::Fields;
  41. use Dpkg::Control;
  42. use Dpkg::Changelog::Parse;
  43. use Dpkg::Deps;
  44. use Dpkg::Dist::Files;
  45. use Dpkg::Util qw(:list);
  46. use Dpkg::File;
  47. use Dpkg::Version;
  48. use Dpkg::Vendor qw(get_current_vendor run_vendor_hook);
  49. textdomain('dpkg-dev');
  50. my $controlfile = 'debian/control';
  51. my $changelogfile = 'debian/changelog';
  52. my $changelogformat;
  53. my $fileslistfile = 'debian/files';
  54. my $uploadfilesdir = '..';
  55. my $outputfile;
  56. my $stdout = 0;
  57. my $admindir = $Dpkg::ADMINDIR;
  58. my %use_feature = (
  59. path => 0,
  60. );
  61. my @build_profiles = get_build_profiles();
  62. my $buildinfo_format = '0.2';
  63. my $buildinfo;
  64. my $checksums = Dpkg::Checksums->new();
  65. my %archadded;
  66. my @archvalues;
  67. sub get_build_date {
  68. my $date;
  69. setlocale(LC_TIME, 'C');
  70. $date = strftime('%a, %d %b %Y %T %z', localtime);
  71. setlocale(LC_TIME, '');
  72. return $date;
  73. }
  74. # There is almost the same function in dpkg-checkbuilddeps, they probably
  75. # should be factored out.
  76. sub parse_status {
  77. my $status = shift;
  78. my $facts = Dpkg::Deps::KnownFacts->new();
  79. my %depends;
  80. my @essential_pkgs;
  81. local $/ = '';
  82. open my $status_fh, '<', $status or syserr(g_('cannot open %s'), $status);
  83. while (<$status_fh>) {
  84. next unless /^Status: .*ok installed$/m;
  85. my ($package) = /^Package: (.*)$/m;
  86. my ($version) = /^Version: (.*)$/m;
  87. my ($arch) = /^Architecture: (.*)$/m;
  88. my ($multiarch) = /^Multi-Arch: (.*)$/m;
  89. $facts->add_installed_package($package, $version, $arch, $multiarch);
  90. if (/^Essential: yes$/m) {
  91. push @essential_pkgs, $package;
  92. }
  93. if (/^Provides: (.*)$/m) {
  94. my $provides = deps_parse($1, reduce_arch => 1, union => 1);
  95. next if not defined $provides;
  96. deps_iterate($provides, sub {
  97. my $dep = shift;
  98. $facts->add_provided_package($dep->{package}, $dep->{relation},
  99. $dep->{version}, $package);
  100. });
  101. }
  102. foreach my $deptype (qw(Pre-Depends Depends)) {
  103. next unless /^$deptype: (.*)$/m;
  104. my $depends = $1;
  105. foreach (split /,\s*/, $depends) {
  106. push @{$depends{"$package:$arch"}}, $_;
  107. }
  108. }
  109. }
  110. close $status_fh;
  111. return ($facts, \%depends, \@essential_pkgs);
  112. }
  113. sub append_deps {
  114. my $pkgs = shift;
  115. foreach my $dep_str (@_) {
  116. next unless $dep_str;
  117. my $deps = deps_parse($dep_str, reduce_restrictions => 1,
  118. build_dep => 1,
  119. build_profiles => \@build_profiles);
  120. # We add every sub-dependencies as we cannot know which package in
  121. # an OR dependency has been effectively used.
  122. deps_iterate($deps, sub {
  123. push @{$pkgs},
  124. $_[0]->{package} . (defined $_[0]->{archqual} ? ':' . $_[0]->{archqual} : '');
  125. 1
  126. });
  127. }
  128. }
  129. sub collect_installed_builddeps {
  130. my $control = shift;
  131. my ($facts, $depends, $essential_pkgs) = parse_status("$admindir/status");
  132. my %seen_pkgs;
  133. my @unprocessed_pkgs;
  134. # Parse essential packages list.
  135. append_deps(\@unprocessed_pkgs,
  136. @{$essential_pkgs},
  137. run_vendor_hook('builtin-build-depends'),
  138. $control->get_source->{'Build-Depends'});
  139. if (build_has_any(BUILD_ARCH_DEP)) {
  140. append_deps(\@unprocessed_pkgs,
  141. $control->get_source->{'Build-Depends-Arch'});
  142. }
  143. if (build_has_any(BUILD_ARCH_INDEP)) {
  144. append_deps(\@unprocessed_pkgs,
  145. $control->get_source->{'Build-Depends-Indep'});
  146. }
  147. my $installed_deps = Dpkg::Deps::AND->new();
  148. while (my $pkg_name = shift @unprocessed_pkgs) {
  149. next if $seen_pkgs{$pkg_name};
  150. $seen_pkgs{$pkg_name} = 1;
  151. my $required_architecture;
  152. if ($pkg_name =~ /\A(.*):(.*)\z/) {
  153. $pkg_name = $1;
  154. my $arch = $2;
  155. $required_architecture = $arch if $arch !~ /\A(?:all|any|native)\Z/
  156. }
  157. my $pkg;
  158. my $qualified_pkg_name;
  159. foreach my $installed_pkg (@{$facts->{pkg}->{$pkg_name}}) {
  160. if (!defined $required_architecture ||
  161. $required_architecture eq $installed_pkg->{architecture}) {
  162. $pkg = $installed_pkg;
  163. $qualified_pkg_name = $pkg_name . ':' . $installed_pkg->{architecture};
  164. last;
  165. }
  166. }
  167. if (defined $pkg) {
  168. my $version = $pkg->{version};
  169. my $architecture = $pkg->{architecture};
  170. my $new_deps_str = defined $depends->{$qualified_pkg_name} ? deps_concat(@{$depends->{$qualified_pkg_name}}) : '';
  171. my $new_deps = deps_parse($new_deps_str);
  172. if (!defined $required_architecture) {
  173. $installed_deps->add(Dpkg::Deps::Simple->new("$pkg_name (= $version)"));
  174. } else {
  175. $installed_deps->add(Dpkg::Deps::Simple->new("$qualified_pkg_name (= $version)"));
  176. # Dependencies of foreign packages are also foreign packages
  177. # (or Arch:all) so we need to qualify them as well. We figure
  178. # out if the package is actually foreign by searching for an
  179. # installed package of the right architecture.
  180. deps_iterate($new_deps, sub {
  181. my $dep = shift;
  182. $dep->{archqual} //= $architecture
  183. if any { $_[0]->{architecture} eq $architecture }, @{$facts->{pkg}->{$dep->{package}}};
  184. 1;
  185. });
  186. }
  187. # We add every sub-dependencies as we cannot know which package
  188. # in an OR dependency has been effectively used.
  189. deps_iterate($new_deps, sub {
  190. push @unprocessed_pkgs,
  191. $_[0]->{package} . (defined $_[0]->{archqual} ? ':' . $_[0]->{archqual} : '');
  192. 1
  193. });
  194. } elsif (defined $facts->{virtualpkg}->{$pkg_name}) {
  195. # virtual package: we cannot know for sure which implementation
  196. # is the one that has been used, so let's add them all...
  197. foreach my $provided (@{$facts->{virtualpkg}->{$pkg_name}}) {
  198. my ($provided_by, $provided_rel, $provided_ver) = @{$provided};
  199. push @unprocessed_pkgs, $provided_by;
  200. }
  201. }
  202. # else: it is a package in an OR dependency that has been otherwise
  203. # satisfied.
  204. }
  205. $installed_deps->simplify_deps(Dpkg::Deps::KnownFacts->new());
  206. $installed_deps->sort();
  207. $installed_deps = "\n" . $installed_deps->output();
  208. $installed_deps =~ s/, /,\n/g;
  209. return $installed_deps;
  210. }
  211. sub cleansed_environment {
  212. # Consider only whitelisted variables which are not supposed to leak
  213. # local user information.
  214. my %env = map {
  215. $_ => $ENV{$_}
  216. } grep {
  217. exists $ENV{$_}
  218. } get_build_env_whitelist();
  219. # Record flags from dpkg-buildflags.
  220. my $bf = Dpkg::BuildFlags->new();
  221. $bf->load_system_config();
  222. $bf->load_user_config();
  223. $bf->load_environment_config();
  224. foreach my $flag ($bf->list()) {
  225. next if $bf->get_origin($flag) eq 'vendor';
  226. # We do not need to record *_{STRIP,APPEND,PREPEND} as they
  227. # have been used already to compute the above value.
  228. $env{"DEB_${flag}_SET"} = $bf->get($flag);
  229. }
  230. return join "\n", map { $_ . '="' . ($env{$_} =~ s/"/\\"/gr) . '"' }
  231. sort keys %env;
  232. }
  233. sub version {
  234. printf g_("Debian %s version %s.\n"), $Dpkg::PROGNAME, $Dpkg::PROGVERSION;
  235. printf g_('
  236. This is free software; see the GNU General Public License version 2 or
  237. later for copying conditions. There is NO warranty.
  238. ');
  239. }
  240. sub usage {
  241. printf g_(
  242. 'Usage: %s [<option>...]')
  243. . "\n\n" . g_(
  244. "Options:
  245. --build=<type>[,...] specify the build <type>: full, source, binary,
  246. any, all (default is \'full\').
  247. -c<control-file> get control info from this file.
  248. -l<changelog-file> get per-version info from this file.
  249. -f<files-list-file> get .deb files list from this file.
  250. -F<changelog-format> force changelog format.
  251. -O[<buildinfo-file>] write to stdout (or <buildinfo-file>).
  252. -u<upload-files-dir> directory with files (default is '..').
  253. --always-include-path always include Build-Path.
  254. --admindir=<directory> change the administrative directory.
  255. -?, --help show this help message.
  256. --version show the version.
  257. "), $Dpkg::PROGNAME;
  258. }
  259. my $build_opts = Dpkg::BuildOptions->new();
  260. $build_opts->parse_features('buildinfo', \%use_feature);
  261. while (@ARGV) {
  262. $_ = shift @ARGV ;
  263. if (m/^--build=(.*)$/) {
  264. set_build_type_from_options($1, $_);
  265. } elsif (m/^-c(.*)$/) {
  266. $controlfile = $1;
  267. } elsif (m/^-l(.*)$/) {
  268. $changelogfile = $1;
  269. } elsif (m/^-f(.*)$/) {
  270. $fileslistfile = $1;
  271. } elsif (m/^-F([0-9a-z]+)$/) {
  272. $changelogformat = $1;
  273. } elsif (m/^-u(.*)$/) {
  274. $uploadfilesdir = $1;
  275. } elsif (m/^-O$/) {
  276. $stdout = 1;
  277. } elsif (m/^-O(.*)$/) {
  278. $outputfile = $1;
  279. } elsif (m/^--buildinfo-id=.*$/) {
  280. # Deprecated option
  281. warning('--buildinfo-id is deprecated, it is without effect');
  282. } elsif (m/^--always-include-path$/) {
  283. $use_feature{path} = 1;
  284. } elsif (m/^--admindir=(.*)$/) {
  285. $admindir = $1;
  286. } elsif (m/^-(?:\?|-help)$/) {
  287. usage();
  288. exit(0);
  289. } elsif (m/^--version$/) {
  290. version();
  291. exit(0);
  292. } else {
  293. usageerr(g_("unknown option '%s'"), $_);
  294. }
  295. }
  296. my $control = Dpkg::Control::Info->new($controlfile);
  297. my $fields = Dpkg::Control->new(type => CTRL_FILE_BUILDINFO);
  298. my $dist = Dpkg::Dist::Files->new();
  299. # Retrieve info from the current changelog entry.
  300. my %options = (file => $changelogfile);
  301. $options{changelogformat} = $changelogformat if $changelogformat;
  302. my $changelog = changelog_parse(%options);
  303. # Retrieve info from the former changelog entry to handle binNMUs.
  304. $options{count} = 1;
  305. $options{offset} = 1;
  306. my $prev_changelog = changelog_parse(%options);
  307. my $sourceversion = $changelog->{'Binary-Only'} ?
  308. $prev_changelog->{'Version'} : $changelog->{'Version'};
  309. my $binaryversion = $changelog->{'Version'};
  310. # Include .dsc if available.
  311. my $spackage = $changelog->{'Source'};
  312. (my $sversion = $sourceversion) =~ s/^\d+://;
  313. if (build_has_any(BUILD_SOURCE)) {
  314. my $dsc = "${spackage}_${sversion}.dsc";
  315. $checksums->add_from_file("$uploadfilesdir/$dsc", key => $dsc);
  316. push @archvalues, 'source';
  317. }
  318. my $dist_count = 0;
  319. $dist_count = $dist->load($fileslistfile) if -e $fileslistfile;
  320. if (build_has_any(BUILD_BINARY)) {
  321. error(g_('binary build with no binary artifacts found; .buildinfo is meaningless'))
  322. if $dist_count == 0;
  323. foreach my $file ($dist->get_files()) {
  324. # Make us a bit idempotent.
  325. next if $file->{filename} =~ m/\.buildinfo$/;
  326. my $path = "$uploadfilesdir/$file->{filename}";
  327. $checksums->add_from_file($path, key => $file->{filename});
  328. if (defined $file->{package_type} and $file->{package_type} =~ m/^u?deb$/) {
  329. push @archvalues, $file->{arch}
  330. if defined $file->{arch} and not $archadded{$file->{arch}}++;
  331. }
  332. }
  333. }
  334. $fields->{'Format'} = $buildinfo_format;
  335. $fields->{'Source'} = $spackage;
  336. $fields->{'Binary'} = join(' ', map { $_->{'Package'} } $control->get_packages());
  337. # Avoid overly long line by splitting over multiple lines.
  338. if (length($fields->{'Binary'}) > 980) {
  339. $fields->{'Binary'} =~ s/(.{0,980}) /$1\n/g;
  340. }
  341. $fields->{'Architecture'} = join ' ', sort @archvalues;
  342. $fields->{'Version'} = $binaryversion;
  343. if ($changelog->{'Binary-Only'}) {
  344. $fields->{'Source'} .= ' (' . $sourceversion . ')';
  345. $fields->{'Binary-Only-Changes'} =
  346. $changelog->{'Changes'} . "\n\n"
  347. . ' -- ' . $changelog->{'Maintainer'}
  348. . ' ' . $changelog->{'Date'};
  349. }
  350. $fields->{'Build-Origin'} = get_current_vendor();
  351. $fields->{'Build-Architecture'} = get_build_arch();
  352. $fields->{'Build-Date'} = get_build_date();
  353. my $cwd = cwd();
  354. if ($use_feature{path}) {
  355. $fields->{'Build-Path'} = $cwd;
  356. } else {
  357. # Only include the build path if its root path is considered acceptable
  358. # by the vendor.
  359. foreach my $root_path (run_vendor_hook('builtin-system-build-paths')) {
  360. if (index($cwd, $root_path) == 0) {
  361. $fields->{'Build-Path'} = $cwd;
  362. last;
  363. }
  364. }
  365. }
  366. $checksums->export_to_control($fields);
  367. $fields->{'Installed-Build-Depends'} = collect_installed_builddeps($control);
  368. $fields->{'Environment'} = "\n" . cleansed_environment();
  369. # Generate the buildinfo filename.
  370. if ($stdout) {
  371. # Nothing to do.
  372. } elsif (defined $outputfile) {
  373. $buildinfo = basename($outputfile);
  374. } else {
  375. my $arch;
  376. if (build_has_any(BUILD_ARCH_DEP)) {
  377. $arch = get_host_arch();
  378. } elsif (build_has_any(BUILD_ARCH_INDEP)) {
  379. $arch = 'all';
  380. } elsif (build_has_any(BUILD_SOURCE)) {
  381. $arch = 'source';
  382. }
  383. $buildinfo = "${spackage}_${sversion}_${arch}.buildinfo";
  384. $outputfile = "$uploadfilesdir/$buildinfo";
  385. }
  386. # Write out the generated .buildinfo file.
  387. if ($stdout) {
  388. $fields->output(\*STDOUT);
  389. } else {
  390. my $section = $control->get_source->{'Section'} || '-';
  391. my $priority = $control->get_source->{'Priority'} || '-';
  392. # Obtain a lock on debian/control to avoid simultaneous updates
  393. # of debian/files when parallel building is in use
  394. my $lockfh;
  395. my $lockfile = 'debian/control';
  396. $lockfile = $controlfile if not -e $lockfile;
  397. sysopen $lockfh, $lockfile, O_WRONLY
  398. or syserr(g_('cannot write %s'), $lockfile);
  399. file_lock($lockfh, $lockfile);
  400. $dist = Dpkg::Dist::Files->new();
  401. $dist->load($fileslistfile) if -e $fileslistfile;
  402. $dist->add_file($buildinfo, $section, $priority);
  403. $dist->save("$fileslistfile.new");
  404. rename "$fileslistfile.new", $fileslistfile
  405. or syserr(g_('install new files list file'));
  406. # Release the lock
  407. close $lockfh or syserr(g_('cannot close %s'), $lockfile);
  408. $fields->save("$outputfile.new");
  409. rename "$outputfile.new", $outputfile
  410. or syserr(g_("cannot install output buildinfo file '%s'"), $outputfile);
  411. }
  412. 1;