[BACK]Return to pkg-config CVS log [TXT][DIR] Up to [local] / src / usr.bin / pkg-config

Annotation of src/usr.bin/pkg-config/pkg-config, Revision 1.34

1.1       ckuethe     1: #!/usr/bin/perl
1.34    ! jasper      2: # $OpenBSD: pkg-config,v 1.33 2011/03/14 08:40:13 jasper Exp $
1.1       ckuethe     3:
                      4: #$CSK: pkgconfig.pl,v 1.39 2006/11/27 16:26:20 ckuethe Exp $
                      5: # Copyright (c) 2006 Chris Kuethe <ckuethe@openbsd.org>
                      6: #
                      7: # Permission to use, copy, modify, and distribute this software for any
                      8: # purpose with or without fee is hereby granted, provided that the above
                      9: # copyright notice and this permission notice appear in all copies.
                     10: #
                     11: # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
                     12: # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
                     13: # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
                     14: # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
                     15: # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
                     16: # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
                     17: # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
                     18:
                     19: use strict;
                     20: use warnings;
                     21: use Getopt::Long;
1.4       espie      22: use File::Basename;
1.11      espie      23: use OpenBSD::PkgConfig;
1.1       ckuethe    24:
1.27      jasper     25: my @PKGPATH = qw(/usr/lib/pkgconfig /usr/local/lib/pkgconfig /usr/X11R6/lib/pkgconfig);
1.1       ckuethe    26:
1.16      espie      27: if (defined($ENV{PKG_CONFIG_LIBDIR}) && $ENV{PKG_CONFIG_LIBDIR}) {
                     28:        @PKGPATH = split /:/, $ENV{PKG_CONFIG_LIBDIR};
                     29: } elsif (defined($ENV{PKG_CONFIG_PATH}) && $ENV{PKG_CONFIG_PATH}) {
1.24      ckuethe    30:        unshift(@PKGPATH, split /:/, $ENV{PKG_CONFIG_PATH});
1.1       ckuethe    31: }
                     32:
                     33: my $logfile = '';
1.16      espie      34: if (defined($ENV{PKG_CONFIG_LOGFILE}) && $ENV{PKG_CONFIG_LOGFILE}) {
                     35:        $logfile = $ENV{PKG_CONFIG_LOGFILE};
1.1       ckuethe    36: }
                     37:
1.33      jasper     38: my $allow_uninstalled =
1.16      espie      39:        defined $ENV{PKG_CONFIG_DISABLE_UNINSTALLED} ? 0 : 1;
1.14      espie      40: my $found_uninstalled = 0;
                     41:
1.32      jasper     42: my $version = 0.23; # pretend to be this version of pkgconfig
1.10      espie      43:
                     44: my %configs = ();
                     45: my %mode = ();
1.11      espie      46: my $variables = {};
1.10      espie      47: my $D = 0; # debug flag
1.1       ckuethe    48:
1.14      espie      49: {
1.16      espie      50:     my $d = $ENV{PKG_CONFIG_TOP_BUILD_DIR};
1.14      espie      51:     if (defined $d) {
                     52:            $variables->{pc_top_builddir} = $d;
                     53:     } else {
                     54:            $variables->{pc_top_builddir} = '$(top_builddir)';
                     55:     }
1.32      jasper     56:
                     57:     my $s = $ENV{PKG_CONFIG_SYSROOT_DIR};
                     58:     if (defined $s) {
                     59:            $variables->{pc_sysrootdir} = $s;
                     60:     }
                     61:     # The default '/' is implied.
1.14      espie      62: }
1.29      jasper     63:
                     64: $D = 1 if defined($ENV{PKG_CONFIG_DEBUG_SPEW});
1.7       ckuethe    65:
1.4       espie      66: if ($logfile) {
                     67:        open my $L, ">>" . $logfile;
                     68:        print $L '[' . join('] [', $0, @ARGV) . "]\n";
                     69:        close $L;
1.1       ckuethe    70: }
                     71:
                     72: # combo arg-parsing and dependency resolution loop. Hopefully when the loop
                     73: # terminates, we have a full list of packages upon which we depend, and the
                     74: # right set of compiler and linker flags to use them.
                     75: #
                     76: # as each .pc file is loaded, it is stored in %configs, indexed by package
                     77: # name. this makes it possible to then pull out flags or do substitutions
