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

1.1       ckuethe     1: #!/usr/bin/perl
1.66    ! jasper      2: # $OpenBSD: pkg-config,v 1.65 2011/06/16 08:33:54 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}) {
1.66    ! jasper     30:        @PKGPATH = split(/:/, $ENV{PKG_CONFIG_LIBDIR});
1.16      espie      31: } elsif (defined($ENV{PKG_CONFIG_PATH}) && $ENV{PKG_CONFIG_PATH}) {
1.66    ! jasper     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.62      jasper    119: say_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.66    ! jasper    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}) {
1.66    ! jasper    214:        push @vlist, do_cflags($dep_cfg_list) if $mode{cflags};
        !           215:        push @vlist, do_libs($dep_cfg_list) if $mode{libs};
        !           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.62      jasper    255:                        say_debug("package $p " . lc($property) . " " . join(',', @$deps));
1.26      jasper    256:                }
1.43      jasper    257:        };
                    258:
1.66    ! jasper    259:        if (defined $mode{cflags}
        !           260:            or ($mode{static} && $mode{libs})
        !           261:            or $mode{printrequiresprivate}) {
1.64      jasper    262:                &$get_props("Requires.private");
                    263:        }
1.43      jasper    264:        &$get_props("Requires");
1.26      jasper    265:
1.11      espie     266: }
                    267:
1.1       ckuethe   268: # look for the .pc file in each of the PKGPATH elements. Return the path or
                    269: # undef if it's not there
1.4       espie     270: sub pathresolve
                    271: {
                    272:        my ($p) = @_;
                    273:
1.14      espie     274:        if ($allow_uninstalled && $p !~ m/\-uninstalled$/) {
                    275:                foreach my $d (@PKGPATH) {
                    276:                        my $f = "$d/$p-uninstalled.pc";
1.62      jasper    277:                        say_debug("pathresolve($p) looking in $f");
1.14      espie     278:                        if (-f $f) {
                    279:                                $found_uninstalled = 1;
                    280:                                return $f;
                    281:                        }
                    282:                }
                    283:        }
                    284:
1.4       espie     285:        foreach my $d (@PKGPATH) {
1.10      espie     286:                my $f = "$d/$p.pc";
1.62      jasper    287:                say_debug("pathresolve($p) looking in $f");
1.10      espie     288:                return $f if -f $f;
1.1       ckuethe   289:        }
1.10      espie     290:        return undef;
1.1       ckuethe   291: }
                    292:
1.11      espie     293: sub get_config
                    294: {
                    295:        my ($f) = @_;
                    296:
                    297:        my $cfg;
1.33      jasper    298:        eval {
1.11      espie     299:            $cfg = OpenBSD::PkgConfig->read_file($f);
                    300:        };
                    301:        if (!$@) {
1.37      jasper    302:                return validate_config($f, $cfg);
1.11      espie     303:        } else {
1.62      jasper    304:                say_debug($@);
1.11      espie     305:        }
                    306:        return undef;
                    307: }
                    308:
1.13      espie     309: sub cache_find_config
                    310: {
                    311:        my $name = shift;
                    312:
1.62      jasper    313:        say_debug("processing $name");
1.13      espie     314:
                    315:        if (exists $configs{$name}) {
                    316:                return $configs{$name};
                    317:        } else {
                    318:                return $configs{$name} = find_config($name);
                    319:        }
1.37      jasper    320: }
                    321:
                    322: # Required elements for a valid .pc file: Name, Description, Version
                    323: sub validate_config
                    324: {
                    325:        my ($f, $cfg) = @_;
                    326:        my @required_elems = ('Name', 'Description', 'Version');
1.58      jasper    327:
                    328:        # Check if we're dealing with an empty file, but don't error out just
                    329:        # yet, we'll do that when we realize there's no Name field.
1.61      jasper    330:        if (stat($f)->size == 0) {
1.58      jasper    331:                my $p = $f;
                    332:                $p =~ s/(^.*\/)(.*?)$/$2/g;
1.62      jasper    333:                say_error("Package file '$p' appears to be empty");
1.58      jasper    334:        }
1.37      jasper    335:
                    336:        foreach (@required_elems) {
1.58      jasper    337:                my $e = $cfg->get_property($_, $variables);
1.37      jasper    338:                if (!defined $e) {
1.39      jasper    339:                        $f =~ s/(^.*\/)(.*?)\.pc$/$2/g;
1.62      jasper    340:                        say_error("Package '$f' has no $_: field");
1.37      jasper    341:                        return undef;
                    342:                }
                    343:        }
                    344:
                    345:        return $cfg;
1.13      espie     346: }
                    347:
1.35      espie     348: # pkg-config won't install a pkg-config.pc file itself, but it may be
1.63      jasper    349: # listed as a dependency in other files. so prime the cache with self.
1.35      espie     350: sub setup_self
                    351: {
                    352:        my $pkg_pc = OpenBSD::PkgConfig->new;
                    353:        $pkg_pc->add_property('Version', $version);
1.38      jasper    354:        $pkg_pc->add_variable('pc_path', join(":", @PKGPATH));
1.63      jasper    355:        $pkg_pc->add_property('URL', "http://www.openbsd.org/cgi-bin/man.cgi?query=pkg-config");
                    356:        $pkg_pc->add_property('Description', "fetch metadata about installed software packages");
1.35      espie     357:        $configs{'pkg-config'} = $pkg_pc;
                    358: }
                    359:
1.11      espie     360: sub find_config
                    361: {
                    362:        my ($p) = @_;
                    363:        my $f = pathresolve($p);
1.64      jasper    364:
                    365:        return get_config($f) if defined($f);
                    366:
1.62      jasper    367:        say_error("Package $p was not found in the pkg-config search path");
1.61      jasper    368:
1.11      espie     369:        return undef;
                    370: }
1.1       ckuethe   371:
1.11      espie     372: sub stringize
1.4       espie     373: {
1.11      espie     374:        my $list = shift;
1.21      simon     375:        my $sep = shift || ',';
1.4       espie     376:
1.11      espie     377:        if (defined $list) {
1.21      simon     378:                return join($sep, @$list)
1.11      espie     379:        } else {
                    380:                return '';
1.1       ckuethe   381:        }
                    382: }
                    383:
                    384: #if the variable option is set, pull out the named variable
1.4       espie     385: sub do_variable
                    386: {
1.11      espie     387:        my ($p, $v) = @_;
1.1       ckuethe   388:
1.13      espie     389:        my $cfg = cache_find_config($p);
                    390:
                    391:        if (defined $cfg) {
1.11      espie     392:                my $value = $cfg->get_variable($v, $variables);
                    393:                if (defined $value) {
1.13      espie     394:                        push(@vlist, $value);
1.11      espie     395:                }
1.19      espie     396:                return undef;
1.11      espie     397:        }
1.19      espie     398:        $rc = 1;
1.1       ckuethe   399: }
                    400:
1.30      jasper    401: #if the modversion or print-provides options are set,
                    402: #pull out the compiler flags
1.4       espie     403: sub do_modversion
                    404: {
1.11      espie     405:        my ($p) = @_;
1.1       ckuethe   406:
1.13      espie     407:        my $cfg = cache_find_config($p);
                    408:
                    409:        if (defined $cfg) {
1.11      espie     410:                my $value = $cfg->get_property('Version', $variables);
                    411:                if (defined $value) {
1.60      jasper    412:                        if (defined($mode{printprovides})){
                    413:                                print "$p = " . stringize($value) . "\n";
1.30      jasper    414:                                return undef;
                    415:                        } else {
1.60      jasper    416:                                print stringize($value), "\n";
1.30      jasper    417:                                return undef;
                    418:                        }
1.11      espie     419:                }
                    420:        }
1.13      espie     421:        $rc = 1;
1.1       ckuethe   422: }
                    423:
                    424: #if the cflags option is set, pull out the compiler flags
