The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.
# Build the whole PDL distribtuion

$::PP_VERBOSE = 0; # =1 makes PP waffle a lot
use Config;
use IO::File;

sub checkbuggysetup {
  # detect buggy Perl setups
  if (!$forcebuild &&
      $Config{osname} eq 'solaris' &&
      ($Config{cc} =~ /gcc/ || $Config{gccversion} ne '') &&
      $Config{usemymalloc} eq 'y'
     ) {
    die <<'EOM';

FATAL BUG IN YOUR PERL SETUP DETECTED. BUILD TERMINATED.

On this platform the combination of gcc and Perl's malloc
are buggy. The optimizations lead to random coredumps
and make PDL essentially unusable.

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
WORKAROUND: YOU MUST RECOMPILE PERL WITH 'usemymalloc=n' !!!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

You may override and force the build by including the
'FORCE' switch:

  perl Makefile.PL FORCE

However, you will most likely end up with an unusable
PDL build unless *all* optimizations are disabled!
YOU HAVE BEEN WARNED!!

EOM
  }

  # check for red hat 5.8.0 problem described at
  # http://bugzilla.redhat.com/bugzilla/show_bug.cgi?id=87682
  if ($^V eq v5.8.0 &&
      $Config{config_args} =~ m/-Dcf_by=Red Hat/) {
    $redhat580problem = 1;
  }
}

sub getpdl_config {
  my ($pdl_conf_file) = @_;

  # First read in distribution config file
  # and convert y/n to 1/0
  #
  require './perldl.conf';
  foreach (values %PDL_CONFIG) { s/^y.*/1/i; s/^n.*/0/i; }

  %PDL_CONFIG_DIST = %PDL_CONFIG; # Save standard values

  # Now read in the users config file if specified
  # and convert y/n to 1/0
  #
  if (-f $pdl_conf_file) {
    warn "\nINFORMATION: using file $pdl_conf_file to set configuration defaults\n\n";
    require $pdl_conf_file;
    foreach(values %PDL_CONFIG){ s/^y.*/1/i; s/^n.*/0/i; }
  }

  # Sanity checking of user supplied keys (look for ones not defined in dist)

  for(keys %PDL_CONFIG) {
    if(!exists($PDL_CONFIG_DIST{$_})) {
      die << "EOD";
Invalid key $_ found in user supplied $pdl_conf_file
  - this key appears to be no longer in use.
  Please review configuration options and check the comments in
  the file perldl.conf that came with this distribution
EOD
    }
  }

  # Merge in default options where not supplied in users file

  for(keys %PDL_CONFIG_DIST) {
    $PDL_CONFIG{$_} = $PDL_CONFIG_DIST{$_} unless exists $PDL_CONFIG{$_};
  }
}

sub check_f77conf {
  my ($seen_f77conf) = @_;
  if ($seen_f77conf) {
    eval 'require File::Spec';
    unless ($@ eq "") {
      print STDERR "can't load File::Spec, skipping f77conf\n";
      # skip if we don't have File::Spec
      return 0;
    }
    $pdl_f77conf = File::Spec->rel2abs($pdl_f77conf)
      unless File::Spec->file_name_is_absolute($pdl_f77conf);
    $PDL_CONFIG{F77CONF} = $pdl_f77conf;
    return 1;
  } else { return 0 }
}

sub make_Version_pm {
  # Get Version from Basic/PDL.pm and generated Basic/Core/Version.pm from it
  require 'Basic/PDL.pm';

  my $versionFile = 'Basic/Core/Version.pm';
  my $fh = IO::File->new( ">$versionFile" ) or 
    die("Can't Open '$versionFile' for Writing!\n");
  print $fh <<"EOVF";

package PDL::Version;

#  This File was autogenerated by MakeFile.PL from the version
#  number in PDL.pm. It is used by PDL::Lite and others as
#  a single, consistent place to get the current PDL version.


\$VERSION='$PDL::VERSION';

1;

EOVF
  $fh->close();
}

sub make_badval_dependencies {
  # Are we using bad values or not? Are we using NaN or not?
  # NOTE:	
  #  only create if there's been a change (or the file doesn't exist)
  #  since *.pd uses this as a dependency
  #
  my $badFile = "Basic/Core/badsupport.p";
  my $create_badFile = 1;

  if ( -e $badFile ) {
    require $badFile;
    if ( $bvalflag == $PDL_CONFIG{WITH_BADVAL} and
	 $usenan   == $PDL_CONFIG{BADVAL_USENAN} ) {
      $create_badFile = 0;
    }
  }

  if ( $create_badFile ) {
    my $fh = IO::File->new( ">$badFile" )
      or die "Can't open '$badFile' for writing.!\n";
    print $fh "# Autogenerated by top-level Makefile.PL ".(localtime)."\n";
    print $fh "\$bvalflag = $PDL_CONFIG{WITH_BADVAL};\n";
    print $fh "\$usenan   = $PDL_CONFIG{BADVAL_USENAN};\n";
    print $fh "1;  # return true\n";
    $fh->close;
  }
}

