File: Makefile.pl

package info (click to toggle)
libwhisker2-perl 2.4-1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 664 kB
  • ctags: 303
  • sloc: perl: 7,262; makefile: 52
file content (508 lines) | stat: -rw-r--r-- 12,250 bytes parent folder | download
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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
#!/usr/bin/perl
#
# Generic perl application Makefile
# Copyright 2006 rain forest puppy
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#

$VERSION	=	'2.4';		# version of the app
$PACKAGE	=	'LW2';		# name of the app
$TARGET		=	'LW2.pm';	# target build filename

$SRCDIR		= 	'src';		# dir containing .pl parts
$MAIN		=	'globals.pl_';	# main app logic/global library logic
$HEADER		=	'header.pod';	# POD/file header
$FOOTER		= 	'footer.pod';	# POD/file footer

$LIBRARY	=	 1;		# is it a library?
$HASPOD		=	 1;		# does it have embedded POD?

$DESTDIR	= '';		# installation directory prefix

#### supported build options #########################################

# general commands supported by this makefile

$COMMANDS{clean}	= \&command_clean;
$COMMANDS{lib}		= \&command_build		if($LIBRARY);
$COMMANDS{build}	= \&command_build		if(!$LIBRARY);
$COMMANDS{install}	= \&command_install;
$COMMANDS{uninstall}	= \&command_uninstall;
$COMMANDS{support}	= \&command_support;
$COMMANDS{sockdiag}	= \&command_socket_diag;
$COMMANDS{nopod} 	= \&command_strip_pod		if($HASPOD);


# commands specific to this app

$COMMANDS{install_lw1} = \&command_install_compat;


#### external modules ################################################

# modules to check for and track if they are installed
#
# Module values:
#	0 = just try to load module, but don't error if not available
#	1 = abort build if module isn't available

%MODULES = (
	'Socket'	=> 0,
	'MIME::Base64'	=> 0,
	'MD5'		=> 0,
	'Net::SSLeay'	=> 0,
	'Net::SSL'	=> 0,
	'POSIX'		=> 0
);

#### end config ######################################################

$|++;

# internal vars
%BUILD		= ();
$CWD		= ();
$COMMAND 	= '';
%DESCRIPTIONS	= ();

# first check arguments
if($ARGV[0] eq ''){
	print STDOUT "$PACKAGE version $VERSION build options:\n\n";

	# load the command descriptions
	while(<DATA>){
		tr/\r\n//d;
		my ($name,$desc)=split(/\t/,$_,2);
		$DESCRIPTIONS{$name}=$desc;
	}

	foreach (keys %COMMANDS){
		print STDOUT "- Makefile.pl $_";
		if(defined $DESCRIPTIONS{$_}){
			print STDOUT "\t",$DESCRIPTIONS{$_};
		}
		print STDOUT "\n";
	}
	print STDOUT "\n";
	exit;
}

# the makefile requires Config, Cwd, and Pod::Man modules
$MODULES{Config}	= 0;
$MODULES{Cwd}		= 0;
$MODULES{'Pod::Man'}	= 0;

# next check for external modules
foreach (keys %MODULES){
	eval "use $_;";
	if(!$@){
		$MODULES{$_}++;
	} else {
		if($MODULES{$_}>0){
			print STDERR "Error: module '$_' required.\n";
			exit;
		}
	}
}

# adjust DESTDIR, if needed
$DESTDIR = $ENV{DESTDIR} if(defined $ENV{DESTDIR});

# parse command line build options
while($COMMAND = shift @ARGV){

	if(defined $COMMANDS{$COMMAND}){
		$COMMANDS{$COMMAND}->();
	} else {
		print STDERR "Error: bad build command '$COMMAND'\n";
		exit;
	}
}

exit;

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

sub command_clean {
	unlink $TARGET if(-e $TARGET);
	print STDOUT "Clean.\n";
}

sub command_install {
	command_install_library()	if($LIBRARY);
	command_install_pod()		if($HASPOD);
}

