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

1.1       ckuethe     1: #!/usr/bin/perl
1.39    ! jasper      2: # $OpenBSD: pkg-config,v 1.38 2011/03/18 15:18: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 = ();
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) {
1.39    ! jasper    327:                        $f =~ s/(^.*\/)(.*?)\.pc$/$2/g;
        !           328:                        print STDERR "Package '$f' has no $_: field\n";
1.37      jasper    329:                        return undef;
                    330:                }
                    331:        }
                    332:
                    333:        return $cfg;
1.13      espie     334: }
                    335:
1.35      espie     336: # pkg-config won't install a pkg-config.pc file itself, but it may be
                    337:
                    338: # listed as a dependency in other files.
                    339: # so, prime the cache with self
                    340: sub setup_self
                    341: {
                    342:        my $pkg_pc = OpenBSD::PkgConfig->new;
                    343:        $pkg_pc->add_property('Version', $version);
1.38      jasper    344:        $pkg_pc->add_variable('pc_path', join(":", @PKGPATH));
1.35      espie     345:        $configs{'pkg-config'} = $pkg_pc;
                    346: }
                    347:
1.11      espie     348: sub find_config
                    349: {
                    350:        my ($p) = @_;
                    351:        my $f = pathresolve($p);
                    352:        if (defined $f) {
                    353:                return get_config($f);
                    354:        }
1.13      espie     355:        if ($mode{printerr}) {
1.33      jasper    356:            print STDERR
1.13      espie     357:                "Package $p was not found in the pkg-config search path\n";
                    358:        }
1.11      espie     359:        return undef;
                    360: }
1.1       ckuethe   361:
1.11      espie     362: sub stringize
1.4       espie     363: {
1.11      espie     364:        my $list = shift;
1.21      simon     365:        my $sep = shift || ',';
1.4       espie     366:
1.11      espie     367:        if (defined $list) {
1.21      simon     368:                return join($sep, @$list)
1.11      espie     369:        } else {
                    370:                return '';
1.1       ckuethe   371:        }
                    372: }
                    373:
                    374: #if the variable option is set, pull out the named variable
1.4       espie     375: sub do_variable
                    376: {
1.11      espie     377:        my ($p, $v) = @_;
1.1       ckuethe   378:
1.13      espie     379:        my $cfg = cache_find_config($p);
                    380:
                    381:        if (defined $cfg) {
1.11      espie     382:                my $value = $cfg->get_variable($v, $variables);
                    383:                if (defined $value) {
1.13      espie     384:                        push(@vlist, $value);
1.11      espie     385:                }
1.19      espie     386:                return undef;
1.11      espie     387:        }
1.19      espie     388:        $rc = 1;
1.1       ckuethe   389: }
                    390:
1.30      jasper    391: #if the modversion or print-provides options are set,
                    392: #pull out the compiler flags
1.4       espie     393: sub do_modversion
                    394: {
1.11      espie     395:        my ($p) = @_;
1.1       ckuethe   396:
1.13      espie     397:        my $cfg = cache_find_config($p);
                    398:
                    399:        if (defined $cfg) {
1.11      espie     400:                my $value = $cfg->get_property('Version', $variables);
                    401:                if (defined $value) {
1.30      jasper    402:                        if (!defined($mode{printprovides})){
                    403:                                print stringize($value), "\n";
                    404:                                return undef;
                    405:                        } else {
                    406:                                print "$p = " . stringize($value) . "\n";
                    407:                                return undef;
                    408:                        }
1.11      espie     409:                }
                    410:        }
1.13      espie     411:        $rc = 1;
1.1       ckuethe   412: }
                    413:
                    414: #if the cflags option is set, pull out the compiler flags
1.4       espie     415: sub do_cflags
                    416: {
1.14      espie     417:        my $list = shift;
                    418:
1.11      espie     419:        my $cflags = [];
1.1       ckuethe   420:
1.14      espie     421:        foreach my $pkg (@$list) {
1.11      espie     422:                my $l = $configs{$pkg}->get_property('Cflags', $variables);
                    423:                push(@$cflags, @$l) if defined $l;
                    424:        }
1.32      jasper    425:        my $a = OpenBSD::PkgConfig->compress($cflags,
1.11      espie     426:                sub {
                    427:                        local $_ = shift;
                    428:                        if (($mode{cflags} & 1) && /^-I/ ||
                    429:                            ($mode{cflags} & 2) && !/^-I/) {
                    430:                            return 1;
                    431:                        } else {
                    432:                            return 0;
1.4       espie     433:                        }
1.11      espie     434:                });
1.32      jasper    435:        if (defined($a) && defined($variables->{pc_sysrootdir})){
1.36      jasper    436:                $a =~ s/[\w]?-I/$&$variables->{pc_sysrootdir}/g;
1.32      jasper    437:        }
                    438:
                    439:        return $a;
1.1       ckuethe   440: }
                    441:
                    442: #if the lib option is set, pull out the linker flags