1.4       espie     425: sub do_cflags
                    426: {
1.14      espie     427:        my $list = shift;
                    428:
1.11      espie     429:        my $cflags = [];
1.1       ckuethe   430:
1.14      espie     431:        foreach my $pkg (@$list) {
1.11      espie     432:                my $l = $configs{$pkg}->get_property('Cflags', $variables);
                    433:                push(@$cflags, @$l) if defined $l;
                    434:        }
1.32      jasper    435:        my $a = OpenBSD::PkgConfig->compress($cflags,
1.11      espie     436:                sub {
                    437:                        local $_ = shift;
                    438:                        if (($mode{cflags} & 1) && /^-I/ ||
                    439:                            ($mode{cflags} & 2) && !/^-I/) {
                    440:                            return 1;
                    441:                        } else {
                    442:                            return 0;
1.4       espie     443:                        }
1.11      espie     444:                });
1.32      jasper    445:        if (defined($a) && defined($variables->{pc_sysrootdir})){
1.36      jasper    446:                $a =~ s/[\w]?-I/$&$variables->{pc_sysrootdir}/g;
1.32      jasper    447:        }
                    448:
                    449:        return $a;
1.1       ckuethe   450: }
                    451:
                    452: #if the lib option is set, pull out the linker flags
1.4       espie     453: sub do_libs
                    454: {
1.14      espie     455:        my $list = shift;
                    456:
1.11      espie     457:        my $libs = [];
1.1       ckuethe   458:
1.14      espie     459:        foreach my $pkg (@$list) {
1.11      espie     460:                my $l = $configs{$pkg}->get_property('Libs', $variables);
                    461:                push(@$libs, @$l) if defined $l;
                    462:        }
1.66    ! jasper    463:
        !           464:        # Get the linker path directives (-L).
1.13      espie     465:        my $a = OpenBSD::PkgConfig->compress($libs,
1.11      espie     466:                sub {
                    467:                        local $_ = shift;
1.13      espie     468:                        if (($mode{libs} & 2) && /^-L/ ||
1.11      espie     469:                            ($mode{libs} & 4) && !/^-[lL]/) {
                    470:                            return 1;
                    471:                        } else {
                    472:                            return 0;
1.4       espie     473:                        }
1.11      espie     474:                });
1.32      jasper    475:
                    476:        if (defined($variables->{pc_sysrootdir})){
1.36      jasper    477:                $a =~ s/[\w]?-[lL]/$&$variables->{pc_sysrootdir}/g;
1.32      jasper    478:        }
                    479:
1.13      espie     480:        if ($mode{libs} & 1) {
                    481:                my $b = OpenBSD::PkgConfig->rcompress($libs,
1.66    ! jasper    482:                            sub { shift =~ m/^-l/; });
1.13      espie     483:                return ($a, $b);
                    484:        } else {
                    485:                return $a;
                    486:        }
1.1       ckuethe   487: }
                    488:
                    489: #list all packages