1.34    ! jasper     78: # without having to go back and reload the files from disk.
1.1       ckuethe    79:
                     80: Getopt::Long::Configure('no_ignore_case');
                     81: GetOptions(    'debug' => \$D,
                     82:                'help' => \&help, #does not return
                     83:                'usage' => \&help, #does not return
1.14      espie      84:                'list-all' => \$mode{list},
1.1       ckuethe    85:                'version' => sub { print "$version\n" ; exit(0);} ,
1.11      espie      86:                'errors-to-stdout' => sub { $mode{estdout} = 1},
                     87:                'print-errors' => sub { $mode{printerr} = 1},
                     88:                'silence-errors' => sub { $mode{printerr} = 0},
1.23      jasper     89:                'short-errors' => sub { $mode{printerr} = 0},
1.14      espie      90:                'atleast-pkgconfig-version=s' => \$mode{myminvers},
1.30      jasper     91:                'print-provides' => \$mode{printprovides},
                     92:                'print-requires' => \$mode{printrequires},
                     93:                'print-requires-private' => \$mode{printrequiresprivate},
1.11      espie      94:
                     95:                'cflags' => sub { $mode{cflags} = 3},
                     96:                'cflags-only-I' => sub { $mode{cflags} |= 1},
                     97:                'cflags-only-other' => sub { $mode{cflags} |= 2},
                     98:                'libs' => sub { $mode{libs} = 7},
                     99:                'libs-only-l' => sub { $mode{libs} |= 1},
                    100:                'libs-only-L' => sub { $mode{libs} |= 2},
                    101:                'libs-only-other' => sub { $mode{libs} |= 4},
                    102:                'exists' => sub { $mode{exists} = 1} ,
                    103:                'static' => sub { $mode{static} = 1},
                    104:                'uninstalled' => sub { $mode{uninstalled} = 1},
1.14      espie     105:                'atleast-version=s' => \$mode{minversion},
                    106:                'exact-version=s' => \$mode{exactversion},
                    107:                'max-version=s' => \$mode{maxversion},
1.13      espie     108:                'modversion' => \$mode{modversion},
1.11      espie     109:                'variable=s' => \$mode{variable},
                    110:                'define-variable=s' => $variables,
1.1       ckuethe   111:        );
                    112:
1.14      espie     113: # Initial value of printerr depends on the options...
                    114: if (!defined $mode{printerr}) {
1.33      jasper    115:        if (defined $mode{libs} || defined $mode{cflags}
1.14      espie     116:            || defined $mode{version} || defined $mode{list}) {
                    117:                $mode{printerr} = 1;
                    118:        } else {
                    119:                $mode{printerr} = 0;
                    120:        }
                    121: }
                    122:
1.4       espie     123: print STDERR "\n[" . join('] [', $0, @ARGV) . "]\n" if $D;
1.13      espie     124:
                    125: my $rc = 0;
1.1       ckuethe   126:
1.14      espie     127: # XXX pkg-config is a bit weird
1.10      espie     128: {
                    129: my $p = join(' ', @ARGV);
1.14      espie     130: $p =~ s/^\s+//;
1.4       espie     131: @ARGV = split /\s+/, $p;
1.10      espie     132: }
1.1       ckuethe   133:
1.14      espie     134: if ($mode{myminvers}) {
                    135:        exit self_version($mode{myminvers});
                    136: }
                    137:
                    138: if ($mode{list}) {
                    139:        exit do_list();
1.1       ckuethe   140: }
                    141:
