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

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