[ID 20001025.011] [PATCH] t/io/open.t perl@7369[ 7350] breaks VMS perl
[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 #require "JNIConfig";
11
12 if ($^O eq 'solaris') {
13     $LIBPATH = " -R$Config{archlib}/CORE -L$Config{archlib}/CORE";
14 } elsif ($^O eq 'MSWin32') {
15     $LIBPATH = " -L$Config{archlib}\\CORE";
16 } else {
17     $LIBPATH = " -L$Config{archlib}/CORE";
18 }
19 #$LIBS = " -lperl";
20
21 # Figure out where Java might live
22 #
23 my @JAVA_HOME_GUESSES = qw(/usr/local/java /usr/java /usr/local/jdk117_v3
24                            /usr/local/lib/kaffe C:\\JDK1.1.8
25                            C:\\JDK1.2.1 );
26 if (! defined $ENV{JAVA_HOME}) {
27         print "You didn't define JAVA_HOME, so I'm trying a few guesses.\n";
28         print "If this fails, you might want to try setting JAVA_HOME and\n";
29         print "running me again.\n";
30 } else {
31         @JAVA_HOME_GUESSES = ( $ENV{JAVA_HOME} );
32 }
33
34 # Let's find out where jni.h lives
35 #
36 my @INCLUDE = find_includes();
37 $INC = join(" -I", ("", @INCLUDE));
38
39 # Let's find out the name of the Java shared library
40 #
41 my @JAVALIBS = find_libs();
42
43 # Find out some defines based on the library we are linking to
44 #
45 foreach (@JAVALIBS) {
46     if ( /javai.lib$/ or /jvm.lib$/) { # We're on Win32
47         $INC =~ s#/#\\#g;
48         $INC =~ s#\\$##;
49         $CCFLAGS .= "-DWIN32 -Z7 -D_DEBUG";
50         $MYEXTLIB = $libjava;
51     } elsif (/libkaffevm.so$/) {
52         $CCFLAGS .= "-DKAFFE";
53     }
54 }
55
56 # Let's find out the path of the library we need to link against.
57 #
58 foreach (@JAVALIBS) {
59     if ( /javai.lib$/ or /jvm.lib$/) { # We're on Win32
60         $_ =~ s#/#\\\\#g;
61     }
62     my ($libname, $libpath, $libsuffix) = fileparse($_, ("\.so", "\.lib"));
63     $libname =~ s/^lib//;
64     if ($^O eq 'solaris') {
65         $LIBPATH .= " -R$libpath -L$libpath"
66     } else {
67         $LIBPATH .= " -L$libpath"
68     }
69     $LIBS .= " -l$libname";
70 }
71
72 # Do we need -D_REENTRANT?
73 if ($LIBPATH =~ /native/) {
74     print "Looks like native threads...\n";
75     $CCFLAGS .= " -D_REENTRANT";
76 }
77
78 if ($opt_e) {
79     print "We're embedding Perl in Java via libPerlInterpreter.so.\n";
80     eval `../setvars -perl`;
81     $CCFLAGS .= " -DEMBEDDEDPERL";
82     $LIBPATH .= " -R$ENV{JPL_HOME}/lib/$ARCHNAME -L$ENV{JPL_HOME}/lib/$ARCHNAME";
83     $LIBS    .= " -lPerlInterpreter";
84 }
85
86 # Needed for JNI.
87 if ($^O eq 'solaris') {
88     $LIBS = " -lthread -lc $LIBS"; #-lthread must be first!!!
89     $CCFLAGS .= " -D_REENTRANT";
90 }
91
92 my %Makefile = (
93     NAME        => 'JNI',
94     VERSION_FROM => 'JNI.pm',
95     DEFINE        => '',
96     LINKTYPE => 'dynamic',
97     INC        => $INC,
98     CCFLAGS => "$Config{ccflags} $CCFLAGS", 
99     ($Config{archname} =~ /mswin32.*-object/i ? ('CAPI' => 'TRUE') : ()),
100 );
101
102 $Makefile{LIBS} = ["$LIBPATH $LIBS"];
103 if ($MYEXTLIB) {
104     $Makefile{MYEXTLIB} = $MYEXTLIB;
105 }
106
107 # See lib/ExtUtils/MakeMaker.pm for details of how to influence
108 # the contents of the Makefile that is written.
109 #
110 WriteMakefile(%Makefile);
111
112 # subroutine to find a library
113 #
114 sub find_stuff {
115
116     my ($candidates, $locations) = @_;
117
118     my ($pos,$lib);
119     $wanted = sub {
120         foreach my $name (@$candidates) {
121             $pos = $File::Find::name;
122             if (/$name$/ && $pos !~ /green_threads/ && $pos !~ /include-old/) {
123                     $lib = $pos;
124             }
125         }
126     };
127     
128     use File::Find;
129     foreach my $guess (@$locations) {
130         next unless -d $guess;
131         find (\&$wanted, $guess);
132     }
133     if (! $lib) {
134         print "Could not find @$candidates\n";
135     } else {
136         print "Found @$candidates as $lib\n\n";
137     }
138     return $lib;
139 }
140
141 # Extra lib for Java 1.2
142 #
143 sub find_libs {
144
145     my $libjava = find_stuff(['libjava.so', 'libkaffevm.so', 'javai.lib', 'jvm.lib'],
146                         \@JAVA_HOME_GUESSES);
147     my $libjvm  = find_stuff(['libjvm.so'],  \@JAVA_HOME_GUESSES);
148     if ($libjvm) { # JDK 1.2
149         my $libhpi  = find_stuff(['libhpi.so'], \@JAVA_HOME_GUESSES);
150         my $libawt  = find_stuff(['libawt.so'], \@JAVA_HOME_GUESSES);
151         return($libjava, $libjvm, $libhpi, $libawt);
152     } else {
153         return($libjava);
154     }
155
156 }
157
158 # We need to find jni.h and jni_md.h
159 #
160 sub find_includes {
161
162     my @CANDIDATES = qw(jni.h jni_md.h);
163     my @includes;
164
165     sub find_inc {
166         foreach my $name (@CANDIDATES) {
167             if (/$name$/) {
168                 my ($hname, $hpath, $hsuffix) = 
169                     fileparse($File::Find::name, ("\.h", "\.H"));
170                 unless ($hpath =~ /include-old/) {
171                     print "Found $hname$hsuffix in $hpath\n";
172                     push @includes, $hpath;
173                 }
174             }
175         }
176     }
177     
178     use File::Find;
179     foreach my $guess (@JAVA_HOME_GUESSES) {
180         next unless -d $guess;
181         find (\&find_inc, $guess);
182     }
183     if (! @includes) {
184         die "Could not find Java includes!";
185     } else {
186         print join("\n", @includes), "\n";
187     }
188     return @includes;
189 }
190