1.13      espie     142: my $cfg_full_list = [];
1.14      espie     143: my $top_config = [];
1.1       ckuethe   144:
                    145: while (@ARGV){
1.13      espie     146:        my $p = shift @ARGV;
                    147:        my $op = undef;
                    148:        my $v = undef;
                    149:        if (@ARGV >= 2  && $ARGV[0] =~ /[<=>]+/ &&
                    150:            $ARGV[1] =~ /[0-9\.]+/) {
                    151:                $op = shift @ARGV;
                    152:                $v = shift @ARGV;
1.1       ckuethe   153:        }
                    154:        $p =~ s/,//g;
1.13      espie     155:        handle_config($p, $op, $v, $cfg_full_list);
1.14      espie     156:        push(@$top_config, $p);
                    157: }
                    158:
                    159: if ($mode{exists}) {
                    160:        exit $rc;
                    161: }
                    162:
                    163: if ($mode{uninstalled}) {
                    164:        $rc = 1 unless $found_uninstalled;
                    165:        exit $rc;
1.11      espie     166: }
1.1       ckuethe   167:
1.30      jasper    168: if ($mode{modversion} || $mode{printprovides}) {
1.14      espie     169:        for my $pkg (@$top_config) {
                    170:                do_modversion($pkg);
                    171:        }
                    172: }
1.13      espie     173:
1.30      jasper    174: if ($mode{printrequires} || $mode{printrequiresprivate}) {
                    175:        for my $pkg (@$top_config) {
                    176:                print_requires($pkg);
                    177:        }
                    178: }
                    179:
1.14      espie     180: if ($mode{minversion}) {
                    181:        my $v = $mode{minversion};
                    182:        for my $pkg (@$top_config) {
                    183:                $rc = 1 unless versionmatch($configs{$pkg}, '>=', $v);
                    184:        }
                    185:        exit $rc;
                    186: }
                    187:
                    188: if ($mode{exactversion}) {
                    189:        my $v = $mode{exactversion};
                    190:        for my $pkg (@$top_config) {
                    191:                $rc = 1 unless versionmatch($configs{$pkg}, '=', $v);
                    192:        }
                    193:        exit $rc;
                    194: }
                    195:
                    196: if ($mode{minversion}) {
                    197:        my $v = $mode{maxversion};
                    198:        for my $pkg (@$top_config) {
                    199:                $rc = 1 unless versionmatch($configs{$pkg}, '<=', $v);
                    200:        }
                    201:        exit $rc;
                    202: }
                    203:
                    204: my @vlist = ();
                    205:
                    206: if ($mode{variable}) {
                    207:        for my $pkg (@$top_config) {
                    208:                do_variable($pkg, $mode{variable});
                    209:        }
                    210: }
                    211:
                    212: my $dep_cfg_list = simplify_and_reverse($cfg_full_list);
                    213:
                    214: if ($mode{cflags} || $mode{libs} || $mode{variable}) {
                    215:     push @vlist, do_cflags($dep_cfg_list) if $mode{cflags};
                    216:     push @vlist, do_libs($dep_cfg_list) if $mode{libs};
1.17      espie     217:     print join(' ', @vlist), "\n" if $rc == 0;
1.1       ckuethe   218: }
                    219:
1.13      espie     220: exit $rc;
1.1       ckuethe   221:
                    222: ###########################################################################
                    223:
1.11      espie     224: sub handle_config
                    225: {
1.13      espie     226:        my ($p, $op, $v, $list) = @_;
1.26      jasper    227:        my $cfg;
1.13      espie     228:
1.26      jasper    229:        # pkg-config won't install a pkg-config.pc file itself, but it may be
                    230:        # listed as a dependency in other files.
                    231:        # If we encounter a dependency on pkg-config, check if our version
                    232:        # is sufficient and error out if not.
                    233:        if ($p eq "pkg-config"){
                    234:                if ($v > $version) {
                    235:                        print STDERR "pkg-config version $version too old, $v required.\n" if $D;
                    236:                        $rc = 1;
                    237:                        return undef;
                    238:                }
                    239:        } else {
                    240:                $cfg = cache_find_config($p);
1.11      espie     241:
1.26      jasper    242:                unshift @$list, $p if defined $cfg;
1.15      espie     243:
1.26      jasper    244:                if (!defined $cfg) {
1.13      espie     245:                        $rc = 1;
                    246:                        return undef;
                    247:                }
1.11      espie     248:
1.26      jasper    249:                if (defined $op) {
                    250:                        if (!versionmatch($cfg, $op, $v)) {
                    251:                                mismatch($p, $cfg, $op, $v) if $mode{printerr};
                    252:                                $rc = 1;
                    253:                                return undef;
                    254:                        }
                    255:                }
                    256:
                    257:                my $deps = $cfg->get_property('Requires', $variables);
                    258:                if (defined $deps) {
                    259:                        for my $dep (@$deps) {
                    260:                                if ($dep =~ m/^(.*?)\s*([<=>]+)\s*([\d\.]+)$/) {
                    261:                                        handle_config($1, $2, $3, $list);
                    262:                                } else {
                    263:                                        handle_config($dep, undef, undef, $list);
                    264:                                }
1.13      espie     265:                        }
1.26      jasper    266:                        print STDERR "package $p requires ",
                    267:                            join(',', @$deps), "\n" if $D;
1.13      espie     268:                }
1.11      espie     269:
1.26      jasper    270:                $deps = $cfg->get_property('Requires.private', $variables);
                    271:                if (defined $deps) {
                    272:                        for my $dep (@$deps) {
                    273:                                if ($dep =~ m/^(.*?)\s*([<=>]+)\s*([\d\.]+)$/) {
                    274:                                        handle_config($1, $2, $3, $list);
                    275:                                } else {
                    276:                                        handle_config($dep, undef, undef, $list);
                    277:                                }
1.13      espie     278:                        }
1.26      jasper    279:                        print STDERR "package $p requires (private)",
                    280:                            join(',', @$deps), "\n" if $D;
1.13      espie     281:                }
1.11      espie     282:        }
                    283: }
                    284:
1.1       ckuethe   285: # look for the .pc file in each of the PKGPATH elements. Return the path or
                    286: # undef if it's not there
1.4       espie     287: sub pathresolve
                    288: {
                    289:        my ($p) = @_;
                    290:
1.14      espie     291:        if ($allow_uninstalled && $p !~ m/\-uninstalled$/) {
                    292:                foreach my $d (@PKGPATH) {
                    293:                        my $f = "$d/$p-uninstalled.pc";
                    294:                        print STDERR "pathresolve($p) looking in $f\n" if $D;
                    295:                        if (-f $f) {
                    296:                                $found_uninstalled = 1;
                    297:                                return $f;
                    298:                        }
                    299:                }
                    300:        }
                    301:
1.4       espie     302:        foreach my $d (@PKGPATH) {
1.10      espie     303:                my $f = "$d/$p.pc";
1.4       espie     304:                print STDERR "pathresolve($p) looking in $f\n" if $D;
1.10      espie     305:                return $f if -f $f;
1.1       ckuethe   306:        }
1.10      espie     307:        return undef;
1.1       ckuethe   308: }
                    309:
1.11      espie     310: sub get_config
                    311: {
                    312:        my ($f) = @_;
                    313:
                    314:        my $cfg;
1.33      jasper    315:        eval {
1.11      espie     316:            $cfg = OpenBSD::PkgConfig->read_file($f);
                    317:        };
                    318:        if (!$@) {
                    319:                return $cfg;
                    320:        } else {
1.12      espie     321:                print STDERR $@, "\n" if $D;
1.11      espie     322:        }
                    323:        return undef;
                    324: }
                    325:
1.13      espie     326: sub cache_find_config
                    327: {
                    328:        my $name = shift;
                    329:
                    330:        print STDERR "processing $name\n" if $D;
                    331:
                    332:        if (exists $configs{$name}) {
                    333:                return $configs{$name};
                    334:        } else {
                    335:                return $configs{$name} = find_config($name);
                    336:        }
                    337: }
                    338:
1.11      espie     339: sub find_config
                    340: {
                    341:        my ($p) = @_;
                    342:        my $f = pathresolve($p);
                    343:        if (defined $f) {
                    344:                return get_config($f);
                    345:        }
1.13      espie     346:        if ($mode{printerr}) {
1.33      jasper    347:            print STDERR
1.13      espie     348:                "Package $p was not found in the pkg-config search path\n";
                    349:        }
1.11      espie     350:        return undef;
                    351: }