sub make_Types_pm {
  # make sure we have Types.pm ready for prime time
  die "Types.pm.PL not found in Basic/Core"
    unless -f 'Basic/Core/Types.pm.PL';
  my $usebvals = $PDL_CONFIG{WITH_BADVAL} ? "BADVALS=1" : "";
  system "$Config{perlpath} Basic/Core/Types.pm.PL $usebvals";
  die "error creating Basic/Core/Types.pm" unless -f 'Basic/Core/Types.pm';
}

# very simple formatter, assumes structures are *not* nested
# used by make_PDL_Config_pm
sub myformat {
  my $entry = shift;
  if (ref $entry eq 'ARRAY') {
    my $list = join ',', (map {('"'.quotemeta($_).'"')} @$entry);
    return "[$list]";
  } elsif (ref $entry eq 'HASH') {
    my $list = join ",\n", (map {('"'.quotemeta($_).'" => "'.
			       quotemeta($entry->{$_}).'"')} keys %$entry);
    $list = "\n$list\n\t\t" unless $list =~ /^\s*$/;
    return "{$list}";
  } else {
     return join '', '"',quotemeta($PDL_CONFIG{$_}),'"';
  }
}

sub make_PDL_Config_pm {
  print STDERR "Writing Basic/Core/Config.pm\n";

  my $fh = IO::File->new( ">Basic/Core/Config.pm" )
    or die "Couldn't open Config.pm for writing";
  print $fh "
# AUTOMATICALLY GENERATED BY THE PDL TOPLEVEL Makefile.PL.
# DO NOT HAND-EDIT - CHANGES WILL BE LOST UPON YOUR NEXT
#  'perl Makefile.PL'!!!
package PDL;
\%PDL::Config = (\n";
  for(keys %PDL_CONFIG) {
    $fh->print( "\t$_\t=>\t" );
    if(defined $PDL_CONFIG{$_}) {
      $fh->print( myformat($PDL_CONFIG{$_}) );
    } else {
      $fh->print( "undef" );
    }
    $fh->print(",\n");
  }
  $fh->print( ");\n1;" );
  $fh->close();
}

#
# the actual script begins here
#

BEGIN{
  # Version test.
  eval "use 5.6.0";
  die "\nPDL requires Perl v5.6.0 or later\n\n" if $@ ne "";
  # test for critical modules
  @hasnt = ();
  my @test = (['Filter::Util::Call','Filter'],
	      ['Text::Balanced','Text::Balanced'],
	     );
  for my $mod (@test) {
    eval "use $mod->[0]";
    push @hasnt, $mod->[1] if $@;
  }
} # end BEGIN

$seen_pdlconf = 0;

# Scan ARGV for config file argument
@ARGV = map {
	if(/^PDLCONF=(.*)$/) {
		$seen_pdlconf=1;
		$pdl_conf_file = $1; ();
	} elsif (/^F77CONF=(.*)$/) {
		$seen_f77conf=1;
		$pdl_f77conf=$1; ();
	} elsif (/^FORCE$/i) {
		$forcebuild=1;
		();
        } else {
		$_
	}
} @ARGV;

warn "WARNING: forcing build...\n" if $forcebuild;

checkbuggysetup(); # check for buggy Perl setups

if(!$seen_pdlconf) {
	my $defname = "$ENV{HOME}/.perldl.conf";
	if(-f $defname) {
		$pdl_conf_file = $defname;
	}
}

# needs to be called before any of the make_XX routines
getpdl_config($pdl_conf_file);
$seen_f77conf = check_f77conf($seen_f77conf);

make_Version_pm();

make_badval_dependencies();

make_Types_pm();

use ExtUtils::MakeMaker;

# only perform one test if required modules are missing
# the test will print an informational message and fail
my %notestsifmodulesmissing = @hasnt ? # are any required modules missing ?
  (test => {TESTS => 't/requiredmods.t'}) : ();

my @podpms = map { $_.".pod", '$(INST_LIBDIR)/PDL/' . $_ .".pod"} 
  qw/perldl pdldoc/;

@prereq = (
	   File::Spec       => 0.6,
	   Filter::Util::Call => 0, # for PDL::NiceSlice
	   Text::Balanced     => 0, # for PDL::NiceSlice
	   Inline             => 0.43,
	  );
push @prereq, ('ExtUtils::F77' => 1.10) unless $seen_f77conf;

WriteMakefile(
	      'PREREQ_PM' => { @prereq },
	      'NAME' => 'PDL',
	      'VERSION_FROM' => 'Basic/Core/Version.pm',
	      'EXE_FILES' => ['perldl', 'pdldoc'],
	      'PM' => { @podpms }, # so that the script docs are picked up
	      'MAN3PODS' => {}, # don't pick up the script pods again
	      'OPTIMIZE'  => $PDL_CONFIG{OPTIMIZE} || $Config{optimize},
	      'linkext'  => {LINKTYPE => ''},  # No linking required
                                               # in this directory
	      'dist'     => { COMPRESS => 'gzip', SUFFIX => 'gz'},
	      'clean' => {
		  'FILES' => 'perldl pdldoc pdldoc.db pdldoc.pod perldl.pod ' .
		      'tbyte.tif tmp0 tmp0.hdr tushort.tif ' .
		      'MANIFEST.bak tmp1* tmpraw* t/tmpraw* t/tmp1* ' .
                      '_Inline/'
		      },
	      'realclean' => {'FILES' => 'Basic/Core/Config.pm'},
	      ($] ge '5.005') ? (
				 'AUTHOR' => 'PerlDL Developers (perldl@jach.hawaii.edu)',
				 'ABSTRACT' => 'Perl Data Language',
				 'BINARY_LOCATION' => 'PDL.tar.gz',
				 ) : (),

	      %notestsifmodulesmissing,
	      );


