| 1 | =head1 NAME | ||
| 2 | |||
| 3 | PDL::Core::Dev - PDL development module | ||
| 4 | |||
| 5 | =head1 DESCRIPTION | ||
| 6 | |||
| 7 | This module encapsulates most of the stuff useful for | ||
| 8 | PDL development and is often used from within Makefile.PL's. | ||
| 9 | |||
| 10 | =head1 SYNOPSIS | ||
| 11 | |||
| 12 | use PDL::Core::Dev; | ||
| 13 | |||
| 14 | =head1 FUNCTIONS | ||
| 15 | |||
| 16 | =cut | ||
| 17 | |||
| 18 | # Stuff used in development/install environment of PDL Makefile.PL's | ||
| 19 | # - not part of PDL itself. | ||
| 20 | |||
| 21 | package PDL::Core::Dev; | ||
| 22 | |||
| 23 | use File::Path; | ||
| 24 | use File::Basename; | ||
| 25 | use ExtUtils::Manifest; | ||
| 26 | use English; require Exporter; | ||
| 27 | |||
| 28 | @ISA = qw( Exporter ); | ||
| 29 | |||
| 30 | @EXPORT = qw( isbigendian genpp %PDL_DATATYPES | ||
| 31 | PDL_INCLUDE PDL_TYPEMAP | ||
| 32 | PDL_AUTO_INCLUDE PDL_BOOT | ||
| 33 | PDL_INST_INCLUDE PDL_INST_TYPEMAP | ||
| 34 | pdlpp_postamble_int pdlpp_stdargs_int | ||
| 35 | pdlpp_postamble pdlpp_stdargs write_dummy_make | ||
| 36 | unsupported getcyglib trylink | ||
| 37 | pdlpp_mkgen | ||
| 38 | ); | ||
| 39 | |||
| 40 | my $quotation_mark = $^O =~ /MSWin32/i ? '"' : "'"; | ||
| 41 | |||
| 42 | # Installation locations | ||
| 43 | # beware: whereami_any now appends the /Basic or /PDL directory as appropriate | ||
| 44 | |||
| 45 | # The INST are here still just in case we want to change something later. | ||
| 46 | |||
| 47 | # print STDERR "executing PDL::Core::Dev from",join(',',caller),"\n"; | ||
| 48 | |||
| 49 | # Return library locations | ||
| 50 | |||
| 51 | sub PDL_INCLUDE { '"-I'.whereami_any().'/Core"' }; | ||
| 52 | sub PDL_TYPEMAP { whereami_any().'/Core/typemap.pdl' }; | ||
| 53 | # sub PDL_INST_INCLUDE { '-I'.whereami_any().'/Core' }; | ||
| 54 | # sub PDL_INST_TYPEMAP { whereami_any().'/Core/typemap.pdl' }; | ||
| 55 | |||
| 56 | sub PDL_INST_INCLUDE {&PDL_INCLUDE} | ||
| 57 | sub PDL_INST_TYPEMAP {&PDL_TYPEMAP} | ||
| 58 | |||
| 59 | sub PDL_AUTO_INCLUDE { | ||
| 60 | my ($symname) = @_; | ||
| 61 | $symname ||= 'PDL'; | ||
| 62 | return << "EOR"; | ||
| 63 | #include <pdlcore.h> | ||
| 64 | static Core* $symname; /* Structure holds core C functions */ | ||
| 65 | static SV* CoreSV; /* Gets pointer to perl var holding core structure */ | ||
| 66 | EOR | ||
| 67 | } | ||
| 68 | |||
| 69 | sub PDL_BOOT { | ||
| 70 | my ($symname, $module) = @_; | ||
| 71 | $symname ||= 'PDL'; | ||
| 72 | $module ||= 'The code'; | ||
| 73 | return << "EOR"; | ||
| 74 | |||
| 75 | perl_require_pv ("PDL/Core.pm"); /* make sure PDL::Core is loaded */ | ||
| 76 | #ifndef aTHX_ | ||
| 77 | #define aTHX_ | ||
| 78 | #endif | ||
| 79 | if (SvTRUE (ERRSV)) Perl_croak(aTHX_ "%s",SvPV_nolen (ERRSV)); | ||
| 80 | CoreSV = perl_get_sv("PDL::SHARE",FALSE); /* SV* value */ | ||
| 81 | if (CoreSV==NULL) | ||
| 82 | Perl_croak(aTHX_ "We require the PDL::Core module, which was not found"); | ||
| 83 | $symname = INT2PTR(Core*,SvIV( CoreSV )); /* Core* value */ | ||
| 84 | if ($symname->Version != PDL_CORE_VERSION) | ||
| 85 | Perl_croak(aTHX_ "[$symname->Version: \%d PDL_CORE_VERSION: \%d XS_VERSION: \%s] $module needs to be recompiled against the newly installed PDL", $symname->Version, PDL_CORE_VERSION, XS_VERSION); | ||
| 86 | |||
| 87 | EOR | ||
| 88 | } | ||
| 89 | |||
| 90 | # whereami_any returns appended 'Basic' or 'PDL' dir as appropriate | ||
| 91 | use Cwd qw/abs_path/; | ||
| 92 | sub whereami_any { | ||
| 93 | my $dir = (&whereami(1) or &whereami_inst(1) or | ||
| 94 | die "Unable to determine ANY directory path to PDL::Core::Dev module\n"); | ||
| 95 | return abs_path($dir); | ||
| 96 | } | ||
| 97 | |||
| 98 | sub whereami { | ||
| 99 | for $dir (@INC,qw|. .. ../.. ../../.. ../../../..|) { | ||
| 100 | return ($_[0] ? $dir . '/Basic' : $dir) | ||
| 101 | if -e "$dir/Basic/Core/Dev.pm"; | ||
| 102 | } | ||
| 103 | die "Unable to determine UNINSTALLED directory path to PDL::Core::Dev module\n" | ||
| 104 | if !$_[0]; | ||
| 105 | return undef; | ||
| 106 | } | ||
| 107 | |||
| 108 | sub whereami_inst { | ||
| 109 | for $dir (@INC,map {$_."/blib"} qw|. .. ../.. ../../.. ../../../..|) { | ||
| 110 | return ($_[0] ? $dir . '/PDL' : $dir) | ||
| 111 | if -e "$dir/PDL/Core/Dev.pm"; | ||
| 112 | } | ||
| 113 | die "Unable to determine INSTALLED directory path to PDL::Core::Dev module\n" | ||
| 114 | if !$_[0]; | ||
| 115 | return undef; | ||
| 116 | } | ||
| 117 | |||
| 118 | # | ||
| 119 | # To access PDL's configuration use %PDL::Config. Makefile.PL has been set up | ||
| 120 | # to create this variable so it is available during 'perl Makefile.PL' and | ||
| 121 | # it can be eval-ed during 'make' | ||
| 122 | |||
| 123 | unless ( %PDL::Config ) { | ||
| 124 | |||
| 125 | # look for the distribution and then the installed version | ||
| 126 | # (a manual version of whereami_any) | ||
| 127 | # | ||
| 128 | my $dir; | ||
| 129 | $dir = whereami(1); | ||
| 130 | if ( defined $dir ) { | ||
| 131 | $dir = abs_path($dir . "/Core"); | ||
| 132 | } else { | ||
| 133 | # as no argument given whereami_inst will die if it fails | ||
| 134 | # (and it also returns a slightly different path than whereami(1) | ||
| 135 | # does, since it does not include "/PDL") | ||
| 136 | # | ||
| 137 | $dir = whereami_inst; | ||
| 138 | $dir = abs_path($dir . "/PDL"); | ||
| 139 | } | ||
| 140 | |||
| 141 | my $dir2 = $dir; | ||
| 142 | $dir2 =~ s/\}/\\\}/g; | ||
| 143 | eval sprintf('require q{%s/Config.pm};', $dir2); | ||
| 144 | |||
| 145 | die "Unable to find PDL's configuration info\n [$@]" | ||
| 146 | if $@; | ||
| 147 | } | ||
| 148 | |||
| 149 | # Data types to C types mapping | ||
| 150 | # get the map from Types.pm | ||
| 151 | { | ||
| 152 | # load PDL::Types only if it has not been previously loaded | ||
| 153 | my $loaded_types = grep (m%(PDL|Core)/Types[.]pm$%, keys %INC); | ||
| 154 | $@ = ''; # reset | ||
| 155 | eval('require "'.whereami_any().'/Core/Types.pm"') # lets dist Types.pm win | ||
| 156 | unless $loaded_types; # only when PDL::Types not yet loaded | ||
| 157 | if($@) { # if PDL::Types doesn't work try with full path (during build) | ||
| 158 | my $foo = $@; | ||
| 159 | $@=""; | ||
| 160 | eval('require PDL::Types'); | ||
| 161 | if($@) { | ||
| 162 | die "can't find PDL::Types: $foo and $@" unless $@ eq ""; | ||
| 163 | } | ||
| 164 | } | ||
| 165 | } | ||
| 166 | PDL::Types->import(); | ||
| 167 | |||
| 168 | my $inc = defined $PDL::Config{MALLOCDBG}->{include} ? | ||
| 169 | "$PDL::Config{MALLOCDBG}->{include}" : ''; | ||
| 170 | my $libs = defined $PDL::Config{MALLOCDBG}->{libs} ? | ||
| 171 | "$PDL::Config{MALLOCDBG}->{libs}" : ''; | ||
| 172 | |||
| 173 | %PDL_DATATYPES = (); | ||
| 174 | foreach $key (keys %PDL::Types::typehash) { | ||
| 175 | $PDL_DATATYPES{$PDL::Types::typehash{$key}->{'sym'}} = | ||
| 176 | $PDL::Types::typehash{$key}->{'ctype'}; | ||
| 177 | } | ||
| 178 | |||
| 179 | # non-blocking IO configuration | ||
| 180 | |||
| 181 | $O_NONBLOCK = defined $Config{'o_nonblock'} ? $Config{'o_nonblock'} | ||
| 182 | : 'O_NONBLOCK'; | ||
| 183 | |||
| 184 | =head2 isbigendian | ||
| 185 | |||
| 186 | =for ref | ||
| 187 | |||
| 188 | Is the machine big or little endian? | ||
| 189 | |||
| 190 | =for example | ||
| 191 | |||
| 192 | print "Your machins is big endian.\n" if isbigendian(); | ||
| 193 | |||
| 194 | returns 1 if the machine is big endian, 0 if little endian, | ||
| 195 | or dies if neither. It uses the C<byteorder> element of | ||
| 196 | perl's C<%Config> array. | ||
| 197 | |||
| 198 | =for usage | ||
| 199 | |||
| 200 | my $retval = isbigendian(); | ||
| 201 | |||
| 202 | =cut | ||
| 203 | |||
| 204 | # ' emacs parsing dummy | ||
| 205 | |||
| 206 | # big/little endian? | ||
| 207 | sub isbigendian { | ||
| 208 | use Config; | ||
| 209 | my $byteorder = $Config{byteorder} || | ||
| 210 | die "ERROR: Unable to find 'byteorder' in perl's Config\n"; | ||
| 211 | return 1 if $byteorder eq "4321"; | ||
| 212 | return 1 if $byteorder eq "87654321"; | ||
| 213 | return 0 if $byteorder eq "1234"; | ||
| 214 | return 0 if $byteorder eq "12345678"; | ||
| 215 | die "ERROR: PDL does not understand your machine's byteorder ($byteorder)\n"; | ||
| 216 | } | ||
| 217 | |||
| 218 | #################### PDL Generic PreProcessor #################### | ||
| 219 | # | ||
| 220 | # Preprocesses *.g files to *.c files allowing 'generic' | ||
| 221 | # type code which is converted to code for each type. | ||
| 222 | # | ||
| 223 | # e.g. the code: | ||
| 224 | # | ||
| 225 | # pdl x; | ||
| 226 | # GENERICLOOP(x.datatype) | ||
| 227 | # generic *xx = x.data; | ||
| 228 | # for(i=0; i<nvals; i++) | ||
| 229 | # xx[i] = i/nvals; | ||
| 230 | # ENDGENERICLOOP | ||
| 231 | # | ||
| 232 | # is converted into a giant switch statement: | ||
| 233 | # | ||
| 234 | # pdl x; | ||
| 235 | # switch (x.datatype) { | ||
| 236 | # | ||
| 237 | # case PDL_L: | ||
| 238 | # { | ||
| 239 | # PDL_Long *xx = x.data; | ||
| 240 | # for(i=0; i<nvals; i++) | ||
| 241 | # xx[i] = i/nvals; | ||
| 242 | # }break; | ||
| 243 | # | ||
| 244 | # case PDL_F: | ||
| 245 | # { | ||
| 246 | # PDL_Float *xx = x.data; | ||
| 247 | # | ||
| 248 | # .... etc. ..... | ||
| 249 | # | ||
| 250 | # 'generic' is globally substituted for each relevant data type. | ||
| 251 | # | ||
| 252 | # This is used in PDL to write generic functions (taking pdl or void | ||
| 253 | # objects) which is still efficient with perl writing the actual C | ||
| 254 | # code for each type. | ||
| 255 | # | ||
| 256 | # 1st version - Karl Glazebrook 4/Aug/1996. | ||
| 257 | # | ||
| 258 | # Also makes the followings substitutions: | ||
| 259 | # | ||
| 260 | # (i) O_NONBLOCK - open flag for non-blocking I/O (5/Aug/96) | ||
| 261 | # | ||
| 262 | |||
| 263 | |||
| 264 | sub genpp { | ||
| 265 | |||
| 266 | $gotstart = 0; @gencode = (); | ||
| 267 | |||
| 268 | while (<>) { # Process files in @ARGV list - result to STDOUT | ||
| 269 | |||
| 270 | # Do the miscellaneous substitutions first | ||
| 271 | |||
| 272 | s/O_NONBLOCK/$O_NONBLOCK/go; # I/O | ||
| 273 | |||
| 274 | if ( m/ (\s*)? \b GENERICLOOP \s* \( ( [^\)]* ) \) ( \s*; )? /x ){ # Start of generic code | ||
| 275 | #print $MATCH, "=$1=\n"; | ||
| 276 | |||
| 277 | die "Found GENERICLOOP while searching for ENDGENERICLOOP\n" if $gotstart; | ||
| 278 | $loopvar = $2; | ||
| 279 | $indent = $1; | ||
| 280 | print $PREMATCH; | ||
| 281 | |||
| 282 | @gencode = (); # Start saving code | ||
| 283 | push @gencode, $POSTMATCH; | ||
| 284 | $gotstart = 1; | ||
| 285 | next; | ||
| 286 | } | ||
| 287 | |||
| 288 | if ( m/ \b ENDGENERICLOOP ( \s*; )? /x ) { | ||
| 289 | |||
| 290 | die "Found ENDGENERICLOOP while searching for GENERICLOOP\n" unless $gotstart; | ||
| 291 | |||
| 292 | push @gencode, $PREMATCH; | ||
| 293 | |||
| 294 | flushgeneric(); # Output the generic code | ||
| 295 | |||
| 296 | print $POSTMATCH; # End of genric code | ||
| 297 | $gotstart = 0; | ||
| 298 | next; | ||
| 299 | } | ||
| 300 | |||
| 301 | if ($gotstart) { | ||
| 302 | push @gencode, $_; | ||
| 303 | } | ||
| 304 | else { | ||
| 305 | print; | ||
| 306 | } | ||
| 307 | |||
| 308 | } # End while | ||
| 309 | } | ||
| 310 | |||
| 311 | sub flushgeneric { # Construct the generic code switch | ||
| 312 | |||
| 313 | print $indent,"switch ($loopvar) {\n\n"; | ||
| 314 | |||
| 315 | for $case (PDL::Types::typesrtkeys()){ | ||
| 316 | |||
| 317 | $type = $PDL_DATATYPES{$case}; | ||
| 318 | |||
| 319 | my $ppsym = $PDL::Types::typehash{$case}->{ppsym}; | ||
| 320 | print $indent,"case $case:\n"; # Start of this case | ||
| 321 | print $indent," {"; | ||
| 322 | |||
| 323 | # Now output actual code with substutions | ||
| 324 | |||
| 325 | for (@gencode) { | ||
| 326 | $line = $_; | ||
| 327 | |||
| 328 | $line =~ s/\bgeneric\b/$type/g; | ||
| 329 | $line =~ s/\bgeneric_ppsym\b/$ppsym/g; | ||
| 330 | |||
| 331 | print " ",$line; | ||
| 332 | } | ||
| 333 | |||
| 334 | print "}break;\n\n"; # End of this case | ||
| 335 | } | ||
| 336 | print $indent,"default:\n"; | ||
| 337 | print $indent,' croak ("Not a known data type code=%d",'.$loopvar.");\n"; | ||
| 338 | print $indent,"}"; | ||
| 339 | |||
| 340 | } | ||
| 341 | |||
| 342 | |||
| 343 | sub genpp_cmdline { | ||
| 344 | my ($in, $out) = @_; | ||
| 345 | require ExtUtils::MM; | ||
| 346 | my $MM = bless { NAME => 'Fake' }, 'MM'; | ||
| 347 | my $devpm = whereami_any()."/Core/Dev.pm"; | ||
| 348 | sprintf($MM->oneliner(<<'EOF'), $devpm) . qq{ "$in" > "$out"}; | ||
| 349 | require "%s"; PDL::Core::Dev->import(); genpp(); | ||
| 350 | EOF | ||
| 351 | } | ||
| 352 | |||
| 353 | |||
| 354 | # Standard PDL postamble | ||
| 355 | # | ||
| 356 | # This is called via .../Gen/Inline/Pdlpp.pm, in the case that the INTERNAL | ||
| 357 | # flag for the compilation is off (grep "ILSM" in that file to find the reference). | ||
| 358 | # If it's ON, then postamble_int gets called instead. | ||
| 359 | |||
| 360 | |||
| 361 | sub postamble { | ||
| 362 | my ($self) = @_; | ||
| 363 | sprintf <<'EOF', genpp_cmdline(qw($< $@)); | ||
| 364 | |||
| 365 | # Rules for the generic preprocessor | ||
| 366 | |||
| 367 | .SUFFIXES: .g | ||
| 368 | .g.c : | ||
| 369 | %s | ||
| 370 | |||
| 371 | EOF | ||
| 372 | } | ||
| 373 | |||
| 374 | # Expects list in format: | ||
| 375 | # [gtest.pd, GTest, PDL::GTest, ['../GIS/Proj', ...] ], [...] | ||
| 376 | # source, prefix,module/package, optional deps | ||
| 377 | # The idea is to support in future several packages in same dir - EUMM | ||
| 378 | # 7.06 supports | ||
| 379 | # each optional dep is a relative dir that a "make" will chdir to and | ||
| 380 | # "make" first - so the *.pd file can then "use" what it makes | ||
| 381 | |||
| 382 | # This is the function internal for PDL. | ||
| 383 | |||
| 384 | sub pdlpp_postamble_int { | ||
| 385 | join '',map { my($src,$pref,$mod, $deps) = @$_; | ||
| 386 | die "If give dependencies, must be array-ref" if $deps and !ref $deps; | ||
| 387 | my $w = whereami_any(); | ||
| 388 | $w =~ s%/((PDL)|(Basic))$%%; # remove the trailing subdir | ||
| 389 | my $top = File::Spec->abs2rel($w); | ||
| 390 | my $basic = File::Spec->catdir($top, 'Basic'); | ||
| 391 | my $core = File::Spec->catdir($basic, 'Core'); | ||
| 392 | my $gen = File::Spec->catdir($basic, 'Gen'); | ||
| 393 | my $depbuild = ''; | ||
| 394 | for my $dep (@{$deps || []}) { | ||
| 395 | my $target = ''; | ||
| 396 | if ($dep eq 'core') { | ||
| 397 | $dep = $top; | ||
| 398 | $target = ' core'; | ||
| 399 | } | ||
| 400 | require ExtUtils::MM; | ||
| 401 | $dep =~ s#([\(\)])#\\$1#g; # in case of unbalanced ( | ||
| 402 | $depbuild .= MM->oneliner("exit(!(chdir q($dep) && !system(q(\$(MAKE)$target))))"); | ||
| 403 | $depbuild .= "\n\t"; | ||
| 404 | } | ||
| 405 | qq| | ||
| 406 | |||
| 407 | $pref.pm: $src $core/Types.pm | ||
| 408 | $depbuild\$(PERLRUNINST) \"-MPDL::PP qw[$mod $mod $pref]\" $src | ||
| 409 | |||
| 410 | $pref.xs: $pref.pm | ||
| 411 | \$(TOUCH) \$@ | ||
| 412 | |||
| 413 | $pref.c: $pref.xs | ||
| 414 | |||
| 415 | $pref\$(OBJ_EXT): $pref.c | ||
| 416 | | | ||
| 417 | } (@_) | ||
| 418 | } | ||
| 419 | |||
| 420 | |||
| 421 | # This is the function to be used outside the PDL tree. | ||
| 422 | sub pdlpp_postamble { | ||
| 423 | join '',map { my($src,$pref,$mod) = @$_; | ||
| 424 | my $w = whereami_any(); | ||
| 425 | $w =~ s%/((PDL)|(Basic))$%%; # remove the trailing subdir | ||
| 426 | qq| | ||
| 427 | |||
| 428 | $pref.pm: $src | ||
| 429 | \$(PERL) "-I$w" \"-MPDL::PP qw[$mod $mod $pref]\" $src | ||
| 430 | |||
| 431 | $pref.xs: $pref.pm | ||
| 432 | \$(TOUCH) \$@ | ||
| 433 | |||
| 434 | $pref.c: $pref.xs | ||
| 435 | |||
| 436 | $pref\$(OBJ_EXT): $pref.c | ||
| 437 | |||
| 438 | install :: | ||
| 439 | \@echo "Updating PDL documentation database..."; | ||
| 440 | \$(ABSPERLRUN) -e ${quotation_mark}exit if \$\$ENV{DESTDIR}; use PDL::Doc; eval { PDL::Doc::add_module(q{$mod}); }; ${quotation_mark}; | ||
| 441 | | | ||
| 442 | } (@_) | ||
| 443 | } | ||
| 444 | |||
| 445 | sub pdlpp_stdargs_int { | ||
| 446 | my($rec) = @_; | ||
| 447 | my($src,$pref,$mod) = @$rec; | ||
| 448 | my $w = whereami(); | ||
| 449 | my $malloclib = exists $PDL::Config{MALLOCDBG}->{libs} ? | ||
| 450 | $PDL::Config{MALLOCDBG}->{libs} : ''; | ||
| 451 | my $mallocinc = exists $PDL::Config{MALLOCDBG}->{include} ? | ||
| 452 | $PDL::Config{MALLOCDBG}->{include} : ''; | ||
| 453 | my $libsarg = $libs || $malloclib ? "$libs $malloclib " : ''; # for Win32 | ||
| 454 | return ( | ||
| 455 | %::PDL_OPTIONS, | ||
| 456 | 'NAME' => $mod, | ||
| 457 | 'VERSION_FROM' => "$w/Basic/Core/Version.pm", | ||
| 458 | 'TYPEMAPS' => [&PDL_TYPEMAP()], | ||
| 459 | 'OBJECT' => "$pref\$(OBJ_EXT)", | ||
| 460 | PM => {"$pref.pm" => "\$(INST_LIBDIR)/$pref.pm"}, | ||
| 461 | MAN3PODS => {"$pref.pm" => "\$(INST_MAN3DIR)/$mod.\$(MAN3EXT)"}, | ||
| 462 | 'INC' => &PDL_INCLUDE()." $inc $mallocinc", | ||
| 463 | 'LIBS' => $libsarg ? [$libsarg] : [], | ||
| 464 | 'clean' => {'FILES' => "$pref.xs $pref.pm $pref\$(OBJ_EXT) $pref.c"}, | ||
| 465 | (eval ($ExtUtils::MakeMaker::VERSION) >= 6.57_02 ? ('NO_MYMETA' => 1) : ()), | ||
| 466 | ); | ||
| 467 | } | ||
| 468 | |||
| 469 | sub pdlpp_stdargs { | ||
| 470 | my($rec) = @_; | ||
| 471 | my($src,$pref,$mod) = @$rec; | ||
| 472 | return ( | ||
| 473 | %::PDL_OPTIONS, | ||
| 474 | 'NAME' => $mod, | ||
| 475 | 'TYPEMAPS' => [&PDL_INST_TYPEMAP()], | ||
| 476 | 'OBJECT' => "$pref\$(OBJ_EXT)", | ||
| 477 | PM => {"$pref.pm" => "\$(INST_LIBDIR)/$pref.pm"}, | ||
| 478 | MAN3PODS => {"$pref.pm" => "\$(INST_MAN3DIR)/$mod.\$(MAN3EXT)"}, | ||
| 479 | 'INC' => &PDL_INST_INCLUDE()." $inc", | ||
| 480 | 'LIBS' => $libs ? ["$libs "] : [], | ||
| 481 | 'clean' => {'FILES' => "$pref.xs $pref.pm $pref\$(OBJ_EXT) $pref.c"}, | ||
| 482 | 'dist' => {'PREOP' => '$(PERL) "-I$(INST_ARCHLIB)" "-I$(INST_LIB)" -MPDL::Core::Dev -e pdlpp_mkgen $(DISTVNAME)' }, | ||
| 483 | (eval ($ExtUtils::MakeMaker::VERSION) >= 6.57_02 ? ('NO_MYMETA' => 1) : ()), | ||
| 484 | ); | ||
| 485 | } | ||
| 486 | |||
| 487 | # pdlpp_mkgen($dir) | ||
| 488 | # - scans $dir/MANIFEST for all *.pd files and creates corresponding *.pm files | ||
| 489 | # in $dir/GENERATED/ subdir; needed for proper doc rendering at metacpan.org | ||
| 490 | # - it is used in Makefile.PL like: | ||
| 491 | # dist => { PREOP=>'$(PERL) -MPDL::Core::Dev -e pdlpp_mkgen $(DISTVNAME)' } | ||
| 492 | # so all the magic *.pm generation happens during "make dist" | ||
| 493 | # - it is intended to be called as a one-liner: | ||
| 494 | # perl -MPDL::Core::Dev -e pdlpp_mkgen DirName | ||
| 495 | # | ||
| 496 | sub pdlpp_mkgen { | ||
| 497 | my $dir = @_ > 0 ? $_[0] : $ARGV[0]; | ||
| 498 | die "pdlpp_mkgen: unspecified directory" unless defined $dir && -d $dir; | ||
| 499 | my $file = "$dir/MANIFEST"; | ||
| 500 | die "pdlpp_mkgen: non-existing '$dir/MANIFEST'" unless -f $file; | ||
| 501 | |||
| 502 | my @pairs = (); | ||
| 503 | my $manifest = ExtUtils::Manifest::maniread($file); | ||
| 504 | for (keys %$manifest) { | ||
| 505 | next if $_ !~ m/\.pd$/; # skip non-pd files | ||
| 506 | next if $_ =~ m/^(t|xt)\//; # skip *.pd files in test subdirs | ||
| 507 | next unless -f $_; | ||
| 508 | my $content = do { local $/; open my $in, '<', $_; <$in> }; | ||
| 509 | if ($content =~ /=head1\s+NAME\s+(\S+)\s+/sg) { | ||
| 510 | push @pairs, [$_, $1]; | ||
| 511 | } | ||
| 512 | else { | ||
| 513 | warn "pdlpp_mkgen: unknown module name for '$_' (use proper '=head1 NAME' section)\n"; | ||
| 514 | } | ||
| 515 | } | ||
| 516 | |||
| 517 | my %added = (); | ||
| 518 | for (@pairs) { | ||
| 519 | my ($pd, $mod) = @$_; | ||
| 520 | (my $prefix = $mod) =~ s|::|/|g; | ||
| 521 | my $manifestpm = "GENERATED/$prefix.pm"; | ||
| 522 | $prefix = "$dir/GENERATED/$prefix"; | ||
| 523 | File::Path::mkpath(dirname($prefix)); | ||
| 524 | #there is no way to use PDL::PP from perl code, thus calling via system() | ||
| 525 | my @in = map { "-I$_" } @INC, 'inc'; | ||
| 526 | my $rv = system($^X, @in, "-MPDL::PP qw[$mod $mod $prefix]", $pd); | ||
| 527 | if ($rv == 0 && -f "$prefix.pm") { | ||
| 528 | $added{$manifestpm} = "mod=$mod pd=$pd (added by pdlpp_mkgen)"; | ||
| 529 | unlink "$prefix.xs"; #we need only .pm | ||
| 530 | } | ||
| 531 | else { | ||
| 532 | warn "pdlpp_mkgen: cannot convert '$pd'\n"; | ||
| 533 | } | ||
| 534 | } | ||
| 535 | |||
| 536 | if (scalar(keys %added) > 0) { | ||
| 537 | #maniadd works only with this global variable | ||
| 538 | local $ExtUtils::Manifest::MANIFEST = $file; | ||
| 539 | ExtUtils::Manifest::maniadd(\%added); | ||
| 540 | } | ||
| 541 | } | ||
| 542 | |||
| 543 | sub unsupported { | ||
| 544 | my ($package,$os) = @_; | ||
| 545 | "No support for $package on $os platform yet. Will skip build process"; | ||
| 546 | } | ||
| 547 | |||
| 548 | sub write_dummy_make { | ||
| 549 | my ($msg) = @_; | ||
| 550 | $msg =~ s#\n*\z#\n#; | ||
| 551 | $msg =~ s#^\s*#\n#gm; | ||
| 552 | print $msg; | ||
| 553 | require ExtUtils::MakeMaker; | ||
| 554 | ExtUtils::MakeMaker::WriteEmptyMakefile(NAME => 'Dummy', DIR => []); | ||
| 555 | } | ||
| 556 | |||
| 557 | sub getcyglib { | ||
| 558 | my ($lib) = @_; | ||
| 559 | my $lp = `gcc -print-file-name=lib$lib.a`; | ||
| 560 | $lp =~ s|/[^/]+$||; | ||
| 561 | $lp =~ s|^([a-z,A-Z]):|//$1|g; | ||
| 562 | return "-L$lp -l$lib"; | ||
| 563 | } | ||
| 564 | |||
| 565 | =head2 trylink | ||
| 566 | |||
| 567 | =for ref | ||
| 568 | |||
| 569 | a perl configure clone | ||
| 570 | |||
| 571 | =for example | ||
| 572 | |||
| 573 | if (trylink 'libGL', '', 'char glBegin(); glBegin();', '-lGL') { | ||
| 574 | $libs = '-lGLU -lGL'; | ||
| 575 | $have_GL = 1; | ||
| 576 | } else { | ||
| 577 | $have_GL = 0; | ||
| 578 | } | ||
| 579 | $maybe = | ||
| 580 | trylink 'libwhatever', $inc, $body, $libs, $cflags, | ||
| 581 | {MakeMaker=>1, Hide=>0, Clean=>1}; | ||
| 582 | |||
| 583 | Try to link some C-code making up the body of a function | ||
| 584 | with a given set of library specifiers | ||
| 585 | |||
| 586 | return 1 if successful, 0 otherwise | ||
| 587 | |||
| 588 | =for usage | ||
| 589 | |||
| 590 | trylink $infomsg, $include, $progbody, $libs [,$cflags,{OPTIONS}]; | ||
| 591 | |||
| 592 | Takes 4 + 2 optional arguments. | ||
| 593 | |||
| 594 | =over 5 | ||
| 595 | |||
| 596 | =item * | ||
| 597 | |||
| 598 | an informational message to print (can be empty) | ||
| 599 | |||
| 600 | =item * | ||
| 601 | |||
| 602 | any commands to be included at the top of the generated C program | ||
| 603 | (typically something like C<#include "mylib.h">) | ||
| 604 | |||
| 605 | =item * | ||
| 606 | |||
| 607 | the body of the program (in function main) | ||
| 608 | |||
| 609 | =item * | ||
| 610 | |||
| 611 | library flags to use for linking. Preprocessing | ||
| 612 | by MakeMaker should be performed as needed (see options and example). | ||
| 613 | |||
| 614 | =item * | ||
| 615 | |||
| 616 | compilation flags. For example, something like C<-I/usr/local/lib>. | ||
| 617 | Optional argument. Empty if omitted. | ||
| 618 | |||
| 619 | =item * | ||
| 620 | |||
| 621 | OPTIONS | ||
| 622 | |||
| 623 | =over | ||
| 624 | |||
| 625 | =item MakeMaker | ||
| 626 | |||
| 627 | Preprocess library strings in the way MakeMaker does things. This is | ||
| 628 | advisable to ensure that your code will actually work after the link | ||
| 629 | specs have been processed by MakeMaker. | ||
| 630 | |||
| 631 | =item Hide | ||
| 632 | |||
| 633 | Controls if linking output etc is hidden from the user or not. | ||
| 634 | On by default except within the build of the PDL distribution | ||
| 635 | where the config value set in F<perldl.conf> prevails. | ||
| 636 | |||
| 637 | =item Clean | ||
| 638 | |||
| 639 | Remove temporary files. Enabled by default. You might want to switch | ||
| 640 | it off during debugging. | ||
| 641 | |||
| 642 | =back | ||
| 643 | |||
| 644 | =back | ||
| 645 | |||
| 646 | =cut | ||
| 647 | |||
| 648 | |||
| 649 | sub trylink { | ||
| 650 | my $opt = ref $_[$#_] eq 'HASH' ? pop : {}; | ||
| 651 | my ($txt,$inc,$body,$libs,$cflags) = @_; | ||
| 652 | $cflags ||= ''; | ||
| 653 | require File::Spec; | ||
| 654 | require File::Temp; | ||
| 655 | my $cdir = sub { return File::Spec->catdir(@_)}; | ||
| 656 | my $cfile = sub { return File::Spec->catfile(@_)}; | ||
| 657 | use Config; | ||
| 658 | |||
| 659 | # check if MakeMaker should be used to preprocess the libs | ||
| 660 | for my $key(keys %$opt) {$opt->{lc $key} = $opt->{$key}} | ||
| 661 | my $mmprocess = exists $opt->{makemaker} && $opt->{makemaker}; | ||
| 662 | my $hide = exists $opt->{hide} ? $opt->{hide} : | ||
| 663 | exists $PDL::Config{HIDE_TRYLINK} ? $PDL::Config{HIDE_TRYLINK} : 1; | ||
| 664 | my $clean = exists $opt->{clean} ? $opt->{clean} : 1; | ||
| 665 | if ($mmprocess) { | ||
| 666 | require ExtUtils::MakeMaker; | ||
| 667 | require ExtUtils::Liblist; | ||
| 668 | my $self = new ExtUtils::MakeMaker {DIR => [],'NAME' => 'NONE'}; | ||
| 669 | |||
| 670 | my @libs = $self->ext($libs, 0); | ||
| 671 | |||
| 672 | print "processed LIBS: $libs[0]\n" unless $hide; | ||
| 673 | $libs = $libs[0]; # replace by preprocessed libs | ||
| 674 | } | ||
| 675 | |||
| 676 | print " Trying $txt...\n " unless $txt =~ /^\s*$/; | ||
| 677 | |||
| 678 | my $HIDE = !$hide ? '' : '>/dev/null 2>&1'; | ||
| 679 | if($^O =~ /mswin32/i) {$HIDE = '>NUL 2>&1'} | ||
| 680 | |||
| 681 | my $tempd; | ||
| 682 | |||
| 683 | $tempd = File::Temp::tempdir(CLEANUP=>1) || die "trylink: could not make TEMPDIR"; | ||
| 684 | ### if($^O =~ /MSWin32/i) {$tempd = File::Spec->tmpdir()} | ||
| 685 | ### else { | ||
| 686 | ### $tempd = $PDL::Config{TEMPDIR} || | ||
| 687 | ### } | ||
| 688 | |||
| 689 | my ($tc,$te) = map {&$cfile($tempd,"testfile$_")} ('.c',''); | ||
| 690 | open FILE,">$tc" or die "trylink: couldn't open testfile `$tc' for writing, $!"; | ||
| 691 | my $prog = <<"EOF"; | ||
| 692 | $inc | ||
| 693 | |||
| 694 | int main(void) { | ||
| 695 | $body | ||
| 696 | |||
| 697 | return 0; | ||
| 698 | |||
| 699 | } | ||
| 700 | |||
| 701 | EOF | ||
| 702 | |||
| 703 | print FILE $prog; | ||
| 704 | close FILE; | ||
| 705 | # print "test prog:\n$prog\n"; | ||
| 706 | # make sure we can overwrite the executable. shouldn't need this, | ||
| 707 | # but if it fails and HIDE is on, the user will never see the error. | ||
| 708 | open(T, ">$te") or die( "unable to write to test executable `$te'"); | ||
| 709 | close T; | ||
| 710 | print "$Config{cc} $cflags -o $te $tc $libs $HIDE ...\n" unless $hide; | ||
| 711 | my $success = (system("$Config{cc} $cflags -o $te $tc $libs $HIDE") == 0) && | ||
| 712 | -e $te ? 1 : 0; | ||
| 713 | unlink "$te","$tc" if $clean; | ||
| 714 | print $success ? "\t\tYES\n" : "\t\tNO\n" unless $txt =~ /^\s*$/; | ||
| 715 | print $success ? "\t\tSUCCESS\n" : "\t\tFAILED\n" | ||
| 716 | if $txt =~ /^\s*$/ && !$hide; | ||
| 717 | return $success; | ||
| 718 | } | ||
| 719 | |||
| 720 | =head2 datatypes_switch | ||
| 721 | |||
| 722 | =for ref | ||
| 723 | |||
| 724 | prints on C<STDOUT> XS text for F<Core.xs>. | ||
| 725 | |||
| 726 | =cut | ||
| 727 | |||
| 728 | sub datatypes_switch { | ||
| 729 | my $ntypes = $#PDL::Types::names; | ||
| 730 | my @m; | ||
| 731 | foreach my $i ( 0 .. $ntypes ) { | ||
| 732 | my $type = PDL::Type->new( $i ); | ||
| 733 | my $typesym = $type->symbol; | ||
| 734 | my $typeppsym = $type->ppsym; | ||
| 735 | my $cname = $type->ctype; | ||
| 736 | $cname =~ s/^PDL_//; | ||
| 737 | push @m, "\tcase $typesym: retval.type = $typesym; retval.value.$typeppsym = PDL.bvals.$cname; break;"; | ||
| 738 | } | ||
| 739 | print map "$_\n", @m; | ||
| 740 | } | ||
| 741 | |||
| 742 | =head2 generate_core_flags | ||
| 743 | |||
| 744 | =for ref | ||
| 745 | |||
| 746 | prints on C<STDOUT> XS text with core flags, for F<Core.xs>. | ||
| 747 | |||
| 748 | =cut | ||
| 749 | |||
| 750 | my %flags = ( | ||
| 751 | hdrcpy => { set => 1 }, | ||
| 752 | fflows => { FLAG => "DATAFLOW_F" }, | ||
| 753 | bflows => { FLAG => "DATAFLOW_B" }, | ||
| 754 | is_inplace => { FLAG => "INPLACE", postset => 1 }, | ||
| 755 | donttouch => { FLAG => "DONTTOUCHDATA" }, | ||
| 756 | allocated => { }, | ||
| 757 | vaffine => { FLAG => "OPT_VAFFTRANSOK" }, | ||
| 758 | anychgd => { FLAG => "ANYCHANGED" }, | ||
| 759 | dimschgd => { FLAG => "PARENTDIMSCHANGED" }, | ||
| 760 | tracedebug => { FLAG => "TRACEDEBUG", set => 1}, | ||
| 761 | ); | ||
| 762 | #if ( $bvalflag ) { $flags{baddata} = { set => 1, FLAG => "BADVAL" }; } | ||
| 763 | |||
| 764 | sub generate_core_flags { | ||
| 765 | # access (read, if set is true then write as well; if postset true then | ||
| 766 | # read first and write new value after that) | ||
| 767 | # to piddle's state | ||
| 768 | foreach my $name ( sort keys %flags ) { | ||
| 769 | my $flag = "PDL_" . ($flags{$name}{FLAG} || uc($name)); | ||
| 770 | if ( $flags{$name}{set} ) { | ||
| 771 | print <<"!WITH!SUBS!"; | ||
| 772 | int | ||
| 773 | $name(x,mode=0) | ||
| 774 | pdl *x | ||
| 775 | int mode | ||
| 776 | CODE: | ||
| 777 | if (items>1) | ||
| 778 | { setflag(x->state,$flag,mode); } | ||
| 779 | RETVAL = ((x->state & $flag) > 0); | ||
| 780 | OUTPUT: | ||
| 781 | RETVAL | ||
| 782 | |||
| 783 | !WITH!SUBS! | ||
| 784 | } elsif ($flags{$name}{postset}) { | ||
| 785 | print <<"!WITH!SUBS!"; | ||
| 786 | int | ||
| 787 | $name(x,mode=0) | ||
| 788 | pdl *x | ||
| 789 | int mode | ||
| 790 | CODE: | ||
| 791 | RETVAL = ((x->state & $flag) > 0); | ||
| 792 | if (items>1) | ||
| 793 | { setflag(x->state,$flag,mode); } | ||
| 794 | OUTPUT: | ||
| 795 | RETVAL | ||
| 796 | |||
| 797 | !WITH!SUBS! | ||
| 798 | } else { | ||
| 799 | print <<"!WITH!SUBS!"; | ||
| 800 | int | ||
| 801 | $name(self) | ||
| 802 | pdl *self | ||
| 803 | CODE: | ||
| 804 | RETVAL = ((self->state & $flag) > 0); | ||
| 805 | OUTPUT: | ||
| 806 | RETVAL | ||
| 807 | |||
| 808 | !WITH!SUBS! | ||
| 809 | } | ||
| 810 | } # foreach: keys %flags | ||
| 811 | } | ||
| 812 | |||
| 813 | =head2 generate_badval_init | ||
| 814 | |||
| 815 | =for ref | ||
| 816 | |||
| 817 | prints on C<STDOUT> XS text with badval initialisation, for F<Core.xs>. | ||
| 818 | |||
| 819 | =cut | ||
| 820 | |||
| 821 | sub generate_badval_init { | ||
| 822 | for my $type (PDL::Types::types()) { | ||
| 823 | my $typename = $type->ctype; | ||
| 824 | $typename =~ s/^PDL_//; | ||
| 825 | my $bval = $type->defbval; | ||
| 826 | if ($PDL::Config{BADVAL_USENAN} && $type->usenan) { | ||
| 827 | # note: no defaults if usenan | ||
| 828 | print "\tPDL.bvals.$typename = PDL.NaN_$type;\n"; #Core NaN value | ||
| 829 | } else { | ||
| 830 | print "\tPDL.bvals.$typename = PDL.bvals.default_$typename = $bval;\n"; | ||
| 831 | } | ||
| 832 | } | ||
| 833 | # PDL.bvals.Byte = PDL.bvals.default_Byte = UCHAR_MAX; | ||
| 834 | # PDL.bvals.Short = PDL.bvals.default_Short = SHRT_MIN; | ||
| 835 | # PDL.bvals.Ushort = PDL.bvals.default_Ushort = USHRT_MAX; | ||
| 836 | # PDL.bvals.Long = PDL.bvals.default_Long = INT_MIN; | ||
| 837 | |||
| 838 | } | ||
| 839 | |||
| 840 | 1; |