2 # Copyright 2002-2007 The ChatSpike Development Team
3 # <brain@chatspike.net>
4 # <Craig@chatspike.net>
6 # Licensed under GPL, please see the COPYING file
10 package make::utilities;
11 use Exporter 'import';
14 @EXPORT = qw(make_rpath pkgconfig_get_include_dirs pkgconfig_get_lib_dirs pkgconfig_check_version translate_functions promptstring vcheck);
16 # Parse the output of a *_config program,
17 # such as pcre_config, take out the -L
18 # directive and return an rpath for it.
20 # \033[1;32msrc/Makefile\033[0m
22 my %already_added = ();
24 sub promptstring($$$$$)
26 my ($prompt, $configitem, $default, $package, $commandlineswitch) = @_;
28 if (!$main::interactive)
30 undef $opt_commandlineswitch;
31 GetOptions ("$commandlineswitch=s" => \$opt_commandlineswitch);
32 if (defined $opt_commandlineswitch)
34 print "\033[1;32m$opt_commandlineswitch\033[0m\n";
35 $var = $opt_commandlineswitch;
39 die "Could not detect $package! Please specify the $prompt via the command line option \033[1;32m--$commandlineswitch=\"/path/to/file\"\033[0m";
44 print "\nPlease enter the $prompt?\n";
45 print "[\033[1;32m$default\033[0m] -> ";
46 chomp($var = <STDIN>);
52 $main::config{$configitem} = $var;
57 my ($executable, $module) = @_;
58 chomp($data = `$executable`);
60 while ($data =~ /-L(\S+)/)
63 if (!exists $already_added{$libpath})
65 print "Adding extra library path to \033[1;32m$module\033[0m ... \033[1;32m$libpath\033[0m\n";
66 $already_added{$libpath} = 1;
68 $output .= "-Wl,--rpath -Wl,$libpath -L$libpath ";
76 if (!exists $ENV{PKG_CONFIG_PATH})
78 $ENV{PKG_CONFIG_PATH} = "/usr/lib/pkgconfig:/usr/local/lib/pkgconfig:/usr/local/libdata/pkgconfig:/usr/X11R6/libdata/pkgconfig";
82 $ENV{PKG_CONFIG_PATH} .= ":/usr/local/lib/pkgconfig:/usr/local/libdata/pkgconfig:/usr/X11R6/libdata/pkgconfig";
86 sub pkgconfig_get_include_dirs($$$;$)
88 my ($packagename, $headername, $defaults, $module) = @_;
90 my $key = "default_includedir_$packagename";
91 if (exists $main::config{$key})
93 print "Locating include directory for package \033[1;32m$packagename\033[0m for module \033[1;32m$module\033[0m... ";
94 $ret = $main::config{$key};
95 print "\033[1;32m$ret\033[0m (cached)\n";
101 print "Locating include directory for package \033[1;32m$packagename\033[0m for module \033[1;32m$module\033[0m... ";
103 $v = `pkg-config --modversion $packagename 2>/dev/null`;
104 $ret = `pkg-config --cflags $packagename 2>/dev/null`;
106 if ((!defined $v) || ($v eq ""))
108 $foo = `locate "$headername" | head -n 1`;
109 $foo =~ /(.+)\Q$headername\E/;
112 if ((defined $find) && ($find ne "") && ($find ne $packagename))
114 print "(\033[1;32mFound via search\033[0m) ";
124 if (($defaults ne "") && (($ret eq "") || (!defined $ret)))
126 $ret = "$foo " . $defaults;
129 if ((($ret eq " ") || (!defined $ret)) && ((!defined $v) || ($v eq "")))
131 my $key = "default_includedir_$packagename";
132 if (exists $main::config{$key})
134 $ret = $main::config{$key};
138 $headername =~ s/^\///;
139 promptstring("path to the directory containing $headername", $key, "/usr/include",$packagename,"$packagename-includes");
140 $packagename =~ tr/a-z/A-Z/;
141 $main::config{$key} = "-I$main::config{$key}" . " $defaults -DVERSION_$packagename=\"$v\"";
142 $main::config{$key} =~ s/^\s+//g;
143 $ret = $main::config{$key};
150 my $key = "default_includedir_$packagename";
151 $packagename =~ tr/a-z/A-Z/;
152 $main::config{$key} = "$ret -DVERSION_$packagename=\"$v\"";
153 $main::config{$key} =~ s/^\s+//g;
154 $ret = $main::config{$key};
155 print "\033[1;32m$ret\033[0m (version $v)\n";
163 my ($version1, $version2) = @_;
164 $version1 =~ s/\-r(\d+)/\.\1/g; # minor revs/patchlevels
165 $version2 =~ s/\-r(\d+)/\.\1/g;
166 $version1 =~ s/p(\d+)/\.\1/g;
167 $version2 =~ s/p(\d+)/\.\1/g;
168 $version1 =~ s/\-//g;
169 $version2 =~ s/\-//g;
170 $version1 =~ s/a-z//g;
171 $version2 =~ s/a-z//g;
172 my @v1 = split('\.', $version1);
173 my @v2 = split('\.', $version2);
174 for ($curr = 0; $curr < scalar(@v1); $curr++)
176 if ($v1[$curr] < $v2[$curr])
184 sub pkgconfig_check_version($$;$)
186 my ($packagename, $version, $module) = @_;
190 print "Checking version of package \033[1;32m$packagename\033[0m is >= \033[1;32m$version\033[0m... ";
192 $v = `pkg-config --modversion $packagename 2>/dev/null`;
197 if ((defined $v) && ($v ne ""))
199 if (vcheck($v,$version) == 1)
201 print "\033[1;32mYes (version $v)\033[0m\n";
206 print "\033[1;32mNo (version $v)\033[0m\n";
210 # If we didnt find it, we cant definitively say its too old.
211 # Return ok, and let pkgconflibs() or pkgconfincludes() pick up
212 # the missing library later on.
213 print "\033[1;32mNo (not found)\033[0m\n";
217 sub pkgconfig_get_lib_dirs($$$;$)
219 my ($packagename, $libname, $defaults, $module) = @_;
221 my $key = "default_libdir_$packagename";
222 if (exists $main::config{$key})
224 print "Locating library directory for package \033[1;32m$packagename\033[0m for module \033[1;32m$module\033[0m... ";
225 $ret = $main::config{$key};
226 print "\033[1;32m$ret\033[0m (cached)\n";
232 print "Locating library directory for package \033[1;32m$packagename\033[0m for module \033[1;32m$module\033[0m... ";
234 $v = `pkg-config --modversion $packagename 2>/dev/null`;
235 $ret = `pkg-config --libs $packagename 2>/dev/null`;
237 if ((!defined $v) || ($v eq ""))
239 $foo = `locate "$libname" | head -n 1`;
240 $foo =~ /(.+)\Q$libname\E/;
243 if ((defined $find) && ($find ne "") && ($find ne $packagename))
245 print "(\033[1;32mFound via search\033[0m) ";
256 if (($defaults ne "") && (($ret eq "") || (!defined $ret)))
258 $ret = "$foo " . $defaults;
261 if ((($ret eq " ") || (!defined $ret)) && ((!defined $v) || ($v eq "")))
263 my $key = "default_libdir_$packagename";
264 if (exists $main::config{$key})
266 $ret = $main::config{$key};
271 promptstring("path to the directory containing $libname", $key, "/usr/lib",$packagename,"$packagename-libs");
272 $main::config{$key} = "-L$main::config{$key}" . " $defaults";
273 $main::config{$key} =~ s/^\s+//g;
274 $ret = $main::config{$key};
281 print "\033[1;32m$ret\033[0m (version $v)\n";
282 my $key = "default_libdir_$packagename";
283 $main::config{$key} = $ret;
284 $main::config{$key} =~ s/^\s+//g;
291 # Translate a $CompileFlags etc line and parse out function calls
292 # to functions within these modules at configure time.
293 sub translate_functions($$)
295 my ($line,$module) = @_;
299 $module =~ /modules*\/(.+?)$/;
302 # This is only a cursory check, just designed to catch casual accidental use of backticks.
303 # There are pleanty of ways around it, but its not supposed to be for security, just checking
304 # that people are using the new configuration api as theyre supposed to and not just using
305 # backticks instead of eval(), being as eval has accountability. People wanting to get around
306 # the accountability will do so anyway.
307 if (($line =~ /`/) && ($line !~ /eval\(.+?`.+?\)/))
309 die "Developers should no longer use backticks in configuration macros. Please use exec() and eval() macros instead. Offending line: $line (In module: $module)";
311 while ($line =~ /exec\("(.+?)"\)/)
313 print "Executing program for module \033[1;32m$module\033[0m ... \033[1;32m$1\033[0m\n";
316 $line =~ s/exec\("(.+?)"\)/$replace/;
318 while ($line =~ /eval\("(.+?)"\)/)
320 print "Evaluating perl code for module \033[1;32m$module\033[0m ... ";
325 } until sysopen(TF, $tmpfile, O_RDWR|O_CREAT|O_EXCL|O_NOFOLLOW, 0700);
326 print "(Created and executed \033[1;32m$tmpfile\033[0m)\n";
329 my $replace = `perl $tmpfile`;
331 $line =~ s/eval\("(.+?)"\)/$replace/;
333 while ($line =~ /pkgconflibs\("(.+?)","(.+?)","(.+?)"\)/)
335 my $replace = pkgconfig_get_lib_dirs($1, $2, $3, $module);
336 $line =~ s/pkgconflibs\("(.+?)","(.+?)","(.+?)"\)/$replace/;
338 while ($line =~ /pkgconfversion\("(.+?)","(.+?)"\)/)
340 if (pkgconfig_check_version($1, $2, $module) != 1)
342 die "Version of package $1 is too old. Please upgrade it to version \033[1;32m$2\033[0m or greater and try again.";
344 # This doesnt actually get replaced with anything
345 $line =~ s/pkgconfversion\("(.+?)","(.+?)"\)//;
347 while ($line =~ /pkgconflibs\("(.+?)","(.+?)",""\)/)
349 my $replace = pkgconfig_get_lib_dirs($1, $2, "", $module);
350 $line =~ s/pkgconflibs\("(.+?)","(.+?)",""\)/$replace/;
352 while ($line =~ /pkgconfincludes\("(.+?)","(.+?)",""\)/)
354 my $replace = pkgconfig_get_include_dirs($1, $2, "", $module);
355 $line =~ s/pkgconfincludes\("(.+?)","(.+?)",""\)/$replace/;
357 while ($line =~ /pkgconfincludes\("(.+?)","(.+?)","(.+?)"\)/)
359 my $replace = pkgconfig_get_include_dirs($1, $2, $3, $module);
360 $line =~ s/pkgconfincludes\("(.+?)","(.+?)","(.+?)"\)/$replace/;
362 while ($line =~ /rpath\("(.+?)"\)/)
364 my $replace = make_rpath($1,$module);
365 $replace = "" if ($^O =~ /darwin/i);
366 $line =~ s/rpath\("(.+?)"\)/$replace/;
372 $err =~ s/at .+? line \d+.*//g;
373 print "\n\nConfiguration failed. The following error occured:\n\n$err\n";