dpkg-genbuildinfo.pl 15 KB

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