1use strict;
2use warnings;
3use Cwd;
4use ExtUtils::MakeMaker;
5use File::Copy;
6
7my $libs  = '';
8my $def   = '';
9my $inc   = '';
10my $obj   = '';
11my $clean = '';
12my $inc_dir = undef;
13my $lib_dir = undef;
14my $rh_builtin_dir = 'librhash';
15my $rh_local_dir   = getcwd() . '/../../librhash';
16my $rh_type = $ENV{'LIBRHASH'} || 'auto';
17my $custom_inc = $ENV{'LIBRHASH_INC'} || '';
18my $custom_ld  = $ENV{'LIBRHASH_LD'}  || '';
19
20sub has_librhash {
21	return (-f $_[0] . '/rhash.h');
22}
23
24if ($rh_type eq 'auto')
25{
26	$rh_type = ($custom_ld =~ /-L/ ? 'custom' :
27		has_librhash($rh_builtin_dir) ? 'builtin' :
28		has_librhash($rh_local_dir) ? 'local' : 'system' );
29	print "Selected librhash type: $rh_type\n";
30}
31if ($rh_type ne 'custom')
32{
33	$inc_dir = ($rh_type eq 'builtin' ? $rh_builtin_dir :
34		$rh_type eq 'local' ? $rh_local_dir :
35		$rh_type eq 'system' ? '' : die("Unknown type LIBRHASH='$rh_type'"));
36	$lib_dir = $inc_dir if $rh_type ne 'builtin';
37	!$inc_dir || -d $inc_dir || die "Not a directory: '$inc_dir'";
38	!$inc_dir || has_librhash($inc_dir) || die "No librhash headers at: '$inc_dir'";
39	$inc  = "-I$inc_dir" if $inc_dir;
40	$libs = "-L$lib_dir" if $lib_dir;
41	$libs .= ' -lrhash'  if $rh_type ne 'builtin';
42} else {
43	# set custom compile and linking flags
44	$inc = $custom_inc;
45	$libs = $custom_ld;
46}
47
48# copy and rename *.c files by prepending underscore '_'
49sub copy_c_files($) {
50	my $from_dir = $_[0];
51	my @result = ();
52	(opendir my($dh), $from_dir) or die "Can't open $from_dir: $!";
53	my @files = grep { /(?<!\Atest_hashes)\.c$/ } readdir $dh;
54	closedir $dh;
55	for (@files) {
56		my ($from, $to) = ("$from_dir/$_", "_$_");
57		push @result, $to;
58
59		my ($df, $dt) = ((stat($from))[9], (stat($to))[9]);
60		next if(defined($dt) && defined($df) && $dt >= $df);
61		#print "copy $from -> $to\n";
62		copy($from, $to)
63			or die "Can't copy $from to $to: $!";
64	}
65	return @result;
66}
67
68if($rh_type eq 'builtin') {
69	# using sources of the builtin librhash
70	my @c_files = copy_c_files($rh_builtin_dir);
71	$clean = join(' ', @c_files);
72	$obj = join(' ', map { s/\.c$/\$(OBJ_EXT)/; $_ } @c_files) . ' ';
73	$def = '-DRHASH_XVERSION=0' # QuickFix
74}
75
76# make setting optional MakeMaker parameters more readable
77sub OPTIONAL {
78	return () unless $ExtUtils::MakeMaker::VERSION ge shift;
79	return @_;
80}
81
82# see ExtUtils::MakeMaker.pm for details of how to influence
83# the contents of the Makefile that is written
84WriteMakefile(
85    NAME         => 'Crypt::Rhash',
86    ABSTRACT     => 'Library for computing message digests and magnet links',
87    AUTHOR       => 'Aleksey Kravchenko',
88    VERSION_FROM => 'Rhash.pm', # finds $VERSION
89    OPTIONAL( '6.31',
90        LICENSE => 'open_source',
91    ),
92    OPTIONAL( '6.46',
93        # Use META_ADD instead of META_MERGE so that we can remove
94        # any build-time dependencies that MakeMaker will put into
95        # the requires field.
96        META_ADD => {
97            resources => {
98                homepage    => 'http://rhash.sf.net/',
99                bugtracker  => 'https://github.com/rhash/RHash/issues',
100                license     => 'https://github.com/rhash/RHash/blob/master/COPYING',
101                repository  => 'https://github.com/rhash/RHash',
102            },
103        },
104    ),
105
106    LIBS         => [ $libs ],
107    DEFINE       => $def,
108    INC          => $inc,
109    OBJECT       => $obj . 'Rhash$(OBJ_EXT)',
110    clean        => {
111        FILES => $clean,
112    },
113);
114