sub command_uninstall {
	command_uninstall_library()	if($LIBRARY);
	command_uninstall_pod()		if($HASPOD);
}

sub command_install_pod {
	return if(!$HASPOD);
	if($MODULES{'Pod::Man'}==0){
		print STDERR "WARNING: Pod::Man not available; man page not installed\n";
		return;
	}
	command_build() if(!-e $TARGET);
	die("Can not install without Config.pm") if($MODULES{Config}==0);
	$CWD=&cwd if($MODULES{Cwd}>0);
	my $where=$DESTDIR . $Config{'man3direxp'};
	my $t = $TARGET;
	if($LIBRARY){
		$t="$PACKAGE.3pm";
	} else {
		$t=~s/\.pl$//i;
		$t.='.3';
	}
	if(!-e $where){
	  print STDOUT "WARNING!\n\n",
		"The local man3 site directory does not exist:\n",
		"$where\n\nPlease create this directory and try again.\n\n";
		exit;
	}

	my $parser = Pod::Man->new (
			release => $VERSION, 
			section => 3,
			name => $PACKAGE
		);

	open(IN,'<'.$TARGET)||puke($TARGET);
	$temp = <IN>;
	if($temp=~m/^# NOPOD NOTICE:/){
		print STDERR "Pod has been stripped; not installing man page\n";
		return;
	}
	
	chdir($where);
	open(OUT,'>'.$t)||die("Can't open $where/$t for write");
	chmod 0644, $t;

	$parser->parse_from_filehandle(\*IN,\*OUT);

	close(IN);
	close(OUT);
	if(-s "$t"){
		print STDOUT "$t installed to $where\n";
	} else {
		print STDOUT "Error installing $t to $where\n";
	}
	exit if($MODULES{Cwd}==0);
	chdir($CWD);
}

sub command_uninstall_pod {
	die("Can not uninstall without Config.pm") if($MODULES{Config}==0);
	$CWD=&cwd if($MODULES{Cwd}>0);
	my $where=$DESTDIR . $Config{'man3direxp'};
	my $t = $TARGET;
	if($LIBRARY){
		$t="$PACKAGE.3pm";
	} else {
		$t=~s/\.pl$//i;
		$t.='.3';
	}
	chdir($where);
	if(-e $t){
		unlink $t;
		print STDOUT "$t uninstalled.\n";
	} else {
		print STDOUT "$t not installed.\n";
	}
	exit if($MODULES{Cwd}==0);
	chdir($CWD);
}

sub command_install_library {
	return if(!$LIBRARY);
	command_build() if(!-e $TARGET);
	die("Can not install without Config.pm") if($MODULES{Config}==0);
	$CWD=&cwd if($MODULES{Cwd}>0);
	my $where=$DESTDIR . $Config{'installsitelib'};
	if(!-e $where){
	  print STDOUT "WARNING!\n\n",
		"The local perl site directory does not exist:\n",
		"$where\n\nPlease create this directory and try again.\n\n";
		exit;	
	}
	open(IN,'<'.$TARGET)||puke($TARGET);
	chdir($where);
	open(OUT,'>'.$TARGET)||die("Can't open $where/$TARGET for write");
	chmod 0755, $TARGET;
	while(<IN>){ 
		print OUT; 
	}
	close(IN); 
	close(OUT);
	if(-s "$TARGET"){
		print STDOUT "$TARGET installed to $where\n";
	} else { 
		print STDOUT "Error installing $TARGET to $where\n"; 
	}
	exit if($MODULES{Cwd}==0);
	chdir($CWD);
}

sub command_uninstall_library {
	die("Can not uninstall without Config.pm") if($MODULES{Config}==0);
	$CWD=&cwd if($MODULES{Cwd}>0);
	my $where=$DESTDIR . $Config{'installsitelib'};
	chdir($where);
	if(-e $TARGET){
		unlink $TARGET;
		print STDOUT "$PACKAGE uninstalled.\n";
	} else {
		print STDOUT "$PACKAGE not installed.\n";
	}
	exit if($MODULES{Cwd}==0);
	chdir($CWD);
}