1.1       ckuethe   352:
1.11      espie     353: sub stringize
1.4       espie     354: {
1.11      espie     355:        my $list = shift;
1.21      simon     356:        my $sep = shift || ',';
1.4       espie     357:
1.11      espie     358:        if (defined $list) {
1.21      simon     359:                return join($sep, @$list)
1.11      espie     360:        } else {
                    361:                return '';
1.1       ckuethe   362:        }
                    363: }
                    364:
                    365: #if the variable option is set, pull out the named variable
1.4       espie     366: sub do_variable
                    367: {
1.11      espie     368:        my ($p, $v) = @_;
1.1       ckuethe   369:
1.13      espie     370:        my $cfg = cache_find_config($p);
                    371:
                    372:        if (defined $cfg) {
1.11      espie     373:                my $value = $cfg->get_variable($v, $variables);
                    374:                if (defined $value) {
1.13      espie     375:                        push(@vlist, $value);
1.11      espie     376:                }
1.19      espie     377:                return undef;
1.11      espie     378:        }
1.19      espie     379:        $rc = 1;
1.1       ckuethe   380: }
                    381:
1.30      jasper    382: #if the modversion or print-provides options are set,
                    383: #pull out the compiler flags
1.4       espie     384: sub do_modversion
                    385: {
1.11      espie     386:        my ($p) = @_;
1.1       ckuethe   387:
1.13      espie     388:        my $cfg = cache_find_config($p);
                    389:
                    390:        if (defined $cfg) {
1.11      espie     391:                my $value = $cfg->get_property('Version', $variables);
                    392:                if (defined $value) {
1.30      jasper    393:                        if (!defined($mode{printprovides})){
                    394:                                print stringize($value), "\n";
                    395:                                return undef;
                    396:                        } else {
                    397:                                print "$p = " . stringize($value) . "\n";
                    398:                                return undef;
                    399:                        }
1.11      espie     400:                }
                    401:        }
1.13      espie     402:        $rc = 1;
1.1       ckuethe   403: }
                    404:
                    405: #if the cflags option is set, pull out the compiler flags
1.4       espie     406: sub do_cflags
                    407: {
1.14      espie     408:        my $list = shift;
                    409:
1.11      espie     410:        my $cflags = [];
1.1       ckuethe   411:
1.14      espie     412:        foreach my $pkg (@$list) {
1.11      espie     413:                my $l = $configs{$pkg}->get_property('Cflags', $variables);
                    414:                push(@$cflags, @$l) if defined $l;
                    415:        }
1.32      jasper    416:        my $a = OpenBSD::PkgConfig->compress($cflags,
1.11      espie     417:                sub {
                    418:                        local $_ = shift;
                    419:                        if (($mode{cflags} & 1) && /^-I/ ||
                    420:                            ($mode{cflags} & 2) && !/^-I/) {
                    421:                            return 1;
                    422:                        } else {
                    423:                            return 0;
1.4       espie     424:                        }
1.11      espie     425:                });
1.32      jasper    426:        if (defined($a) && defined($variables->{pc_sysrootdir})){
                    427:                $a =~ s/^-I/$&$variables->{pc_sysrootdir}/g;
                    428:        }
                    429:
                    430:        return $a;
1.1       ckuethe   431: }
                    432:
                    433: #if the lib option is set, pull out the linker flags
1.4       espie     434: sub do_libs
                    435: {
1.14      espie     436:        my $list = shift;
                    437:
1.11      espie     438:        my $libs = [];
1.1       ckuethe   439:
1.14      espie     440:        foreach my $pkg (@$list) {
1.11      espie     441:                my $l = $configs{$pkg}->get_property('Libs', $variables);
                    442:                push(@$libs, @$l) if defined $l;
                    443:        }
1.13      espie     444:        my $a = OpenBSD::PkgConfig->compress($libs,
1.11      espie     445:                sub {
                    446:                        local $_ = shift;
1.13      espie     447:                        if (($mode{libs} & 2) && /^-L/ ||
1.11      espie     448:                            ($mode{libs} & 4) && !/^-[lL]/) {
                    449:                            return 1;
                    450:                        } else {
                    451:                            return 0;
1.4       espie     452:                        }
1.11      espie     453:                });
1.32      jasper    454:
                    455:        if (defined($variables->{pc_sysrootdir})){
                    456:                $a =~ s/^-[lL]/$&$variables->{pc_sysrootdir}/g;
                    457:        }
                    458:
1.13      espie     459:        if ($mode{libs} & 1) {
                    460:                my $b = OpenBSD::PkgConfig->rcompress($libs,
                    461:                        sub { shift =~ m/^-l/; });
                    462:                return ($a, $b);
                    463:        } else {
                    464:                return $a;
                    465:        }
1.1       ckuethe   466: }
                    467:
                    468: #list all packages
