pp_semop can fit within Perl_pp_shmwrite().
[p5sagit/p5-mst-13.2.git] / jpl / JNI / Makefile.PL
1 #!/usr/bin/perl
2 use ExtUtils::MakeMaker;
3 use Getopt::Std;
4 use Config;
5 $ARCHNAME = $Config{archname};
6 use File::Basename;
7
8 getopts('e'); # embedding?
9
10 $CCFLAGS .= $ENV{CCFLAGS} if defined $ENV{CCFLAGS};
11
12 # $USE_KAFFE is a boolean that tells us whether or not we should use Kaffe.
13 # Set by find_includes (it seemed as good a place as any).
14
15 # Note that we don't check to see the version of Kaffe is one we support.
16 #  Currently, the only one we support is the one from CVS.
17
18 my $USE_KAFFE = 0;
19
20 #require "JNIConfig";
21
22 if ($^O eq 'solaris') {
23     $LIBPATH = " -R$Config{archlib}/CORE -L$Config{archlib}/CORE";
24 } elsif ($^O eq 'MSWin32') {
25     $LIBPATH = " -L$Config{archlib}\\CORE";
26     # MSR - added MS VC++ default library path
27     # bjepson - fixed to support path names w/spaces in them.
28     push(@WINLIBS, (split"\;",$ENV{LIB}));
29     grep s/\\$//, @WINLIBS; # eliminate trailing \
30     grep s/\/$//, @WINLIBS; # eliminate trailing /
31     $LIBPATH .= join(" ", "", map { qq["-L$_" ] } @WINLIBS);
32 } else {
33     $LIBPATH = " -L$Config{archlib}/CORE";
34 }
35 #$LIBS = " -lperl";
36
37 # Figure out where Java might live
38 #
39 # MSR - added JDK 1.3
40 #
41
42 my @JAVA_HOME_GUESSES = qw(/usr/local/java /usr/java /usr/local/jdk117_v3
43                          C:\\JDK1.1.8 C:\\JDK1.2.1 C:\\JDK1.2.2 C:\\JDK1.3 );
44
45 my @KAFFE_PREFIX_GUESSES = qw(/usr/local /usr);
46
47 if (! defined $ENV{JAVA_HOME}) {
48         print "You didn't define JAVA_HOME, so I'm trying a few guesses.\n";
49         print "If this fails, you might want to try setting JAVA_HOME and\n";
50         print "running me again.\n";
51 } else {
52         @JAVA_HOME_GUESSES = ( $ENV{JAVA_HOME} );
53 }
54
55 if (! defined $ENV{KAFFE_PREFIX}) {
56         print "\nYou didn't define KAFFE_PREFIX, so I'm trying a few guesses.",
57           "\nIf this fails, and you are using Kaffe, you might want to try\n",
58           "setting KAFFE_PREFIX and running me again.\n",
59           "If you want to ignore any possible Kaffe installation, set the\n",
60           "KAFFE_PREFIX to and empty string.\n\n";
61 } else {
62         @KAFFE_PREFIX_GUESSES = ($ENV{KAFFE_PREFIX} eq "") ? () :
63                                                        ( $ENV{KAFFE_PREFIX} );
64 }
65
66 my(@KAFFE_INCLUDE_GUESSES, @KAFFE_LIB_GUESSES);
67 foreach my $kaffePrefix (@KAFFE_PREFIX_GUESSES) {
68      push(@KAFFE_INCLUDE_GUESSES, "$kaffePrefix/include/kaffe");
69      push(@KAFFE_LIB_GUESSES, "$kaffePrefix/lib");
70      push(@KAFFE_LIB_GUESSES, "$kaffePrefix/lib/kaffe");
71 }
72     $guess .= "/include/kaffe";
73
74 # Let's find out where jni.h lives
75 #
76 my @INCLUDE = find_includes();
77
78 if ($^O eq 'MSWin32') {
79   # MSR - added MS VC++ default include path
80   push(@INCLUDE,(split"\;",$ENV{INCLUDE}));
81   grep s/\\$//, @INCLUDE; # remove trailing \
82   grep s/\/$//, @INCLUDE; # remove trailing \
83   $INC = join("", map { qq["-I$_" ] } @INCLUDE);
84
85 } else {
86   $INC = join(" -I", ("", @INCLUDE));
87 }
88
89 # Let's find out the name of the Java shared library
90 #
91 my @JAVALIBS = find_libs();
92
93 # Find out some defines based on the library we are linking to
94 #
95 foreach (@JAVALIBS) {
96     if ( $^O eq 'MSWin32') { # We're on Win32
97         $INC =~ s#/#\\#g;
98         $INC =~ s#\\$##;
99           print $INC, "\n";
100         $CCFLAGS .= " -DWIN32 -Z7 -D_DEBUG";
101         $MYEXTLIB = "$libjava";
102       }
103 }
104
105 $CCFLAGS .= " -DKAFFE" if ($USE_KAFFE);
106
107 # Let's find out the path of the library we need to link against.
108 #
109 foreach (@JAVALIBS) {
110     if ($^O eq 'MSWin32') { # We're on Win32
111         $_ =~ s#/#\\\\#g;
112     }
113     my ($libname, $libpath, $libsuffix) = fileparse($_, ("\.so", "\.lib"));
114     $libname =~ s/^lib//;
115     if ($^O eq 'solaris') {
116         $LIBPATH .= " -R$libpath -L$libpath"
117     } else {
118         $LIBPATH .= " -L$libpath"
119     }
120     $LIBS .= " -l$libname";
121 }
122
123 # Do we need -D_REENTRANT?
124 if ($LIBPATH =~ /native/) {
125     print "Looks like native threads...\n";
126     $CCFLAGS .= " -D_REENTRANT";
127 }
128
129 if ($opt_e) {
130     print "We're embedding Perl in Java via libPerlInterpreter.so.\n";
131     eval `../setvars -perl`;
132     $CCFLAGS .= " -DEMBEDDEDPERL";
133     $LIBPATH .= " -R$ENV{JPL_HOME}/lib/$ARCHNAME -L$ENV{JPL_HOME}/lib/$ARCHNAME";
134     $LIBS    .= " -lPerlInterpreter";
135 }
136
137 # Needed for JNI.
138 if ($^O eq 'solaris') {
139     $LIBS = " -lthread -lc $LIBS"; #-lthread must be first!!!
140     $CCFLAGS .= " -D_REENTRANT";
141 }
142
143 # MSR - clean up LIBS
144 $LIBS =~ s/-l$//;
145
146 #
147 # Next, build JNI/Config.pm.  This is a superfluous thing for the SUN and
148 # Microsoft JDKs, but absolutely necessary for Kaffe.  I think at some
149 # point, the Microsoft and SUN implementations should use JNI::Config, too.
150 #
151
152 if (! -d "JNI") {
153     mkdir("JNI", 0755) || die "Unable to make JNI directory: $!";
154 }
155 open(JNICONFIG, ">JNI/Config.pm") || die "Unable to open JNI/Config.pm: $!";
156
157 print JNICONFIG "# DO NOT EDIT!   Autogenerated by JNI/Makefile.PL\n\n",
158                 "package JNI::Config;\nuse strict;\nuse Carp;\n",
159                 "\nuse vars qw(\$KAFFE \$LIB_JAVA \$CLASS_HOME ",
160                 "\$LIB_HOME);\n\n",
161                 "\$KAFFE = $USE_KAFFE;\n\$LIB_JAVA = \"$JAVALIBS[0]\";\n";
162 if ($USE_KAFFE) {
163   my $path = $JAVALIBS[0];
164   $path =~ s%/(kaffe/)?libkaffevm.so$%%;
165
166   print JNICONFIG "\$LIB_HOME = \"$path/kaffe\";\n";
167   $path =~ s%/lib%%;
168   print JNICONFIG "\$CLASS_HOME = \"$path/share/kaffe\";\n";
169 }
170 print JNICONFIG "\n\n1;\n";
171 close JNICONFIG;
172
173
174 my %Makefile = (
175     NAME        => 'JNI',
176     VERSION_FROM => 'JNI.pm',
177     DEFINE        => '',
178     LINKTYPE => 'dynamic',
179     INC        => $INC,
180     CCFLAGS => "$Config{ccflags} $CCFLAGS", 
181     ($Config{archname} =~ /mswin32.*-object/i ? ('CAPI' => 'TRUE') : ()),
182
183     clean => {FILES => "JNI/* JNI"}
184 );
185
186 $Makefile{LIBS} = ["$LIBPATH $LIBS"];
187 if ($MYEXTLIB) {
188     $Makefile{MYEXTLIB} = $MYEXTLIB;
189 }
190
191 # See lib/ExtUtils/MakeMaker.pm for details of how to influence
192 # the contents of the Makefile that is written.
193 #
194 WriteMakefile(%Makefile);
195
196 if ($USE_KAFFE) {
197   my $path = $JAVALIBS[0];
198   $path =~ s%/libkaffevm.so$%%;
199   print "\n\n***NOTE: be sure to have:\n",
200             "              LD_LIBRARY_PATH=$path\n",
201             "         in your enviornment (or installed as a system dynamic\n",
202             "         library location) when you compile and run this.\n";
203 }
204
205 # subroutine to find a library
206 #
207 sub find_stuff {
208
209     my ($candidates, $locations) = @_;
210     my $lib;
211     $wanted = sub {
212         foreach my $name (@$candidates) {
213             if (/$name$/ and ! /green_threads/ and !/include-old/) {
214                     $lib = $File::Find::name;
215             }
216         }
217     };
218     
219     use File::Find;
220     foreach my $guess (@$locations) {
221         next unless -d $guess;
222         find (\&$wanted, $guess);
223     }
224     if (! $lib) {
225         print "Could not find @$candidates\n";
226     } else {
227         print "Found @$candidates as $lib\n\n";
228     }
229     return $lib;
230 }
231
232 # Extra lib for Java 1.2
233 #
234 #  if we want KAFFE, check for it, otherwise search for Java
235
236 sub find_libs {
237   my($libjava, $libawt, $libjvm);
238
239   if ($USE_KAFFE) {
240     $libjava = find_stuff(['libkaffevm.so'], \@KAFFE_LIB_GUESSES);
241     $libawt = find_stuff(['libawt.so'], \@KAFFE_LIB_GUESSES);
242   } else {
243     $libjava = find_stuff(['libjava.so', 'javai.lib', 'jvm.lib'],
244                              \@JAVA_HOME_GUESSES);
245     $libjvm  = find_stuff(['libjvm.so'],  \@JAVA_HOME_GUESSES);
246     $libawt  = find_stuff(['libawt.so'], \@JAVA_HOME_GUESSES);
247     if (defined $libjvm) { # JDK 1.2
248       my $libhpi  = find_stuff(['libhpi.so'], \@JAVA_HOME_GUESSES);
249       return($libjava, $libjvm, $libhpi, $libawt);
250     }
251   }
252   return($libjava, $libawt);
253 }
254
255 # We need to find jni.h and jni_md.h
256 #
257
258 # Always do find_includes as the first operation, as it has the side effect
259 # of deciding whether or not we are looking for Kaffe.  --bkuhn
260
261 sub find_includes {
262
263   my @CANDIDATES = qw(jni.h jni_md.h);
264   my @includes;
265
266   sub find_inc {
267     foreach my $name (@CANDIDATES) {
268       if (/$name$/) {
269         my ($hname, $hpath, $hsuffix) = 
270           fileparse($File::Find::name, ("\.h", "\.H"));
271         unless ($hpath =~ /include-old/) {
272           print "Found $hname$hsuffix in $hpath\n";
273           push @includes, $hpath;
274         } 
275       }
276     }
277   }
278     
279   use File::Find;
280   foreach my $guess (@KAFFE_INCLUDE_GUESSES) {
281     next unless -d $guess;
282     find (\&find_inc, $guess);
283   }
284   # If we have found includes, then we are using Kaffe.
285   if (@includes > 0) {
286     $USE_KAFFE = 1;
287   } else {
288     foreach my $guess (@JAVA_HOME_GUESSES) {
289       next unless -d $guess;
290       find (\&find_inc, $guess);
291     }
292   }
293   die "Could not find Java includes!" unless (@includes);
294
295   return @includes;
296 }
297