sub command_build {
	$CWD=&cwd if($MODULES{Cwd}>0);

	# open target file for output
	open(OUT,'>'.$TARGET)||die("Can't open $TARGET for write");
	chmod 0755, $TARGET;

	# print out the shebang line
	print OUT "#!",$^X,"\n";

	# embed the package and version info
	print OUT "# $PACKAGE version $VERSION\n";

	# switch to the src directory
	opendir(DIR,$SRCDIR);
	chdir($SRCDIR);

	# print out the initial header and infoz
	readlib($HEADER,1);

	if($LIBRARY){
		print OUT "package $PACKAGE;\n";
		print OUT "\$",$PACKAGE,"::VERSION=\"$VERSION\";\n";
	} else {
		print OUT "\$VERSION=\"$VERSION\";\n";
	}
	print OUT "\$PACKAGE='",$PACKAGE,"';\n";

	# handle main logic
	print OUT "\n";
	if($LIBRARY){
		print OUT "BEGIN {\n";
		print OUT "package $PACKAGE;\n";
		print OUT "\$PACKAGE='",$PACKAGE,"';\n";
	}
	readlib($MAIN,0);
	if($LIBRARY){
		print OUT "\n} # BEGIN\n\n";
	}

	# handle all the source files
	&readlibs;	

	# and now the footer
	readlib($FOOTER,1);
	print OUT "1;\n" if($LIBRARY);

	# we're all done; print status and cleanup
	print STDOUT "$PACKAGE built.\n";
	close(OUT);
	closedir(DIR);
	exit if($MODULES{Cwd}==0);
	chdir($CWD);
}

sub command_strip_pod {
	return if(!$HASPOD);
	command_build() if(!-e $TARGET);
	open(OUT,'>'."$TARGET.nopod") || die("Couldn't open $TARGET.nopod");
	open(IN,'<'.$TARGET) || puke($TARGET);
	&strip_pod;
	close(OUT); 
	close(IN);
	unlink $TARGET;
	rename "$TARGET.nopod", $TARGET;
	chmod 0755, $TARGET;
	print STDOUT "POD removed.\n";
}

sub command_support {
	print STDOUT "Perl $] on '$^O'\n";
	print STDOUT "Architecture: '$Config{archname}'\n"
		if($MODULES{Config}>0);
	print STDOUT "\n";

	foreach $lib (keys %MODULES){
		print STDOUT $lib, ' ->', ' 'x(20-length($lib));
		if($MODULES{$lib}>0){
			print STDOUT 'yes';
			my $name = $lib.'::VERSION';
			if(defined $$name){
				print STDOUT ' (version '.$$name.')';
			}
			print "\n";
		
		} else {
			print STDOUT "no\n";
		}
	}
}

sub command_socket_diag {
        use Socket;
        use POSIX;
        my @what=qw(
                INADDR_ANY INADDR_BROADCAST INADDR_LOOPBACK INADDR_NONE
                AF_INET PF_INET MSG_OOB
                SOCK_DGRAM SOCK_RAW SOCK_SEQPACKET SOCK_STREAM
                SOL_SOCKET SOMAXCONN F_GETFL F_SETFL O_NONBLOCK
                EINPROGRESS EWOULDBLOCK
                SO_BROADCAST SO_KEEPALIVE SO_LINGER SO_OOBINLINE
                SO_RCVBUF SO_RCVLOWAT SO_RCVTIMEO SO_REUSEADDR SO_SNDBUF
                SO_SNDLOWAT SO_SNDTIMEO SO_TYPE SO_USELOOPBACK
        );
        print STDOUT "\nPerl: $^O\n";
        print STDOUT "Perl version: $]\n";
        print STDOUT "Uname processor: ",`uname -m`;
        print STDOUT "Uname kernel: ",`uname -r`;
	print STDOUT "Socket defines:\n\n";
        map { verify($_) } @what;
}


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

