This repository has been archived by the owner on Mar 7, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
/
Base.pm
185 lines (123 loc) · 3.54 KB
/
Base.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package Alien::Base;
use strict;
use warnings;
our $VERSION = '0.000_005';
$VERSION = eval $VERSION;
use Carp;
use File::chdir;
use File::ShareDir ();
use Scalar::Util qw/blessed/;
use Perl::OSType qw/is_os_type/;
use Config;
use Capture::Tiny qw/capture_merged/;
sub import {
my $class = shift;
return if $class->install_type('system');
my $libs = $class->libs;
my @L = $libs =~ /-L(\S+)/g;
#TODO investigate using Env module for this (VMS problems?)
my $var = is_os_type('Windows') ? 'PATH' : 'LD_RUN_PATH';
my @LL = @L;
unshift @LL, $ENV{$var} if $ENV{$var};
no strict 'refs';
$ENV{$var} = join( $Config::Config{path_sep}, @LL )
unless ${ $class . "::AlienEnv" }{$var}++;
# %Alien::MyLib::AlienEnv has keys like ENV_VAR => int (true if loaded)
}
sub dist_dir {
my $class = shift;
my $dist = $class;
$dist =~ s/::/-/g;
return eval { File::ShareDir::dist_dir $dist } or $class->{build_share_dir};
}
sub new { return bless {}, $_[0] }
sub cflags {
my $self = shift;
return $self->_keyword('Cflags', @_);
}
sub libs {
my $self = shift;
return $self->_keyword('Libs', @_);
}
sub install_type {
my $self = shift;
my $type = $self->config('install_type');
return @_ ? $type eq $_[0] : $type;
}
sub _keyword {
my $self = shift;
my $keyword = shift;
# use pkg-config if installed system-wide
if ($self->install_type('system')) {
my $name = $self->config('name');
my $command = "pkg-config --\L$keyword\E $name";
chomp ( my $pcdata = capture_merged { system( $command ) } );
croak "Could not call pkg-config: $!" if $!;
$pcdata =~ s/\s*$//;
return $pcdata;
}
# use parsed info from build .pc file
my $dist_dir = $self->dist_dir;
my @pc = $self->pkgconfig(@_);
my @strings =
map { $_->keyword($keyword, { alien_dist_dir => $dist_dir }) }
@pc;
return join( ' ', @strings );
}
sub pkgconfig {
my $self = shift;
my %all = %{ $self->config('pkgconfig') };
croak "No Alien::Base::PkgConfig objects are stored!"
unless keys %all;
# Run through all pkgconfig objects and ensure that their modules are loaded:
for my $pkg_obj (values %all) {
my $perl_module_name = blessed $pkg_obj;
eval "require $perl_module_name";
}
return @all{@_} if @_;
my $manual = delete $all{_manual};
if (keys %all) {
return values %all;
} else {
return $manual;
}
}
# helper method to call Alien::MyLib::ConfigData->config(@_)
sub config {
my $class = shift;
$class = blessed $class || $class;
my $config = $class . '::ConfigData';
eval "require $config";
warn $@ if $@;
return $config->config(@_);
}
1;
__END__
__POD__
=head1 NAME
Alien::Base - Base classes for Alien:: modules
=head1 SYNOPSIS
package Alien::MyLibrary;
use strict;
use warnings;
use parent 'Alien::Base';
1;
=head1 DESCRIPTION
L<Alien::Base> comprises base classes to help in the construction of C<Alien::> modules. Modules in the L<Alien> namespace are used to locate and install (if necessary) external libraries needed by other Perl modules.
This is the documentation for the L<Alien::Base> module itself. To learn more about the system as a whole please see L<Alien::Base::Authoring>.
=head1 SEE ALSO
=over
=item *
L<Module::Build>
=item *
L<Alien>
=back
=head1 SOURCE REPOSITORY
L<http://github.com/jberger/Alien-Base>
=head1 AUTHOR
Joel Berger, E<lt>joel.a.berger@gmail.comE<gt>
=head1 COPYRIGHT AND LICENSE
Copyright (C) 2012 by Joel Berger
This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.
=cut