]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - make/utilities.pm
Add snomasks to /check on a user (QA, please test), also return CMD_LOCALONLY as...
[user/henk/code/inspircd.git] / make / utilities.pm
1 package make::utilities;
2 use Exporter 'import';
3 use POSIX;
4 use Getopt::Long;
5 @EXPORT = qw(make_rpath pkgconfig_get_include_dirs pkgconfig_get_lib_dirs pkgconfig_check_version translate_functions promptstring vcheck);
6
7 # Parse the output of a *_config program,
8 # such as pcre_config, take out the -L
9 # directive and return an rpath for it.
10
11 # \033[1;32msrc/Makefile\033[0m
12
13 my %already_added = ();
14
15 sub promptstring($$$$$)
16 {
17         my ($prompt, $configitem, $default, $package, $commandlineswitch) = @_;
18         my $var;
19         if (!$main::interactive)
20         {
21                 undef $opt_commandlineswitch;
22                 GetOptions ("$commandlineswitch=s" => \$opt_commandlineswitch);
23                 if (defined $opt_commandlineswitch)
24                 {
25                         print "\033[1;32m$opt_commandlineswitch\033[0m\n";
26                         $var = $opt_commandlineswitch;
27                 }
28                 else
29                 {
30                         die "Could not detect $package! Please specify the $prompt via the command line option \033[1;32m--$commandlineswitch=\"/path/to/file\"\033[0m";
31                 }
32         }
33         else
34         {
35                 print "\nPlease enter the $prompt?\n";
36                 print "[\033[1;32m$default\033[0m] -> ";
37                 chomp($var = <STDIN>);
38         }
39         if ($var eq "")
40         {
41                 $var = $default;
42         }
43         $main::config{$configitem} = $var;
44 }
45
46 sub make_rpath($;$)
47 {
48         my ($executable, $module) = @_;
49         chomp($data = `$executable`);
50         my $output = "";
51         while ($data =~ /-L(\S+)/)
52         {
53                 $libpath = $1;
54                 if (!exists $already_added{$libpath})
55                 {
56                         print "Adding extra library path to \033[1;32m$module\033[0m ... \033[1;32m$libpath\033[0m\n";
57                         $already_added{$libpath} = 1;
58                 }
59                 $output .= "-Wl,--rpath -Wl,$libpath -L$libpath ";
60                 $data =~ s/-L(\S+)//;
61         }
62         return $output;
63 }
64
65 sub extend_pkg_path()
66 {
67         if (!exists $ENV{PKG_CONFIG_PATH})
68         {
69                 $ENV{PKG_CONFIG_PATH} = "/usr/lib/pkgconfig:/usr/local/lib/pkgconfig:/usr/local/libdata/pkgconfig:/usr/X11R6/libdata/pkgconfig";
70         }
71         else
72         {
73                 $ENV{PKG_CONFIG_PATH} .= ":/usr/local/lib/pkgconfig:/usr/local/libdata/pkgconfig:/usr/X11R6/libdata/pkgconfig";
74         }
75 }
76
77 sub pkgconfig_get_include_dirs($$$;$)
78 {
79         my ($packagename, $headername, $defaults, $module) = @_;
80
81         my $key = "default_includedir_$packagename";
82         if (exists $main::config{$key})
83         {
84                 print "Locating include directory for package \033[1;32m$packagename\033[0m for module \033[1;32m$module\033[0m... ";
85                 $ret = $main::config{$key};
86                 print "\033[1;32m$ret\033[0m (cached)\n";
87                 return $ret;
88         }
89
90         extend_pkg_path();
91
92         print "Locating include directory for package \033[1;32m$packagename\033[0m for module \033[1;32m$module\033[0m... ";
93
94         $v = `pkg-config --modversion $packagename 2>/dev/null`;
95         $ret = `pkg-config --cflags $packagename 2>/dev/null`;
96
97         if ((!defined $v) || ($v eq ""))
98         {
99                 $foo = `locate "$headername" | head -n 1`;
100                 $foo =~ /(.+)\Q$headername\E/;
101                 $find = $1;
102                 chomp($find);
103                 if ((defined $find) && ($find ne "") && ($find ne $packagename))
104                 {
105                         print "(\033[1;32mFound via search\033[0m) ";
106                         $foo = "-I$1";
107                 }
108                 else
109                 {
110                         $foo = " ";
111                         undef $v;
112                 }
113                 $ret = "$foo";
114         }
115         if (($defaults ne "") && (($ret eq "") || (!defined $ret)))
116         {
117                 $ret = "$foo " . $defaults;
118         }
119         chomp($ret);
120         if ((($ret eq " ") || (!defined $ret)) && ((!defined $v) || ($v eq "")))
121         {
122                 my $key = "default_includedir_$packagename";
123                 if (exists $main::config{$key})
124                 {
125                         $ret = $main::config{$key};
126                 }
127                 else
128                 {
129                         $headername =~ s/^\///;
130                         promptstring("path to the directory containing $headername", $key, "/usr/include",$packagename,"$packagename-includes");
131                         $packagename =~ tr/a-z/A-Z/;
132                         $main::config{$key} = "-I$main::config{$key}" . " $defaults -DVERSION_$packagename=\"$v\"";
133                         $main::config{$key} =~ s/^\s+//g;
134                         $ret = $main::config{$key};
135                         return $ret;
136                 }
137         }
138         else
139         {
140                 chomp($v);
141                 my $key = "default_includedir_$packagename";
142                 $packagename =~ tr/a-z/A-Z/;
143                 $main::config{$key} = "$ret -DVERSION_$packagename=\"$v\"";
144                 $main::config{$key} =~ s/^\s+//g;
145                 $ret = $main::config{$key};
146                 print "\033[1;32m$ret\033[0m (version $v)\n";
147         }
148         $ret =~ s/^\s+//g;
149         return $ret;
150 }
151
152 sub vcheck($$)
153 {
154         my ($version1, $version2) = @_;
155         $version1 =~ s/\-r(\d+)/\.\1/g; # minor revs/patchlevels
156         $version2 =~ s/\-r(\d+)/\.\1/g;
157         $version1 =~ s/p(\d+)/\.\1/g;
158         $version2 =~ s/p(\d+)/\.\1/g;
159         $version1 =~ s/\-//g;
160         $version2 =~ s/\-//g;
161         $version1 =~ s/a-z//g;
162         $version2 =~ s/a-z//g;
163         my @v1 = split('\.', $version1);
164         my @v2 = split('\.', $version2);
165         for ($curr = 0; $curr < scalar(@v1); $curr++)
166         {
167                 if ($v1[$curr] < $v2[$curr])
168                 {
169                         return 0;
170                 }
171         }
172         return 1;
173 }
174
175 sub pkgconfig_check_version($$;$)
176 {
177         my ($packagename, $version, $module) = @_;
178
179         extend_pkg_path();
180
181         print "Checking version of package \033[1;32m$packagename\033[0m is >= \033[1;32m$version\033[0m... ";
182
183         $v = `pkg-config --modversion $packagename 2>/dev/null`;
184         if (defined $v)
185         {
186                 chomp($v);
187         }
188         if ((defined $v) && ($v ne ""))
189         {
190                 if (vcheck($v,$version) == 1)
191                 {
192                         print "\033[1;32mYes (version $v)\033[0m\n";
193                         return 1;
194                 }
195                 else
196                 {
197                         print "\033[1;32mNo (version $v)\033[0m\n";
198                         return 0;
199                 }
200         }
201         # If we didnt find it, we  cant definitively say its too old.
202         # Return ok, and let pkgconflibs() or pkgconfincludes() pick up
203         # the missing library later on.
204         print "\033[1;32mNo (not found)\033[0m\n";
205         return 1;
206 }
207
208 sub pkgconfig_get_lib_dirs($$$;$)
209 {
210         my ($packagename, $libname, $defaults, $module) = @_;
211
212         my $key = "default_libdir_$packagename";
213         if (exists $main::config{$key})
214         {
215                 print "Locating library directory for package \033[1;32m$packagename\033[0m for module \033[1;32m$module\033[0m... ";
216                 $ret = $main::config{$key};
217                 print "\033[1;32m$ret\033[0m (cached)\n";
218                 return $ret;
219         }
220
221         extend_pkg_path();
222
223         print "Locating library directory for package \033[1;32m$packagename\033[0m for module \033[1;32m$module\033[0m... ";
224
225         $v = `pkg-config --modversion $packagename 2>/dev/null`;
226         $ret = `pkg-config --libs $packagename 2>/dev/null`;
227
228         if ((!defined $v) || ($v eq ""))
229         {
230                 $foo = `locate "$libname" | head -n 1`;
231                 $foo =~ /(.+)\Q$libname\E/;
232                 $find = $1;
233                 chomp($find);
234                 if ((defined $find) && ($find ne "") && ($find ne $packagename))
235                 {
236                         print "(\033[1;32mFound via search\033[0m) ";
237                         $foo = "-L$1";
238                 }
239                 else
240                 {
241                         $foo = " ";
242                         undef $v;
243                 }
244                 $ret = "$foo";
245         }
246
247         if (($defaults ne "") && (($ret eq "") || (!defined $ret)))
248         {
249                 $ret = "$foo " . $defaults;
250         }
251         chomp($ret);
252         if ((($ret eq " ") || (!defined $ret)) && ((!defined $v) || ($v eq "")))
253         {
254                 my $key = "default_libdir_$packagename";
255                 if (exists $main::config{$key})
256                 {
257                         $ret = $main::config{$key};
258                 }
259                 else
260                 {
261                         $libname =~ s/^\///;
262                         promptstring("path to the directory containing $libname", $key, "/usr/lib",$packagename,"$packagename-libs");
263                         $main::config{$key} = "-L$main::config{$key}" . " $defaults";
264                         $main::config{$key} =~ s/^\s+//g;
265                         $ret = $main::config{$key};
266                         return $ret;
267                 }
268         }
269         else
270         {
271                 chomp($v);
272                 print "\033[1;32m$ret\033[0m (version $v)\n";
273                 my $key = "default_libdir_$packagename";
274                 $main::config{$key} = $ret;
275                 $main::config{$key} =~ s/^\s+//g;
276                 $ret =~ s/^\s+//g;
277         }
278         $ret =~ s/^\s+//g;
279         return $ret;
280 }
281
282 # Translate a $CompileFlags etc line and parse out function calls
283 # to functions within these modules at configure time.
284 sub translate_functions($$)
285 {
286         my ($line,$module) = @_;
287
288         eval
289         {
290                 $module =~ /modules*\/(.+?)$/;
291                 $module = $1;
292
293                 # This is only a cursory check, just designed to catch casual accidental use of backticks.
294                 # There are pleanty of ways around it, but its not supposed to be for security, just checking
295                 # that people are using the new configuration api as theyre supposed to and not just using
296                 # backticks instead of eval(), being as eval has accountability. People wanting to get around
297                 # the accountability will do so anyway.
298                 if (($line =~ /`/) && ($line !~ /eval\(.+?`.+?\)/))
299                 {
300                         die "Developers should no longer use backticks in configuration macros. Please use exec() and eval() macros instead. Offending line: $line (In module: $module)";
301                 }
302                 while ($line =~ /exec\("(.+?)"\)/)
303                 {
304                         print "Executing program for module \033[1;32m$module\033[0m ... \033[1;32m$1\033[0m\n";
305                         my $replace = `$1`;
306                         chomp($replace);
307                         $line =~ s/exec\("(.+?)"\)/$replace/;
308                 }
309                 while ($line =~ /eval\("(.+?)"\)/)
310                 {
311                         print "Evaluating perl code for module \033[1;32m$module\033[0m ... ";
312                         my $tmpfile;
313                         do
314                         {
315                                 $tmpfile = tmpnam();
316                         } until sysopen(TF, $tmpfile, O_RDWR|O_CREAT|O_EXCL|O_NOFOLLOW, 0700);
317                         print "(Created and executed \033[1;32m$tmpfile\033[0m)\n";
318                         print TF $1;
319                         close TF;
320                         my $replace = `perl $tmpfile`;
321                         chomp($replace);
322                         $line =~ s/eval\("(.+?)"\)/$replace/;
323                 }
324                 while ($line =~ /pkgconflibs\("(.+?)","(.+?)","(.+?)"\)/)
325                 {
326                         my $replace = pkgconfig_get_lib_dirs($1, $2, $3, $module);
327                         $line =~ s/pkgconflibs\("(.+?)","(.+?)","(.+?)"\)/$replace/;
328                 }
329                 while ($line =~ /pkgconfversion\("(.+?)","(.+?)"\)/)
330                 {
331                         if (pkgconfig_check_version($1, $2, $module) != 1)
332                         {
333                                 die "Version of package $1 is too old. Please upgrade it to version \033[1;32m$2\033[0m or greater and try again.";
334                         }
335                         # This doesnt actually get replaced with anything
336                         $line =~ s/pkgconfversion\("(.+?)","(.+?)"\)//;
337                 }
338                 while ($line =~ /pkgconflibs\("(.+?)","(.+?)",""\)/)
339                 {
340                         my $replace = pkgconfig_get_lib_dirs($1, $2, "", $module);
341                         $line =~ s/pkgconflibs\("(.+?)","(.+?)",""\)/$replace/;
342                 }
343                 while ($line =~ /pkgconfincludes\("(.+?)","(.+?)",""\)/)
344                 {
345                         my $replace = pkgconfig_get_include_dirs($1, $2, "", $module);
346                         $line =~ s/pkgconfincludes\("(.+?)","(.+?)",""\)/$replace/;
347                 }
348                 while ($line =~ /pkgconfincludes\("(.+?)","(.+?)","(.+?)"\)/)
349                 {
350                         my $replace = pkgconfig_get_include_dirs($1, $2, $3, $module);
351                         $line =~ s/pkgconfincludes\("(.+?)","(.+?)","(.+?)"\)/$replace/;
352                 }
353                 while ($line =~ /rpath\("(.+?)"\)/)
354                 {
355                         my $replace = make_rpath($1,$module);
356                         $replace = "" if ($^O =~ /darwin/i);
357                         $line =~ s/rpath\("(.+?)"\)/$replace/;
358                 }
359         };
360         if ($@)
361         {
362                 $err = $@;
363                 $err =~ s/at .+? line \d+.*//g;
364                 print "\n\nConfiguration failed. The following error occured:\n\n$err\n";
365                 exit;
366         }
367         else
368         {
369                 return $line;
370         }
371 }
372
373 1;
374