dpkg-genbuildinfo.pl 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  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 = '1.0';
  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. return unless defined $facts->{pkg}->{$dep->{package}};
  183. $dep->{archqual} //= $architecture
  184. if any { $_[0]->{architecture} eq $architecture }, @{$facts->{pkg}->{$dep->{package}}};
  185. 1;
  186. });
  187. }
  188. # We add every sub-dependencies as we cannot know which package
  189. # in an OR dependency has been effectively used.
  190. deps_iterate($new_deps, sub {
  191. push @unprocessed_pkgs,
  192. $_[0]->{package} . (defined $_[0]->{archqual} ? ':' . $_[0]->{archqual} : '');
  193. 1
  194. });
  195. } elsif (defined $facts->{virtualpkg}->{$pkg_name}) {
  196. # virtual package: we cannot know for sure which implementation
  197. # is the one that has been used, so let's add them all...
  198. foreach my $provided (@{$facts->{virtualpkg}->{$pkg_name}}) {
  199. my ($provided_by, $provided_rel, $provided_ver) = @{$provided};
  200. push @unprocessed_pkgs, $provided_by;
  201. }
  202. }
  203. # else: it is a package in an OR dependency that has been otherwise
  204. # satisfied.
  205. }
  206. $installed_deps->simplify_deps(Dpkg::Deps::KnownFacts->new());
  207. $installed_deps->sort();
  208. $installed_deps = "\n" . $installed_deps->output();
  209. $installed_deps =~ s/, /,\n/g;
  210. return $installed_deps;
  211. }
  212. sub cleansed_environment {
  213. # Consider only whitelisted variables which are not supposed to leak
  214. # local user information.
  215. my %env = map {
  216. $_ => $ENV{$_}
  217. } grep {
  218. exists $ENV{$_}
  219. } get_build_env_whitelist();
  220. # Record flags from dpkg-buildflags.
  221. my $bf = Dpkg::BuildFlags->new();
  222. $bf->load_system_config();
  223. $bf->load_user_config();
  224. $bf->load_environment_config();
  225. foreach my $flag ($bf->list()) {
  226. next if $bf->get_origin($flag) eq 'vendor';
  227. # We do not need to record *_{STRIP,APPEND,PREPEND} as they
  228. # have been used already to compute the above value.
  229. $env{"DEB_${flag}_SET"} = $bf->get($flag);
  230. }
  231. return join "\n", map { $_ . '="' . ($env{$_} =~ s/"/\\"/gr) . '"' }
  232. sort keys %env;
  233. }
  234. sub version {
  235. printf g_("Debian %s version %s.\n"), $Dpkg::PROGNAME, $Dpkg::PROGVERSION;
  236. printf g_('
  237. This is free software; see the GNU General Public License version 2 or
  238. later for copying conditions. There is NO warranty.
  239. ');
  240. }
  241. sub usage {
  242. printf g_(
  243. 'Usage: %s [<option>...]')
  244. . "\n\n" . g_(
  245. "Options:
  246. --build=<type>[,...] specify the build <type>: full, source, binary,
  247. any, all (default is \'full\').
  248. -c<control-file> get control info from this file.
  249. -l<changelog-file> get per-version info from this file.
  250. -f<files-list-file> get .deb files list from this file.
  251. -F<changelog-format> force changelog format.
  252. -O[<buildinfo-file>] write to stdout (or <buildinfo-file>).
  253. -u<upload-files-dir> directory with files (default is '..').
  254. --always-include-path always include Build-Path.
  255. --admindir=<directory> change the administrative directory.
  256. -?, --help show this help message.
  257. --version show the version.
  258. "), $Dpkg::PROGNAME;
  259. }
  260. my $build_opts = Dpkg::BuildOptions->new();
  261. $build_opts->parse_features('buildinfo', \%use_feature);
  262. while (@ARGV) {
  263. $_ = shift @ARGV ;
  264. if (m/^--build=(.*)$/) {
  265. set_build_type_from_options($1, $_);
  266. } elsif (m/^-c(.*)$/) {
  267. $controlfile = $1;
  268. } elsif (m/^-l(.*)$/) {
  269. $changelogfile = $1;
  270. } elsif (m/^-f(.*)$/) {
  271. $fileslistfile = $1;
  272. } elsif (m/^-F([0-9a-z]+)$/) {
  273. $changelogformat = $1;
  274. } elsif (m/^-u(.*)$/) {
  275. $uploadfilesdir = $1;
  276. } elsif (m/^-O$/) {
  277. $stdout = 1;
  278. } elsif (m/^-O(.*)$/) {
  279. $outputfile = $1;
  280. } elsif (m/^--buildinfo-id=.*$/) {
  281. # Deprecated option
  282. warning('--buildinfo-id is deprecated, it is without effect');
  283. } elsif (m/^--always-include-path$/) {
  284. $use_feature{path} = 1;
  285. } elsif (m/^--admindir=(.*)$/) {
  286. $admindir = $1;
  287. } elsif (m/^-(?:\?|-help)$/) {
  288. usage();
  289. exit(0);
  290. } elsif (m/^--version$/) {
  291. version();
  292. exit(0);
  293. } else {
  294. usageerr(g_("unknown option '%s'"), $_);
  295. }
  296. }
  297. my $control = Dpkg::Control::Info->new($controlfile);
  298. my $fields = Dpkg::Control->new(type => CTRL_FILE_BUILDINFO);
  299. my $dist = Dpkg::Dist::Files->new();
  300. # Retrieve info from the current changelog entry.
  301. my %options = (file => $changelogfile);
  302. $options{changelogformat} = $changelogformat if $changelogformat;
  303. my $changelog = changelog_parse(%options);
  304. # Retrieve info from the former changelog entry to handle binNMUs.
  305. $options{count} = 1;
  306. $options{offset} = 1;
  307. my $prev_changelog = changelog_parse(%options);
  308. my $sourceversion = $changelog->{'Binary-Only'} ?
  309. $prev_changelog->{'Version'} : $changelog->{'Version'};
  310. my $binaryversion = $changelog->{'Version'};
  311. # Include .dsc if available.
  312. my $spackage = $changelog->{'Source'};
  313. (my $sversion = $sourceversion) =~ s/^\d+://;
  314. if (build_has_any(BUILD_SOURCE)) {
  315. my $dsc = "${spackage}_${sversion}.dsc";
  316. $checksums->add_from_file("$uploadfilesdir/$dsc", key => $dsc);
  317. push @archvalues, 'source';
  318. }
  319. my $dist_count = 0;
  320. $dist_count = $dist->load($fileslistfile) if -e $fileslistfile;
  321. if (build_has_any(BUILD_BINARY)) {
  322. error(g_('binary build with no binary artifacts found; .buildinfo is meaningless'))
  323. if $dist_count == 0;
  324. foreach my $file ($dist->get_files()) {
  325. # Make us a bit idempotent.
  326. next if $file->{filename} =~ m/\.buildinfo$/;
  327. my $path = "$uploadfilesdir/$file->{filename}";
  328. $checksums->add_from_file($path, key => $file->{filename});
  329. if (defined $file->{package_type} and $file->{package_type} =~ m/^u?deb$/) {
  330. push @archvalues, $file->{arch}
  331. if defined $file->{arch} and not $archadded{$file->{arch}}++;
  332. }
  333. }
  334. }
  335. $fields->{'Format'} = $buildinfo_format;
  336. $fields->{'Source'} = $spackage;
  337. $fields->{'Binary'} = join(' ', map { $_->{'Package'} } $control->get_packages());
  338. # Avoid overly long line by splitting over multiple lines.
  339. if (length($fields->{'Binary'}) > 980) {
  340. $fields->{'Binary'} =~ s/(.{0,980}) /$1\n/g;
  341. }
  342. $fields->{'Architecture'} = join ' ', sort @archvalues;
  343. $fields->{'Version'} = $binaryversion;
  344. if ($changelog->{'Binary-Only'}) {
  345. $fields->{'Source'} .= ' (' . $sourceversion . ')';
  346. $fields->{'Binary-Only-Changes'} =
  347. $changelog->{'Changes'} . "\n\n"
  348. . ' -- ' . $changelog->{'Maintainer'}
  349. . ' ' . $changelog->{'Date'};
  350. }
  351. $fields->{'Build-Origin'} = get_current_vendor();
  352. $fields->{'Build-Architecture'} = get_build_arch();
  353. $fields->{'Build-Date'} = get_build_date();
  354. my $cwd = cwd();
  355. if ($use_feature{path}) {
  356. $fields->{'Build-Path'} = $cwd;
  357. } else {
  358. # Only include the build path if its root path is considered acceptable
  359. # by the vendor.
  360. foreach my $root_path (run_vendor_hook('builtin-system-build-paths')) {
  361. if (index($cwd, $root_path) == 0) {
  362. $fields->{'Build-Path'} = $cwd;
  363. last;
  364. }
  365. }
  366. }
  367. $checksums->export_to_control($fields);
  368. $fields->{'Installed-Build-Depends'} = collect_installed_builddeps($control);
  369. $fields->{'Environment'} = "\n" . cleansed_environment();
  370. # Generate the buildinfo filename.
  371. if ($stdout) {
  372. # Nothing to do.
  373. } elsif (defined $outputfile) {
  374. $buildinfo = basename($outputfile);
  375. } else {
  376. my $arch;
  377. if (build_has_any(BUILD_ARCH_DEP)) {
  378. $arch = get_host_arch();
  379. } elsif (build_has_any(BUILD_ARCH_INDEP)) {
  380. $arch = 'all';
  381. } elsif (build_has_any(BUILD_SOURCE)) {
  382. $arch = 'source';
  383. }
  384. $buildinfo = "${spackage}_${sversion}_${arch}.buildinfo";
  385. $outputfile = "$uploadfilesdir/$buildinfo";
  386. }
  387. # Write out the generated .buildinfo file.
  388. if ($stdout) {
  389. $fields->output(\*STDOUT);
  390. } else {
  391. my $section = $control->get_source->{'Section'} || '-';
  392. my $priority = $control->get_source->{'Priority'} || '-';
  393. # Obtain a lock on debian/control to avoid simultaneous updates
  394. # of debian/files when parallel building is in use
  395. my $lockfh;
  396. my $lockfile = 'debian/control';
  397. $lockfile = $controlfile if not -e $lockfile;
  398. sysopen $lockfh, $lockfile, O_WRONLY
  399. or syserr(g_('cannot write %s'), $lockfile);
  400. file_lock($lockfh, $lockfile);
  401. $dist = Dpkg::Dist::Files->new();
  402. $dist->load($fileslistfile) if -e $fileslistfile;
  403. $dist->add_file($buildinfo, $section, $priority);
  404. $dist->save("$fileslistfile.new");
  405. rename "$fileslistfile.new", $fileslistfile
  406. or syserr(g_('install new files list file'));
  407. # Release the lock
  408. close $lockfh or syserr(g_('cannot close %s'), $lockfile);
  409. $fields->save("$outputfile.new");
  410. rename "$outputfile.new", $outputfile
  411. or syserr(g_("cannot install output buildinfo file '%s'"), $outputfile);
  412. }
  413. 1;