1.4       espie     490: sub do_list
                    491: {
1.1       ckuethe   492:        my ($p, $x, $y, @files, $fname, $name);
1.20      espie     493:        my $error = 0;
                    494:
1.33      jasper    495:        foreach my $p (@PKGPATH) {
                    496:                push(@files, <$p/*.pc>);
1.4       espie     497:        }
1.1       ckuethe   498:
                    499:        # Scan the lengths of the package names so I can make a format
                    500:        # string to line the list up just like the real pkgconfig does.
                    501:        $x = 0;
1.4       espie     502:        foreach my $f (@files) {
                    503:                $fname = basename($f, '.pc');
                    504:                $y = length $fname;
1.1       ckuethe   505:                $x = (($y > $x) ? $y : $x);
                    506:        }
                    507:        $x *= -1;
                    508:
1.4       espie     509:        foreach my $f (@files) {
1.11      espie     510:                my $cfg = get_config($f);
1.20      espie     511:                if (!defined $cfg) {
1.62      jasper    512:                        say_warning("Problem reading file $f");
1.20      espie     513:                        $error = 1;
                    514:                        next;
                    515:                }
1.4       espie     516:                $fname = basename($f, '.pc');
1.33      jasper    517:                printf("%${x}s %s - %s\n", $fname,
1.53      jasper    518:                    stringize($cfg->get_property('Name', $variables), ' '),
1.21      simon     519:                    stringize($cfg->get_property('Description', $variables),
                    520:                    ' '));
1.1       ckuethe   521:        }
1.20      espie     522:        return $error;
1.1       ckuethe   523: }
                    524:
1.4       espie     525: sub help
                    526: {
1.1       ckuethe   527:        print <<EOF
                    528: Usage: $0 [options]
                    529: --debug        - turn on debugging output
                    530: --help - this message
                    531: --usage - this message
                    532: --list-all - show all packages that $0 can find
1.8       ckuethe   533: --version - print version of pkgconfig
                    534: --errors-to-stdout - direct error messages to stdout rather than stderr
                    535: --print-errors - print error messages in case of error
1.34      jasper    536: --print-provides - print all the modules the given package provides
                    537: --print-requires - print all the modules the given package requires
                    538: --print-requires-private - print all the private modules the given package requires
1.66    ! jasper    539: --silence-errors - don\'t print error messages in case of error
1.1       ckuethe   540: --atleast-pkgconfig-version [version] - require a certain version of pkgconfig
                    541: --cflags package [versionspec] [package [versionspec]]
                    542: --cflags-only-I - only output -Iincludepath flags
                    543: --cflags-only-other - only output flags that are not -I
1.11      espie     544: --define-variable=NAME=VALUE - define variables
1.1       ckuethe   545: --libs package [versionspec] [package [versionspec]]
                    546: --libs-only-l - only output -llib flags
                    547: --libs-only-L - only output -Llibpath flags
                    548: --libs-only-other - only output flags that are not -l or -L
                    549: --exists package [versionspec] [package [versionspec]]
                    550: --uninstalled - allow for uninstalled versions to be used
1.8       ckuethe   551: --static - adjust output for static linking
                    552: --atleast-version [version] - require a certain version of a package
                    553: --modversion [package] - query the version of a package
                    554: --variable var package - return the definition of <var> in <package>
1.1       ckuethe   555: EOF
                    556: ;
1.22      simon     557:        exit 0;
1.1       ckuethe   558: }
                    559:
                    560: # do we meet/beat the version the caller requested?
1.4       espie     561: sub self_version
                    562: {
                    563:        my ($v) = @_;
                    564:        my (@a, @b);
                    565:
1.66    ! jasper    566:        @a = split(/\./, $v);
        !           567:        @b = split(/\./, $version);
1.1       ckuethe   568:
1.4       espie     569:        if (($b[0] >= $a[0]) && ($b[1] >= $a[1])) {
1.14      espie     570:                return 0;
1.1       ckuethe   571:        } else {
1.14      espie     572:                return 1;
                    573:        }
                    574: }
                    575:
                    576: sub compare
                    577: {
                    578:        my ($a, $b) = @_;
1.46      jasper    579:        my ($full_a, $full_b) = ($a, $b);
                    580:        my (@suffix_a, @suffix_b);
1.14      espie     581:
1.28      jasper    582:        return 0 if ($a eq $b);
1.14      espie     583:
1.46      jasper    584:        # is there a valid non-numeric suffix to deal with later?
1.58      jasper    585:        # accepted are (in order): a(lpha) < b(eta) < rc < ' '.
1.46      jasper    586:        # suffix[0] is the 'alpha' part, suffix[1] is the '1' part in 'alpha1'.
1.59      jasper    587:        if ($a =~ s/(rc|beta|b|alpha|a)(\d+)$//) {
1.62      jasper    588:                say_debug("valid suffix $1$2 found in $a$1$2.");
1.46      jasper    589:                $suffix_a[0] = $1;
                    590:                $suffix_a[1] = $2;
                    591:        }
                    592:
1.59      jasper    593:        if ($b =~ s/(rc|beta|b|alpha|a)(\d+)$//) {
1.62      jasper    594:                say_debug("valid suffix $1$2 found in $b$1$2.");
1.46      jasper    595:                $suffix_b[0] = $1;
                    596:                $suffix_b[1] = $2;
                    597:        }
                    598:
1.66    ! jasper    599:        my @a = split(/\./, $a);
        !           600:        my @b = split(/\./, $b);
1.14      espie     601:
                    602:        while (@a && @b) { #so long as both lists have something
1.46      jasper    603:                if (!(@suffix_a || @suffix_b)) {
                    604:                        # simple comparison when no suffixes are in the game.
1.48      jasper    605:                        my $rc = compare_numeric($a[0], $b[0], 0);
                    606:                        return $rc if defined($rc);
1.46      jasper    607:                } else {
                    608:                        # extended comparison.
1.56      jasper    609:                        if (((@a == 1) || (@b == 1)) &&
1.46      jasper    610:                            ($a[0] == $b[0])){
                    611:                                # one of the arrays has reached the last element,
                    612:                                # compare the suffix.
                    613:
                    614:                                # directly compare suffixes, provided both suffixes
                    615:                                # are present.
                    616:                                if (@suffix_a && @suffix_b) {
                    617:                                        my $first_char = sub {
                    618:                                                return substr(shift, 0, 1);
                    619:                                        };
                    620:
                    621:                                        # suffixes are equal, compare on numeric
                    622:                                        if (&$first_char($suffix_a[0]) eq
                    623:                                            &$first_char($suffix_b[0])) {
1.48      jasper    624:                                                return compare_numeric($suffix_a[1], $suffix_b[1], 1);
1.46      jasper    625:                                        }
                    626:
1.47      jasper    627:                                        # rc beats beta beats alpha
1.46      jasper    628:                                        if (&$first_char($suffix_a[0]) lt &$first_char($suffix_b[0])) {
1.62      jasper    629:                                                say_debug("$full_a (installed) < $full_b (wanted)");
1.46      jasper    630:                                                return -1;
                    631:                                        } else {
1.62      jasper    632:                                                say_debug("$full_a (installed) > $full_b (wanted)");
1.46      jasper    633:                                                return 1;
                    634:                                        }
                    635:
                    636:                                } else {
                    637:                                        # one of either is lacking a suffix,
                    638:                                        # thereby beating the other.
                    639:                                        # e.g.: 1.02 > 1.02b1
                    640:                                        if (@suffix_a) { # a is older
1.62      jasper    641:                                                say_debug("$full_a (installed) < $full_b (wanted)");
1.55      jasper    642:                                                return 1;
1.46      jasper    643:                                        }
                    644:
                    645:                                        if (@suffix_b) { # b is older
1.62      jasper    646:                                                say_debug("$full_a (installed) > $full_b (wanted)");
1.55      jasper    647:                                                return -1;
1.46      jasper    648:                                        }
                    649:                                }
                    650:                        } else {
1.48      jasper    651:                                my $rc = compare_numeric($a[0], $b[0], 0);
                    652:                                return $rc if defined($rc);
1.46      jasper    653:                        }
                    654:                }
1.14      espie     655:                shift @a; shift @b;
                    656:        }
                    657:        return 1 if @a;
                    658:        return -1 if @b;
                    659:        return 0;
1.48      jasper    660: }
                    661:
                    662: # simple numeric comparison, with optional equality test.
                    663: sub compare_numeric
                    664: {
                    665:        my ($x, $y, $eq) = @_;
                    666:
                    667:        return 1 if $x > $y;
                    668:        return -1 if $x < $y;
                    669:        return 0 if (($x == $y) and ($eq == 1));
                    670:        return undef;
1.1       ckuethe   671: }
                    672:
                    673: # got a package meeting the requested specific version?
1.4       espie     674: sub versionmatch
                    675: {
1.14      espie     676:        my ($cfg, $op, $want) = @_;
1.33      jasper    677:
1.1       ckuethe   678:        # can't possibly match if we can't find the file
1.11      espie     679:        return 0 if !defined $cfg;
                    680:
1.14      espie     681:        my $inst = stringize($cfg->get_property('Version', $variables));
1.11      espie     682:
1.1       ckuethe   683:        # can't possibly match if we can't find the version string
1.14      espie     684:        return 0 if $inst eq '';
1.1       ckuethe   685:
1.62      jasper    686:        say_debug("comparing $want (wanted) to $inst (installed)");
1.14      espie     687:        my $value = compare($inst, $want);
1.31      jasper    688:        if    ($op eq '>=') { return $value >= 0; }
                    689:        elsif ($op eq '=')  { return $value == 0; }
                    690:        elsif ($op eq '!=') { return $value != 0; }
                    691:        elsif ($op eq '<')  { return $value < 0; }
                    692:        elsif ($op eq '>')  { return $value > 0; }
                    693:        elsif ($op eq '<=') { return $value <= 0; }
1.13      espie     694: }
                    695:
                    696: sub mismatch
                    697: {
                    698:        my ($p, $cfg, $op, $v) = @_;
1.53      jasper    699:        my $name = stringize($cfg->get_property('Name'), ' ');
1.41      jasper    700:        my $version = stringize($cfg->get_property('Version'));
                    701:        my $url = stringize($cfg->get_property('URL'));
                    702:
1.62      jasper    703:        say_warning("Requested '$p $op $v' but version of $name is $version");
                    704:        say_warning("You may find new versions of $name at $url") if $url;
1.13      espie     705: }
                    706:
                    707: sub simplify_and_reverse
                    708: {
                    709:        my $reqlist = shift;
                    710:        my $dejavu = {};
                    711:        my $result = [];
                    712:
                    713:        for my $item (@$reqlist) {
                    714:                if (!$dejavu->{$item}) {
                    715:                        unshift @$result, $item;
                    716:                        $dejavu->{$item} = 1;
                    717:                }
                    718:        }
                    719:        return $result;
1.30      jasper    720: }
                    721:
                    722: # retrieve and print Requires(.private)
                    723: sub print_requires
                    724: {
                    725:        my ($p) = @_;
                    726:
                    727:        my $cfg = cache_find_config($p);
                    728:
                    729:        if (defined($cfg)) {
                    730:                my $value;
                    731:
                    732:                if (defined($mode{printrequires})) {
                    733:                        $value = $cfg->get_property('Requires', $variables);
                    734:                } elsif (defined($mode{printrequiresprivate})) {
                    735:                        $value = $cfg->get_property('Requires.private', $variables);
                    736:                } else {
1.62      jasper    737:                        say_debug("Unknown mode for print_requires.");
1.30      jasper    738:                        return 1;
                    739:                }
                    740:
                    741:                if (defined($value)) {
                    742:                        print "$_\n" foreach (@$value);
                    743:                        return undef;
                    744:                }
                    745:        }
                    746:
                    747:        $rc = 1;
1.35      espie     748: }
                    749:
                    750: sub beautify_list
                    751: {
                    752:        return join(' ', map {"[$_]"} @_);
1.61      jasper    753: }
                    754:
1.62      jasper    755: sub say_debug
1.61      jasper    756: {
1.62      jasper    757:        say_msg(shift) if $mode{debug};
1.61      jasper    758: }
                    759:
1.62      jasper    760: sub say_error
1.61      jasper    761: {
1.62      jasper    762:        say_msg(shift) if $mode{printerr}
                    763: }
                    764:
                    765: sub say_warning
                    766: {
                    767:        say_msg(shift);
                    768: }
                    769:
                    770: sub say_msg
                    771: {
1.63      jasper    772:        my $str = shift;
1.62      jasper    773:
                    774:        # If --errors-to-stdout was given, close STDERR (to be safe),
                    775:        # then dup the output to STDOUT and delete the key from %mode so we
                    776:        # won't keep checking it. STDERR stays dup'ed.
                    777:        if ($mode{estdout}) {
                    778:                close(STDERR);
                    779:                open(STDERR, ">&STDOUT") or die "Can't dup STDOUT: $!";
                    780:                delete($mode{estdout});
                    781:        }
                    782:
                    783:        print STDERR $str . "\n";
1.1       ckuethe   784: }