Debian Codegraph @

debian/dh_girepository

raw
1 #! /usr/bin/perl -w
2
3 =head1 NAME
4
5 dh_girepository - compute dependencies for GObject introspection packages
6
7 =cut
8
9 use strict;
10 use Cwd qw(realpath);
11 use File::Find;
12 use Dpkg::Deps;
13 use Dpkg::Control;
14 use Debian::Debhelper::Dh_Lib;
15
16 =head1 SYNOPSIS
17
18 B<dh_girepository> [I<debhelper options>] [-lI<directory>] [-pI<directory>] [-XI<item>] [I<private [...]>]
19
20 =head1 DESCRIPTION
21
22 dh_girepository is a debhelper program to compute dependencies for packages
23 shipping GObject introspection data.
24
25 The dependencies are generated in the ${gir:Depends} substitution variable.
26
27 The minimal version of the generated dependencies is calculated by looking
28 first at the version of the packages declared in the build-dependencies of the
29 building package. If a package is not declared in the build-dependencies, the
30 minimal version of that package is calculated by looking at the version of the
31 package containing the corresponding .gir file defined in the
32 build-dependencies.
33
34 Note that dh_girepository will not be executed automatically by dh; you
35 need to use C<dh $@ --with gir> for C<dh> to include it.
36
37 =head1 OPTIONS
38
39 Note that the B<-p> option conflicts with the B<-p> option defined by
40 L<debhelper(7)> (see L<Debian bug #703941|https://bugs.debian.org/703941>).
41 To run B<dh_girepository> on a single package, use
42 C<dh_girepository --package=gir1.2-foo-1.0> instead of the usual B<-p>.
43
44 =over 4
45
46 =item B<-l>I<directory>
47
48 Specify a directory (or a colon-separated list of directories) where to look
49 for the .gir XML files that were used to generate the .typelib files that
50 are scanned. This option is only necessary if those files are not shipped in
51 another, architecture-dependent package.
52
53 =item B<-p>I<directory>
54
55 Specify a directory (or a colon-separated list of directories) where to look
56 for the dependencies. This is useful when a dependency ships the .typelib
57 in a private directory.
58
59 =item B<-X>I<item>
60
61 Exclude files that contain I<item> anywhere in their filename from being
62 analyzed.
63
64 =item I<private [...]>
65 List of directories where to look for typelibs and the corresponding .gir
66 files. Useful when the package installs its typelibs in a private
67 directory, such as /usr/lib/<package>. Library dependencies are also looked
68 there, in case your typelib depends on a library that you ship on a private
69 directory.
70
71 =back
72
73 =head1 CONFORMS TO
74
75 GObject introspection mini policy as of 2011-03-30.
76
77 =cut
78
79 # Initialisation code
80 init(options => {
81 "l=s", => \$dh{L_PARAMS},
82 "p=s", => \$dh{P_PARAMS},
83 });
84 my @paths_first = ();
85 my @privdirs = ();
86 if ($dh{L_PARAMS}) {
87 push @paths_first, split /:/, $dh{L_PARAMS};
88 }
89 if ($dh{P_PARAMS}) {
90 push @privdirs, split /:/, $dh{P_PARAMS};
91 }
92 my @archpackages = getpackages("arch");
93
94 my $triplet = dpkg_architecture_value("DEB_HOST_MULTIARCH");
95 my $gnu_type = dpkg_architecture_value("DEB_HOST_GNU_TYPE");
96 my $typelib_multiarch_path = "/usr/lib/$triplet/girepository-1.0";
97 my $legacy_typelib_path = "/usr/lib/girepository-1.0";
98
99 my @public_typelibdirs = ($typelib_multiarch_path, $legacy_typelib_path);
100 my @typelibdirs = (@ARGV, @public_typelibdirs);
101 my $gir_multiarch_path = "/usr/lib/$triplet/gir-1.0";
102 my $gir_path = "/usr/share/gir-1.0";
103 my @public_girdirs = ($gir_path, $gir_multiarch_path);
104 my @girdirs = (@ARGV, @public_girdirs);
105 my @privlibdirs = (@ARGV);
106 my @libdirs = ("/lib/$triplet", "/lib", "/usr/lib/$triplet", "/usr/lib", @privlibdirs);
107
108 # Get Build-Depends in an array
109 my @bdeps;
110
111 my $ctrl = Dpkg::Control->new(type => CTRL_INFO_SRC);
112 $ctrl->load('debian/control');
113 foreach my $field (qw(Build-Depends Build-Depends-Arch Build-Depends-Indep)) {
114 if (defined $ctrl->{$field}) {
115 my $value = deps_parse($ctrl->{$field}, build_dep => 1);
116 push @bdeps, split ', ', $value->output;
117 }
118 }
119
120 # We can’t parse .typelib files, so we need the corresponding .gir
121 # somewhere in the same source package (or with -l).
122
123 sub find_gir {
124 my $req = shift;
125 $req =~ s/\.typelib$//;
126 my $f;
127 foreach my $path (@paths_first) {
128 $f = "$path/$req.gir";
129 if (-f $f) {
130 verbose_print ("Found $req.gir in $path");
131 return $f;
132 }
133 }
134 foreach my $otherpkg (@archpackages) {
135 foreach my $girdir (@girdirs) {
136 $f = tmpdir($otherpkg)."$girdir/$req.gir";
137 if (-f $f) {
138 verbose_print ("Found $req.gir in $otherpkg package");
139 return $f;
140 }
141 }
142 }
143 error("Could not find gir file for $req.typelib");
144 }
145
146 # Split GLib-2.0 into (GLib, 2.0)
147 sub split_name_version {
148 my $stem = shift;
149 error("Unexpected GIR name-version: $stem") unless $stem =~ m{^([A-Za-z0-9_]+)-([0-9.]+)$};
150 return ($1, $2);
151 }
152
153 # Convert (1.2, GLib, 2.0) into gir1.2-glib-2.0
154 sub gir_package_name {
155 my $format = shift;
156 my $name = shift;
157 my $version = shift;
158 error("Unexpected GIR name $name") unless $name =~ m{^[A-Za-z0-9_]+$};
159 error("Unexpected GIR version $version") unless $version =~ m{^[0-9.]+$};
160 my $lcname = lc $name;
161 $lcname =~ tr/_/-/;
162 return "gir${format}-${lcname}-${version}";
163 }
164
165 sub require_gir {
166 my $format = shift;
167 my $stem = shift;
168 my $package = shift;
169
170 my $fullpath = "";
171 my $req = "${stem}.gir";
172 foreach my $girdir (@girdirs) {
173 $fullpath = "$girdir/$req";
174
175 verbose_print ("Dependency: $req");
176 foreach my $girdir (@girdirs) {
177 if (-f tmpdir($package)."$girdir/$req") {
178 verbose_print(" found in the same package");
179 return;
180 }
181 }
182 foreach my $otherpkg (@archpackages) {
183 if (-f tmpdir($otherpkg)."$fullpath") {
184 error("Dependency on $otherpkg with a different format than $format")
185 if ($otherpkg =~ /^gir[0-9.]+-/ and $otherpkg !~ /^gir$format/);
186 verbose_print("$package Depends $otherpkg (= \${binary:Version}) for $req");
187 addsubstvar ($package, "gir:Depends", $otherpkg, "= \${binary:Version}");
188 return;
189 }
190 }
191 foreach my $privpath (@privdirs) {
192 if (-f "$privpath/$req") {
193 verbose_print (" found in $privpath");
194 $fullpath = "$privpath/$req";
195 last;
196 }
197 }
198
199 if (-f "$fullpath") {
200 # /usr/share/gir-1.0 is higher precedence than /usr/lib/*/gir-1.0
201 # because of how upstream handles XDG_DATA_DIRS, but when we
202 # provide a legacy non-multiarch-friendly location like
203 # /usr/share/gir-1.0/GLib-2.0.gir as a symlink to a multiarch
204 # co-installable location like /usr/lib/*/gir-1.0/GLib-2.0.gir,
205 # we would prefer the generated dependency to reflect the latter.
206 # In particular this matters for libgirepository1.0-dev.
207 my $realpath = realpath($fullpath);
208
209 if ($realpath eq "$gir_multiarch_path/$req") {
210 $fullpath = $realpath;
211 }
212
213 my @output = (split ':', `dpkg -S $fullpath 2>/dev/null`);
214 error("$fullpath does not belong to any package") unless @output;
215 my $deppkg = $output[0];
216 error("Dependency on $deppkg with a different format than $format")
217 if ($deppkg =~ /^gir[0-9.]+-/ and $deppkg !~ /^gir$format/);
218
219 my $dir = dirname($fullpath);
220 my $in_public_dir = (grep { $_ eq $dir } @public_girdirs);
221 my ($name, $version) = split_name_version($stem);
222 my $canonical_name = gir_package_name($format, $name, $version)."-dev";
223
224 if ($canonical_name ne $deppkg and $in_public_dir) {
225 my $provides = qx_cmd('dpkg-query', '-W', '-f', '${Provides}', $deppkg);
226
227 if ($provides =~ /(^|,)\s*\Q$canonical_name\E\s*\(=/) {
228 $deppkg = $canonical_name;
229 my $found = 0;
230
231 foreach my $bdep (@bdeps) {
232 if ($bdep =~ /^\s*\Q$canonical_name\E(\s|$)/) {
233 $found = 1;
234 last;
235 }
236 }
237 if (! $found) {
238 warning("Missing Build-Depends: $canonical_name (ideally with <!nogir>)");
239 }
240 } else {
241 warning("$deppkg should have Provides: $canonical_name (= \${binary:Version})");
242 }
243 }
244
245 # Look for version information in build-dependencies
246 my $found = 0;
247 foreach my $bdep (@bdeps) {
248 if ($bdep =~ /^\s*([a-z0-9\._\-\+]+)\s*\((.*)\)/) {
249 if ($1 eq $deppkg or $1 eq "${canonical_name}") {
250 verbose_print("$package Depends $1 ($2) for $req (version taken from $1 in B-D)");
251 addsubstvar ($package, "gir:Depends", $1, $2);
252 $found = 1;
253 }
254 }
255 }
256 if (! $found) {
257 verbose_print("$package Depends $deppkg for $req");
258 addsubstvar ($package, "gir:Depends", $deppkg);
259 }
260 return;
261 }
262 }
263 error("Could not find $req dependency");
264 }
265
266 # Function used for dependencies on other .typelib files
267
268 sub require_typelib {
269 my $format = shift;
270 my $req = shift;
271 my $package = shift;
272 my $fullpath = "";
273 foreach my $typelibdir (@typelibdirs) {
274 $fullpath = "$typelibdir/$req";
275
276 verbose_print ("Dependency: $req");
277 foreach my $typelibdir (@typelibdirs) {
278 if (-f tmpdir($package)."$typelibdir/$req") {
279 verbose_print(" found in the same package");
280 return;
281 }
282 }
283 foreach my $otherpkg (@archpackages) {
284 if (-f tmpdir($otherpkg)."$fullpath") {
285 error("Dependency on $otherpkg with a different format than $format") unless $otherpkg =~ /^gir$format/;
286 verbose_print("$package Depends $otherpkg (= \${binary:Version}) for $req");
287 addsubstvar ($package, "gir:Depends", $otherpkg, "= \${binary:Version}");
288 return;
289 }
290 }
291 foreach my $privpath (@privdirs) {
292 if (-f "$privpath/$req") {
293 verbose_print (" found in $privpath");
294 $fullpath = "$privpath/$req";
295 last;
296 }
297 }
298
299 if (-f "$fullpath") {
300 my @output = (split ':', `dpkg -S $fullpath 2>/dev/null`);
301 error("$fullpath does not belong to any package") unless @output;
302 my $deppkg = $output[0];
303 error("Dependency on $deppkg with a different format than $format") unless $deppkg =~ /^gir$format/;
304
305 my $dir = dirname($fullpath);
306 my $in_public_dir = (grep { $_ eq $dir } @public_typelibdirs);
307 my $stem = $req;
308 $stem =~ s/\.typelib$//;
309 my ($name, $version) = split_name_version($stem);
310 my $canonical_name = gir_package_name($format, $name, $version);
311
312 if ($canonical_name ne $deppkg and $in_public_dir) {
313 my $provides = qx_cmd('dpkg-query', '-W', '-f', '${Provides}', $deppkg);
314
315 if ($provides =~ /(^|,)\s*\Q$canonical_name\E\s*\(=/) {
316 $deppkg = $canonical_name;
317 } else {
318 warning("$deppkg should have Provides: $canonical_name (= \${binary:Version})");
319 }
320 }
321
322 # Look for version information in build-dependencies
323 my $found = 0;
324 foreach my $bdep (@bdeps) {
325 if ($bdep =~ /^\s*([a-z0-9\._\-\+]+)\s*\((.*)\)/) {
326 if ($1 eq $deppkg or $1 eq $canonical_name) {
327 verbose_print("$package Depends $1 ($2) for $req (version taken from $1 in B-D)");
328 addsubstvar ($package, "gir:Depends", $1, $2);
329 $found = 1;
330 } elsif ($1 eq "${canonical_name}-dev") {
331 verbose_print("$package Depends $deppkg ($2) for $req (version taken from $1 in B-D)");
332 addsubstvar ($package, "gir:Depends", $deppkg, $2);
333 $found = 1;
334 }
335 }
336 }
337 if (! $found) {
338 my $fullpath_gir = "";
339 (my $req_gir = $req) =~ s/\.typelib$/\.gir/;
340 foreach my $girdir (@girdirs) {
341 if (-f "$girdir/$req_gir") {
342 $fullpath_gir = "$girdir/$req_gir";
343 my @output_gir = (split ':', `dpkg -S $fullpath_gir 2>/dev/null`);
344 error("$fullpath_gir does not belong to any package") unless @output_gir;
345 my $deppkg_gir = $output_gir[0];
346 foreach my $bdep (@bdeps) {
347 if ($bdep =~ /^\s*([a-z0-9\._\-\+]+)\s*\((.*)\)/) {
348 if ($1 eq $deppkg_gir or $1 eq "${canonical_name}-dev") {
349 verbose_print("$package Depends $deppkg ($2) for $req (version taken from $1 in B-D)");
350 addsubstvar ($package, "gir:Depends", $deppkg, $2);
351 $found = 1;
352 }
353 }
354 }
355 }
356 }
357 }
358 if (! $found) {
359 verbose_print("$package Depends $deppkg for $req");
360 addsubstvar ($package, "gir:Depends", $deppkg);
361 }
362 return;
363 }
364 }
365 error("Could not find $req dependency");
366 }
367
368
369 sub find_library_in_package {
370 my $req = shift;
371 my $package = shift;
372 my $tmp = "";
373 if ($package) {
374 $tmp = tmpdir ($package);
375 }
376 my @loclibdirs = grep -d, map "$tmp$_", @libdirs;
377 foreach my $libdir (@loclibdirs) {
378 if (-f "$libdir/$req" or -l "$libdir/$req") {
379 return "$libdir/$req";
380 }
381 }
382 }
383
384 sub find_library {
385 my $req = shift;
386 my $package = shift;
387
388 my $file = find_library_in_package ($req, $package);
389 if ($file) {
390 verbose_print (" found in the same package");
391 } else {
392 foreach my $otherpkg (@archpackages) {
393 $file = find_library_in_package ($req, $otherpkg);
394 if ($file) {
395 verbose_print (" found in $otherpkg");
396 last;
397 }
398 }
399 }
400 if (!$file) {
401 $file = find_library_in_package ($req);
402 if ($file) {
403 verbose_print (" found on filesystem");
404 } else {
405 error ("Could not find library $req");
406 }
407 }
408
409 if (-l $file and not -f $file) {
410 # We have a symbolic link that points to another package
411 verbose_print (" ... it's a symlink ...");
412 return find_library (readlink ($file), $package);
413 }
414 return $file;
415 }
416
417 foreach my $package (@{$dh{DOPACKAGES}}) {
418 my $tmp = tmpdir($package);
419 my $ext = pkgext($package);
420 my @bin_files = ();
421 my @c_files = ();
422 my @typelib_deps = ();
423 my @gir_deps = ();
424 my @provides = ();
425 my $format;
426 my $gir_multiarch_required = 0;
427 verbose_print("Package: $package");
428
429 foreach my $dir (@girdirs) {
430 my $girdir = "$tmp$dir";
431 next unless -d $girdir;
432
433 my $in_public_dir = (grep { $_ eq $dir } @public_girdirs);
434 if ($in_public_dir) {
435 verbose_print("Public GIR XML directory: $tmp$dir");
436 } else {
437 verbose_print("Private GIR XML directory: $tmp$dir");
438 }
439
440 opendir(DIRHANDLE, $girdir);
441 while (my $girxml = readdir(DIRHANDLE)) {
442 next unless $girxml =~ /\.gir$/;
443 next if excludefile($girxml);
444 verbose_print ("GIR XML: $girxml");
445 if ($dir eq $gir_multiarch_path) {
446 warning("$dir/$girxml will not be compatible with older GIR XML consumers");
447 $gir_multiarch_required = 1;
448 }
449 my $stem = $girxml;
450 $stem =~ s/\.gir$//;
451 my ($name, $version) = split_name_version($stem);
452 my $deptypelib="$name-$version.typelib";
453 if ($in_public_dir && ! grep { $_ eq $deptypelib } @typelib_deps) {
454 push @typelib_deps, $deptypelib;
455 }
456 my $girfile = "$girdir/$girxml";
457 error("Unable to open $girfile") unless open (my $f, "<", $girfile);
458 while (<$f>) {
459 # "Parse" the XML file
460 chomp;
461 if (/<repository.+?version="(.*?)"/) {
462 # gir format version
463 $format="$1";
464 } elsif (/<include\s+name="(.*?)"\s+version="(.*?)"\/>/) {
465 # Dependency on another GIR XML file
466 my $stem="$1-$2";
467 verbose_print (" Dependency: $stem.gir");
468 if ($in_public_dir && ! grep { $_ eq $stem } @gir_deps) {
469 push @gir_deps, $stem;
470 }
471 }
472 }
473 close $f;
474
475 error("Unable to determine gir format") unless $format;
476 my $canonical_name = gir_package_name($format, $name, $version) . "-dev";
477
478 if ($package ne $canonical_name and $in_public_dir) {
479 push @provides, $canonical_name;
480 }
481 }
482 }
483
484 foreach my $dir (@typelibdirs) {
485 my $typelibdir = "$tmp$dir";
486 next unless -d $typelibdir;
487
488 my $in_public_dir = grep { $_ eq $dir } @public_typelibdirs;
489 if ($in_public_dir) {
490 verbose_print("Public typelib directory: $tmp$dir");
491 } else {
492 verbose_print("Private typelib directory: $tmp$dir");
493 }
494
495 opendir(DIRHANDLE, $typelibdir);
496 while (my $typelib = readdir(DIRHANDLE)) {
497 next unless $typelib =~ /\.typelib$/;
498 next if excludefile ($typelib);
499 verbose_print ("Typelib: $typelib");
500 if ($dir eq $legacy_typelib_path) {
501 warning("$dir/$typelib should be installed into $typelib_multiarch_path");
502 }
503 my $stem = $typelib;
504 $stem =~ s/\.typelib$//;
505 my ($name, $version) = split_name_version($stem);
506 my $girfile = find_gir ($typelib);
507 error("Unable to open $girfile") unless open (my $f, "<", $girfile);
508 verbose_print ("$girfile...");
509 my @libraries = ();
510 my @symbols = ();
511 my $infunction = 0;
512 my $intype = 0;
513 while (<$f>) {
514 # "Parse" the XML file
515 chomp;
516 if (/<repository.+?version="(.*?)"/) {
517 # gir format version
518 $format="$1";
519 } elsif (/<include\s+name="(.*?)"\s+version="(.*?)"\/>/) {
520 # Dependency on another typelib file
521 my $deptypelib="$1-$2.typelib";
522 verbose_print (" Dependency: $deptypelib");
523 if (! grep { $_ eq $deptypelib } @typelib_deps) {
524 push @typelib_deps, $deptypelib;
525 }
526 } elsif (/shared-library="(.*?)"/) {
527 # Dependency on a shared library
528 foreach my $shlibname (split ",", $1) {
529 if ($shlibname !~ /\.so/) {
530 $shlibname = "lib$shlibname.so"
531 }
532 verbose_print (" Library: $shlibname");
533 push @libraries, find_library ($shlibname, $package);
534 }
535 } elsif (/<(?:(?:virtual-)?method|constructor|function)\s.*c:identifier="(.*?)"/) {
536 push @symbols, $1;
537 } elsif (/<(?:(?:virtual-)?method|constructor|function)/) {
538 $infunction = 1;
539 } elsif ($infunction and /c:identifier="(.*?)"/) {
540 push @symbols, $1;
541 } elsif (/<(?:class|interface|enumeration|bitfield|boxed|record|union)\s.*glib:(?:get-type|.*?-func)="(.*?)"/) {
542 push @symbols, $1 unless $1 =~ /^intern/;
543 } elsif (/<(?:class|interface|enumeration|bitfield|boxed|record|union)/) {
544 $intype = 1;
545 } elsif ($intype and /glib:(?:get-type|.*?-func)="(.*?)"/) {
546 push @symbols, $1 unless $1 =~ /^intern/;
547 }
548 if (/>$/) {
549 $infunction = 0;
550 $intype = 0;
551 }
552 }
553 close $f;
554 error("Unable to determine gir format") unless $format;
555 error("Package name $package doesn't match gir format $format")
556 unless $package =~ /^gir$format/
557 or not $typelibdir =~ /usr\/lib\/girepository/;
558
559 verbose_print(sprintf(" %d symbols found", $#symbols+1));
560 if (@libraries or @symbols) {
561 my $c_file = "$typelibdir/$typelib.c";
562 my $bin_file = "$typelibdir/$typelib.so";
563 verbose_print (" writing $c_file");
564 if (!$dh{NO_ACT}){
565 error("Unable to open $girfile") unless open (F, ">", $c_file);
566 print F "void gir_dummy_function () {\n";
567 foreach my $symbol (@symbols) {
568 print F "$symbol ();\n";
569 }
570 print F "}";
571 close F;
572 }
573 push @c_files, $c_file;
574
575 # Build a dummy binary using all referenced symbols and libraries
576 # We use -shared so that gcc doesn’t try to resolve references
577 verbose_print (" building $bin_file");
578 doit (("${gnu_type}-gcc", "-Wno-implicit-function-declaration", "-shared", "-fPIC", "-o", $bin_file, $c_file, @libraries));
579 push @bin_files, $bin_file;
580 }
581
582 my $canonical_name = gir_package_name($format, $name, $version);
583
584 if ($package ne $canonical_name and $in_public_dir) {
585 push @provides, $canonical_name;
586 }
587 }
588 }
589 if (@bin_files) {
590 # dpkg-shlibdeps expects this directory to exist
591 if (! -d "$tmp/DEBIAN") {
592 install_dir("$tmp/DEBIAN");
593 }
594
595 # Let dpkg-shlibdeps generate the corresponding dependencies
596 # It must run first since otherwise it overwrites the variable
597 push @privlibdirs, $ENV{"LD_LIBRARY_PATH"} if $ENV{"LD_LIBRARY_PATH"};
598 my $llp = join (":", @privlibdirs);
599 complex_doit ("LD_LIBRARY_PATH=$llp dpkg-shlibdeps -pgir -Tdebian/${ext}substvars -xlibc6 -xlibc0 @bin_files");
600 }
601 doit (("rm", "-f", @c_files, @bin_files));
602
603 # This must come after dpkg-shlibdeps, otherwise dpkg-shlibdeps will
604 # delete ${gir:anything} from the substvars
605 foreach my $canonical_name (@provides) {
606 verbose_print("$package Provides $canonical_name (= \${binary:Version})");
607 addsubstvar($package, "gir:Provides", $canonical_name, "= \${binary:Version}");
608 }
609
610 # Generate dependencies on other .typelib files
611 foreach my $dep (@typelib_deps) {
612 require_typelib($format, $dep, $package);
613 }
614
615 # Generate dependencies on other .gir files
616 foreach my $stem (@gir_deps) {
617 require_gir($format, $stem, $package);
618 }
619
620 if ($gir_multiarch_required) {
621 # This version has the necessary Breaks to ensure that if
622 # libgirepository-1.0-1 is installed, then it's a version that
623 # will load GIR XML from the multiarch location
624 addsubstvar($package, "gir:Depends", "gir1.2-glib-2.0", ">= 1.78.1-3~");
625 }
626 }
627
628 =head1 SEE ALSO
629
630 L<debhelper(7)>
631
632 This program is a part of gobject-introspection but is made to work with
633 debhelper.
634
635 =head1 AUTHOR
636
637 Josselin Mouette <[email protected]>
638
639 =cut