1.4       espie     443: sub do_libs
                    444: {
1.14      espie     445:        my $list = shift;
                    446:
1.11      espie     447:        my $libs = [];
1.1       ckuethe   448:
1.14      espie     449:        foreach my $pkg (@$list) {
1.11      espie     450:                my $l = $configs{$pkg}->get_property('Libs', $variables);
                    451:                push(@$libs, @$l) if defined $l;
                    452:        }
1.13      espie     453:        my $a = OpenBSD::PkgConfig->compress($libs,
1.11      espie     454:                sub {
                    455:                        local $_ = shift;
1.13      espie     456:                        if (($mode{libs} & 2) && /^-L/ ||
1.11      espie     457:                            ($mode{libs} & 4) && !/^-[lL]/) {
                    458:                            return 1;
                    459:                        } else {
                    460:                            return 0;
1.4       espie     461:                        }
1.11      espie     462:                });
1.32      jasper    463:
                    464:        if (defined($variables->{pc_sysrootdir})){
1.36      jasper    465:                $a =~ s/[\w]?-[lL]/$&$variables->{pc_sysrootdir}/g;
1.32      jasper    466:        }
                    467:
1.13      espie     468:        if ($mode{libs} & 1) {
                    469:                my $b = OpenBSD::PkgConfig->rcompress($libs,
                    470:                        sub { shift =~ m/^-l/; });
                    471:                return ($a, $b);
                    472:        } else {
                    473:                return $a;
                    474:        }
1.1       ckuethe   475: }
                    476:
                    477: #list all packages
