dpkg-genbuildinfo.pl 15 KB

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