summaryrefslogtreecommitdiff
path: root/jpl/JNI/Makefile.PL
blob: 146e3d079f6dc5ae514a42c9b25f3222553dd47a (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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
#!/usr/bin/perl
use ExtUtils::MakeMaker;
use Getopt::Std;
use Config;
$ARCHNAME = $Config{archname};
use File::Basename;

getopts('e'); # embedding?

$CCFLAGS .= $ENV{CCFLAGS} if defined $ENV{CCFLAGS};

# $USE_KAFFE is a boolean that tells us whether or not we should use Kaffe.
# Set by find_includes (it seemed as good a place as any).

# Note that we don't check to see the version of Kaffe is one we support.
#  Currently, the only one we support is the one from CVS.

my $USE_KAFFE = 0;

#require "JNIConfig";

if ($^O eq 'solaris') {
    $LIBPATH = " -R$Config{archlib}/CORE -L$Config{archlib}/CORE";
} elsif ($^O eq 'MSWin32') {
    $LIBPATH = " -L$Config{archlib}\\CORE";
    # MSR - added MS VC++ default library path
    # bjepson - fixed to support path names w/spaces in them.
    push(@WINLIBS, (split"\;",$ENV{LIB}));
    grep s/\\$//, @WINLIBS; # eliminate trailing \
    grep s/\/$//, @WINLIBS; # eliminate trailing /
    $LIBPATH .= join(" ", "", map { qq["-L$_" ] } @WINLIBS);
} else {
    $LIBPATH = " -L$Config{archlib}/CORE";
}
#$LIBS = " -lperl";

# Figure out where Java might live
#
# MSR - added JDK 1.3
#

my @JAVA_HOME_GUESSES = qw(/usr/local/java /usr/java /usr/local/jdk117_v3
                         C:\\JDK1.1.8 C:\\JDK1.2.1 C:\\JDK1.2.2 C:\\JDK1.3 );

my @KAFFE_PREFIX_GUESSES = qw(/usr/local /usr);

if (! defined $ENV{JAVA_HOME}) {
        print "You didn't define JAVA_HOME, so I'm trying a few guesses.\n";
        print "If this fails, you might want to try setting JAVA_HOME and\n";
        print "running me again.\n";
} else {
        @JAVA_HOME_GUESSES = ( $ENV{JAVA_HOME} );
}

if (! defined $ENV{KAFFE_PREFIX}) {
        print "\nYou didn't define KAFFE_PREFIX, so I'm trying a few guesses.",
          "\nIf this fails, and you are using Kaffe, you might want to try\n",
          "setting KAFFE_PREFIX and running me again.\n",
          "If you want to ignore any possible Kaffe installation, set the\n",
          "KAFFE_PREFIX to and empty string.\n\n";
} else {
        @KAFFE_PREFIX_GUESSES = ($ENV{KAFFE_PREFIX} eq "") ? () :
                                                       ( $ENV{KAFFE_PREFIX} );
}

my(@KAFFE_INCLUDE_GUESSES, @KAFFE_LIB_GUESSES);
foreach my $kaffePrefix (@KAFFE_PREFIX_GUESSES) {
     push(@KAFFE_INCLUDE_GUESSES, "$kaffePrefix/include/kaffe");
     push(@KAFFE_LIB_GUESSES, "$kaffePrefix/lib");
     push(@KAFFE_LIB_GUESSES, "$kaffePrefix/lib/kaffe");
}
    $guess .= "/include/kaffe";

# Let's find out where jni.h lives
#
my @INCLUDE = find_includes();

if ($^O eq 'MSWin32') {
  # MSR - added MS VC++ default include path
  push(@INCLUDE,(split"\;",$ENV{INCLUDE}));
  grep s/\\$//, @INCLUDE; # remove trailing \
  grep s/\/$//, @INCLUDE; # remove trailing \
  $INC = join("", map { qq["-I$_" ] } @INCLUDE);

} else {
  $INC = join(" -I", ("", @INCLUDE));
}

# Let's find out the name of the Java shared library
#
my @JAVALIBS = find_libs();

# Find out some defines based on the library we are linking to
#
foreach (@JAVALIBS) {
    if ( $^O eq 'MSWin32') { # We're on Win32
        $INC =~ s#/#\\#g;
        $INC =~ s#\\$##;
	  print $INC, "\n";
        $CCFLAGS .= " -DWIN32 -Z7 -D_DEBUG";
        $MYEXTLIB = "$libjava";
      }
}

$CCFLAGS .= " -DKAFFE" if ($USE_KAFFE);

# Let's find out the path of the library we need to link against.
#
foreach (@JAVALIBS) {
    if ($^O eq 'MSWin32') { # We're on Win32
        $_ =~ s#/#\\\\#g;
    }
    my ($libname, $libpath, $libsuffix) = fileparse($_, ("\.so", "\.lib"));
    $libname =~ s/^lib//;
    if ($^O eq 'solaris') {
        $LIBPATH .= " -R$libpath -L$libpath"
    } else {
        $LIBPATH .= " -L$libpath"
    }
    $LIBS .= " -l$libname";
}

# Do we need -D_REENTRANT?
if ($LIBPATH =~ /native/) {
    print "Looks like native threads...\n";
    $CCFLAGS .= " -D_REENTRANT";
}

if ($opt_e) {
    print "We're embedding Perl in Java via libPerlInterpreter.so.\n";
    eval `../setvars -perl`;
    $CCFLAGS .= " -DEMBEDDEDPERL";
    $LIBPATH .= " -R$ENV{JPL_HOME}/lib/$ARCHNAME -L$ENV{JPL_HOME}/lib/$ARCHNAME";
    $LIBS    .= " -lPerlInterpreter";
}

# Needed for JNI.
if ($^O eq 'solaris') {
    $LIBS = " -lthread -lc $LIBS"; #-lthread must be first!!!
    $CCFLAGS .= " -D_REENTRANT";
}

# MSR - clean up LIBS
$LIBS =~ s/-l$//;

#
# Next, build JNI/Config.pm.  This is a superfluous thing for the SUN and
# Microsoft JDKs, but absolutely necessary for Kaffe.  I think at some
# point, the Microsoft and SUN implementations should use JNI::Config, too.
#

if (! -d "JNI") {
    mkdir("JNI", 0755) || die "Unable to make JNI directory: $!";
}
open(JNICONFIG, ">JNI/Config.pm") || die "Unable to open JNI/Config.pm: $!";

print JNICONFIG "# DO NOT EDIT!   Autogenerated by JNI/Makefile.PL\n\n",
                "package JNI::Config;\nuse strict;\nuse Carp;\n",
                "\nuse vars qw(\$KAFFE \$LIB_JAVA \$CLASS_HOME ",
                "\$LIB_HOME);\n\n",
                "\$KAFFE = $USE_KAFFE;\n\$LIB_JAVA = \"$JAVALIBS[0]\";\n";
if ($USE_KAFFE) {
  my $path = $JAVALIBS[0];
  $path =~ s%/(kaffe/)?libkaffevm.so$%%;

  print JNICONFIG "\$LIB_HOME = \"$path/kaffe\";\n";
  $path =~ s%/lib%%;
  print JNICONFIG "\$CLASS_HOME = \"$path/share/kaffe\";\n";
}
print JNICONFIG "\n\n1;\n";
close JNICONFIG;


my %Makefile = (
    NAME        => 'JNI',
    VERSION_FROM => 'JNI.pm',
    DEFINE        => '',
    LINKTYPE => 'dynamic',
    INC        => $INC,
    CCFLAGS => "$Config{ccflags} $CCFLAGS", 
    ($Config{archname} =~ /mswin32.*-object/i ? ('CAPI' => 'TRUE') : ()),

    clean => {FILES => "JNI/* JNI"}
);

$Makefile{LIBS} = ["$LIBPATH $LIBS"];
if ($MYEXTLIB) {
    $Makefile{MYEXTLIB} = $MYEXTLIB;
}

# See lib/ExtUtils/MakeMaker.pm for details of how to influence
# the contents of the Makefile that is written.
#
WriteMakefile(%Makefile);

if ($USE_KAFFE) {
  my $path = $JAVALIBS[0];
  $path =~ s%/libkaffevm.so$%%;
  print "\n\n***NOTE: be sure to have:\n",
            "              LD_LIBRARY_PATH=$path\n",
            "         in your enviornment (or installed as a system dynamic\n",
            "         library location) when you compile and run this.\n";
}

# subroutine to find a library
#
sub find_stuff {

    my ($candidates, $locations) = @_;
    my $lib;
    $wanted = sub {
        foreach my $name (@$candidates) {
            if (/$name$/ and ! /green_threads/ and !/include-old/) {
                    $lib = $File::Find::name;
            }
        }
    };
    
    use File::Find;
    foreach my $guess (@$locations) {
        next unless -d $guess;
        find (\&$wanted, $guess);
    }
    if (! $lib) {
        print "Could not find @$candidates\n";
    } else {
        print "Found @$candidates as $lib\n\n";
    }
    return $lib;
}

# Extra lib for Java 1.2
#
#  if we want KAFFE, check for it, otherwise search for Java

sub find_libs {
  my($libjava, $libawt, $libjvm);

  if ($USE_KAFFE) {
    $libjava = find_stuff(['libkaffevm.so'], \@KAFFE_LIB_GUESSES);
    $libawt = find_stuff(['libawt.so'], \@KAFFE_LIB_GUESSES);
  } else {
    $libjava = find_stuff(['libjava.so', 'javai.lib', 'jvm.lib'],
                             \@JAVA_HOME_GUESSES);
    $libjvm  = find_stuff(['libjvm.so'],  \@JAVA_HOME_GUESSES);
    $libawt  = find_stuff(['libawt.so'], \@JAVA_HOME_GUESSES);
    if (defined $libjvm) { # JDK 1.2
      my $libhpi  = find_stuff(['libhpi.so'], \@JAVA_HOME_GUESSES);
      return($libjava, $libjvm, $libhpi, $libawt);
    }
  }
  return($libjava, $libawt);
}

# We need to find jni.h and jni_md.h
#

# Always do find_includes as the first operation, as it has the side effect
# of deciding whether or not we are looking for Kaffe.  --bkuhn

sub find_includes {

  my @CANDIDATES = qw(jni.h jni_md.h);
  my @includes;

  sub find_inc {
    foreach my $name (@CANDIDATES) {
      if (/$name$/) {
	my ($hname, $hpath, $hsuffix) = 
	  fileparse($File::Find::name, ("\.h", "\.H"));
	unless ($hpath =~ /include-old/) {
	  print "Found $hname$hsuffix in $hpath\n";
	  push @includes, $hpath;
	} 
      }
    }
  }
    
  use File::Find;
  foreach my $guess (@KAFFE_INCLUDE_GUESSES) {
    next unless -d $guess;
    find (\&find_inc, $guess);
  }
  # If we have found includes, then we are using Kaffe.
  if (@includes > 0) {
    $USE_KAFFE = 1;
  } else {
    foreach my $guess (@JAVA_HOME_GUESSES) {
      next unless -d $guess;
      find (\&find_inc, $guess);
    }
  }
  die "Could not find Java includes!" unless (@includes);

  return @includes;
}