sub command_install_compat {
	die("Can not install without Config.pm") if($MODULES{Config}==0);
	$CWD=&cwd if($MODULES{Cwd}>0);
	my $where=$DESTDIR . $Config{'installsitelib'};
	if(!-e $where){
	  print STDOUT "WARNING!\n\n",
		"The local perl site directory does not exist:\n",
		"$where\n\nPlease create this directory and try again.\n\n";
		exit;	
	}
	open(IN,'<'.'compat/LW.pm')||puke('compat/LW.pm');
	chdir($where);
	open(OUT,'>'.'LW.pm')||die("Can't open $where/LW.pm for write");
	chmod 0755, 'LW.pm';
	while(<IN>){ 
		print OUT; 
	}
	close(IN); 
	close(OUT);
	if(-s "LW.pm"){
		print STDOUT "LW.pm bridge installed to $where\n";
	} else { 
		print STDOUT "Error installing LW.pm to $where\n"; 
	}
	exit if($MODULES{Cwd}==0);
	chdir($CWD);
}

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

sub puke {
	my $file = shift;
	print STDERR "Build error: missing/corrupted file $file\n";
	eval "close(OUT)";
	exit;
}

sub readlib {
	my $file=shift;
	my $replace_flag=shift||0;
	return if(defined $BUILD{$file});
	puke($file) if(!-e $file);
	$BUILD{$file}++;
	open(IN,'<'.$file)||puke($file);
	while(<IN>){		
		next if(m/^#GPL/ || m/^#LIC/);
		s/\r\n$/\n/;
		if($replace_flag){
			s/\$VERSION/$VERSION/g;
			s/\$TARGET/$TARGET/g;
			s/\$PACKAGE/$PACKAGE/g;
		}
		print OUT $_; 
	}
	close(IN);	
}

sub readlibs {
	my $file;
	my @FF=();
	while($file=readdir(DIR)){
		next if($file=~/^\./);
		next if($file eq $MAIN);
		next if($file eq $HEADER);
		next if($file eq $FOOTER);
		next if($file =~ /^_/);
		push(@FF,$file);
	}
	my @FE = sort @FF;
	foreach $file (@FE){
		readlib($file,0); }
}

	
sub strip_pod {
	my $inpod=0;
	my $last='';

	# put a small notice in the file to keep people from wondering where
	# all the whitespace went...
	print OUT "# NOPOD NOTICE: the documentation and whitespace have been stripped\n";
	print OUT "# from this file in order to reduce filesize.\n#\n\n";

	my $IN_INITIAL_COMMENTS=1;
	while(<IN>){
		s/^[ \t]+//; # remove leading whitespace
		my $line=$_;
		next if(m/^#/ && !$IN_INITIAL_COMMENTS);
		tr/\r\n//d; # remote CRLF
		if($IN_INITIAL_COMMENTS && !m/^#/){ 
			$IN_INITIAL_COMMENTS=0; 
			next; 
		}
		next if($_ eq '');
		$inpod=1 if($line=~/^=(head1|item|pod|back)/);
		if(!$inpod){
			$line=~tr/\r//d;
			print OUT $line if(!($line eq "\n" && $last eq "\n"));
		}
		$inpod=0 if($line=~/^=cut/);
		$last = $line;
	}
}

sub verify {
        my $temp=-1;
        my $name=shift;
        eval { $temp=sprintf("%lu",&$name) };
        $temp="\t$temp" if(length($name)<7);
        print STDOUT "\t$name:\t$temp\n";
}


__DATA__
nopod	Strip the POD documentation and whitespace
clean	Clean up the build tree
lib	Build the library
build	Build the application
install	Install the components to the Perl site directory
uninstall	Uninstall/remove the components from your system
support	List various external module support information
sockdiag	Diagnostics for troubleshooting Socket.pm problems
install_lw1 Install the LW.pm compatiblity bridge