summaryrefslogtreecommitdiff
path: root/Makefile.PL
blob: e29019d5abdda0430c0ead5ea5b503db9e670cc0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
# Makefile.PL for Perl module WWW::Curl
# Check out the README file for more information.

use inc::Module::Install;

name			'WWW-Curl';
abstract		'Perl extension interface for libcurl';
author			'Cris Bailiff <c.bailiff+curl at devsecure.com>';
license			'MPL or MIT/X-derivate';
perl_version		'5.006001';
no_index		directory => 'template';
repository		'http://github.com/szbalint/WWW--Curl';
# This is a hack. If you have libcurl installed, just specify curl.h below
# and comment out this line.
if ($^O ne 'MSWin32') {
    if (!$ENV{CURL_CONFIG}) {
	    requires_external_bin	'curl-config';
    }
} else {
	print	"Sorry, no automated install is available on Windows,\n".
		"please see the README.Win32 file on instructions for a manual install.\n";
	exit(0);
}

my $curl_config = $ENV{CURL_CONFIG} || 'curl-config';

my $vernum = `${curl_config} --vernum`; chomp $vernum;
my $version = `${curl_config} --version`; chomp $version; 

my $minimum_ver = hex("070a08");

if ($vernum && hex($vernum) <= $minimum_ver) {
	print	"Your currently installed libcurl version - $version - is too old.\n".
		"This module doesn't seek compatibility with versions older than 7.10.8\n".
		"Proceed manually if you know what you're doing.\n";
	exit(0);
}

print "The version is $version\n";

my @includes = qw();
my ($cflags,$lflags, $ldflags) = ('','','');

# You may need to specify where to find curl.h on your platform
# These are guesses only, in case curl-config is not telling us.

if ($^O ne 'MSWin32') {
	push @includes, qw(
		/usr/include
		/usr/local/curl/include
		/usr/local/include/curl
		../../include
		../curl/include
	);
}

#
# Get curl to tell us where it is, if we can.
#

if ($^O ne 'MSWin32') {
	$cflags = `${curl_config} --cflags`;
	$lflags = `${curl_config} --libs`;
}

# can't find link flags, make some guesses
if (!defined($lflags)) {
    $lflags="-lcurl";
    print "Guessing your linker flags as: $lflags\n";
}

my ($flag) = ($cflags =~ m/-I(\S+)/);

if (defined $flag) {
    unshift @includes, $flag;    # first guess
}

# try the path given on the command line, if any
if (defined($ARGV[0])) {
    unshift @includes, $ARGV[0];
};

my $curl_d = "";
my $curl_h;

# otherwise try a list of common locations
foreach my $try (@includes) {
    if (-f $try . "/curl/curl.h") {
        $curl_d = $try;
        $curl_h = $try . "/curl/curl.h";
        last;
    }
}