1.4       espie     478: sub do_list
                    479: {
1.1       ckuethe   480:        my ($p, $x, $y, @files, $fname, $name);
1.20      espie     481:        my $error = 0;
                    482:
1.33      jasper    483:        foreach my $p (@PKGPATH) {
                    484:                push(@files, <$p/*.pc>);
1.4       espie     485:        }
1.1       ckuethe   486:
                    487:        # Scan the lengths of the package names so I can make a format
                    488:        # string to line the list up just like the real pkgconfig does.
                    489:        $x = 0;
1.4       espie     490:        foreach my $f (@files) {
                    491:                $fname = basename($f, '.pc');
                    492:                $y = length $fname;
1.1       ckuethe   493:                $x = (($y > $x) ? $y : $x);
                    494:        }
                    495:        $x *= -1;
                    496:
1.4       espie     497:        foreach my $f (@files) {
1.11      espie     498:                my $cfg = get_config($f);
1.20      espie     499:                if (!defined $cfg) {
                    500:                        print STDERR "Problem reading file $f\n";
                    501:                        $error = 1;
                    502:                        next;
                    503:                }
1.4       espie     504:                $fname = basename($f, '.pc');
1.33      jasper    505:                printf("%${x}s %s - %s\n", $fname,
                    506:                    stringize($cfg->get_property('Name', $variables)),
1.21      simon     507:                    stringize($cfg->get_property('Description', $variables),
                    508:                    ' '));
1.1       ckuethe   509:        }
1.20      espie     510:        return $error;
1.1       ckuethe   511: }
                    512:
1.4       espie     513: sub help
                    514: {
1.1       ckuethe   515:        print <<EOF
                    516: Usage: $0 [options]
                    517: --debug        - turn on debugging output
                    518: --help - this message
                    519: --usage - this message
                    520: --list-all - show all packages that $0 can find
1.8       ckuethe   521: --version - print version of pkgconfig
                    522: --errors-to-stdout - direct error messages to stdout rather than stderr
                    523: --print-errors - print error messages in case of error
1.34      jasper    524: --print-provides - print all the modules the given package provides
                    525: --print-requires - print all the modules the given package requires
                    526: --print-requires-private - print all the private modules the given package requires
1.8       ckuethe   527: --silence-errors - don't print error messages in case of error
1.1       ckuethe   528: --atleast-pkgconfig-version [version] - require a certain version of pkgconfig
                    529: --cflags package [versionspec] [package [versionspec]]
                    530: --cflags-only-I - only output -Iincludepath flags
                    531: --cflags-only-other - only output flags that are not -I
1.11      espie     532: --define-variable=NAME=VALUE - define variables
1.1       ckuethe   533: --libs package [versionspec] [package [versionspec]]
                    534: --libs-only-l - only output -llib flags
                    535: --libs-only-L - only output -Llibpath flags
                    536: --libs-only-other - only output flags that are not -l or -L
                    537: --exists package [versionspec] [package [versionspec]]
                    538: --uninstalled - allow for uninstalled versions to be used
1.8       ckuethe   539: --static - adjust output for static linking
                    540: --atleast-version [version] - require a certain version of a package
                    541: --modversion [package] - query the version of a package
                    542: --variable var package - return the definition of <var> in <package>
1.1       ckuethe   543: EOF
                    544: ;
1.22      simon     545:        exit 0;
1.1       ckuethe   546: }
                    547:
                    548: # do we meet/beat the version the caller requested?
1.4       espie     549: sub self_version
                    550: {
                    551:        my ($v) = @_;
                    552:        my (@a, @b);
                    553:
                    554:        @a = split /\./, $v;
                    555:        @b = split /\./, $version;
1.1       ckuethe   556:
1.4       espie     557:        if (($b[0] >= $a[0]) && ($b[1] >= $a[1])) {
1.14      espie     558:                return 0;
1.1       ckuethe   559:        } else {
1.14      espie     560:                return 1;
                    561:        }
                    562: }
                    563:
                    564: sub compare
                    565: {
                    566:        my ($a, $b) = @_;
                    567:
1.28      jasper    568:        return 0 if ($a eq $b);
1.14      espie     569:
                    570:        my @a = split /\./, $a;
                    571:        my @b = split /\./, $b;
                    572:
                    573:        while (@a && @b) { #so long as both lists have something
                    574:                return 1 if $a[0] > $b[0];
                    575:                return -1 if $a[0] < $b[0];
                    576:                shift @a; shift @b;
                    577:        }
                    578:        return 1 if @a;
                    579:        return -1 if @b;
                    580:        return 0;
1.1       ckuethe   581: }
                    582:
                    583: # got a package meeting the requested specific version?
1.4       espie     584: sub versionmatch
                    585: {
1.14      espie     586:        my ($cfg, $op, $want) = @_;
1.33      jasper    587:
1.1       ckuethe   588:        # can't possibly match if we can't find the file
1.11      espie     589:        return 0 if !defined $cfg;
                    590:
1.14      espie     591:        my $inst = stringize($cfg->get_property('Version', $variables));
1.11      espie     592:
1.1       ckuethe   593:        # can't possibly match if we can't find the version string
1.14      espie     594:        return 0 if $inst eq '';
1.1       ckuethe   595:
1.14      espie     596:        print "comparing $want (wanted) to $inst (installed)\n" if $D;
                    597:        my $value = compare($inst, $want);
1.31      jasper    598:        if    ($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; }
                    602:        elsif ($op eq '>')  { return $value > 0; }
                    603:        elsif ($op eq '<=') { return $value <= 0; }
1.13      espie     604: }
                    605:
                    606: sub mismatch
                    607: {
                    608:        my ($p, $cfg, $op, $v) = @_;
                    609:        print STDERR "Requested '$p $op $v' but version of ",
                    610:            stringize($cfg->get_property('Name')), " is ",
                    611:            stringize($cfg->get_property('Version')), "\n";
                    612: }
                    613:
                    614: sub simplify_and_reverse
                    615: {
                    616:        my $reqlist = shift;
                    617:        my $dejavu = {};
                    618:        my $result = [];
                    619:
                    620:        for my $item (@$reqlist) {
                    621:                if (!$dejavu->{$item}) {
                    622:                        unshift @$result, $item;
                    623:                        $dejavu->{$item} = 1;
                    624:                }
                    625:        }
                    626:        return $result;
1.30      jasper    627: }
                    628:
                    629: # retrieve and print Requires(.private)
                    630: sub print_requires
                    631: {
                    632:        my ($p) = @_;
                    633:
                    634:        my $cfg = cache_find_config($p);
                    635:
                    636:        if (defined($cfg)) {
                    637:                my $value;
                    638:
                    639:                if (defined($mode{printrequires})) {
                    640:                        $value = $cfg->get_property('Requires', $variables);
                    641:                } elsif (defined($mode{printrequiresprivate})) {
                    642:                        $value = $cfg->get_property('Requires.private', $variables);
                    643:                } else {
                    644:                        print STDERR "Unknown mode for print_requires.\n" if $D;
                    645:                        return 1;
                    646:                }
                    647:
                    648:                if (defined($value)) {
                    649:                        print "$_\n" foreach (@$value);
                    650:                        return undef;
                    651:                }
                    652:        }
                    653:
                    654:        $rc = 1;
1.35      espie     655: }
                    656:
                    657: sub beautify_list
                    658: {
                    659:        return join(' ', map {"[$_]"} @_);
1.1       ckuethe   660: }