App-whichdll

 view release on metacpan or  search on metacpan

lib/App/whichdll.pm  view on Meta::CPAN

package App::whichdll;

use strict;
use warnings;
use 5.008001;
use FFI::CheckLib 0.28 qw( find_lib );
use Getopt::Long qw( GetOptions );
use Path::Tiny qw( path );

# ABSTRACT: Find dynamic libraries
our $VERSION = '0.04'; # VERSION


sub main
{
  local @ARGV;
  (undef, @ARGV) = @_;

  my %opts;

  my @alien;

  GetOptions(
    "a"       => \$opts{a},
    "v"       => \$opts{v},
    "s"       => \$opts{s},
    "x"       => \$opts{x},
    "alien=s" => \@alien,
  ) || return _usage();
  return _version() if $opts{v};
  return _usage()   unless @ARGV;

  my %seen;

  my @extra_args;
  if(@alien)
  {
    push @extra_args, alien => \@alien;
  }

  foreach my $name (@ARGV)
  {
    my @result;
    if($opts{a} || $name eq '*')
    {
      @result = find_lib( lib => '*', verify => sub { ($name eq '*') || ($_[0] eq $name) }, @extra_args);
    }
    else
    {
      my $result = find_lib( lib => $name, @extra_args );
      push @result, $result if defined $result;
    }

    unless($opts{s})
    {
      foreach my $path (map { path($_) } @result)
      {
        my $dir = path($path)->parent->realpath;
        $path = $dir->child($path->basename);
        if(-l $path)
        {
          my $target = path(readlink $path)->absolute($dir);
          if(-e $target)
          {
            $target = $target->realpath;
            next if (!$opts{x}) && $seen{$target}++;
            print "$path => $target\n";
          }
          else
          {
            next if (!$opts{x}) && $seen{$target}++;
            print "$path => !! $target !!\n";
          }
        }
        else
        {
          next if (!$opts{x}) && $seen{$path}++;
          print "$path\n";
        }
      }
    }

    unless(@result)
    {
      print STDERR "$0: no $name in dynamic library path\n" unless $opts{s};
      return 1;
    }
  }

  return 0;
}

sub _version
{
  my $my_version = $App::whichdll::VERSION || 'dev';
  print <<"EOF";
whichdll running FFI::CheckLib $FFI::CheckLib::VERSION
                 App::whichdll $my_version

Copyright 2017 Graham Ollis

This program is free software; you may redistribute it and/or modify
it under the same terms as Perl itself.
EOF
  2;
}



( run in 2.500 seconds using v1.01-cache-2.11-cpan-39bf76dae61 )