forked from niner/inline-python-pm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Makefile.PL
381 lines (332 loc) · 12.3 KB
/
Makefile.PL
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
use Data::Dumper;
use Config;
use Cwd qw(abs_path);
use ExtUtils::MakeMaker 6.64;
use Getopt::Long;
use File::Basename;
GetOptions(
'gdb:s' => \$gdb,
debug => \$debug,
help => \$help,
);
usage() if $help;
#============================================================================
# What python are we going to try?
#============================================================================
my $sel = $ENV{INLINE_PYTHON_EXECUTABLE};
my $num_python3 = 0;
unless ($sel) {
my @pythons;
my %pythons;
my $sep = $^O eq 'MSWin32' ? ";" : ":";
my $exe = $^O eq 'MSWin32' ? ".exe" : "";
for $p (split /$sep/, $ENV{PATH}) {
$p =~ s/^~/$ENV{HOME}/;
for $exe_version ('3', '') {
my $py = "$p/python$exe_version$exe";
next unless -f $py and -x $py;
next if $pythons{abs_path($py)}++; # filter symlinked duplicates
my $version = get_python_major_version($py);
push @pythons, { path => $py, version => $version };
$num_python3++ if ($version == 3);
}
}
# Keep them in PATH order.
# @pythons = sort { $a->{path} cmp $b->{path} } @pythons;
my $num = 1;
print "Found these python executables on your PATH:\n";
print $num++ . ". " . $_->{path} . "\n" for @pythons;
if (@pythons == 1 and not $sel) {
$sel = $pythons[0];
print "Using the only python executable I could find\n";
print 'Set the INLINE_PYTHON_EXECUTABLE environment variable to'
. " the full path to your python executable to override this selection.\n";
} elsif ($num_python3 == 1) { # Prefer python 3
for my $python (@pythons) {
if ($python->{version} == 3) {
$sel = $python;
print "Using the only python 3 executable I could find even though python 2 was also found\n";
print 'Set the INLINE_PYTHON_EXECUTABLE environment variable to'
. " the full path to your python executable to override this selection.\n";
last;
}
}
}
unless ($sel) {
$sel = prompt("Use which?", '1');
if ($sel =~ /^\d+$/) {
die 'Invalid number. Please enter only numbers from 1 to ' . ($num - 1)
. " or the full path to your python executable.\n"
. 'Set the INLINE_PYTHON_EXECUTABLE environment variable to'
. " the full path to your python executable to avoid this question.\n"
if $sel > ($num - 1);
$sel = $pythons[$sel - 1];
}
}
}
$sel = { path => $sel } unless ref $sel eq 'HASH'; # in case the user entered a path
print "Using $sel->{path}\n";
my $py_major_version = get_python_major_version($sel->{path});
#============================================================================
# Interrogate the python interpreter (or the user) for required flags
#============================================================================
interrogate($sel);
# Fix up the libpath and libpython
die "Could not find Python.h in include path. make will not work" unless -e "$sel->{incpath}/Python.h";
substr($sel->{incpath}, 0, 0) = "-I";
substr($sel->{libpath}, 0, 0) = "-L";
$sel->{libpython} =~ s/lib(.*)(?:\.\Q$Config{dlext}\E|\Q$Config{_a}\E)/-l$1/;
my @flags;
push @flags, debug_flag() if defined $gdb;
push @flags, '-DI_PY_DEBUG' if $debug;
push @flags, "-DPY_MAJOR_VERSION=$py_major_version";
push @flags, 'none (perl Makefile.PL --help for details)' unless @flags;
print <<END;
Using These Settings:
Extra Libs: $sel->{syslibs}
Python Lib: $sel->{libpath} $sel->{libpython}
Includes: $sel->{incpath}
Extra Flags: @flags
END
#============================================================================
# Finalize, and write the makefile
#============================================================================
$defs = join ' ', qw(-DEXPOSE_PERL -DCREATE_PYTHON -UCREATE_PERL),
$debug ? "-DI_PY_DEBUG" : ();
WriteMakefile(
$defs ? (DEFINE => $defs) : (),
defined $gdb ? (OPTIMIZE => debug_flag()) : (),
INC => $sel->{incpath},
LIBS => (join " ", @$sel{qw(libpath libpython syslibs)}),
NAME => 'Inline::Python',
ABSTRACT_FROM => 'Python.pod',
AUTHOR => 'Neil Watkiss <[email protected]>',
LICENSE => 'perl',
VERSION_FROM => 'Python.pm',
PREREQ_PM => {
'Inline' => 0.46,
'Digest::MD5' => 2.50,
'Data::Dumper' => 0,
},
TEST_REQUIRES => {
'Test' => 0,
'Test::More' => 0,
'Test::Deep' => 0,
'Test::Number::Delta' => 0,
'Proc::ProcessTable' => '0.53',
},
OBJECT => 'Python.o py2pl.o perlmodule.o util.o',
META_MERGE => {
"meta-spec" => { version => 2 },
resources => {
repository => {
type => 'git',
url => 'http://github.com/niner/inline-python-pm.git',
web => 'http://github.com/niner/inline-python-pm',
},
},
},
clean => {FILES => 'blib_test/'},
);
#============================================================================
# Tries to ask the python interpreter what libraries we need, where its
# include directories are, etc.
#============================================================================
sub interrogate {
my $ref = shift;
return query_options($ref) unless test_interrogate($ref);
$ref->{syslibs} = get_config_var($ref, "LIBS");
$ref->{incpath} = get_config_var($ref, "INCLUDEPY");
$ref->{libpath} = get_config_var($ref, "LIBPL");
$ref->{ldlib} = get_config_var($ref, "LDLIBRARY");
$ref->{libpython} = get_config_var($ref, "LIBRARY");
my $tmp = rindex($ref->{libpython}, '/') + 1;
$ref->{libpython} = substr($ref->{libpython}, $tmp);
if ($ref->{libpython} eq 'None') {
special_get_libpath($ref);
}
$ref->{libpath} = join '/', (get_config_var($ref, "LIBDEST"),
'config')
if ($ref->{libpath} eq 'None');
return query_options($ref) unless sanity_check($ref);
}
sub special_get_libpath {
# For when sysconfig does not work (i.e. on Windows)
my $ref = shift;
my $val = `$ref->{path} -c "import distutils.command.build_ext; d=distutils.core.Distribution(); b=distutils.command.build_ext.build_ext(d);b.finalize_options();print(b.library_dirs[0])" 2>&1`;
chomp $val;
$ref->{libpath} = $val;
$ref->{libpython} = basename((glob("$val/libpython*.a"))[0]);
return $val;
}
sub test_interrogate {
my $ref = shift;
`$ref->{path} -c "import distutils.sysconfig; distutils.sysconfig.get_config_var" 2>&1`;
print <<END if $?;
This python is so old it doesn't know how to answer my questions.
Instead, you will be asked a series of questions about it. If possible,
I will give you a set of reasonable options to choose from. You can
always enter the complete answer yourself if none of mine are correct.
END
#' stupid vim.
return $? == 0 ? 1 : 0;
}
sub sanity_check {
my $ref = shift;
$ref->{libpython} = $ref->{ldlib}
if not -f join '/', $ref->{libpath}, $ref->{libpython}
and -f join '/', $ref->{libpath}, $ref->{ldlib};
unless (-d $ref->{libpath} &&
-d $ref->{incpath} &&
(-f join '/', $ref->{libpath}, $ref->{libpython})
) {
print <<END and return 0;
This python's configuration files are messed up. You'll have have to
answer the questions yourself. Here is what Python said:
Extra Libs: $sel->{syslibs}
Python Library: $sel->{libpath}/$sel->{libpython}
Include Path: $sel->{incpath}
END
# ' stupid vim.
}
return 1;
}
sub get_python_major_version {
my $exe = shift;
my $version = `$exe --version 2>&1`;
$version =~ /(\d+)\./;
return $1;
}
sub get_config_var {
my $ref = shift;
my $key = shift;
my $exe = $ref->{path};
my $val = `$exe -c "import distutils.sysconfig; print(distutils.sysconfig.get_config_var('$key'))"`;
chomp $val;
return $val;
}
sub query_options {
my $ref = shift;
# Every python I've seen needs pthreads. Obviously not on windows.
my $libs_guess = $ref->{syslibs} ? $ref->{syslibs} :
$^O eq 'MSWin32' ? '' : '-lpthread';
print <<END;
1. LIBS option. I need to know what extra libraries, if any,
are required by this build of python. I recommend this:
${ $libs_guess ? \$libs_guess : \"No extra libraries" }
END
$ref->{syslibs} = prompt("Enter extra libraries (e.g. -lfoo -lbar)",
$libs_guess);
print <<END;
2. LIBRARY option. The location of the python library.
Inline::Python needs to link against it to use Python.
Here are the libraries I know about:
END
my @libs = show_python_libs($ref);
my $lib = prompt("Which? Or enter another.", '1');
$lib = $libs[$lib-1] if $lib =~ /^\d+$/;
$lib =~ s|\\|/|g;
$ref->{libpath} = substr($lib, 0, rindex($lib, '/'));
$ref->{libpython} = substr($lib, rindex($lib, '/')+1);
print <<END;
3. INCLUDE option. The location of the python include files.
Inline::Python needs these to compile.
Here are the locations I know about:
END
my @incs = show_python_incs($ref);
my $inc = prompt("Which? Or enter another.", '1');
$inc = $incs[$inc-1] if $inc =~ /^\d+$/;
$ref->{incpath} = $inc;
}
#============================================================================
# Python libraries to look for
#============================================================================
sub show_python_libs {
my $ref = shift;
my $exe = $ref->{path};
# Convert the exe into a glob where we might find a library:
$exe =~ s|[^/]+$||;
$exe .= "../lib/python*/config/libpython*";
my @py_libs =
(
(map { $exe . $_ } '.a', '.so', '.lib'),
'/usr/lib64/libpython*.a',
'/usr/lib64/libpython*.so',
'/usr/lib/libpython*.a',
'/usr/lib/libpython*.so',
'/usr/lib64/python*/config/libpython*.a',
'/usr/lib64/python*/config/libpython*.so',
'/usr/lib/python*/config/libpython*.a',
'/usr/lib/python*/config/libpython*.so',
'/usr/local/lib/libpython*.a',
'/usr/local/lib/libpython*.so',
'/usr/local/ActivePython-*/lib/python*/config/libpython*.a',
'/usr/local/ActivePython-*/lib/python*/config/libpython*.so',
# Win32 support
'C:/Python*/libs/python*.lib',
'C:/Program Files/Python*/libs/python*.lib',
);
my (@found, %found);
push @found, grep { -f && $found{abspath($_)}++ == 0 } glob for @py_libs;
@found = sort map { abspath($_) } @found;
my $num = '1';
print "\t " . $num++ . ") " . $_ . "\n" for @found;
print "\n";
return @found;
}
#============================================================================
# Python include files to look for
#============================================================================
sub show_python_incs {
my $ref = shift;
my $exe = $ref->{path};
# Convert the exe into a glob where we might find the includes:
$exe =~ s|[^/]+$||;
$exe .= "../include/python*";
my @py_incs =
(
$exe,
'/usr/local/ActivePython-*/include/python*',
'/usr/include/python*',
'/usr/local/include/python*',
# Win32 support
'C:/Python*/include',
'C:/Program Files/Python*/include',
);
my (@found, %found);
push @found, grep { -d && $found{abspath($_)}++ == 0 } glob for @py_incs;
@found = sort map { abspath($_) } @found;
my $num = 1;
print "\t " . $num++ . ") " . $_ . "\n" for @found;
print "\n";
return @found;
}
# This can deal with files as well as directories
sub abspath {
use Cwd qw(abs_path);
my ($path, $file) = shift;
if (-f $path) {
my @p = split '/', $path;
$path = join '/', @p[0..$#p-1]; # can't use -2 in a range
$file = $p[-1];
}
$path = abs_path($path);
return defined $file ? join '/', $path, $file : $path;
}
sub debug_flag {
return $gdb if $gdb;
$Config{osname} eq 'MSWin32' ? return '-Zi' : return '-g';
}
sub usage {
print <<'END';
Options:
-gdb: Turn on compiler's debugging flag (use my guess).
-gdb=x Pass your own debugging flag, not mine.
-debug: Turn on many diagnostic print statements inside Inline::Python.
This option is useful for tracing the execution path when
debugging.
-help: This output.
END
# ' stupid vim
exit 0;
}