if (!defined($curl_h)) {
     die "Cannot find curl.h - cannot build constants files  - see Makefile.PL";
} else {
     my %constants;
     $curl_d = "-I" . $curl_d;
     print "Found curl.h in $curl_h\n";
     my @syms;
     my $has_cpp = 0;
     open(H_IN, "-|", "cpp", $curl_h) and $has_cpp++;
     unless ($has_cpp) {
         warn "No working cpp ($!).  Parsing curl.h in Perl";
         open(H_IN, "<", $curl_h) or die("Can't open curl.h at path $curl_h, because: ".$!);
     }
     while ( <H_IN> ) {
         if ( /enum\s+(\S+\s+)?{/ .. /}/ ) {
             s/^\s+//;
             next unless /^CURL/;
             chomp;
             s/^(CURL[A-Za-z0-9_]*_) ([A-Za-z0-9_])/$1$2/;  # Strip intervening space from ISO CPP macro
             s/[,\s].*//;
             s/=.*$//;
             next unless /^\w+$/;
             push @syms, $_;
         }
    }
    close H_IN;
    open (H, "<", $curl_h) or die ("Cannot open $curl_h: ".$!);
    while(<H>) {
        if (/^#define (CURL[A-Za-z0-9_]*)/) {
            push @syms, $1;
        }
    }
    close H;

    for my $e (sort @syms) {
       if($e =~ /(OBSOLETE|^CURL_EXTERN|_LAST\z|_LASTENTRY\z)/) {
          next;
       }
       my ($group) = $e =~ m/^([^_]+_)/;
       $constants{$group}->{$e} = $e;
    }
    unless ($has_cpp) {
    open(CURL_H, "<" . $curl_h) or die "Can't open curl.h\n";
    while (<CURL_H>) {
        if ($_ =~ m/CINIT\(/ and $_ !~ m/#/) {
            my ($option, $type, $code) = m/.*CINIT\((\w*)\s*,\s*(\w+)\s*,\s*(\d+).*/;
	    $constants{"CURLOPT_"}->{"CURLOPT_".$option} = "CURLOPT_".$option;
	} elsif ($_ =~ m/^#define CURLOPT_\w+\s+CURLOPT_\w+/) {
            my ($option, $value) =
                m/^#define CURLOPT_(\w+)\s+CURLOPT_(\w+)/;
	    $constants{"CURLOPT_"}->{"CURLOPT_".$option} = "CURLOPT_".$value;
        } elsif ($_ =~ m/^\s*((CURLINFO_|CURLSHOPT_|CURLE_)\w+)/) {
	    $constants{$2}->{$1}= $1;
        } elsif ($_ =~ m/^\s*((CURL_)(?:\w+))(?:[,\s]*)(?:\/\*.*)?$/) {
	    $constants{$2}->{$1} = $1;
        } elsif ($_ =~ m/^\s*((CURLPROXY_)(?:\w+))\s*=\s*\d+/) {
            $constants{$2}->{$1} = $1;
        } elsif ($_ =~ m/CFINIT\(/ and $_ !~ m/#/) {
            my ($option) = m/.*CFINIT\((\w*)\s*.*/;
	    $constants{"CURLFORM_"}->{"CURLFORM_".$option} = "CURLFORM_".$option;
        }
    }
    close(CURL_H);
    }
    print "Building curlopt-constants.c for your libcurl version\n";

    open(CURL_XS, ">curlopt-constants.c")
        or die "Can't write curlopt-constants.c\n";

    # boilerplate xs constant function here
    print CURL_XS <<HERE
static int
constant(const char *name)
{
    errno = 0;
HERE
        ;
for my $group (reverse sort keys %constants) {
	my $grouplength = length($group);
	my $groupref = $constants{$group};
	my @constants = keys %{$constants{$group}};
	print CURL_XS <<HERE2

    if (strncmp(name, "$group", $grouplength) == 0) {
        name += $grouplength;
        switch (*name) {
HERE2
        ;

    for my $next_initial ('A' .. 'Z') {
        print CURL_XS "        case '$next_initial':\n";
        my $count = 0;
        foreach my $option (sort @constants) {
            my $remainder = substr($option, length($group), length($option));
            my $initial = substr($remainder, 0, 1);
            if ($next_initial eq $initial) {

                print CURL_XS
"            if (strEQ(name, \"$remainder\")) return "."$groupref->{$option};\n";

                $count++;
            }
        }
        if ($count or $next_initial eq 'Z') {
            print CURL_XS "            break;\n";
        }
    }

    print CURL_XS "    };\n";
    print CURL_XS "    }\n";
}

    print CURL_XS <<HERE

    errno = EINVAL;
    return 0;
}
HERE
        ;

    close(CURL_XS);

    print "Building Easy.pm constants for your libcurl version\n";

    open(EASY_PM,    ">lib/WWW/Curl/Easy.pm")    or die "Can't create lib/WWW/Curl/Easy.pm\n";
    open(EASY_PM_IN, "template/Easy.pm.tmpl") or die "Can't read template/Easy.pm.tmpl\n";
    while (my $line = <EASY_PM_IN>) {
        if ($line !~ m/^\@CURLOPT_INCLUDE\@/) {
			print EASY_PM $line;
        } else {
	for my $group (reverse sort keys %constants) {	
            for my $option (sort keys %{$constants{$group}}) {
                next unless $option;
                print EASY_PM $option."\n";
            }
        }
	}
    }
    close(EASY_PM);
    close(EASY_PM_IN);

	print "Building Share.pm constants for your libcurl version\n";

    open(SHARE_PM,   ">lib/WWW/Curl/Share.pm")    or die "Can't create lib/WWW/Curl/Share.pm\n";
    open(SHARE_PM_IN, "template/Share.pm.tmpl") or die "Can't read template/Share.pm.tmpl\n";
    while (my $line = <SHARE_PM_IN>) {
        if ($line !~ m/^(.*?)\@CURLSHOPT_INCLUDE\@/) {
            print SHARE_PM $line;
        } else {
            foreach my $option (sort keys %{$constants{CURLSHOPT_}}) {
                print SHARE_PM $1 . $option . "\n";
            }
            foreach my $option (sort keys %{$constants{CURL_LOCK_}}) {
                print SHARE_PM $1 . $option . "\n";
            }
        }
    }
    close(SHARE_PM);
    close(SHARE_PM_IN);
}

# Let Module::Install generate META.yml and other necessary files.
WriteMakefile(
    'NAME'         => 'WWW::Curl',
    'VERSION_FROM' => 'lib/WWW/Curl.pm',      # finds $VERSION
    'LIBS'         => "$ldflags $lflags",        # e.g., '-lm'
    'INC'          => $curl_d,        # e.g., '-I/usr/include/other'
    'clean' => { FILES => "curlopt-constants.c head.out body.out" }
);