# where to find psql for running the tests
 PSQLDIR = $(bindir)
 
+# where to find xsubpp for building XS.
+XSUBPPDIR = $(shell $(PERL) -e 'use List::Util qw(first); print first { -r "$$_/ExtUtils/xsubpp" } @INC')
+
 include $(top_srcdir)/src/Makefile.shlib
 
 plperl.o: perlchunks.h plperl_opmask.h plperl_helpers.h
 
 SPI.c: SPI.xs
    @if [ x"$(perl_privlibexp)" = x"" ]; then echo "configure switch --with-perl was not specified."; exit 1; fi
-   $(PERL) $(perl_privlibexp)/ExtUtils/xsubpp -typemap $(perl_privlibexp)/ExtUtils/typemap $< >$@
+   $(PERL) $(XSUBPPDIR)/ExtUtils/xsubpp -typemap $(perl_privlibexp)/ExtUtils/typemap $< >$@
 
 Util.c: Util.xs
    @if [ x"$(perl_privlibexp)" = x"" ]; then echo "configure switch --with-perl was not specified."; exit 1; fi
-   $(PERL) $(perl_privlibexp)/ExtUtils/xsubpp -typemap $(perl_privlibexp)/ExtUtils/typemap $< >$@
+   $(PERL) $(XSUBPPDIR)/ExtUtils/xsubpp -typemap $(perl_privlibexp)/ExtUtils/typemap $< >$@
 
 
 install: all install-lib install-data
 
 use Solution;
 use Cwd;
 use File::Copy;
+use Config;
+use List::Util qw(first);
 
 use Exporter;
 our (@ISA, @EXPORT_OK);
             (my $xsc = $xs) =~ s/\.xs/.c/;
             if (Solution::IsNewer("$plperlsrc$xsc","$plperlsrc$xs"))
             {
+                my $xsubppdir = first { -e "$_\\ExtUtils\\xsubpp.BAT" } @INC;
                 print "Building $plperlsrc$xsc...\n";
                 system( $solution->{options}->{perl}
                       . '/bin/perl '
                       . $solution->{options}->{perl}
-                      . '/lib/ExtUtils/xsubpp -typemap '
+                      . "$xsubppdir/ExtUtils/xsubpp -typemap "
                       . $solution->{options}->{perl}
                       . '/lib/ExtUtils/typemap '
                       . "$plperlsrc$xs "