Annotation of src/usr.bin/pkg-config/pkg-config, Revision 1.72
1.1 ckuethe 1: #!/usr/bin/perl
1.72 ! espie 2: # $OpenBSD: pkg-config,v 1.71 2012/03/19 10:25:17 ajacoutot Exp $
1.40 jasper 3: # $CSK: pkgconfig.pl,v 1.39 2006/11/27 16:26:20 ckuethe Exp $
1.1 ckuethe 4:
5: # Copyright (c) 2006 Chris Kuethe <ckuethe@openbsd.org>
1.40 jasper 6: # Copyright (c) 2011 Jasper Lievisse Adriaanse <jasper@openbsd.org>
1.1 ckuethe 7: #
8: # Permission to use, copy, modify, and distribute this software for any
9: # purpose with or without fee is hereby granted, provided that the above
10: # copyright notice and this permission notice appear in all copies.
11: #
12: # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13: # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14: # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15: # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16: # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17: # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18: # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19:
20: use strict;
21: use warnings;
1.70 jasper 22: use Config;
1.1 ckuethe 23: use Getopt::Long;
1.4 espie 24: use File::Basename;
1.58 jasper 25: use File::stat;
1.11 espie 26: use OpenBSD::PkgConfig;
1.1 ckuethe 27:
1.71 ajacouto 28: my @PKGPATH = qw(/usr/lib/pkgconfig
29: /usr/local/lib/pkgconfig
30: /usr/local/share/pkgconfig
31: /usr/X11R6/lib/pkgconfig
32: /usr/X11R6/share/pkgconfig);
1.1 ckuethe 33:
1.16 espie 34: if (defined($ENV{PKG_CONFIG_LIBDIR}) && $ENV{PKG_CONFIG_LIBDIR}) {
1.66 jasper 35: @PKGPATH = split(/:/, $ENV{PKG_CONFIG_LIBDIR});
1.16 espie 36: } elsif (defined($ENV{PKG_CONFIG_PATH}) && $ENV{PKG_CONFIG_PATH}) {
1.66 jasper 37: unshift(@PKGPATH, split(/:/, $ENV{PKG_CONFIG_PATH}));
1.1 ckuethe 38: }
39:
40: my $logfile = '';
1.50 jasper 41: if (defined($ENV{PKG_CONFIG_LOG}) && $ENV{PKG_CONFIG_LOG}) {
42: $logfile = $ENV{PKG_CONFIG_LOG};
1.1 ckuethe 43: }
44:
1.33 jasper 45: my $allow_uninstalled =
1.16 espie 46: defined $ENV{PKG_CONFIG_DISABLE_UNINSTALLED} ? 0 : 1;
1.14 espie 47: my $found_uninstalled = 0;
48:
1.69 jasper 49: my $version = 0.26; # pretend to be this version of pkgconfig
1.10 espie 50:
51: my %configs = ();
1.35 espie 52: setup_self();
53:
1.10 espie 54: my %mode = ();
1.11 espie 55: my $variables = {};
1.1 ckuethe 56:
1.56 jasper 57: $variables->{pc_top_builddir} = $ENV{PKG_CONFIG_TOP_BUILD_DIR} //
1.35 espie 58: '$(top_builddir)';
59:
60: $variables->{pc_sysrootdir} //= $ENV{PKG_CONFIG_SYSROOT_DIR};
61: # The default '/' is implied.
1.29 jasper 62:
1.61 jasper 63: defined $ENV{PKG_CONFIG_DEBUG_SPEW} ? $mode{debug} = 1 : $mode{debug} = 0;
1.7 ckuethe 64:
1.4 espie 65: if ($logfile) {
1.35 espie 66: open my $L, ">>" , $logfile or die;
1.51 jasper 67: print $L beautify_list($0, @ARGV), "\n";
1.4 espie 68: close $L;
1.1 ckuethe 69: }
70:
71: # combo arg-parsing and dependency resolution loop. Hopefully when the loop
72: # terminates, we have a full list of packages upon which we depend, and the
73: # right set of compiler and linker flags to use them.
74: #
75: # as each .pc file is loaded, it is stored in %configs, indexed by package
76: # name. this makes it possible to then pull out flags or do substitutions
1.34 jasper 77: # without having to go back and reload the files from disk.
1.1 ckuethe 78:
79: Getopt::Long::Configure('no_ignore_case');
1.68 jasper 80: GetOptions( 'debug' => \$mode{debug},
81: 'help' => \&help, #does not return
82: 'usage' => \&help, #does not return
83: 'list-all' => \$mode{list},
84: 'version' => sub { print "$version\n" ; exit(0);} ,
85: 'errors-to-stdout' => sub { $mode{estdout} = 1},
86: 'print-errors' => sub { $mode{printerr} = 1},
87: 'silence-errors' => sub { $mode{printerr} = 0},
88: 'short-errors' => sub { $mode{printerr} = 0},
1.14 espie 89: 'atleast-pkgconfig-version=s' => \$mode{myminvers},
1.68 jasper 90: 'print-provides' => \$mode{printprovides},
91: 'print-requires' => \$mode{printrequires},
1.30 jasper 92: 'print-requires-private' => \$mode{printrequiresprivate},
1.11 espie 93:
1.68 jasper 94: 'cflags' => sub { $mode{cflags} = 3},
95: 'cflags-only-I' => sub { $mode{cflags} |= 1},
96: 'cflags-only-other' => sub { $mode{cflags} |= 2},
97: 'libs' => sub { $mode{libs} = 7},
98: 'libs-only-l' => sub { $mode{libs} |= 1},
99: 'libs-only-L' => sub { $mode{libs} |= 2},
100: 'libs-only-other' => sub { $mode{libs} |= 4},
101: 'exists' => sub { $mode{exists} = 1} ,
102: 'static' => sub { $mode{static} = 1},
103: 'uninstalled' => sub { $mode{uninstalled} = 1},
104: 'atleast-version=s' => \$mode{minversion},
105: 'exact-version=s' => \$mode{exactversion},
106: 'max-version=s' => \$mode{maxversion},
107: 'modversion' => \$mode{modversion},
108: 'variable=s' => \$mode{variable},
109: 'define-variable=s' => $variables,
1.1 ckuethe 110: );
111:
1.70 jasper 112: # Unconditionally switch to static mode on static arches as --static
113: # may not have been passed explicitly, but we don't want to re-order
114: # and simplify the libs like we do for shared architectures.
115: {
116: my @static_archs = qw(m88k vax);
117: my $machine_arch = $Config{'ARCH'};
118: if (grep { $_ eq $machine_arch } @static_archs){
119: $mode{static} = 1;
120: }
121: }
122:
1.14 espie 123: # Initial value of printerr depends on the options...
124: if (!defined $mode{printerr}) {
1.61 jasper 125: if (defined $mode{libs}
126: or defined $mode{cflags}
127: or defined $mode{version}
128: or defined $mode{list}) {
1.14 espie 129: $mode{printerr} = 1;
130: } else {
131: $mode{printerr} = 0;
132: }
133: }
134:
1.62 jasper 135: say_debug("\n" . beautify_list($0, @ARGV));
1.13 espie 136:
137: my $rc = 0;
1.1 ckuethe 138:
1.14 espie 139: # XXX pkg-config is a bit weird
1.10 espie 140: {
141: my $p = join(' ', @ARGV);
1.14 espie 142: $p =~ s/^\s+//;
1.66 jasper 143: @ARGV = split(/\,?\s+/, $p);
1.10 espie 144: }
1.1 ckuethe 145:
1.14 espie 146: if ($mode{myminvers}) {
147: exit self_version($mode{myminvers});
148: }
149:
150: if ($mode{list}) {
151: exit do_list();
1.1 ckuethe 152: }
153:
1.13 espie 154: my $cfg_full_list = [];
1.14 espie 155: my $top_config = [];
1.1 ckuethe 156:
157: while (@ARGV){
1.13 espie 158: my $p = shift @ARGV;
159: my $op = undef;
160: my $v = undef;
1.35 espie 161: if (@ARGV >= 2 && $ARGV[0] =~ /^[<=>]+$/ &&
1.59 jasper 162: $ARGV[1] =~ /^[\d\.]+[\w\.]*$/) {
1.13 espie 163: $op = shift @ARGV;
164: $v = shift @ARGV;
1.1 ckuethe 165: }
1.52 jasper 166: # For these modes we just need some meta-information and
167: # parsing the requirements is not needed.
168: if (!($mode{modversion} || $mode{printprovides})) {
169: handle_config($p, $op, $v, $cfg_full_list);
170: }
1.14 espie 171: push(@$top_config, $p);
172: }
173:
174: if ($mode{exists}) {
175: exit $rc;
176: }
177:
178: if ($mode{uninstalled}) {
179: $rc = 1 unless $found_uninstalled;
180: exit $rc;
1.11 espie 181: }
1.1 ckuethe 182:
1.30 jasper 183: if ($mode{modversion} || $mode{printprovides}) {
1.14 espie 184: for my $pkg (@$top_config) {
185: do_modversion($pkg);
186: }
187: }
1.13 espie 188:
1.30 jasper 189: if ($mode{printrequires} || $mode{printrequiresprivate}) {
190: for my $pkg (@$top_config) {
191: print_requires($pkg);
192: }
193: }
194:
1.14 espie 195: if ($mode{minversion}) {
196: my $v = $mode{minversion};
197: for my $pkg (@$top_config) {
198: $rc = 1 unless versionmatch($configs{$pkg}, '>=', $v);
199: }
200: exit $rc;
201: }
202:
203: if ($mode{exactversion}) {
204: my $v = $mode{exactversion};
205: for my $pkg (@$top_config) {
206: $rc = 1 unless versionmatch($configs{$pkg}, '=', $v);
207: }
208: exit $rc;
209: }
210:
211: if ($mode{minversion}) {
212: my $v = $mode{maxversion};
213: for my $pkg (@$top_config) {
214: $rc = 1 unless versionmatch($configs{$pkg}, '<=', $v);
215: }
216: exit $rc;
217: }
218:
219: my @vlist = ();
220:
221: if ($mode{variable}) {
222: for my $pkg (@$top_config) {
223: do_variable($pkg, $mode{variable});
224: }
225: }
226:
1.70 jasper 227: my $dep_cfg_list = $cfg_full_list;
228:
1.72 ! espie 229: if ($mode{static}){
! 230: $dep_cfg_list = [reverse(@$cfg_full_list)];
! 231: } else {
1.70 jasper 232: $dep_cfg_list = simplify_and_reverse($cfg_full_list);
233: }
1.14 espie 234:
235: if ($mode{cflags} || $mode{libs} || $mode{variable}) {
1.66 jasper 236: push @vlist, do_cflags($dep_cfg_list) if $mode{cflags};
237: push @vlist, do_libs($dep_cfg_list) if $mode{libs};
238: print join(' ', @vlist), "\n" if $rc == 0;
1.1 ckuethe 239: }
240:
1.13 espie 241: exit $rc;
1.1 ckuethe 242:
243: ###########################################################################
244:
1.11 espie 245: sub handle_config
246: {
1.13 espie 247: my ($p, $op, $v, $list) = @_;
1.35 espie 248: my $cfg = cache_find_config($p);
1.13 espie 249:
1.35 espie 250: unshift @$list, $p if defined $cfg;
1.11 espie 251:
1.35 espie 252: if (!defined $cfg) {
253: $rc = 1;
254: return undef;
255: }
1.15 espie 256:
1.35 espie 257: if (defined $op) {
258: if (!versionmatch($cfg, $op, $v)) {
259: mismatch($p, $cfg, $op, $v) if $mode{printerr};
1.13 espie 260: $rc = 1;
261: return undef;
262: }
1.35 espie 263: }
1.11 espie 264:
1.43 jasper 265: my $get_props = sub {
266: my $property = shift;
267:
268: my $deps = $cfg->get_property($property, $variables);
269: if (defined $deps) {
270: for my $dep (@$deps) {
1.46 jasper 271: if ($dep =~ m/^(.*?)\s*([<=>]+)\s*([\d\.]+|[\d\.]+[\w]*[\d]+)$/) {
1.43 jasper 272: handle_config($1, $2, $3, $list);
273: } else {
274: handle_config($dep, undef, undef, $list);
275: }
1.26 jasper 276: }
1.62 jasper 277: say_debug("package $p " . lc($property) . " " . join(',', @$deps));
1.26 jasper 278: }
1.43 jasper 279: };
280:
1.66 jasper 281: if (defined $mode{cflags}
282: or ($mode{static} && $mode{libs})
283: or $mode{printrequiresprivate}) {
1.64 jasper 284: &$get_props("Requires.private");
285: }
1.43 jasper 286: &$get_props("Requires");
1.26 jasper 287:
1.11 espie 288: }
289:
1.1 ckuethe 290: # look for the .pc file in each of the PKGPATH elements. Return the path or
291: # undef if it's not there
1.4 espie 292: sub pathresolve
293: {
294: my ($p) = @_;
295:
1.14 espie 296: if ($allow_uninstalled && $p !~ m/\-uninstalled$/) {
297: foreach my $d (@PKGPATH) {
298: my $f = "$d/$p-uninstalled.pc";
1.62 jasper 299: say_debug("pathresolve($p) looking in $f");
1.14 espie 300: if (-f $f) {
301: $found_uninstalled = 1;
302: return $f;
303: }
304: }
305: }
306:
1.4 espie 307: foreach my $d (@PKGPATH) {
1.10 espie 308: my $f = "$d/$p.pc";
1.62 jasper 309: say_debug("pathresolve($p) looking in $f");
1.10 espie 310: return $f if -f $f;
1.1 ckuethe 311: }
1.10 espie 312: return undef;
1.1 ckuethe 313: }
314:
1.11 espie 315: sub get_config
316: {
317: my ($f) = @_;
318:
319: my $cfg;
1.33 jasper 320: eval {
1.11 espie 321: $cfg = OpenBSD::PkgConfig->read_file($f);
322: };
323: if (!$@) {
1.37 jasper 324: return validate_config($f, $cfg);
1.11 espie 325: } else {
1.62 jasper 326: say_debug($@);
1.11 espie 327: }
328: return undef;
329: }
330:
1.13 espie 331: sub cache_find_config
332: {
333: my $name = shift;
334:
1.62 jasper 335: say_debug("processing $name");
1.13 espie 336:
337: if (exists $configs{$name}) {
338: return $configs{$name};
339: } else {
340: return $configs{$name} = find_config($name);
341: }
1.37 jasper 342: }
343:
344: # Required elements for a valid .pc file: Name, Description, Version
345: sub validate_config
346: {
347: my ($f, $cfg) = @_;
348: my @required_elems = ('Name', 'Description', 'Version');
1.58 jasper 349:
350: # Check if we're dealing with an empty file, but don't error out just
351: # yet, we'll do that when we realize there's no Name field.
1.61 jasper 352: if (stat($f)->size == 0) {
1.58 jasper 353: my $p = $f;
354: $p =~ s/(^.*\/)(.*?)$/$2/g;
1.62 jasper 355: say_error("Package file '$p' appears to be empty");
1.58 jasper 356: }
1.37 jasper 357:
358: foreach (@required_elems) {
1.58 jasper 359: my $e = $cfg->get_property($_, $variables);
1.37 jasper 360: if (!defined $e) {
1.39 jasper 361: $f =~ s/(^.*\/)(.*?)\.pc$/$2/g;
1.62 jasper 362: say_error("Package '$f' has no $_: field");
1.37 jasper 363: return undef;
364: }
365: }
366:
367: return $cfg;
1.13 espie 368: }
369:
1.35 espie 370: # pkg-config won't install a pkg-config.pc file itself, but it may be
1.63 jasper 371: # listed as a dependency in other files. so prime the cache with self.
1.35 espie 372: sub setup_self
373: {
374: my $pkg_pc = OpenBSD::PkgConfig->new;
375: $pkg_pc->add_property('Version', $version);
1.38 jasper 376: $pkg_pc->add_variable('pc_path', join(":", @PKGPATH));
1.63 jasper 377: $pkg_pc->add_property('URL', "http://www.openbsd.org/cgi-bin/man.cgi?query=pkg-config");
378: $pkg_pc->add_property('Description', "fetch metadata about installed software packages");
1.35 espie 379: $configs{'pkg-config'} = $pkg_pc;
380: }
381:
1.11 espie 382: sub find_config
383: {
384: my ($p) = @_;
385: my $f = pathresolve($p);
1.64 jasper 386:
387: return get_config($f) if defined($f);
388:
1.62 jasper 389: say_error("Package $p was not found in the pkg-config search path");
1.61 jasper 390:
1.11 espie 391: return undef;
392: }
1.1 ckuethe 393:
1.11 espie 394: sub stringize
1.4 espie 395: {
1.11 espie 396: my $list = shift;
1.21 simon 397: my $sep = shift || ',';
1.4 espie 398:
1.11 espie 399: if (defined $list) {
1.21 simon 400: return join($sep, @$list)
1.11 espie 401: } else {
402: return '';
1.1 ckuethe 403: }
404: }
405:
406: #if the variable option is set, pull out the named variable
1.4 espie 407: sub do_variable
408: {
1.11 espie 409: my ($p, $v) = @_;
1.1 ckuethe 410:
1.13 espie 411: my $cfg = cache_find_config($p);
412:
413: if (defined $cfg) {
1.11 espie 414: my $value = $cfg->get_variable($v, $variables);
415: if (defined $value) {
1.13 espie 416: push(@vlist, $value);
1.11 espie 417: }
1.19 espie 418: return undef;
1.11 espie 419: }
1.19 espie 420: $rc = 1;
1.1 ckuethe 421: }
422:
1.30 jasper 423: #if the modversion or print-provides options are set,
424: #pull out the compiler flags
1.4 espie 425: sub do_modversion
426: {
1.11 espie 427: my ($p) = @_;
1.1 ckuethe 428:
1.13 espie 429: my $cfg = cache_find_config($p);
430:
431: if (defined $cfg) {
1.11 espie 432: my $value = $cfg->get_property('Version', $variables);
433: if (defined $value) {
1.60 jasper 434: if (defined($mode{printprovides})){
435: print "$p = " . stringize($value) . "\n";
1.30 jasper 436: return undef;
437: } else {
1.60 jasper 438: print stringize($value), "\n";
1.30 jasper 439: return undef;
440: }
1.11 espie 441: }
442: }
1.13 espie 443: $rc = 1;
1.1 ckuethe 444: }
445:
446: #if the cflags option is set, pull out the compiler flags
1.4 espie 447: sub do_cflags
448: {
1.14 espie 449: my $list = shift;
450:
1.11 espie 451: my $cflags = [];
1.1 ckuethe 452:
1.14 espie 453: foreach my $pkg (@$list) {
1.11 espie 454: my $l = $configs{$pkg}->get_property('Cflags', $variables);
455: push(@$cflags, @$l) if defined $l;
456: }
1.32 jasper 457: my $a = OpenBSD::PkgConfig->compress($cflags,
1.11 espie 458: sub {
459: local $_ = shift;
460: if (($mode{cflags} & 1) && /^-I/ ||
461: ($mode{cflags} & 2) && !/^-I/) {
462: return 1;
463: } else {
464: return 0;
1.4 espie 465: }
1.11 espie 466: });
1.32 jasper 467: if (defined($a) && defined($variables->{pc_sysrootdir})){
1.36 jasper 468: $a =~ s/[\w]?-I/$&$variables->{pc_sysrootdir}/g;
1.32 jasper 469: }
470:
471: return $a;
1.1 ckuethe 472: }
473:
474: #if the lib option is set, pull out the linker flags
1.4 espie 475: sub do_libs
476: {
1.14 espie 477: my $list = shift;
478:
1.11 espie 479: my $libs = [];
1.1 ckuethe 480:
1.68 jasper 481: # In static mode, we have to make sure we discover the libs in dependency
482: # order, not in search order. Ordering matters for static linking:
483: # Start with Libs (first our own, then dependencies), and append
484: # Libs.private (same order as for Libs).
1.14 espie 485: foreach my $pkg (@$list) {
1.11 espie 486: my $l = $configs{$pkg}->get_property('Libs', $variables);
487: push(@$libs, @$l) if defined $l;
1.67 jasper 488: if ($mode{static}) {
489: my $lp = $configs{$pkg}->get_property('Libs.private', $variables);
490: push(@$libs, @$lp) if defined $lp;
491: }
1.11 espie 492: }
1.66 jasper 493:
1.68 jasper 494: # Get the linker path directives (-L) and store it in $a.
495: # $b will be the actual libraries.
1.13 espie 496: my $a = OpenBSD::PkgConfig->compress($libs,
1.11 espie 497: sub {
498: local $_ = shift;
1.13 espie 499: if (($mode{libs} & 2) && /^-L/ ||
1.11 espie 500: ($mode{libs} & 4) && !/^-[lL]/) {
501: return 1;
502: } else {
503: return 0;
1.4 espie 504: }
1.11 espie 505: });
1.32 jasper 506:
507: if (defined($variables->{pc_sysrootdir})){
1.36 jasper 508: $a =~ s/[\w]?-[lL]/$&$variables->{pc_sysrootdir}/g;
1.32 jasper 509: }
510:
1.13 espie 511: if ($mode{libs} & 1) {
512: my $b = OpenBSD::PkgConfig->rcompress($libs,
1.66 jasper 513: sub { shift =~ m/^-l/; });
1.13 espie 514: return ($a, $b);
515: } else {
516: return $a;
517: }
1.1 ckuethe 518: }
519:
520: #list all packages
1.4 espie 521: sub do_list
522: {
1.1 ckuethe 523: my ($p, $x, $y, @files, $fname, $name);
1.20 espie 524: my $error = 0;
525:
1.33 jasper 526: foreach my $p (@PKGPATH) {
527: push(@files, <$p/*.pc>);
1.4 espie 528: }
1.1 ckuethe 529:
530: # Scan the lengths of the package names so I can make a format
531: # string to line the list up just like the real pkgconfig does.
532: $x = 0;
1.4 espie 533: foreach my $f (@files) {
534: $fname = basename($f, '.pc');
535: $y = length $fname;
1.1 ckuethe 536: $x = (($y > $x) ? $y : $x);
537: }
538: $x *= -1;
539:
1.4 espie 540: foreach my $f (@files) {
1.11 espie 541: my $cfg = get_config($f);
1.20 espie 542: if (!defined $cfg) {
1.62 jasper 543: say_warning("Problem reading file $f");
1.20 espie 544: $error = 1;
545: next;
546: }
1.4 espie 547: $fname = basename($f, '.pc');
1.33 jasper 548: printf("%${x}s %s - %s\n", $fname,
1.53 jasper 549: stringize($cfg->get_property('Name', $variables), ' '),
1.21 simon 550: stringize($cfg->get_property('Description', $variables),
551: ' '));
1.1 ckuethe 552: }
1.20 espie 553: return $error;
1.1 ckuethe 554: }
555:
1.4 espie 556: sub help
557: {
1.1 ckuethe 558: print <<EOF
559: Usage: $0 [options]
560: --debug - turn on debugging output
561: --help - this message
562: --usage - this message
563: --list-all - show all packages that $0 can find
1.8 ckuethe 564: --version - print version of pkgconfig
565: --errors-to-stdout - direct error messages to stdout rather than stderr
566: --print-errors - print error messages in case of error
1.34 jasper 567: --print-provides - print all the modules the given package provides
568: --print-requires - print all the modules the given package requires
569: --print-requires-private - print all the private modules the given package requires
1.66 jasper 570: --silence-errors - don\'t print error messages in case of error
1.1 ckuethe 571: --atleast-pkgconfig-version [version] - require a certain version of pkgconfig
572: --cflags package [versionspec] [package [versionspec]]
573: --cflags-only-I - only output -Iincludepath flags
574: --cflags-only-other - only output flags that are not -I
1.11 espie 575: --define-variable=NAME=VALUE - define variables
1.1 ckuethe 576: --libs package [versionspec] [package [versionspec]]
577: --libs-only-l - only output -llib flags
578: --libs-only-L - only output -Llibpath flags
579: --libs-only-other - only output flags that are not -l or -L
580: --exists package [versionspec] [package [versionspec]]
581: --uninstalled - allow for uninstalled versions to be used
1.8 ckuethe 582: --static - adjust output for static linking
583: --atleast-version [version] - require a certain version of a package
584: --modversion [package] - query the version of a package
585: --variable var package - return the definition of <var> in <package>
1.1 ckuethe 586: EOF
587: ;
1.22 simon 588: exit 0;
1.1 ckuethe 589: }
590:
591: # do we meet/beat the version the caller requested?
1.4 espie 592: sub self_version
593: {
594: my ($v) = @_;
595: my (@a, @b);
596:
1.66 jasper 597: @a = split(/\./, $v);
598: @b = split(/\./, $version);
1.1 ckuethe 599:
1.4 espie 600: if (($b[0] >= $a[0]) && ($b[1] >= $a[1])) {
1.14 espie 601: return 0;
1.1 ckuethe 602: } else {
1.14 espie 603: return 1;
604: }
605: }
606:
607: sub compare
608: {
609: my ($a, $b) = @_;
1.46 jasper 610: my ($full_a, $full_b) = ($a, $b);
611: my (@suffix_a, @suffix_b);
1.14 espie 612:
1.28 jasper 613: return 0 if ($a eq $b);
1.14 espie 614:
1.46 jasper 615: # is there a valid non-numeric suffix to deal with later?
1.58 jasper 616: # accepted are (in order): a(lpha) < b(eta) < rc < ' '.
1.46 jasper 617: # suffix[0] is the 'alpha' part, suffix[1] is the '1' part in 'alpha1'.
1.59 jasper 618: if ($a =~ s/(rc|beta|b|alpha|a)(\d+)$//) {
1.62 jasper 619: say_debug("valid suffix $1$2 found in $a$1$2.");
1.46 jasper 620: $suffix_a[0] = $1;
621: $suffix_a[1] = $2;
622: }
623:
1.59 jasper 624: if ($b =~ s/(rc|beta|b|alpha|a)(\d+)$//) {
1.62 jasper 625: say_debug("valid suffix $1$2 found in $b$1$2.");
1.46 jasper 626: $suffix_b[0] = $1;
627: $suffix_b[1] = $2;
628: }
629:
1.66 jasper 630: my @a = split(/\./, $a);
631: my @b = split(/\./, $b);
1.14 espie 632:
633: while (@a && @b) { #so long as both lists have something
1.46 jasper 634: if (!(@suffix_a || @suffix_b)) {
635: # simple comparison when no suffixes are in the game.
1.48 jasper 636: my $rc = compare_numeric($a[0], $b[0], 0);
637: return $rc if defined($rc);
1.46 jasper 638: } else {
639: # extended comparison.
1.56 jasper 640: if (((@a == 1) || (@b == 1)) &&
1.46 jasper 641: ($a[0] == $b[0])){
642: # one of the arrays has reached the last element,
643: # compare the suffix.
644:
645: # directly compare suffixes, provided both suffixes
646: # are present.
647: if (@suffix_a && @suffix_b) {
648: my $first_char = sub {
649: return substr(shift, 0, 1);
650: };
651:
652: # suffixes are equal, compare on numeric
653: if (&$first_char($suffix_a[0]) eq
654: &$first_char($suffix_b[0])) {
1.48 jasper 655: return compare_numeric($suffix_a[1], $suffix_b[1], 1);
1.46 jasper 656: }
657:
1.47 jasper 658: # rc beats beta beats alpha
1.46 jasper 659: if (&$first_char($suffix_a[0]) lt &$first_char($suffix_b[0])) {
1.62 jasper 660: say_debug("$full_a (installed) < $full_b (wanted)");
1.46 jasper 661: return -1;
662: } else {
1.62 jasper 663: say_debug("$full_a (installed) > $full_b (wanted)");
1.46 jasper 664: return 1;
665: }
666:
667: } else {
668: # one of either is lacking a suffix,
669: # thereby beating the other.
670: # e.g.: 1.02 > 1.02b1
671: if (@suffix_a) { # a is older
1.62 jasper 672: say_debug("$full_a (installed) < $full_b (wanted)");
1.55 jasper 673: return 1;
1.46 jasper 674: }
675:
676: if (@suffix_b) { # b is older
1.62 jasper 677: say_debug("$full_a (installed) > $full_b (wanted)");
1.55 jasper 678: return -1;
1.46 jasper 679: }
680: }
681: } else {
1.48 jasper 682: my $rc = compare_numeric($a[0], $b[0], 0);
683: return $rc if defined($rc);
1.46 jasper 684: }
685: }
1.14 espie 686: shift @a; shift @b;
687: }
688: return 1 if @a;
689: return -1 if @b;
690: return 0;
1.48 jasper 691: }
692:
693: # simple numeric comparison, with optional equality test.
694: sub compare_numeric
695: {
696: my ($x, $y, $eq) = @_;
697:
698: return 1 if $x > $y;
699: return -1 if $x < $y;
700: return 0 if (($x == $y) and ($eq == 1));
701: return undef;
1.1 ckuethe 702: }
703:
704: # got a package meeting the requested specific version?
1.4 espie 705: sub versionmatch
706: {
1.14 espie 707: my ($cfg, $op, $want) = @_;
1.33 jasper 708:
1.1 ckuethe 709: # can't possibly match if we can't find the file
1.11 espie 710: return 0 if !defined $cfg;
711:
1.14 espie 712: my $inst = stringize($cfg->get_property('Version', $variables));
1.11 espie 713:
1.1 ckuethe 714: # can't possibly match if we can't find the version string
1.14 espie 715: return 0 if $inst eq '';
1.1 ckuethe 716:
1.62 jasper 717: say_debug("comparing $want (wanted) to $inst (installed)");
1.14 espie 718: my $value = compare($inst, $want);
1.31 jasper 719: if ($op eq '>=') { return $value >= 0; }
720: elsif ($op eq '=') { return $value == 0; }
721: elsif ($op eq '!=') { return $value != 0; }
722: elsif ($op eq '<') { return $value < 0; }
723: elsif ($op eq '>') { return $value > 0; }
724: elsif ($op eq '<=') { return $value <= 0; }
1.13 espie 725: }
726:
727: sub mismatch
728: {
729: my ($p, $cfg, $op, $v) = @_;
1.53 jasper 730: my $name = stringize($cfg->get_property('Name'), ' ');
1.41 jasper 731: my $version = stringize($cfg->get_property('Version'));
732: my $url = stringize($cfg->get_property('URL'));
733:
1.62 jasper 734: say_warning("Requested '$p $op $v' but version of $name is $version");
735: say_warning("You may find new versions of $name at $url") if $url;
1.13 espie 736: }
737:
738: sub simplify_and_reverse
739: {
740: my $reqlist = shift;
741: my $dejavu = {};
742: my $result = [];
743:
744: for my $item (@$reqlist) {
745: if (!$dejavu->{$item}) {
746: unshift @$result, $item;
747: $dejavu->{$item} = 1;
748: }
749: }
750: return $result;
1.30 jasper 751: }
752:
753: # retrieve and print Requires(.private)
754: sub print_requires
755: {
756: my ($p) = @_;
757:
758: my $cfg = cache_find_config($p);
759:
760: if (defined($cfg)) {
761: my $value;
762:
763: if (defined($mode{printrequires})) {
764: $value = $cfg->get_property('Requires', $variables);
765: } elsif (defined($mode{printrequiresprivate})) {
766: $value = $cfg->get_property('Requires.private', $variables);
767: } else {
1.62 jasper 768: say_debug("Unknown mode for print_requires.");
1.30 jasper 769: return 1;
770: }
771:
772: if (defined($value)) {
773: print "$_\n" foreach (@$value);
774: return undef;
775: }
776: }
777:
778: $rc = 1;
1.35 espie 779: }
780:
781: sub beautify_list
782: {
783: return join(' ', map {"[$_]"} @_);
1.61 jasper 784: }
785:
1.62 jasper 786: sub say_debug
1.61 jasper 787: {
1.62 jasper 788: say_msg(shift) if $mode{debug};
1.61 jasper 789: }
790:
1.62 jasper 791: sub say_error
1.61 jasper 792: {
1.62 jasper 793: say_msg(shift) if $mode{printerr}
794: }
795:
796: sub say_warning
797: {
798: say_msg(shift);
799: }
800:
801: sub say_msg
802: {
1.63 jasper 803: my $str = shift;
1.62 jasper 804:
805: # If --errors-to-stdout was given, close STDERR (to be safe),
806: # then dup the output to STDOUT and delete the key from %mode so we
807: # won't keep checking it. STDERR stays dup'ed.
808: if ($mode{estdout}) {
809: close(STDERR);
810: open(STDERR, ">&STDOUT") or die "Can't dup STDOUT: $!";
811: delete($mode{estdout});
812: }
813:
814: print STDERR $str . "\n";
1.1 ckuethe 815: }