1use ExtUtils::MakeMaker;
2use Config qw(%Config);
3
4#### Build information
5my $statvfs_header = "sys/statvfs.h";
6my $statfs_header = "sys/statfs.h";
7my $statvfs_symbol = "d_statvfs";
8my $statvfs_header_def = "i_sysstatvfs";
9my $statfs_symbol = "d_statfs_s";  #### Really the struct def, we equate it to statfs being defined
10my $statfs_header_def = "i_sysstatfs";
11my $define = "";
12my $statfs_use_mount = 0;
13my $xs_file = 'Df.xs';
14
15
16print "OS = $Config{osname}\n";
17
18#### Windows
19if($Config{osname} =~ /^MSWin/i) {
20	print "This module does not support Windows.\n";
21	die "You might try Filesys::DfPortable instead.\n";
22}
23
24#### Check for the existance of statvfs
25if(check_statvfs()) {
26	####$define .= "-DDF_STATVFS ";
27	copy_xs("XS_statvfs", $xs_file);
28	print "Building with statvfs ....\n";
29}
30
31#### Check for the existance of statfs
32elsif(check_statfs()) {
33	#### use_mount needed for headers
34	($statfs_use_mount) &&
35		($define .= "-DDF_STATFS_USE_MOUNT ") ||
36		($define .= "-DDF_STATFS ");
37
38	#### Needed for 4 arg statfs
39	($Config{osname} =~ /^solaris$/i) &&
40        ($define .= "-DDF_SOLARIS ");
41
42	copy_xs("XS_statfs", $xs_file);
43	print "Building with statfs ....\n";
44}
45
46#### OS/2, old Mac, etc
47else {
48	print "We could not find statvfs, or statfs.\n";
49	die "You need at least one of these to build this module.\n";
50}
51
52
53
54sub check_statvfs {
55	print "Checking for statvfs .....\n";
56	if(exists $Config{$statvfs_symbol} && defined $Config{$statvfs_symbol}) {
57		print "$statvfs_symbol is defined.\n";
58		if(exists $Config{$statvfs_header_def} && defined $Config{$statvfs_header_def}) {
59			print "$statvfs_header_def is defined.\n";
60			return(1);
61		}
62
63		else {
64			print "Weird, $statvfs_header_def is not defined.\n";
65			#### Have never seen a system with statvfs and no sys/statvfs.h header
66			#### Lets see if we can find one
67			if(look_for_header($statvfs_header)) {
68				return(1);
69			}
70
71			else {
72				#### no idea what header would be
73				print "Cannot find a $statvfs_header file\n";
74				print "We will not try to build with statvfs\n";
75				return(0);
76			}
77		}
78	}
79
80	else {
81		print "$statvfs_symbol is not defined\n";
82
83		### OK if we find a header should we build with it?
84		if(look_for_header($statvfs_header)) {
85			return(1);
86		}
87
88		else {
89			#### don't use statvfs
90			print "Cannot find a $statvfs_header file\n";
91			print "We will not try to build with statvfs\n";
92			return(0);
93		}
94	}
95
96	return(0);
97}
98
99
100sub check_statfs {
101	print "Checking for statfs .....\n";
102	if(exists $Config{$statfs_symbol} && defined $Config{$statfs_symbol}) {
103		print "Good, $statfs_symbol is defined.\n";
104		if(exists $Config{$statfs_header_def} && defined $Config{$statfs_header_def}) {
105			print "$statfs_header_def is defined.\n";
106			return(1);
107		}
108
109		else {
110			print "$statfs_header_def not defined.\n";
111			#### check for BSD and Darwin
112			if($Config{osname} =~ /^darwin|^bsd|bsd$/i) {
113				print "You are running Darwin or BSD.\n";
114				print "Will assume you need the mount.h and param.h headers.\n";
115				$statfs_use_mount = 1;
116				return(1);
117			}
118
119			elsif(look_for_header($statfs_header)) {
120				return(1);
121			}
122
123			else {
124				#### don't use statfs
125				print "Cannot find a $statfs_header file\n";
126				print "We will not try to build Statfs\n";
127			}
128		}
129	}
130
131	else {
132		print "$statfs_symbol is not defined\n";
133
134		if(look_for_header($statfs_header)) {
135			return(1);
136		}
137
138		else {
139			print "Cannot find a $statfs_header file\n";
140			print "We will not try to build Statfs\n";
141			return(0);
142		}
143	}
144
145	return(0);
146}
147
148
149sub copy_xs {
150my $source = shift;
151my $dest = shift;
152
153	open(SOURCE, "$source") or die "$! $source\n";
154	open(DEST, ">$dest") or die "$! $dest\n";
155	@contents = <SOURCE>;
156	print DEST @contents;
157	close(DEST);
158	close(SOURCE);
159}
160
161
162sub look_for_header {
163my $header = shift;
164
165  	#my @header_inc = split(/\s+/, join(" ", $Config{usrinc}, $Config{locincpth}));
166  	my @header_inc = split(/\s+/, join(" ", $Config{usrinc}));
167	foreach $header_path (@header_inc) {
168		if(-f $header_path . '/' . $header) {
169			print "Header found:" , $header_path . '/' . $header, "\n";
170			return(1);
171		}
172	}
173
174	return(0);
175}
176
177
178WriteMakefile(
179    'NAME'	=> 'Filesys::Df',
180    'VERSION_FROM' => 'Df.pm', # finds $VERSION
181    'LIBS'	=> [''],   # e.g., '-lm'
182    'DEFINE'	=> $define,     # e.g., '-DHAVE_SOMETHING'
183    'INC'	=> '',     # e.g., '-I/usr/include/other'
184    'clean'     => {FILES => 'Df.xs'},
185    'XSPROTOARG' => '-prototypes'
186);
187