| 1 |
package My::Config; |
| 2 |
|
| 3 |
use strict; |
| 4 |
use warnings; |
| 5 |
use Config; |
| 6 |
use File::Spec; |
| 7 |
use FindBin; |
| 8 |
use Text::ParseWords qw( shellwords ); |
| 9 |
use My::BuildConfig; |
| 10 |
use My::ShareConfig; |
| 11 |
use My::ConfigH; |
| 12 |
use My::ShareConfig; |
| 13 |
use lib 'lib'; |
| 14 |
use FFI::Probe; |
| 15 |
use FFI::Probe::Runner; |
| 16 |
use FFI::Probe::Runner::Builder; |
| 17 |
use File::Glob qw( bsd_glob ); |
| 18 |
use File::Basename qw( basename ); |
| 19 |
use JSON::PP qw( decode_json ); |
| 20 |
|
| 21 |
my @probe_types = split /\n/, <<EOF; |
| 22 |
char |
| 23 |
signed char |
| 24 |
unsigned char |
| 25 |
short |
| 26 |
signed short |
| 27 |
unsigned short |
| 28 |
int |
| 29 |
signed int |
| 30 |
unsigned int |
| 31 |
long |
| 32 |
signed long |
| 33 |
unsigned long |
| 34 |
uint8_t |
| 35 |
int8_t |
| 36 |
uint16_t |
| 37 |
int16_t |
| 38 |
uint32_t |
| 39 |
int32_t |
| 40 |
uint64_t |
| 41 |
int64_t |
| 42 |
size_t |
| 43 |
ssize_t |
| 44 |
SSIZE_T |
| 45 |
float |
| 46 |
double |
| 47 |
long double |
| 48 |
float complex |
| 49 |
double complex |
| 50 |
long double complex |
| 51 |
bool |
| 52 |
_Bool |
| 53 |
pointer |
| 54 |
uintptr_t |
| 55 |
intptr_t |
| 56 |
enum |
| 57 |
senum |
| 58 |
intmax_t |
| 59 |
uintmax_t |
| 60 |
EOF |
| 61 |
|
| 62 |
my @extra_probe_types = split /\n/, <<EOF; |
| 63 |
long long |
| 64 |
signed long long |
| 65 |
unsigned long long |
| 66 |
dev_t |
| 67 |
ino_t |
| 68 |
mode_t |
| 69 |
nlink_t |
| 70 |
uid_t |
| 71 |
gid_t |
| 72 |
off_t |
| 73 |
blksize_t |
| 74 |
blkcnt_t |
| 75 |
time_t |
| 76 |
ptrdiff_t |
| 77 |
wchar_t |
| 78 |
wint_t |
| 79 |
EOF |
| 80 |
|
| 81 |
push @probe_types, @extra_probe_types unless $ENV{FFI_PLATYPUS_NO_EXTRA_TYPES}; |
| 82 |
|
| 83 |
sub new |
| 84 |
{ |
| 85 |
my($class) = @_; |
| 86 |
bless {}, $class; |
| 87 |
} |
| 88 |
|
| 89 |
my $ppport_h = File::Spec->catfile(qw( include ppport.h )); |
| 90 |
|
| 91 |
sub generate_dev |
| 92 |
{ |
| 93 |
if(!-r $ppport_h || -d '.git') |
| 94 |
{ |
| 95 |
my $ppport_version = 3.28; |
| 96 |
require Devel::PPPort; |
| 97 |
die "Devel::PPPort $ppport_version or better required for development" |
| 98 |
unless $Devel::PPPort::VERSION >= $ppport_version; |
| 99 |
|
| 100 |
my $old = ''; |
| 101 |
if(-e $ppport_h) |
| 102 |
{ |
| 103 |
open my $fh, '<', $ppport_h; |
| 104 |
$old = do { local $/; <$fh> }; |
| 105 |
close $fh; |
| 106 |
} |
| 107 |
|
| 108 |
my $content = Devel::PPPort::GetFileContents('include/ppport.h'); |
| 109 |
|
| 110 |
if($content ne $old) |
| 111 |
{ |
| 112 |
print "XX $ppport_h\n"; |
| 113 |
open my $fh, '>', $ppport_h; |
| 114 |
print $fh $content; |
| 115 |
close $fh; |
| 116 |
} |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
sub clean |
| 121 |
{ |
| 122 |
unlink $ppport_h; |
| 123 |
} |
| 124 |
|
| 125 |
sub share_config |
| 126 |
{ |
| 127 |
my($self) = @_; |
| 128 |
$self->{share_config} ||= My::ShareConfig->new; |
| 129 |
} |
| 130 |
|
| 131 |
sub build_config |
| 132 |
{ |
| 133 |
my($self) = @_; |
| 134 |
$self->{build_config} ||= My::BuildConfig->new; |
| 135 |
} |
| 136 |
|
| 137 |
sub probe |
| 138 |
{ |
| 139 |
my($self) = @_; |
| 140 |
|
| 141 |
$self->{probe} ||= FFI::Probe->new( |
| 142 |
runner => $self->probe_runner, |
| 143 |
log => "config.log", |
| 144 |
data_filename => "./blib/lib/auto/share/dist/FFI-Platypus/probe/probe.pl", |
| 145 |
alien => [$self->build_config->get('alien')->{class}], |
| 146 |
cflags => ['-Iinclude'], |
| 147 |
); |
| 148 |
} |
| 149 |
|
| 150 |
sub probe_runner |
| 151 |
{ |
| 152 |
my($self) = @_; |
| 153 |
my $builder = FFI::Probe::Runner::Builder->new; |
| 154 |
my $exe = $builder->exe; |
| 155 |
if(-e $exe) |
| 156 |
{ |
| 157 |
return FFI::Probe::Runner->new( exe => $exe ); |
| 158 |
} |
| 159 |
else |
| 160 |
{ |
| 161 |
return undef; |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
sub probe_runner_build |
| 166 |
{ |
| 167 |
my($self) = @_; |
| 168 |
my $probe = $self->probe; |
| 169 |
my $builder = FFI::Probe::Runner::Builder->new; |
| 170 |
foreach my $key (qw( cc ccflags optimize ld ldflags )) |
| 171 |
{ @{ $builder->$key } = @{ $self->build_config->get('eumm')->{$key} } } |
| 172 |
$builder->build unless -e $builder->exe; |
| 173 |
} |
| 174 |
|
| 175 |
sub configure |
| 176 |
{ |
| 177 |
my($self) = @_; |
| 178 |
|
| 179 |
my $probe = $self->probe; |
| 180 |
|
| 181 |
my $config_h = File::Spec->rel2abs( File::Spec->catfile( 'include', 'ffi_platypus_config.h' ) ); |
| 182 |
return if -r $config_h && ref($self->share_config->get( 'type_map' )) eq 'HASH'; |
| 183 |
|
| 184 |
my $ch = My::ConfigH->new; |
| 185 |
|
| 186 |
$ch->define_var( do { |
| 187 |
my $os = uc $^O; |
| 188 |
$os =~ s/-/_/; |
| 189 |
$os =~ s/[^A-Z0-9_]//g; |
| 190 |
"PERL_OS_$os"; |
| 191 |
} => 1 ); |
| 192 |
|
| 193 |
$ch->define_var( PERL_OS_WINDOWS => 1 ) if $^O =~ /^(MSWin32|cygwin|msys)$/; |
| 194 |
|
| 195 |
{ |
| 196 |
my($major, $minor, $patch) = $] =~ /^(5|[7-9])\.([0-9]{3})([0-9]{3})/; |
| 197 |
$ch->define_var( FFI_PL_PERL_VERSION_MAJOR => int $major ); |
| 198 |
$ch->define_var( FFI_PL_PERL_VERSION_MINOR => int $minor ); |
| 199 |
$ch->define_var( FFI_PL_PERL_VERSION_PATCH => int $patch ); |
| 200 |
} |
| 201 |
|
| 202 |
{ |
| 203 |
my($major, $minor, $patch) = (@{ $self->build_config->get('version') }, 0); |
| 204 |
$ch->define_var( FFI_PL_VERSION_MAJOR => int $major ); |
| 205 |
$ch->define_var( FFI_PL_VERSION_MINOR => int $minor ); |
| 206 |
$ch->define_var( FFI_PL_VERSION_PATCH => int $patch ); |
| 207 |
} |
| 208 |
|
| 209 |
foreach my $header (qw( stdlib stdint sys/types sys/stat unistd alloca dlfcn limits stddef wchar signal inttypes windows sys/cygwin string psapi stdio stdbool complex )) |
| 210 |
{ |
| 211 |
if($probe->check_header("$header.h")) |
| 212 |
{ |
| 213 |
my $var = uc $header; |
| 214 |
$var =~ s{/}{_}g; |
| 215 |
$var = "HAVE_${var}_H"; |
| 216 |
$ch->define_var( $var => 1 ); |
| 217 |
} |
| 218 |
} |
| 219 |
|
| 220 |
if(!$self->build_config->get('config_debug_fake32') && $Config{ivsize} >= 8) |
| 221 |
{ |
| 222 |
$ch->define_var( HAVE_IV_IS_64 => 1 ); |
| 223 |
} |
| 224 |
|
| 225 |
my %type_map; |
| 226 |
my %align; |
| 227 |
|
| 228 |
foreach my $type (@probe_types) |
| 229 |
{ |
| 230 |
if($type =~ /^(float|double|long double)/) |
| 231 |
{ |
| 232 |
if(my $basic = $probe->check_type_float($type)) |
| 233 |
{ |
| 234 |
$type_map{$type} = $basic; |
| 235 |
$align{$type} = $probe->data->{type}->{$type}->{align}; |
| 236 |
} |
| 237 |
} |
| 238 |
elsif($type eq 'pointer') |
| 239 |
{ |
| 240 |
$probe->check_type_pointer; |
| 241 |
$align{pointer} = $probe->data->{type}->{pointer}->{align}; |
| 242 |
} |
| 243 |
elsif($type eq 'enum') |
| 244 |
{ |
| 245 |
if(my $basic = $probe->check_type_enum) |
| 246 |
{ |
| 247 |
$type_map{enum} = $basic; |
| 248 |
$align{$basic} ||= $probe->data->{type}->{enum}->{align}; |
| 249 |
} |
| 250 |
} |
| 251 |
elsif($type eq 'senum') |
| 252 |
{ |
| 253 |
if(my $basic = $probe->check_type_signed_enum) |
| 254 |
{ |
| 255 |
$type_map{senum} = $basic; |
| 256 |
$align{$basic} ||= $probe->data->{type}->{senum}->{align}; |
| 257 |
} |
| 258 |
} |
| 259 |
else |
| 260 |
{ |
| 261 |
if(my $basic = $probe->check_type_int($type)) |
| 262 |
{ |
| 263 |
$type_map{$type} = $basic; |
| 264 |
$align{$basic} ||= $probe->data->{type}->{$type}->{align}; |
| 265 |
} |
| 266 |
elsif($type =~ /^(unsigned |signed )?(char|short|int|long)$/) |
| 267 |
{ |
| 268 |
print "Unable to perform basic type check for: \"$type\"\n"; |
| 269 |
print "Please check config.log for detailed diagnostics.\n"; |
| 270 |
die "unable to configure Platypus"; |
| 271 |
} |
| 272 |
} |
| 273 |
} |
| 274 |
|
| 275 |
# Visual C++ uses SSIZE_T instead of ssize_t |
| 276 |
if($^O eq 'MSWin32' && $Config{ccname} eq 'cl' && defined $type_map{SSIZE_T}) |
| 277 |
{ |
| 278 |
$type_map{ssize_t} = delete $type_map{SSIZE_T}; |
| 279 |
} |
| 280 |
elsif(defined $type_map{SSIZE_T}) |
| 281 |
{ |
| 282 |
delete $type_map{SSIZE_T}; |
| 283 |
} |
| 284 |
|
| 285 |
$ch->define_var( SIZEOF_VOIDP => $probe->data->{type}->{pointer}->{size} ); |
| 286 |
if(my $size = $probe->data->{type}->{'float complex'}->{size}) |
| 287 |
{ $ch->define_var( SIZEOF_FLOAT_COMPLEX => $size ) } |
| 288 |
if(my $size = $probe->data->{type}->{'double complex'}->{size}) |
| 289 |
{ $ch->define_var( SIZEOF_DOUBLE_COMPLEX => $size ) } |
| 290 |
if(my $size = $probe->data->{type}->{'long double complex'}->{size}) |
| 291 |
{ $ch->define_var( SIZEOF_LONG_DOUBLE_COMPLEX => $size ) } |
| 292 |
|
| 293 |
# short aliases |
| 294 |
$type_map{uchar} = $type_map{'unsigned char'}; |
| 295 |
$type_map{ushort} = $type_map{'unsigned short'}; |
| 296 |
$type_map{uint} = $type_map{'unsigned int'}; |
| 297 |
$type_map{ulong} = $type_map{'unsigned long'}; |
| 298 |
|
| 299 |
# on Linux and OS X at least the test for bool fails |
| 300 |
# but _Bool works (even though code using bool seems |
| 301 |
# to work for both). May be because bool is a macro |
| 302 |
# for _Bool or something. |
| 303 |
$type_map{bool} ||= delete $type_map{_Bool}; |
| 304 |
delete $type_map{_Bool}; |
| 305 |
|
| 306 |
$ch->write_config_h; |
| 307 |
|
| 308 |
my %probe; |
| 309 |
if(defined $ENV{FFI_PLATYPUS_PROBE_OVERRIDE}) |
| 310 |
{ |
| 311 |
foreach my $kv (split /:/, $ENV{FFI_PLATYPUS_PROBE_OVERRIDE}) |
| 312 |
{ |
| 313 |
my($k,$v) = split /=/, $kv, 2; |
| 314 |
$probe{$k} = $v; |
| 315 |
} |
| 316 |
} |
| 317 |
|
| 318 |
if($Config{byteorder} =~ /^(1234|12345678)$/) |
| 319 |
{ |
| 320 |
$probe{bigendian} = 0; |
| 321 |
$probe{bigendian64} = 0; |
| 322 |
} |
| 323 |
|
| 324 |
if($self->build_config->get('config_no_alloca')) |
| 325 |
{ |
| 326 |
$probe{alloca} = 0; |
| 327 |
} |
| 328 |
|
| 329 |
foreach my $cfile (bsd_glob 'inc/probe/*.c') |
| 330 |
{ |
| 331 |
my $name = basename $cfile; |
| 332 |
$name =~ s/\.c$//; |
| 333 |
unless(defined $probe{$name}) |
| 334 |
{ |
| 335 |
my $code = do { |
| 336 |
my $fh; |
| 337 |
open $fh, '<', $cfile; |
| 338 |
local $/; |
| 339 |
<$fh>; |
| 340 |
}; |
| 341 |
my $value = $probe->check($name, $code); |
| 342 |
$probe{$name} = $value if defined $value; |
| 343 |
} |
| 344 |
if($probe{$name}) |
| 345 |
{ |
| 346 |
$ch->define_var( "FFI_PL_PROBE_" . uc($name) => 1 ); |
| 347 |
} |
| 348 |
} |
| 349 |
|
| 350 |
my %abi; |
| 351 |
|
| 352 |
if(my $cpp_output = $probe->check_cpp("#include <ffi.h>\n")) |
| 353 |
{ |
| 354 |
if($cpp_output =~ m/typedef\s+enum\s+ffi_abi\s+{(.*?)}/s) |
| 355 |
{ |
| 356 |
my $enum = $1; |
| 357 |
my %seen; |
| 358 |
while($enum =~ s/FFI_([A-Z_0-9]+)//) |
| 359 |
{ |
| 360 |
my $abi = $1; |
| 361 |
next if $seen{$abi}; |
| 362 |
$seen{$abi}++; |
| 363 |
next if $abi =~ /^(FIRST|LAST)_ABI$/; |
| 364 |
$probe->check_eval( |
| 365 |
decl => [ |
| 366 |
"#include \"ffi_platypus.h\"", |
| 367 |
], |
| 368 |
stmt => [ |
| 369 |
"ffi_cif cif;", |
| 370 |
"ffi_type *args[1];", |
| 371 |
"ffi_abi abi;", |
| 372 |
"if(ffi_prep_cif(&cif, FFI_$abi, 0, &ffi_type_void, args) != FFI_OK) { return 2; }", |
| 373 |
], |
| 374 |
eval => { |
| 375 |
"abi.@{[ lc $abi ]}" => [ '%d' => "FFI_$abi" ], |
| 376 |
}, |
| 377 |
); |
| 378 |
} |
| 379 |
if(defined $probe->data->{abi}) |
| 380 |
{ |
| 381 |
%abi = %{ $probe->data->{abi} || {} }; |
| 382 |
} |
| 383 |
else |
| 384 |
{ |
| 385 |
$probe->log("[[[ Unable to verify any ffi_abis ]]]"); |
| 386 |
print "*** Unable to detect ffi_abis ***\n"; |
| 387 |
print "[[[ Unable to verify any ffi_abis ]]]\n"; |
| 388 |
print "[[[ will try all known ABIs ]]]\n"; |
| 389 |
} |
| 390 |
} |
| 391 |
else |
| 392 |
{ |
| 393 |
$probe->log("[[[ ffi_abi enum not found ]]]"); |
| 394 |
print "*** Unable to detect ffi_abis ***\n"; |
| 395 |
print "[[[ ffi_abi enum not found ]]]\n"; |
| 396 |
print "[[[ will try all known ABIs ]]]\n"; |
| 397 |
} |
| 398 |
} |
| 399 |
else |
| 400 |
{ |
| 401 |
$probe->log("[[[ C pre-processor failed... ]]]"); |
| 402 |
print "*** Unable to detect ffi_abis ***\n"; |
| 403 |
print "[[[ C pre-processor failed... ]]]\n"; |
| 404 |
print "[[[ will try all known ABIs ]]]\n"; |
| 405 |
} |
| 406 |
|
| 407 |
unless(%abi) |
| 408 |
{ |
| 409 |
if($probe->check_eval( |
| 410 |
decl => [ |
| 411 |
"#include \"ffi_platypus.h\"", |
| 412 |
], |
| 413 |
eval => { |
| 414 |
"abi.default_abi" => [ '%d' => "FFI_DEFAULT_ABI" ], |
| 415 |
}, |
| 416 |
)) |
| 417 |
{ |
| 418 |
open my $fh, '<', 'inc/abi/abis-all.json' |
| 419 |
or die "unable to read abis-all.json $!"; |
| 420 |
my @abis = @{ decode_json(do { local $/; <$fh> }) }; |
| 421 |
close $fh; |
| 422 |
|
| 423 |
foreach my $abi (@abis) |
| 424 |
{ |
| 425 |
$probe->check_eval( |
| 426 |
decl => [ |
| 427 |
"#include \"ffi_platypus.h\"", |
| 428 |
], |
| 429 |
stmt => [ |
| 430 |
"ffi_cif cif;", |
| 431 |
"ffi_type *args[1];", |
| 432 |
"ffi_abi abi;", |
| 433 |
"if(ffi_prep_cif(&cif, FFI_$abi, 0, &ffi_type_void, args) != FFI_OK) { return 2; }", |
| 434 |
], |
| 435 |
eval => { |
| 436 |
"abi.@{[ lc $abi ]}" => [ '%d' => "FFI_$abi" ], |
| 437 |
}, |
| 438 |
); |
| 439 |
} |
| 440 |
|
| 441 |
%abi = %{ $probe->data->{abi} || {} }; |
| 442 |
} |
| 443 |
else |
| 444 |
{ |
| 445 |
$probe->log("[[[ fatal: unable to determine even the default ABI ]]]"); |
| 446 |
print "Unable to determine even the default ABI\n"; |
| 447 |
die "unable to configure Platypus"; |
| 448 |
} |
| 449 |
} |
| 450 |
|
| 451 |
$ch->write_config_h; |
| 452 |
$self->share_config->set( type_map => \%type_map ); |
| 453 |
$self->share_config->set( align => \%align ); |
| 454 |
$self->share_config->set( probe => \%probe ); |
| 455 |
$self->share_config->set( abi => \%abi ); |
| 456 |
} |
| 457 |
|
| 458 |
sub platform |
| 459 |
{ |
| 460 |
my($self) = @_; |
| 461 |
my %Config = %Config; |
| 462 |
my $eumm = $self->build_config->get('eumm'); |
| 463 |
foreach my $key (keys %$eumm) |
| 464 |
{ |
| 465 |
$Config{$key} = $eumm->{$key}; |
| 466 |
} |
| 467 |
require FFI::Build::Platform; |
| 468 |
FFI::Build::Platform->new(\%Config); |
| 469 |
} |
| 470 |
|
| 471 |
sub alien |
| 472 |
{ |
| 473 |
my($self) = @_; |
| 474 |
|
| 475 |
my $class = $self->build_config->get('alien')->{class}; |
| 476 |
my $pm = "$class.pm"; |
| 477 |
$pm =~ s/::/\//g; |
| 478 |
require $pm; |
| 479 |
|
| 480 |
$self->build_config->get('alien')->{class}; |
| 481 |
} |
| 482 |
|
| 483 |
1; |