summaryrefslogtreecommitdiff
path: root/Java/Makefile.PL
blob: 537f14642dd4500491f02257a0fa0f2075932fa0 (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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
use ExtUtils::MakeMaker ;
use File::Find ;

use strict ;
use File::Spec ;

# The file we just produced in the parent Makefile.PL
require "Inline/Java/default_j2sdk.pl" ;

my $build_jni = $main::build_jni ;
my $build_perl_natives = $main::build_perl_natives ;
my $build_perl_interpreter = $main::build_perl_interpreter ;
my $jvm_lib_type = $main::jvm_lib_type ;

my $jvm_lib = Inline::Java::Portable::portable('JVM_LIB') ;
my $jvm_so = Inline::Java::Portable::portable('JVM_SO') ;

my %so_dirs = () ;

my @files = (
	'jni.h',
	'jni_md.h',
	$jvm_lib,
) ;
if ($jvm_so ne $jvm_lib){
	push @files, $jvm_so ;
}
push @files, 'jvm.cfg' ;

my $files = {
	'jni.h'		=> {
		discard		=> qr/include-old/,
	},
	'jni_md.h'	=> {
		discard		=> qr/include-old/,
	},
	$jvm_lib	=> {
	},
	'jvm.cfg'	=> {
	},
	$jvm_so		=> {
	},
} ;

foreach my $f (@files){
	$files->{$f}->{selected} = undef ;
	$files->{$f}->{choices} = [] ;
	$files->{$f}->{default_choice} = 1 ;
}

my $build_jni_by_dflt = Inline::Java::Portable::portable("BUILD_JNI_BY_DFLT") ;
if (! defined($build_jni)){
  $build_jni = $build_jni_by_dflt;
}
if ($build_jni){
	print "\nBuilding JNI extension.\n\n" ;

	my $jdk_dir = Inline::Java::get_default_j2sdk() ;

	my $symlink = Inline::Java::Portable::portable("GOT_SYMLINK") ;
	find(
		{
			wanted => \&search,
			($symlink ? (follow_fast => 1, follow_skip => 2) : ()),
		},
		$jdk_dir) ;

	my $type = FindDefaultVMType() ;
	if (defined($type)){
		my $cnt = 1 ;
		foreach my $c (@{$files->{$jvm_so}->{choices}}){
			if ($c =~ /$type/){
				$files->{$jvm_so}->{default_choice} = $cnt ;
			}
			$cnt++ ;
		}
	}

	# We no longer need jvm.cfg from now on...
	pop @files ;

	my $done = 0 ;
	foreach my $f (@files){
		my $cnt = scalar(@{$files->{$f}->{choices}}) ;
		if ($cnt == 0){
			print "Can't locate file '$f' anywhere under '$jdk_dir'\n" ;
			$done = 1 ;
			last ;
		}
		elsif ($cnt == 1){
			$files->{$f}->{selected} = $files->{$f}->{choices}->[0] ;
		}
		else {
			my $choose = 1 ;
			if (($f eq $jvm_lib)&&(defined($jvm_lib_type))){
				my @matches = grep {/$jvm_lib_type/} @{$files->{$f}->{choices}} ;
				if (! scalar(@matches)){
					print "WARNING: No $f type matching '$jvm_lib_type' found.\n\n" ;
				}
				elsif (scalar(@matches) == 1){
					print "Automatically selecting '$matches[0]' for $f type.\n\n" ;
					$files->{$f}->{selected} = $matches[0] ;
					$choose = 0 ;
				}
			}
			Choose($f) if $choose ;
		}
	}
	if (! $done){
		# We have all the required files selected.

		CleanSoDirs() ;

		# Cygwin: create gcc-compatible library wrapper for jvm.dll
		if ($^O eq 'cygwin') {
			my $lib = File::Spec->catfile(Cwd::cwd, 'libjvm.dll.a') ;
			my $dll = File::Spec->catfile($files->{$jvm_so}->{selected}, 'jvm.dll') ;
			print "Creating '$lib' for cygwin.\n\n" ;
			system("/usr/bin/dlltool --kill-at --dllname jvm.dll --output-lib '$lib' '$dll'") 
				and print "Error attempting to create '$lib'\n" ;
		}

		print "Building with:\n" ;
		map { print "  " . File::Spec->catfile($files->{$_}->{selected}, $_) . "\n" ;} @files ;

		$done = 0 ;
		if (! $done){
			print
				"\nNote: In order for Inline::Java to use the JNI extension, you will need to\n" . 			    
				"use the JNI configuration option or set the PERL_INLINE_JAVA_JNI environment\n" .
				"variable to a true value. You will also need to add the following directories\n" .
				"to your " . Inline::Java::Portable::portable('SO_LIB_PATH_VAR') . " environment variable:\n" ;
			@main::SO_DIRS = keys %so_dirs ;
			map {print "  $_\n"; } @main::SO_DIRS ;
			print "See README.JNI for more information.\n\n" ;

			@main::I = map { Inline::Java::Portable::portable('SUB_FIX_MAKE_QUOTES', "-I$_") } 
				($files->{'jni.h'}->{selected}, $files->{'jni_md.h'}->{selected}) ;
			@main::L = map { Inline::Java::Portable::portable('SUB_FIX_MAKE_QUOTES', "-L$_") }
				($files->{$jvm_lib}->{selected}) ;
			if ($^O eq 'cygwin') {
				push @main::L, Inline::Java::Portable::portable('SUB_FIX_MAKE_QUOTES', '-L' . Cwd::cwd);
			}

			my $DIR = [] ;
			if (! defined($build_perl_interpreter)){
				print <<TXT;
The PerlInterpreter extension allows Inline::Java to be loaded directly from
Java using an embedded Perl interpreter. It is still EXPERIMENTAL and 
may not build or work properly on all platforms. See documentation for 
more details.
TXT
				if (AskYN("Do you wish to build the PerlInterpreter extension?", 'n')){
					$build_perl_interpreter = 1 ;
				}
				print "\n" ;
			}
			if ($build_perl_interpreter){
				push @{$DIR}, 'PerlInterpreter' ;
			}

			if (! defined($build_perl_natives)){
				print <<TXT;
The PerlNatives extension allows for callbacks to be defined as native
Java methods. It is still EXPERIMENTAL and may not build or work properly
on all platforms. See documentation for more details.
Note: PerlNatives requires J2SDK 1.4 or greater.
TXT
				if (AskYN("Do you wish to build the PerlNatives extension?", 'n')){
					$build_perl_natives = 1 ;
				}
				print "\n" ;
			}
			if ($build_perl_natives){
				push @{$DIR}, 'PerlNatives' ;
			}


			WriteMakefile(
				NAME => 'Inline::Java::JNI',
				VERSION_FROM => 'JNI.pm',
				DIR => $DIR,
				PMLIBDIRS => [File::Spec->catdir('sources', 'org', 'perl', 'inline', 'java')],
				INC => join(' ', @main::I),
				LIBS => [join(' ', @main::L) . " -ljvm"],
				dynamic_lib => { 
					OTHERLDFLAGS => Inline::Java::Portable::portable('OTHERLDFLAGS') 
				},
				# CCFLAGS => '-D_REENTRANT',
			) ;
		}
	}
}
else{
	print "\n" ;
	WriteMakefile(
		NAME => 'Inline::Java::JNI',
		VERSION_FROM => 'JNI.pm',
		DIR => [],
		PMLIBDIRS => [File::Spec->catdir('sources', 'org', 'perl', 'inline', 'java')],
		XS => {},
		C => []
	) ;
}


#################################################


sub search {
	my $file = $_ ;

	my $ext = Inline::Java::Portable::portable('SO_EXT') ;
	if ($File::Find::dir =~ /jre/){
		if ($file =~ /\.$ext$/){
			my $dir = File::Spec->canonpath($File::Find::dir) ;
			$so_dirs{$dir} = 1 ;
		}
	}

	foreach my $f (@files){
		if ($file eq $f){
			my $re = $files->{$f}->{discard} ;
			if ((! $re)||($File::Find::dir !~ /$re/)){
				push @{$files->{$f}->{choices}}, File::Spec->canonpath($File::Find::dir) ;
			}
			last ;
		}
	}
}


sub CleanSoDirs {
	foreach my $d (keys %so_dirs){
		if (-e File::Spec->catfile($d, $jvm_so)){
			delete $so_dirs{$d} ;
		}
		elsif ($d =~ /plugin/){
				delete $so_dirs{$d} ;
		}
		elsif ($d =~ /motif/){
				delete $so_dirs{$d} ;
		}
		elsif ($d =~ /javaws/){
				delete $so_dirs{$d} ;
		}
		elsif ($d =~ /headless/){
				delete $so_dirs{$d} ;
		}
		elsif ($d =~ /xawt/){
				delete $so_dirs{$d} ;
		}
		elsif ($d =~ /_threads/){
			if ($d !~ /native_threads/){
				delete $so_dirs{$d} ;
			}
		}
	}
	$so_dirs{$files->{$jvm_so}->{selected}} = 1 ;
}


sub FindDefaultVMType {
	my $type = undef ;
	my $choices = $files->{'jvm.cfg'}->{choices} ;
	if (scalar(@{$choices})){
		my $cfg = File::Spec->catfile($choices->[0], 'jvm.cfg') ;
		if (open(CFG, "<$cfg")){
			while (<CFG>){
				my $line = $_ ;
				chomp($line) ;
				$line =~ s/^\s+// ;
				$line =~ s/\s+$// ;

				if (! $line){
					next ;
				}
				elsif ($line =~ /^#/){
					next ;
				}
				else{
					($type) = split(/\s+/, $line) ;
					$type =~ s/^-// ;
					last ;
				}
			}
			close(CFG) ;
		}
	}

	return $type ;
}


sub Choose {
	my $f = shift ;

	my $o = $files->{$f} ;
	my $cnt = 0 ;
	my $def = undef ;
	foreach my $f (@{$o->{choices}}){
		$cnt++ ;
		print "$cnt) $f\n" ;
	}
	my $idx = AskSub("Please select from the above list which '$f' to use:",
		$o->{default_choice}, sub {(($_[0] >= 1)&&($_[0] <= $cnt))}) ;

	$o->{selected} = $o->{choices}->[int($idx) - 1] ;
	print "\n" ;
}


# Gets string from stdin
sub Ask {
	my $ques = shift ;
	my $def = shift ;

	return AskSub($ques, $def, undef) ;
}


# Gets yes/no from stdin
sub AskYN {
	my $ques = shift ;
	my $def = shift ;

	my $ans = AskSub($ques, $def, sub {((! $_[0])||($_[0] =~ /^(y|n)$/i))}) ;

	return (($ans =~ /^y$/i) ? 1 : 0) ;
}


sub AskSub {
	my $ques = shift ;
	my $def = shift ;
	my $sub = shift ;

	while (1){
		my $ans = prompt($ques, $def) ;
		if (! $sub){
			return $ans ;
		}
		elsif ($sub->($ans)){
			return $ans ;
		}
	}
}