1 package make::utilities;
4 @EXPORT = qw(make_rpath pkgconfig_get_include_dirs pkgconfig_get_lib_dirs translate_functions promptstring);
6 # Parse the output of a *_config program,
7 # such as pcre_config, take out the -L
8 # directive and return an rpath for it.
10 # \033[1;32msrc/Makefile\033[0m
12 my %already_added = ();
16 my ($prompt, $configitem, $default) = @_;
17 print "\nPlease enter the $prompt?\n";
18 print "[\033[1;32m$default\033[0m] -> ";
19 chomp($var = <STDIN>);
24 $main::config{$configitem} = $var;
29 my ($executable, $module) = @_;
30 chomp($data = `$executable`);
32 while ($data =~ /-L(\S+)/)
35 if (!exists $already_added{$libpath})
37 print "Adding extra library path to \033[1;32m$module\033[0m ... \033[1;32m$libpath\033[0m\n";
38 $already_added{$libpath} = 1;
40 $output .= "-Wl,--rpath -Wl,$libpath -L$libpath ";
48 if (!exists $ENV{PKG_CONFIG_PATH})
50 $ENV{PKG_CONFIG_PATH} = "/usr/lib/pkgconfig:/usr/local/lib/pkgconfig:/usr/local/libdata/pkgconfig:/usr/X11R6/libdata/pkgconfig";
54 $ENV{PKG_CONFIG_PATH} .= ":/usr/local/lib/pkgconfig:/usr/local/libdata/pkgconfig:/usr/X11R6/libdata/pkgconfig";
58 sub pkgconfig_get_include_dirs($$$;$)
60 my ($packagename, $headername, $defaults, $module) = @_;
62 if (exists $main::config{$key})
64 print "Locating include directory for package \033[1;32m$packagename\033[0m for module \033[1;32m$module\033[0m... ";
65 print "\033[1;32m$ret\033[0m (cached)\n";
66 my $key = "default_includedir_$packagename";
67 $ret = $main::config{$key};
73 print "Locating include directory for package \033[1;32m$packagename\033[0m for module \033[1;32m$module\033[0m... ";
75 $v = `pkg-config --modversion $packagename 2>/dev/null`;
76 $ret = `pkg-config --cflags $packagename 2>/dev/null`;
78 if ((!defined $v) || ($v eq ""))
80 $foo = `locate "$headername" | head -n 1`;
81 $foo =~ /(.+)\Q$headername\E/;
92 if (($defaults ne "") && (($ret eq "") || (!defined $ret)))
94 $ret = "$foo " . $defaults;
97 if ((($ret eq " ") || (!defined $ret)) && ((!defined $v) || ($v eq "")))
99 my $key = "default_includedir_$packagename";
100 if (exists $main::config{$key})
102 $ret = $main::config{$key};
106 $headername =~ s/^\///;
107 promptstring("path to the directory containing $headername", $key, "/usr/include");
108 $main::config{$key} = "-I$main::config{$key}" . " $defaults -DVERSION_$libname=\"$v\"";
109 $ret = $main::config{$key};
116 my $key = "default_includedir_$packagename";
117 $main::config{$key} = "$ret -DVERSION_$libname=\"$v\"";
118 print "\033[1;32m$ret\033[0m (version $v)\n";
123 sub pkgconfig_get_lib_dirs($$$;$)
125 my ($packagename, $libname, $defaults, $module) = @_;
127 if (exists $main::config{$key})
129 print "Locating library directory for package \033[1;32m$packagename\033[0m for module \033[1;32m$module\033[0m... ";
130 print "\033[1;32m$ret\033[0m (cached)\n";
131 my $key = "default_libdir_$packagename";
132 $ret = $main::config{$key};
138 print "Locating library directory for package \033[1;32m$packagename\033[0m for module \033[1;32m$module\033[0m... ";
140 $v = `pkg-config --modversion $packagename 2>/dev/null`;
141 $ret = `pkg-config --libs $packagename 2>/dev/null`;
143 if ((!defined $v) || ($v eq ""))
145 $foo = `locate "$libname" | head -n 1`;
146 $foo =~ /(.+)\Q$libname\E/;
158 if (($defaults ne "") && (($ret eq "") || (!defined $ret)))
160 $ret = "$foo " . $defaults;
163 if ((($ret eq " ") || (!defined $ret)) && ((!defined $v) || ($v eq "")))
165 my $key = "default_libdir_$packagename";
166 if (exists $main::config{$key})
168 $ret = $main::config{$key};
173 promptstring("path to the directory containing $libname", $key, "/usr/lib");
175 $main::config{$key} = "-L$main::config{$key}" . " $defaults -DVERSION_$libname=\"$v\"";
176 $ret = $main::config{$key};
183 print "\033[1;32m$ret\033[0m (version $v)\n";
184 my $key = "default_libdir_$packagename";
185 $main::config{$key} = "$ret -DVERSION_$libname=\"$v\"";
187 return "$ret -DVERSION_$libname=\"$v\"";
190 # Translate a $CompileFlags etc line and parse out function calls
191 # to functions within these modules at configure time.
192 sub translate_functions($$)
194 my ($line,$module) = @_;
198 $module =~ /modules*\/(.+?)$/;
201 # This is only a cursory check, just designed to catch casual accidental use of backticks.
202 # There are pleanty of ways around it, but its not supposed to be for security, just checking
203 # that people are using the new configuration api as theyre supposed to and not just using
204 # backticks instead of eval(), being as eval has accountability. People wanting to get around
205 # the accountability will do so anyway.
206 if (($line =~ /`/) && ($line !~ /eval\(.+?`.+?\)/))
208 die "Developers should no longer use backticks in configuration macros. Please use exec() and eval() macros instead. Offending line: $line (In module: $module)";
210 while ($line =~ /exec\("(.+?)"\)/)
212 print "Executing program for module \033[1;32m$module\033[0m ... \033[1;32m$1\033[0m\n";
215 $line =~ s/exec\("(.+?)"\)/$replace/;
217 while ($line =~ /eval\("(.+?)"\)/)
219 print "Evaluating perl code for module \033[1;32m$module\033[0m ... ";
224 } until sysopen(TF, $tmpfile, O_RDWR|O_CREAT|O_EXCL|O_NOFOLLOW, 0700);
225 print "(Created and executed \033[1;32m$tmpfile\033[0m)\n";
228 my $replace = `perl $tmpfile`;
230 $line =~ s/eval\("(.+?)"\)/$replace/;
232 while ($line =~ /pkgconflibs\("(.+?)","(.+?)","(.+?)"\)/)
234 my $replace = pkgconfig_get_lib_dirs($1, $2, $3, $module);
235 $line =~ s/pkgconflibs\("(.+?)","(.+?)","(.+?)"\)/$replace/;
237 while ($line =~ /pkgconflibs\("(.+?)","(.+?)",""\)/)
239 my $replace = pkgconfig_get_lib_dirs($1, $2, "", $module);
240 $line =~ s/pkgconflibs\("(.+?)","(.+?)",""\)/$replace/;
242 while ($line =~ /pkgconfincludes\("(.+?)","(.+?)",""\)/)
244 my $replace = pkgconfig_get_include_dirs($1, $2, "", $module);
245 $line =~ s/pkgconfincludes\("(.+?)","(.+?)",""\)/$replace/;
247 while ($line =~ /pkgconfincludes\("(.+?)","(.+?)","(.+?)"\)/)
249 my $replace = pkgconfig_get_include_dirs($1, $2, $3, $module);
250 $line =~ s/pkgconfincludes\("(.+?)","(.+?)","(.+?)"\)/$replace/;
252 while ($line =~ /rpath\("(.+?)"\)/)
254 my $replace = make_rpath($1,$module);
255 $line =~ s/rpath\("(.+?)"\)/$replace/;
260 print "\n\nConfiguration failed. The following error occured:\n\n$@\n";