[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.37

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