1.4       espie     469: sub do_list
                    470: {
1.1       ckuethe   471:        my ($p, $x, $y, @files, $fname, $name);
1.20      espie     472:        my $error = 0;
                    473:
1.33      jasper    474:        foreach my $p (@PKGPATH) {
                    475:                push(@files, <$p/*.pc>);
1.4       espie     476:        }
1.1       ckuethe   477:
                    478:        # Scan the lengths of the package names so I can make a format
                    479:        # string to line the list up just like the real pkgconfig does.
                    480:        $x = 0;
1.4       espie     481:        foreach my $f (@files) {
                    482:                $fname = basename($f, '.pc');
                    483:                $y = length $fname;
1.1       ckuethe   484:                $x = (($y > $x) ? $y : $x);
                    485:        }
                    486:        $x *= -1;
                    487:
1.4       espie     488:        foreach my $f (@files) {
1.11      espie     489:                my $cfg = get_config($f);
1.20      espie     490:                if (!defined $cfg) {
                    491:                        print STDERR "Problem reading file $f\n";
                    492:                        $error = 1;
                    493:                        next;
                    494:                }
1.4       espie     495:                $fname = basename($f, '.pc');
1.33      jasper    496:                printf("%${x}s %s - %s\n", $fname,
                    497:                    stringize($cfg->get_property('Name', $variables)),
1.21      simon     498:                    stringize($cfg->get_property('Description', $variables),
                    499:                    ' '));
1.1       ckuethe   500:        }
1.20      espie     501:        return $error;
1.1       ckuethe   502: }
                    503:
1.4       espie     504: sub help
                    505: {
1.1       ckuethe   506:        print <<EOF
                    507: Usage: $0 [options]
                    508: --debug        - turn on debugging output
                    509: --help - this message
                    510: --usage - this message
                    511: --list-all - show all packages that $0 can find
1.8       ckuethe   512: --version - print version of pkgconfig
                    513: --errors-to-stdout - direct error messages to stdout rather than stderr
                    514: --print-errors - print error messages in case of error
1.34    ! jasper    515: --print-provides - print all the modules the given package provides
        !           516: --print-requires - print all the modules the given package requires
        !           517: --print-requires-private - print all the private modules the given package requires
1.8       ckuethe   518: --silence-errors - don't print error messages in case of error
1.1       ckuethe   519: --atleast-pkgconfig-version [version] - require a certain version of pkgconfig
                    520: --cflags package [versionspec] [package [versionspec]]
                    521: --cflags-only-I - only output -Iincludepath flags
                    522: --cflags-only-other - only output flags that are not -I
1.11      espie     523: --define-variable=NAME=VALUE - define variables
1.1       ckuethe   524: --libs package [versionspec] [package [versionspec]]
                    525: --libs-only-l - only output -llib flags
                    526: --libs-only-L - only output -Llibpath flags
                    527: --libs-only-other - only output flags that are not -l or -L
                    528: --exists package [versionspec] [package [versionspec]]
                    529: --uninstalled - allow for uninstalled versions to be used
1.8       ckuethe   530: --static - adjust output for static linking
                    531: --atleast-version [version] - require a certain version of a package
                    532: --modversion [package] - query the version of a package
                    533: --variable var package - return the definition of <var> in <package>
1.1       ckuethe   534: EOF
                    535: ;
1.22      simon     536:        exit 0;
1.1       ckuethe   537: }
                    538:
                    539: # do we meet/beat the version the caller requested?
1.4       espie     540: sub self_version
                    541: {
                    542:        my ($v) = @_;
                    543:        my (@a, @b);
                    544:
                    545:        @a = split /\./, $v;
                    546:        @b = split /\./, $version;
1.1       ckuethe   547:
1.4       espie     548:        if (($b[0] >= $a[0]) && ($b[1] >= $a[1])) {
1.14      espie     549:                return 0;
1.1       ckuethe   550:        } else {
1.14      espie     551:                return 1;
                    552:        }
                    553: }
                    554:
                    555: sub compare
                    556: {
                    557:        my ($a, $b) = @_;
                    558:
1.28      jasper    559:        return 0 if ($a eq $b);
1.14      espie     560:
                    561:        my @a = split /\./, $a;
                    562:        my @b = split /\./, $b;
                    563:
                    564:        while (@a && @b) { #so long as both lists have something
                    565:                return 1 if $a[0] > $b[0];
                    566:                return -1 if $a[0] < $b[0];
                    567:                shift @a; shift @b;
                    568:        }
                    569:        return 1 if @a;
                    570:        return -1 if @b;
                    571:        return 0;
1.1       ckuethe   572: }
                    573:
                    574: # got a package meeting the requested specific version?
1.4       espie     575: sub versionmatch
                    576: {
1.14      espie     577:        my ($cfg, $op, $want) = @_;
1.33      jasper    578:
1.1       ckuethe   579:        # can't possibly match if we can't find the file
1.11      espie     580:        return 0 if !defined $cfg;
                    581:
1.14      espie     582:        my $inst = stringize($cfg->get_property('Version', $variables));
1.11      espie     583:
1.1       ckuethe   584:        # can't possibly match if we can't find the version string
1.14      espie     585:        return 0 if $inst eq '';
1.1       ckuethe   586:
1.14      espie     587:        print "comparing $want (wanted) to $inst (installed)\n" if $D;
                    588:        my $value = compare($inst, $want);
1.31      jasper    589:        if    ($op eq '>=') { return $value >= 0; }
                    590:        elsif ($op eq '=')  { return $value == 0; }
                    591:        elsif ($op eq '!=') { return $value != 0; }
                    592:        elsif ($op eq '<')  { return $value < 0; }
                    593:        elsif ($op eq '>')  { return $value > 0; }
                    594:        elsif ($op eq '<=') { return $value <= 0; }
1.13      espie     595: }
                    596:
                    597: sub mismatch
                    598: {
                    599:        my ($p, $cfg, $op, $v) = @_;
                    600:        print STDERR "Requested '$p $op $v' but version of ",
                    601:            stringize($cfg->get_property('Name')), " is ",
                    602:            stringize($cfg->get_property('Version')), "\n";
                    603: }
                    604:
                    605: sub simplify_and_reverse
                    606: {
                    607:        my $reqlist = shift;
                    608:        my $dejavu = {};
                    609:        my $result = [];
                    610:
                    611:        for my $item (@$reqlist) {
                    612:                if (!$dejavu->{$item}) {
                    613:                        unshift @$result, $item;
                    614:                        $dejavu->{$item} = 1;
                    615:                }
                    616:        }
                    617:        return $result;
1.30      jasper    618: }
                    619:
                    620: # retrieve and print Requires(.private)
                    621: sub print_requires
                    622: {
                    623:        my ($p) = @_;
                    624:
                    625:        my $cfg = cache_find_config($p);
                    626:
                    627:        if (defined($cfg)) {
                    628:                my $value;
                    629:
                    630:                if (defined($mode{printrequires})) {
                    631:                        $value = $cfg->get_property('Requires', $variables);
                    632:                } elsif (defined($mode{printrequiresprivate})) {
                    633:                        $value = $cfg->get_property('Requires.private', $variables);
                    634:                } else {
                    635:                        print STDERR "Unknown mode for print_requires.\n" if $D;
                    636:                        return 1;
                    637:                }
                    638:
                    639:                if (defined($value)) {
                    640:                        print "$_\n" foreach (@$value);
                    641:                        return undef;
                    642:                }
                    643:        }
                    644:
                    645:        $rc = 1;
1.1       ckuethe   646: }