detect installed fcgi lib
[catagits/fcgi2.git] / perl / Makefile.PL
1 # $Id: Makefile.PL,v 1.28 2001/09/23 20:29:40 skimo Exp $
2
3 use ExtUtils::MakeMaker;
4 use IO::File;
5 use Config;
6 use Cwd 'cwd';
7 use Getopt::Long;
8
9 @h1 = qw(fastcgi.h fcgiapp.h fcgimisc.h fcgios.h);
10 @h = (@h1, 'fcgi_config.h');
11 @o = qw(FCGI.o);
12 @dist1 = qw(LICENSE.TERMS);
13 @dist2 = qw(fcgiapp.c os_unix.c os_win32.c);
14 @dist3 = (@h1, qw(fcgi_config_x86.h));
15
16 GetOptions ("pure-perl!" => \$pure,
17             "use-installed:s" => \$useinstalled);
18 $pure = "0" unless defined $pure;
19 open(CFG,">FCGI.cfg");
20 print CFG "\$pure = $pure;1;\n";
21 close CFG;
22
23 $libfound = 0;
24 @libs = ();
25
26 if (! $pure) {
27     my $cwd = cwd();
28     my $devkit = "$cwd/..";
29
30     if (defined $useinstalled) {
31         require ExtUtils::Liblist;
32         my $mm = MM->new({NAME => 'FCGI'});
33         my $libspec = $useinstalled ? "-L$useinstalled/lib " : "";
34         $libspec .= "-lfcgi";
35         my @l = $mm->ext($libspec);
36         if ($l[0] || $l[1] || $l[2]) {
37             $prefix = "$useinstalled/include" if $useinstalled;
38             $libfound = 1;
39             push @libs, $libspec;
40         }
41         # avoid duplicate check for complete kit
42         ExtUtils::MakeMaker::WriteEmptyMakefile() if ! -f "Makefile";
43     }
44     if (!$libfound && -d "$devkit/libfcgi" && -d "$devkit/include") {
45         # devkit
46         if (grep { ! -f "$devkit/include/$_" } @dist3 
47                 or grep { ! -f "$devkit/libfcgi/$_" } @dist2)
48         {
49                 warn "This appears to be a FastCGI devkit distribution, " .
50                      "but one or more FastCGI library files are missing. \n" .
51                      "Please check the integrity of the distribution.\n";
52                 exit -1;
53         }
54
55         my $extrarules = join "\n", 
56             map { $b = $_; $b =~ s/\.c$//; my $s="$devkit/libfcgi/$b.c"; 
57                   "$b\$(OBJ_EXT): $s\n\t".
58                   '$(CCCMD) $(CCCDLFLAGS) -I$(PERL_INC) $(DEFINE) '."$s\n"; }
59                 @dist2;
60         eval 'package MY; sub postamble { $extrarules; }';
61         $prefix = $devkit;
62     }
63 }
64
65 $sys = $^O eq 'MSWin32' ? 'win32' : 'unix';
66 push @o, "fcgiapp.o", "os_$sys.o" unless $libfound;
67 $inc = $prefix ? "-I$prefix/include" : '-I.';
68
69 push(@extras, CAPI => 'TRUE')
70      if ($] >= 5.005 and $^O eq 'MSWin32'
71         and $Config{archname} =~ /-object\b/i);
72
73 push(@extras,
74     ABSTRACT => 'Fast CGI module',
75     AUTHOR   => 'Sven Verdoolaege (skimo@kotnet.org)')
76         if ($ExtUtils::MakeMaker::VERSION >= 5.4301); 
77
78 $plfiles = { 'echo.PL' => 'echo.fpl', 
79              'remote.PL' => 'remote.fpl',
80              'threaded.PL' => 'threaded.fpl',
81              'FCGI.PL' => 'FCGI.pm',
82            };
83 $plfiles->{'FCGI.XL'} = 'FCGI.xs' unless $pure;
84 if ($pure) {
85     push @extras,
86         LINKTYPE => ' ';
87 } else {
88
89         if ("$sys" eq "win32") {
90                 push @libs, ":nosearch -lws2_32";
91                 push @extras, 'DEFINE' => '-DDLLAPI=__declspec(dllexport)';
92         }
93
94     push @extras,
95         'LIBS'  => [ "@libs" ],
96         'OBJECT'        => "@o",
97         'INC'   => $inc;
98 }
99         
100 # See lib/ExtUtils/MakeMaker.pm for details of how to influence
101 # the contents of the Makefile that is written.
102
103 # Work around bug in previous versions of MakeMaker
104 WriteMakefile(NAME => 'FCGI') 
105     if $ExtUtils::MakeMaker::VERSION <= 5.4302;
106
107 $mm = MM->new({
108     'NAME'              => 'FCGI',
109     'VERSION_FROM'      => 'version.pm',
110     'dist'              => { 'COMPRESS' => 'gzip -9f', 
111                              'SUFFIX' => 'gz',
112                              'PREOP' => '$(CP) '.join(' ',
113                                 map {"../$_"} @dist1,
114                                 (map {"libfcgi/$_"} @dist2),
115                                 map {"include/$_"} @dist3).' $(DISTVNAME);'.
116                                 '$(CP) MANIFEST MANIFEST.old;'.
117                                 'echo -e '. join('\\\n',@dist1,@dist2,@dist3) .
118                                 '>> $(DISTVNAME)/MANIFEST',
119                               'POSTOP' => 
120                                 '$(MV) MANIFEST.old MANIFEST',
121                             },
122     'clean'             => { FILES => 'config.cache fcgi_config.h' . 
123                                       ' FCGI.xs FCGI.c FCGI.cfg ' .
124                                       (join ' ', values %$plfiles)},
125     'PL_FILES'          => $plfiles,
126     PM                  => {'FCGI.pm' => '$(INST_ARCHLIBDIR)/FCGI.pm'},
127     @extras,
128 });
129 # don't install oldinterface pod
130 delete $mm->{MAN3PODS}{oldinterface.pod};
131 $mm->flush;
132
133 exit if -f 'fcgi_config.h' or $libfound or $pure;
134
135 # CPAN and no installed lib found
136 if ($sys eq "win32") {
137     # configure will almost certainly not run on a normal NT install,
138     # use the pregenerated configuration file
139
140     use File::Copy qw(copy);
141     print "Using prebuilt fcgi_config.h file for Windows\n";
142     unlink("fcgi_config.h");
143     die $! unless copy("fcgi_config_x86.h","fcgi_config.h");
144
145     # Win can't deal with existence of FCGI.xs or absence of FCGI.c
146     unlink("FCGI.xs");
147     open(F, ">FCGI.c"); close(F);
148     $now = time; $before = $now - 600;
149     utime $before, $before, "FCGI.c";
150     utime $now, $now, "FCGI.PL";
151 } else {
152     print "Running ./configure for you\n";
153     print "Please read configure.readme for information on how to run it yourself\n";
154
155     $ENV{'CC'} = $Config{'cc'};
156     system("$Config{sh} configure");
157 }