1 package make::utilities;
5 @EXPORT = qw(make_rpath pkgconfig_get_include_dirs pkgconfig_get_lib_dirs translate_functions promptstring);
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.
11 # \033[1;32msrc/Makefile\033[0m
13 my %already_added = ();
15 sub promptstring($$$$$)
17 my ($prompt, $configitem, $default, $package, $commandlineswitch) = @_;
19 if (!$main::interactive)
21 undef $opt_commandlineswitch;
22 GetOptions ("$commandlineswitch" => \$opt_commandlineswitch);
23 if (defined $opt_commandlineswitch)
25 $var = $opt_commandlineswitch;
29 die "Could not detect $package! Please specify the $prompt via the command line option $commandlineswitch=\"/path/to/file\"";
34 print "\nPlease enter the $prompt?\n";
35 print "[\033[1;32m$default\033[0m] -> ";
36 chomp($var = <STDIN>);
42 $main::config{$configitem} = $var;
47 my ($executable, $module) = @_;
48 chomp($data = `$executable`);
50 while ($data =~ /-L(\S+)/)
53 if (!exists $already_added{$libpath})
55 print "Adding extra library path to \033[1;32m$module\033[0m ... \033[1;32m$libpath\033[0m\n";
56 $already_added{$libpath} = 1;
58 $output .= "-Wl,--rpath -Wl,$libpath -L$libpath ";
66 if (!exists $ENV{PKG_CONFIG_PATH})
68 $ENV{PKG_CONFIG_PATH} = "/usr/lib/pkgconfig:/usr/local/lib/pkgconfig:/usr/local/libdata/pkgconfig:/usr/X11R6/libdata/pkgconfig";
72 $ENV{PKG_CONFIG_PATH} .= ":/usr/local/lib/pkgconfig:/usr/local/libdata/pkgconfig:/usr/X11R6/libdata/pkgconfig";
76 sub pkgconfig_get_include_dirs($$$;$)
78 my ($packagename, $headername, $defaults, $module) = @_;
80 my $key = "default_includedir_$packagename";
81 if (exists $main::config{$key})
83 print "Locating include directory for package \033[1;32m$packagename\033[0m for module \033[1;32m$module\033[0m... ";
84 $ret = $main::config{$key};
85 print "\033[1;32m$ret\033[0m (cached)\n";
91 print "Locating include directory for package \033[1;32m$packagename\033[0m for module \033[1;32m$module\033[0m... ";
93 $v = `pkg-config --modversion $packagename 2>/dev/null`;
94 $ret = `pkg-config --cflags $packagename 2>/dev/null`;
96 if ((!defined $v) || ($v eq ""))
98 $foo = `locate "$headername" | head -n 1`;
99 $foo =~ /(.+)\Q$headername\E/;
110 if (($defaults ne "") && (($ret eq "") || (!defined $ret)))
112 $ret = "$foo " . $defaults;
115 if ((($ret eq " ") || (!defined $ret)) && ((!defined $v) || ($v eq "")))
117 my $key = "default_includedir_$packagename";
118 if (exists $main::config{$key})
120 $ret = $main::config{$key};
124 $headername =~ s/^\///;
125 promptstring("path to the directory containing $headername", $key, "/usr/include",$packagename,"$packagename-includes");
126 $packagename =~ tr/a-z/A-Z/;
127 $main::config{$key} = "-I$main::config{$key}" . " $defaults -DVERSION_$packagename=\"$v\"";
128 $main::config{$key} =~ s/^\s+//g;
129 $ret = $main::config{$key};
136 my $key = "default_includedir_$packagename";
137 $packagename =~ tr/a-z/A-Z/;
138 $main::config{$key} = "$ret -DVERSION_$packagename=\"$v\"";
139 $main::config{$key} =~ s/^\s+//g;
140 $ret = $main::config{$key};
141 print "\033[1;32m$ret\033[0m (version $v)\n";
147 sub pkgconfig_get_lib_dirs($$$;$)
149 my ($packagename, $libname, $defaults, $module) = @_;
151 my $key = "default_libdir_$packagename";
152 if (exists $main::config{$key})
154 print "Locating library directory for package \033[1;32m$packagename\033[0m for module \033[1;32m$module\033[0m... ";
155 $ret = $main::config{$key};
156 print "\033[1;32m$ret\033[0m (cached)\n";
162 print "Locating library directory for package \033[1;32m$packagename\033[0m for module \033[1;32m$module\033[0m... ";
164 $v = `pkg-config --modversion $packagename 2>/dev/null`;
165 $ret = `pkg-config --libs $packagename 2>/dev/null`;
167 if ((!defined $v) || ($v eq ""))
169 $foo = `locate "$libname" | head -n 1`;
170 $foo =~ /(.+)\Q$libname\E/;
182 if (($defaults ne "") && (($ret eq "") || (!defined $ret)))
184 $ret = "$foo " . $defaults;
187 if ((($ret eq " ") || (!defined $ret)) && ((!defined $v) || ($v eq "")))
189 my $key = "default_libdir_$packagename";
190 if (exists $main::config{$key})
192 $ret = $main::config{$key};
197 promptstring("path to the directory containing $libname", $key, "/usr/lib",$packagename,"$packagename-libs");
198 $main::config{$key} = "-L$main::config{$key}" . " $defaults";
199 $main::config{$key} =~ s/^\s+//g;
200 $ret = $main::config{$key};
207 print "\033[1;32m$ret\033[0m (version $v)\n";
208 my $key = "default_libdir_$packagename";
209 $main::config{$key} = $ret;
210 $main::config{$key} =~ s/^\s+//g;
213 return "$ret -DVERSION_$libname=\"$v\"";
216 # Translate a $CompileFlags etc line and parse out function calls
217 # to functions within these modules at configure time.
218 sub translate_functions($$)
220 my ($line,$module) = @_;
224 $module =~ /modules*\/(.+?)$/;
227 # This is only a cursory check, just designed to catch casual accidental use of backticks.
228 # There are pleanty of ways around it, but its not supposed to be for security, just checking
229 # that people are using the new configuration api as theyre supposed to and not just using
230 # backticks instead of eval(), being as eval has accountability. People wanting to get around
231 # the accountability will do so anyway.
232 if (($line =~ /`/) && ($line !~ /eval\(.+?`.+?\)/))
234 die "Developers should no longer use backticks in configuration macros. Please use exec() and eval() macros instead. Offending line: $line (In module: $module)";
236 while ($line =~ /exec\("(.+?)"\)/)
238 print "Executing program for module \033[1;32m$module\033[0m ... \033[1;32m$1\033[0m\n";
241 $line =~ s/exec\("(.+?)"\)/$replace/;
243 while ($line =~ /eval\("(.+?)"\)/)
245 print "Evaluating perl code for module \033[1;32m$module\033[0m ... ";
250 } until sysopen(TF, $tmpfile, O_RDWR|O_CREAT|O_EXCL|O_NOFOLLOW, 0700);
251 print "(Created and executed \033[1;32m$tmpfile\033[0m)\n";
254 my $replace = `perl $tmpfile`;
256 $line =~ s/eval\("(.+?)"\)/$replace/;
258 while ($line =~ /pkgconflibs\("(.+?)","(.+?)","(.+?)"\)/)
260 my $replace = pkgconfig_get_lib_dirs($1, $2, $3, $module);
261 $line =~ s/pkgconflibs\("(.+?)","(.+?)","(.+?)"\)/$replace/;
263 while ($line =~ /pkgconflibs\("(.+?)","(.+?)",""\)/)
265 my $replace = pkgconfig_get_lib_dirs($1, $2, "", $module);
266 $line =~ s/pkgconflibs\("(.+?)","(.+?)",""\)/$replace/;
268 while ($line =~ /pkgconfincludes\("(.+?)","(.+?)",""\)/)
270 my $replace = pkgconfig_get_include_dirs($1, $2, "", $module);
271 $line =~ s/pkgconfincludes\("(.+?)","(.+?)",""\)/$replace/;
273 while ($line =~ /pkgconfincludes\("(.+?)","(.+?)","(.+?)"\)/)
275 my $replace = pkgconfig_get_include_dirs($1, $2, $3, $module);
276 $line =~ s/pkgconfincludes\("(.+?)","(.+?)","(.+?)"\)/$replace/;
278 while ($line =~ /rpath\("(.+?)"\)/)
280 my $replace = make_rpath($1,$module);
281 $line =~ s/rpath\("(.+?)"\)/$replace/;
286 print "\n\nConfiguration failed. The following error occured:\n\n$@\n";