Debian Codegraph @

inc/Alien/FFI/pkgconfig.pm

raw
1 package Alien::FFI::pkgconfig;
2
3 use strict;
4 use warnings;
5 use Config;
6 use IPC::Cmd ();
7 use Capture::Tiny qw( capture );
8 use Env qw( @PKG_CONFIG_PATH );
9 use File::Glob qw( bsd_glob );
10
11 our $VERBOSE = !!$ENV{V};
12
13 sub pkg_config_exe
14 {
15 foreach my $cmd ($ENV{PKG_CONFIG}, qw( pkgconf pkg-config ))
16 {
17 next unless defined $cmd;
18 return $cmd if IPC::Cmd::can_run($cmd);
19 }
20 return;
21 }
22
23 sub _pkg_config
24 {
25 my(@args) = @_;
26
27 local $ENV{PKG_CONFIG_PATH} = $ENV{PKG_CONFIG_PATH};
28
29 if($^O eq 'darwin' && -d '/usr/local/Cellar/libffi')
30 {
31 my($dir) = bsd_glob '/usr/local/Cellar/libffi/*/lib/pkgconfig';
32 push @PKG_CONFIG_PATH, $dir if $dir && -d $dir;
33 }
34
35 my $cmd = pkg_config_exe;
36 if(defined $cmd)
37 {
38 my @cmd = ($cmd, @args);
39 print "+@cmd\n" if $VERBOSE;
40 my($out, $err, $ret) = capture {
41 system @cmd;
42 $?;
43 };
44 chomp $out; chomp $err;
45 print "[out]\n$out\n" if $out ne '' && $VERBOSE;
46 print "[err]\n$err\n" if $err ne '' && $VERBOSE;
47 die "command failed" if $ret;
48 my $value = $out;
49 $value;
50 }
51 else
52 {
53 print "no pkg-config.\n" if $VERBOSE;
54 return;
55 }
56 }
57
58 my $version;
59 my $exists;
60 my $cflags;
61 my $libs;
62
63 sub exists
64 {
65 return $exists if defined $exists;
66 return $exists = '' unless pkg_config_exe;
67 $exists = !!eval { _pkg_config('--exists', 'libffi'); 1 };
68 }
69
70 sub version
71 {
72 unless(defined $version)
73 {
74 $version = _pkg_config('--modversion', 'libffi');
75 }
76
77 $version;
78 }
79
80 sub config
81 {
82 my($class, $key) = @_;
83 die "unimplemented for $key" unless $key eq 'version';
84 $class->version;
85 }
86
87 sub cflags
88 {
89 unless(defined $cflags)
90 {
91 $cflags = _pkg_config('--cflags', 'libffi');
92 }
93
94 $cflags;
95 }
96
97 sub libs
98 {
99 unless(defined $libs)
100 {
101 $libs = _pkg_config('--libs', 'libffi');
102 }
103
104 $libs;
105 }
106
107 sub install_type {'system'}
108
109 sub runtime_prop { return {} }
110
111 1;
112