scripts/mk: On dpkg-build-api >= 1 include buildtools.mk in default.mk
[dpkg.git] / scripts / dpkg-gensymbols.pl
blob0bb0bc0d9a5bfbf1818efe8914e03d7dda524a86
1 #!/usr/bin/perl
3 # dpkg-gensymbols
5 # Copyright © 2007 Raphaël Hertzog
6 # Copyright © 2007-2013 Guillem Jover <guillem@debian.org>
8 # This program is free software; you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 2 of the License, or
11 # (at your option) any later version.
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with this program. If not, see <https://www.gnu.org/licenses/>.
21 use strict;
22 use warnings;
24 use Dpkg ();
25 use Dpkg::Arch qw(get_host_arch);
26 use Dpkg::Package;
27 use Dpkg::BuildAPI qw(get_build_api);
28 use Dpkg::Shlibs qw(get_library_paths);
29 use Dpkg::Shlibs::Objdump;
30 use Dpkg::Shlibs::SymbolFile;
31 use Dpkg::Gettext;
32 use Dpkg::ErrorHandling;
33 use Dpkg::Control::Info;
34 use Dpkg::Changelog::Parse;
35 use Dpkg::Path qw(check_files_are_the_same find_command);
37 textdomain('dpkg-dev');
39 my $packagebuilddir = 'debian/tmp';
41 my $sourceversion;
42 my $stdout;
43 my $oppackage;
44 my $compare = 1; # Bail on missing symbols by default
45 my $quiet = 0;
46 my $input;
47 my $output;
48 my $template_mode = 0; # non-template mode by default
49 my $verbose_output = 0;
50 my $debug = 0;
51 my $host_arch = get_host_arch();
53 sub version {
54 printf g_("Debian %s version %s.\n"), $Dpkg::PROGNAME, $Dpkg::PROGVERSION;
56 printf g_('
57 This is free software; see the GNU General Public License version 2 or
58 later for copying conditions. There is NO warranty.
59 ');
62 sub usage {
63 printf g_(
64 'Usage: %s [<option>...]')
65 . "\n\n" . g_(
66 'Options:
67 -l<library-path> add directory to private shared library search list.
68 -p<package> generate symbols file for package.
69 -P<package-build-dir> temporary build directory instead of debian/tmp.
70 -e<library> explicitly list libraries to scan.
71 -v<version> version of the packages (defaults to
72 version extracted from debian/changelog).
73 -c<level> compare generated symbols file with the reference
74 template in the debian directory and fail if
75 difference is too important; level goes from 0 for
76 no check, to 4 for all checks (default level is 1).
77 -q keep quiet and never emit any warnings or
78 generate a diff between generated symbols
79 file and the reference template.
80 -I<file> force usage of <file> as reference symbols
81 file instead of the default file.
82 -O[<file>] write to stdout (or <file>), not .../DEBIAN/symbols.
83 -t write in template mode (tags are not
84 processed and included in output).
85 -V verbose output; write deprecated symbols and pattern
86 matching symbols as comments (in template mode only).
87 -a<arch> assume <arch> as host architecture when processing
88 symbol files.
89 -d display debug information during work.
90 -?, --help show this help message.
91 --version show the version.
92 '), $Dpkg::PROGNAME;
95 my @files;
96 while (@ARGV) {
97 $_ = shift(@ARGV);
98 if (m/^-p/p) {
99 $oppackage = ${^POSTMATCH};
100 my $err = pkg_name_is_illegal($oppackage);
101 error(g_("illegal package name '%s': %s"), $oppackage, $err) if $err;
102 } elsif (m/^-l(.*)$/) {
103 Dpkg::Shlibs::add_library_dir($1);
104 } elsif (m/^-c(\d)?$/) {
105 $compare = $1 // 1;
106 } elsif (m/^-q$/) {
107 $quiet = 1;
108 } elsif (m/^-d$/) {
109 $debug = 1;
110 } elsif (m/^-v(.+)$/) {
111 $sourceversion = $1;
112 } elsif (m/^-e(.+)$/) {
113 my $file = $1;
114 if (-e $file) {
115 push @files, $file;
116 } else {
117 my @to_add = glob($file);
118 push @files, @to_add;
119 warning(g_("pattern '%s' did not match any file"), $file)
120 unless scalar(@to_add);
122 } elsif (m/^-P(.+)$/) {
123 $packagebuilddir = $1;
124 $packagebuilddir =~ s{/+$}{};
125 } elsif (m/^-O$/) {
126 $stdout = 1;
127 } elsif (m/^-I(.+)$/) {
128 $input = $1;
129 } elsif (m/^-O(.+)$/) {
130 $output = $1;
131 } elsif (m/^-t$/) {
132 $template_mode = 1;
133 } elsif (m/^-V$/) {
134 $verbose_output = 1;
135 } elsif (m/^-a(.+)$/) {
136 $host_arch = $1;
137 } elsif (m/^-(?:\?|-help)$/) {
138 usage();
139 exit(0);
140 } elsif (m/^--version$/) {
141 version();
142 exit(0);
143 } else {
144 usageerr(g_("unknown option '%s'"), $_);
148 report_options(debug_level => $debug);
150 umask 0022; # ensure sane default permissions for created files
152 if (exists $ENV{DPKG_GENSYMBOLS_CHECK_LEVEL}) {
153 $compare = $ENV{DPKG_GENSYMBOLS_CHECK_LEVEL};
156 if (not defined($sourceversion)) {
157 my $changelog = changelog_parse();
158 $sourceversion = $changelog->{'Version'};
160 my $control = Dpkg::Control::Info->new();
161 # Initialize the build API level.
162 get_build_api($control);
163 if (not defined($oppackage)) {
164 my @packages = map { $_->{'Package'} } $control->get_packages();
165 if (@packages == 0) {
166 error(g_('no package stanza found in control info'));
167 } elsif (@packages > 1) {
168 error(g_('must specify package since control info has many (%s)'),
169 "@packages");
171 $oppackage = $packages[0];
174 my $symfile = Dpkg::Shlibs::SymbolFile->new(arch => $host_arch);
175 my $ref_symfile = Dpkg::Shlibs::SymbolFile->new(arch => $host_arch);
176 # Load source-provided symbol information
177 foreach my $file ($input, $output, "debian/$oppackage.symbols.$host_arch",
178 "debian/symbols.$host_arch", "debian/$oppackage.symbols",
179 'debian/symbols')
181 if (defined $file and -e $file) {
182 debug(1, "Using references symbols from $file");
183 $symfile->load($file);
184 $ref_symfile->load($file) if $compare || ! $quiet;
185 last;
189 # Scan package build dir looking for libraries
190 if (not scalar @files) {
191 PATH: foreach my $path (get_library_paths()) {
192 my $libdir = "$packagebuilddir$path";
193 $libdir =~ s{/+}{/}g;
194 lstat $libdir;
195 next if not -d _;
196 next if -l _; # Skip directories which are symlinks
197 # Skip any directory _below_ a symlink as well
198 my $updir = $libdir;
199 while (($updir =~ s{/[^/]*$}{}) and
200 not check_files_are_the_same($packagebuilddir, $updir)) {
201 next PATH if -l $updir;
203 opendir(my $libdir_dh, "$libdir")
204 or syserr(g_("can't read directory %s: %s"), $libdir, $!);
205 push @files, grep {
206 /(\.so\.|\.so$)/ && -f &&
207 Dpkg::Shlibs::Objdump::is_elf($_);
208 } map { "$libdir/$_" } readdir($libdir_dh);
209 closedir $libdir_dh;
213 # Merge symbol information
214 my $od = Dpkg::Shlibs::Objdump->new();
215 foreach my $file (@files) {
216 debug(1, "Scanning $file for symbol information");
217 my $objid = $od->analyze($file);
218 unless (defined($objid) && $objid) {
219 warning(g_("Dpkg::Shlibs::Objdump couldn't parse %s\n"), $file);
220 next;
222 my $object = $od->get_object($objid);
223 if ($object->{SONAME}) { # Objects without soname are of no interest
224 debug(1, "Merging symbols from $file as $object->{SONAME}");
225 if (not $symfile->has_object($object->{SONAME})) {
226 $symfile->create_object($object->{SONAME}, "$oppackage #MINVER#");
228 $symfile->merge_symbols($object, $sourceversion);
229 } else {
230 debug(1, "File $file doesn't have a soname. Ignoring.");
233 $symfile->clear_except(keys %{$od->{objects}});
235 # Write out symbols files
236 if ($stdout) {
237 $output = g_('<standard output>');
238 $symfile->output(\*STDOUT, package => $oppackage,
239 template_mode => $template_mode,
240 with_pattern_matches => $verbose_output,
241 with_deprecated => $verbose_output);
242 } else {
243 unless (defined($output)) {
244 unless ($symfile->is_empty()) {
245 $output = "$packagebuilddir/DEBIAN/symbols";
246 mkdir("$packagebuilddir/DEBIAN") if not -e "$packagebuilddir/DEBIAN";
249 if (defined($output)) {
250 debug(1, "Storing symbols in $output.");
251 $symfile->save($output, package => $oppackage,
252 template_mode => $template_mode,
253 with_pattern_matches => $verbose_output,
254 with_deprecated => $verbose_output);
255 } else {
256 debug(1, 'No symbol information to store.');
260 # Check if generated files differs from reference file
261 my $exitcode = 0;
263 sub compare_problem
265 my ($level, $msg, @args) = @_;
267 if ($compare >= $level) {
268 errormsg($msg, @args);
269 $exitcode = $level;
270 } else {
271 warning($msg, @args) unless $quiet;
275 if ($compare || ! $quiet) {
276 # Compare
277 if (my @libs = $symfile->get_new_libs($ref_symfile)) {
278 compare_problem(4, g_('new libraries appeared in the symbols file: %s'), "@libs");
280 if (my @libs = $symfile->get_lost_libs($ref_symfile)) {
281 compare_problem(3, g_('some libraries disappeared in the symbols file: %s'), "@libs");
283 if ($symfile->get_new_symbols($ref_symfile)) {
284 compare_problem(2, g_('some new symbols appeared in the symbols file: %s'),
285 g_('see diff output below'));
287 if ($symfile->get_lost_symbols($ref_symfile)) {
288 compare_problem(1, g_('some symbols or patterns disappeared in the symbols file: %s'),
289 g_('see diff output below'))
293 unless ($quiet) {
294 require File::Temp;
295 require File::Compare;
297 my $file_label;
299 # Compare template symbols files before and after
300 my $before = File::Temp->new(TEMPLATE => 'dpkg-gensymbolsXXXXXX');
301 my $after = File::Temp->new(TEMPLATE => 'dpkg-gensymbolsXXXXXX');
302 if ($ref_symfile->{file}) {
303 $file_label = $ref_symfile->{file};
304 } else {
305 $file_label = 'new_symbol_file';
307 $ref_symfile->output($before, package => $oppackage, template_mode => 1);
308 $symfile->output($after, package => $oppackage, template_mode => 1);
310 seek $before, 0, 0;
311 seek $after, 0, 0;
313 # Output diffs between symbols files if any
314 if (File::Compare::compare($before, $after) != 0) {
315 if (not defined($output)) {
316 warning(g_('the generated symbols file is empty'));
317 } elsif (defined($ref_symfile->{file})) {
318 warning(g_("%s doesn't match completely %s"),
319 $output, $ref_symfile->{file});
320 } else {
321 warning(g_('no debian/symbols file used as basis for generating %s'),
322 $output);
324 my ($a, $b) = ($before->filename, $after->filename);
325 my $diff_label = sprintf('%s (%s_%s_%s)', $file_label, $oppackage,
326 $sourceversion, $host_arch);
327 system('diff', '-u', '-L', $diff_label, $a, $b) if find_command('diff');
330 exit($exitcode);