1# Copyright (C) 2000-2016 Hajimu UMEMOTO <ume@mahoroba.org>.
2# All rights reserved.
3#
4# Redistribution and use in source and binary forms, with or without
5# modification, are permitted provided that the following conditions
6# are met:
7# 1. Redistributions of source code must retain the above copyright
8#    notice, this list of conditions and the following disclaimer.
9# 2. Redistributions in binary form must reproduce the above copyright
10#    notice, this list of conditions and the following disclaimer in the
11#    documentation and/or other materials provided with the distribution.
12# 3. Neither the name of the project nor the names of its contributors
13#    may be used to endorse or promote products derived from this software
14#    without specific prior written permission.
15#
16# THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
17# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19# ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
20# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26# SUCH DAMAGE.
27
28# $Id: Makefile.PL 659 2016-03-17 06:20:36Z ume $
29
30use Config;
31use ExtUtils::MakeMaker;
32
33if ($^O eq 'MSWin32') {
34    configure_mswin32();
35} else {
36    local $ENV{TMPDIR} = $ENV{TMPDIR};
37    my $path_perl = ($^X =~ m!^/!o) ? $^X : `which $^X`;
38    my $cmd = "CC='$Config{cc}' CFLAGS='$Config{ccflags}'";
39    if ($^O eq 'android') {
40        # Work around a bug in Android's sh:
41        # http://stackoverflow.com/questions/15283220/android-shell-eof
42        $ENV{TMPDIR} ||= File::Spec->tmpdir();
43
44        # /bin/sh doesn't exist on Android, point it to the right
45        # location for sh
46        $cmd .= " CONFIG_SHELL='$Config{sh}'";
47
48        # Call './configure' as 'sh ./configure'
49        $cmd .= " $Config{sh}";
50    }
51    $cmd .= " ./configure";
52    $cmd .= " --with-perl=$path_perl" if ($path_perl);
53    system($cmd);
54}
55
56require './config.pl';
57
58WriteMakefile(
59    NAME	 => 'Socket6',
60    VERSION_FROM => 'Socket6.pm',
61    XSPROTOARG	 => '-noprototypes',	# XXX remove later?
62    PM		 => {'Socket6.pm' => '$(INST_LIBDIR)/Socket6.pm'},
63    CONFIGURE	 => sub { { CC => "${cc}" } },
64    LIBS	 => ["${libs}"],
65    realclean	 => {FILES => 'config.cache config.h config.log config.pl config.status gailookup.pl'},
66);
67
68sub configure_mswin32
69{
70    open(IN, '<config.pl.in') || die "Failed to open file 'config.pl.in' [$!]";
71    open(OUT, '>config.pl')   || die "Failed to open file 'config.pl' [$!]";
72
73    while (<IN>) {
74	if (/\$cc/) {
75	    printf(OUT "\$cc = '%s';\n", $Config::Config{cc});
76	} elsif (/\$libs/) {
77	    printf(OUT "\$libs = 'ws2_32.lib';\n");
78	} else {
79	    print OUT;
80	}
81    }
82
83    close(OUT) || die "Failed to close file 'config.pl' [$!]";
84    close(IN)  || die "Failed to close file 'config.pl.in' [$!]";
85
86    open(IN, '<config.h.in') || die "Failed to open file 'config.h.in' [$!]";
87    open(OUT, '>config.h')   || die "Failed to open file 'config.h' [$!]";
88
89    print(OUT "#ifndef NTDDI_LONGHORN\n");
90    print(OUT "#  define NTDDI_LONGHORN 0x06000000\n");
91    print(OUT "#endif\n");
92
93    while (<IN>) {
94	if (/HAVE_PL_SV_UNDEF/) {
95	    print(OUT "#define HAVE_PL_SV_UNDEF 1\n");
96	} elsif (/HAVE_GETADDRINFO/) {
97	    print(OUT "#define HAVE_GETADDRINFO 1\n");
98	} elsif (/HAVE_GETNAMEINFO/) {
99	    print(OUT "#define HAVE_GETNAMEINFO 1\n");
100	} elsif (/HAVE_SOCKADDR_IN6_SIN6_SCOPE_ID/) {
101	    print(OUT "#define HAVE_SOCKADDR_IN6_SIN6_SCOPE_ID 1\n");
102        } elsif (/HAVE_INET_(PTON|NTOP)/) {
103	    print(OUT "#if (NTDDI_VERSION >= NTDDI_LONGHORN)\n");
104	    print(OUT "  #define HAVE_INET_$1 1\n");
105	    print(OUT "#else\n");
106	    print(OUT "  #undef HAVE_INET_$1\n");
107	    print(OUT "#endif\n");
108	} elsif (/HAVE_SOCKLEN_T/) {
109	    print(OUT "#define HAVE_SOCKLEN_T 1\n");
110	} else {
111	    print OUT;
112	}
113    }
114
115    close(OUT) || die "Failed to close file 'config.h' [$!]";
116    close(IN)  || die "Failed to close file 'config.h.in' [$!]";
117}
118
119package MY;
120sub install {
121    my $inherited = shift->SUPER::install(@_);
122    $inherited =~ s/(install :: (all )?pure_install) doc_install/$1/;
123    $inherited;
124}
125