1 # +------------------------------------+
2 # | Inspire Internet Relay Chat Daemon |
3 # +------------------------------------+
5 # InspIRCd: (C) 2002-2007 InspIRCd Development Team
6 # See: http://www.inspircd.org/wiki/index.php/Credits
8 # This program is free but copyrighted software; see
9 # the file COPYING for details.
11 # ---------------------------------------------------
13 package make::utilities;
17 use Exporter 'import';
20 @EXPORT = qw(make_rpath pkgconfig_get_include_dirs pkgconfig_get_lib_dirs pkgconfig_check_version translate_functions promptstring vcheck);
22 # Parse the output of a *_config program,
23 # such as pcre_config, take out the -L
24 # directive and return an rpath for it.
26 # \033[1;32msrc/Makefile\033[0m
28 my %already_added = ();
30 sub promptstring($$$$$)
32 my ($prompt, $configitem, $default, $package, $commandlineswitch) = @_;
34 if (!$main::interactive)
36 undef $opt_commandlineswitch;
37 GetOptions ("$commandlineswitch=s" => \$opt_commandlineswitch);
38 if (defined $opt_commandlineswitch)
40 print "\033[1;32m$opt_commandlineswitch\033[0m\n";
41 $var = $opt_commandlineswitch;
45 die "Could not detect $package! Please specify the $prompt via the command line option \033[1;32m--$commandlineswitch=\"/path/to/file\"\033[0m";
50 print "\nPlease enter the $prompt?\n";
51 print "[\033[1;32m$default\033[0m] -> ";
52 chomp($var = <STDIN>);
58 $main::config{$configitem} = $var;
63 my ($executable, $module) = @_;
64 chomp($data = `$executable`);
66 while ($data =~ /-L(\S+)/)
69 if (!exists $already_added{$libpath})
71 print "Adding extra library path to \033[1;32m$module\033[0m ... \033[1;32m$libpath\033[0m\n";
72 $already_added{$libpath} = 1;
74 $output .= "-Wl,--rpath -Wl,$libpath -L$libpath ";
82 if (!exists $ENV{PKG_CONFIG_PATH})
84 $ENV{PKG_CONFIG_PATH} = "/usr/lib/pkgconfig:/usr/local/lib/pkgconfig:/usr/local/libdata/pkgconfig:/usr/X11R6/libdata/pkgconfig";
88 $ENV{PKG_CONFIG_PATH} .= ":/usr/local/lib/pkgconfig:/usr/local/libdata/pkgconfig:/usr/X11R6/libdata/pkgconfig";
92 sub pkgconfig_get_include_dirs($$$;$)
94 my ($packagename, $headername, $defaults, $module) = @_;
96 my $key = "default_includedir_$packagename";
97 if (exists $main::config{$key})
99 print "Locating include directory for package \033[1;32m$packagename\033[0m for module \033[1;32m$module\033[0m... ";
100 $ret = $main::config{$key};
101 print "\033[1;32m$ret\033[0m (cached)\n";
107 print "Locating include directory for package \033[1;32m$packagename\033[0m for module \033[1;32m$module\033[0m... ";
109 $v = `pkg-config --modversion $packagename 2>/dev/null`;
110 $ret = `pkg-config --cflags $packagename 2>/dev/null`;
112 if ((!defined $v) || ($v eq ""))
114 $foo = `locate "$headername" | head -n 1`;
115 $foo =~ /(.+)\Q$headername\E/;
118 if ((defined $find) && ($find ne "") && ($find ne $packagename))
120 print "(\033[1;32mFound via search\033[0m) ";
130 if (($defaults ne "") && (($ret eq "") || (!defined $ret)))
132 $ret = "$foo " . $defaults;
135 if ((($ret eq " ") || (!defined $ret)) && ((!defined $v) || ($v eq "")))
137 my $key = "default_includedir_$packagename";
138 if (exists $main::config{$key})
140 $ret = $main::config{$key};
144 $headername =~ s/^\///;
145 promptstring("path to the directory containing $headername", $key, "/usr/include",$packagename,"$packagename-includes");
146 $packagename =~ tr/a-z/A-Z/;
147 $main::config{$key} = "-I$main::config{$key}" . " $defaults -DVERSION_$packagename=\"$v\"";
148 $main::config{$key} =~ s/^\s+//g;
149 $ret = $main::config{$key};
156 my $key = "default_includedir_$packagename";
157 $packagename =~ tr/a-z/A-Z/;
158 $main::config{$key} = "$ret -DVERSION_$packagename=\"$v\"";
159 $main::config{$key} =~ s/^\s+//g;
160 $ret = $main::config{$key};
161 print "\033[1;32m$ret\033[0m (version $v)\n";
169 my ($version1, $version2) = @_;
170 $version1 =~ s/\-r(\d+)/\.\1/g; # minor revs/patchlevels
171 $version2 =~ s/\-r(\d+)/\.\1/g;
172 $version1 =~ s/p(\d+)/\.\1/g;
173 $version2 =~ s/p(\d+)/\.\1/g;
174 $version1 =~ s/\-//g;
175 $version2 =~ s/\-//g;
176 $version1 =~ s/a-z//g;
177 $version2 =~ s/a-z//g;
178 my @v1 = split('\.', $version1);
179 my @v2 = split('\.', $version2);
180 for ($curr = 0; $curr < scalar(@v1); $curr++)
182 if ($v1[$curr] < $v2[$curr])
190 sub pkgconfig_check_version($$;$)
192 my ($packagename, $version, $module) = @_;
196 print "Checking version of package \033[1;32m$packagename\033[0m is >= \033[1;32m$version\033[0m... ";
198 $v = `pkg-config --modversion $packagename 2>/dev/null`;
203 if ((defined $v) && ($v ne ""))
205 if (vcheck($v,$version) == 1)
207 print "\033[1;32mYes (version $v)\033[0m\n";
212 print "\033[1;32mNo (version $v)\033[0m\n";
216 # If we didnt find it, we cant definitively say its too old.
217 # Return ok, and let pkgconflibs() or pkgconfincludes() pick up
218 # the missing library later on.
219 print "\033[1;32mNo (not found)\033[0m\n";
223 sub pkgconfig_get_lib_dirs($$$;$)
225 my ($packagename, $libname, $defaults, $module) = @_;
227 my $key = "default_libdir_$packagename";
228 if (exists $main::config{$key})
230 print "Locating library directory for package \033[1;32m$packagename\033[0m for module \033[1;32m$module\033[0m... ";
231 $ret = $main::config{$key};
232 print "\033[1;32m$ret\033[0m (cached)\n";
238 print "Locating library directory for package \033[1;32m$packagename\033[0m for module \033[1;32m$module\033[0m... ";
240 $v = `pkg-config --modversion $packagename 2>/dev/null`;
241 $ret = `pkg-config --libs $packagename 2>/dev/null`;
243 if ((!defined $v) || ($v eq ""))
245 $foo = `locate "$libname" | head -n 1`;
246 $foo =~ /(.+)\Q$libname\E/;
249 if ((defined $find) && ($find ne "") && ($find ne $packagename))
251 print "(\033[1;32mFound via search\033[0m) ";
262 if (($defaults ne "") && (($ret eq "") || (!defined $ret)))
264 $ret = "$foo " . $defaults;
267 if ((($ret eq " ") || (!defined $ret)) && ((!defined $v) || ($v eq "")))
269 my $key = "default_libdir_$packagename";
270 if (exists $main::config{$key})
272 $ret = $main::config{$key};
277 promptstring("path to the directory containing $libname", $key, "/usr/lib",$packagename,"$packagename-libs");
278 $main::config{$key} = "-L$main::config{$key}" . " $defaults";
279 $main::config{$key} =~ s/^\s+//g;
280 $ret = $main::config{$key};
287 print "\033[1;32m$ret\033[0m (version $v)\n";
288 my $key = "default_libdir_$packagename";
289 $main::config{$key} = $ret;
290 $main::config{$key} =~ s/^\s+//g;
297 # Translate a $CompileFlags etc line and parse out function calls
298 # to functions within these modules at configure time.
299 sub translate_functions($$)
301 my ($line,$module) = @_;
305 $module =~ /modules*\/(.+?)$/;
308 # This is only a cursory check, just designed to catch casual accidental use of backticks.
309 # There are pleanty of ways around it, but its not supposed to be for security, just checking
310 # that people are using the new configuration api as theyre supposed to and not just using
311 # backticks instead of eval(), being as eval has accountability. People wanting to get around
312 # the accountability will do so anyway.
313 if (($line =~ /`/) && ($line !~ /eval\(.+?`.+?\)/))
315 die "Developers should no longer use backticks in configuration macros. Please use exec() and eval() macros instead. Offending line: $line (In module: $module)";
317 while ($line =~ /exec\("(.+?)"\)/)
319 print "Executing program for module \033[1;32m$module\033[0m ... \033[1;32m$1\033[0m\n";
322 $line =~ s/exec\("(.+?)"\)/$replace/;
324 while ($line =~ /execruntime\("(.+?)"\)/)
326 $line =~ s/execruntime\("(.+?)"\)/`\1`/;
328 while ($line =~ /eval\("(.+?)"\)/)
330 print "Evaluating perl code for module \033[1;32m$module\033[0m ... ";
335 } until sysopen(TF, $tmpfile, O_RDWR|O_CREAT|O_EXCL|O_NOFOLLOW, 0700);
336 print "(Created and executed \033[1;32m$tmpfile\033[0m)\n";
339 my $replace = `perl $tmpfile`;
341 $line =~ s/eval\("(.+?)"\)/$replace/;
343 while ($line =~ /pkgconflibs\("(.+?)","(.+?)","(.+?)"\)/)
345 my $replace = pkgconfig_get_lib_dirs($1, $2, $3, $module);
346 $line =~ s/pkgconflibs\("(.+?)","(.+?)","(.+?)"\)/$replace/;
348 while ($line =~ /pkgconfversion\("(.+?)","(.+?)"\)/)
350 if (pkgconfig_check_version($1, $2, $module) != 1)
352 die "Version of package $1 is too old. Please upgrade it to version \033[1;32m$2\033[0m or greater and try again.";
354 # This doesnt actually get replaced with anything
355 $line =~ s/pkgconfversion\("(.+?)","(.+?)"\)//;
357 while ($line =~ /pkgconflibs\("(.+?)","(.+?)",""\)/)
359 my $replace = pkgconfig_get_lib_dirs($1, $2, "", $module);
360 $line =~ s/pkgconflibs\("(.+?)","(.+?)",""\)/$replace/;
362 while ($line =~ /pkgconfincludes\("(.+?)","(.+?)",""\)/)
364 my $replace = pkgconfig_get_include_dirs($1, $2, "", $module);
365 $line =~ s/pkgconfincludes\("(.+?)","(.+?)",""\)/$replace/;
367 while ($line =~ /pkgconfincludes\("(.+?)","(.+?)","(.+?)"\)/)
369 my $replace = pkgconfig_get_include_dirs($1, $2, $3, $module);
370 $line =~ s/pkgconfincludes\("(.+?)","(.+?)","(.+?)"\)/$replace/;
372 while ($line =~ /rpath\("(.+?)"\)/)
374 my $replace = make_rpath($1,$module);
375 $replace = "" if ($^O =~ /darwin/i);
376 $line =~ s/rpath\("(.+?)"\)/$replace/;
382 $err =~ s/at .+? line \d+.*//g;
383 print "\n\nConfiguration failed. The following error occured:\n\n$err\n";