# do *after* WriteMakefile since some options
# are set by the recursively called Makefile.PLs
make_PDL_Config_pm(); # write out config to PDL::Config

# Extra build target to build the doc database
sub MY::postamble {
my $text =
'
doctest ::
	@echo "doctest: Building PDL documentation database in blib ..."
	@$(PERL) -I$(INST_ARCHLIB) -I$(INST_LIB) \
		Doc/scantree.pl 
	@$(PERL)  -I$(INST_ARCHLIB) -I$(INST_LIB) \
		Doc/mkhtmldoc.pl 

doc_site_install ::
	@echo "doc_site_install: Building PDL documentation database ..."
	@$(PERL) -Mblib Doc/scantree.pl $(INSTALLSITEARCH)
	@$(PERL) Doc/mkhtmldoc.pl $(INSTALLSITEARCH)/PDL

doc_perl_install ::
	@echo "doc_perl_install: Building PDL documentation database ..."
	@$(PERL) -Mblib Doc/scantree.pl $(INSTALLARCHLIB)
	@$(PERL) Doc/mkhtmldoc.pl $(INSTALLARCHLIB)/PDL

';

$text .=
"
perldl.pod : perldl subdirs
\tpodselect perldl > perldl.pod

pdldoc.pod : pdldoc subdirs
\tpodselect pdldoc > pdldoc.pod

";

$text .= << 'EOT' if $^O =~ /MSWin/;

DISTWIN32NAME=$(DISTVNAME)-win32

ppm: doctest ppd
	$(MV) blib/lib/PDL/HtmlDocs/PDL blib/html/lib/PDL
	$(COMPRESS) -dc win32/pbmwin32.tar.gz | $(TAR) xf -
	$(MKPATH) $(DISTWIN32NAME)
	$(CP) win32/Readme $(DISTWIN32NAME)
	$(CP) win32/install.ppm .
	$(PERL) -pe "s|</IMPLEMENTATION>|<INSTALL EXEC=\"perl\">install.ppm</INSTALL></IMPLEMENTATION>|" PDL.ppd > PDL.ppd.new
	$(RM) PDL.ppd
	$(MV) PDL.ppd.new PDL.ppd
	$(CP) PDL.ppd $(DISTWIN32NAME)
	$(TAR) cf $(DISTWIN32NAME)/PDL.tar blib install.ppm
	cd $(DISTWIN32NAME)
	$(COMPRESS) PDL.tar
	$(ZIP) $(DISTWIN32NAME).zip *
	$(MV) $(DISTWIN32NAME).zip ..
	cd ..
	$(RM_RF) $(DISTWIN32NAME)
EOT

return $text

}

sub MY::test {
        package MY; # so that "SUPER" works right
        my $inherited = shift->SUPER::test(@_);
	my @lines = split "\n", $inherited;
	my @new = ();
	my $pass = 1;
	foreach (@lines) {
		if ($pass) {
			push @new, $_;
			$pass = 0 if /^test\b/;
		} else {
			if (/^\s*$/) {
				push @new, '';
				$pass = 1;
			}
		}
	}
	return join("\n", @new,"\n");
}

# warn if vital modules are missing
END {
    if (@hasnt) {
      print << 'EOP';

********************************************************
* IMPORTANT: Your installation will not work since it  *
* lacks critical modules.                              *
* ALL TESTS WILL FAIL UNLESS YOU IMMEDIATELY           *
* INSTALL THE FOLLOWING MODULES [available from CPAN]: *
*
EOP

    for (@hasnt) { print "*\t$_\n" }


    print << 'EOP';
*                                                      *
* Please install the missing module(s) and start the   *
* PDL build process again (perl Makefile.PL; ....)     *
*                                                      *
********************************************************

EOP

  }

  if ($redhat580problem) {
    print << "EOP";

************************************************************
* IMPORTANT: You seem to be on a redhat system with        *
* a Perl 5.8.0 installation. Your Perl installation may be *
* broken and generate broken makefiles                     *
* see                                                      *
*                                                          *
* http://bugzilla.redhat.com/bugzilla/show_bug.cgi?id=87682
*                                                          *
* for details and workarounds.                             *
* In particular check the setting of the LANG environment  *
* variable:                                                *
*             current setting LANG=$ENV{LANG}              *
************************************************************

EOP
  }
}