# Copyright (C) 2007, The Perl Foundation.
# $Id: /mirror/trunk/config/gen/makefiles/parrot_embed.in 34867 2009-01-02T21:07:34.725473Z allison  $
#
use Cwd;
use Config;
use File::Copy;
use ExtUtils::MakeMaker;

copy( 'lib/Parrot/Embed.xs', 'Embed.xs' );

# cross-platform commands and paths
my %config;
$config{CC}              = '@cc@';
$config{PARROTDIR}       = '../..';
$config{PARROT}          = '..@slash@..@slash@parrot@exe@';
$config{C_LIBS}          = '@libs@';
$config{INCLUDE}         = "$config{PARROTDIR}/include";
#UNLESS(win32):$config{ALL_PARROT_LIBS} = "@libparrot_ldflags@ $config{C_LIBS}";
$config{ABS_PARROTDIR}   = Cwd::realpath(
                               File::Spec->rel2abs( $config{PARROTDIR} ) );
$config{LDDLFLAGS}       = $Config{lddlflags};

if ($config{CC} eq 'cl')
{
    $config{LDDLFLAGS} .= qq| -libpath:"$config{ABS_PARROTDIR} "|
        .  File::Spec->catfile( $config{ABS_PARROTDIR}, 'libparrot.lib' );
}

WriteMakefile(
    'NAME'         => 'Parrot::Embed',
    'VERSION_FROM' => 'lib/Parrot/Embed.pm',
    'PREREQ_PM'    => { 'ExtUtils::CBuilder' => 0 },
#IF(win32):    'LIBS'         => [ $config{C_LIBS} ],
#ELSE:    'LIBS'         => [ $config{ALL_PARROT_LIBS} ],
#IF(win32):    'OBJECT'       => "@libparrot_ldflags@ Embed@o@",
    'INC'          => "-I$config{INCLUDE}",
    'PM'           => { map { $_ => "blib/$_" } <lib/Parrot/*pm> },
    'clean'        => { FILES => '*.xs t/greet.pbc' },
    'LDDLFLAGS'    => $config{LDDLFLAGS},
);

package MY;

sub postamble
{
    "t/greet.pbc:\n\t$config{PARROT} -o t/greet.pbc t/greet.pir\n";
}

sub test
{
    my $inherited   = shift->SUPER::test(@_);
    return $inherited unless $config{cc} eq 'cl';

    my $dynlib_path = "\tset PATH=%PATH%:$config{ABS_PARROTDIR}";
    $inherited      =~ s{^(test.*ic ::.*)$}{$1\n$dynlib_path}mg;

    return $inherited;
}

sub dynamic_lib
{
    my $inherited  = shift->SUPER::dynamic_lib(@_);
    my $sub_target = quotemeta( ': $(OBJECT)' );
    $inherited     =~ s{($sub_target)}{$1 t/greet.pbc};
    $inherited;
}
