1 package make::utilities;
5 @EXPORT = qw(make_rpath pkgconfig_get_include_dirs pkgconfig_get_lib_dirs translate_functions);
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 = ();
17 my ($executable, $module) = @_;
18 chomp($data = `$executable`);
20 while ($data =~ /-L(\S+)/)
23 if (!exists $already_added{$libpath})
25 print "Adding extra library path to \033[1;32m$module\033[0m ... \033[1;32m$libpath\033[0m\n";
26 $already_added{$libpath} = 1;
28 $output .= "-Wl,--rpath -Wl,$libpath -L$libpath ";
36 if (!exists $ENV{PKG_CONFIG_PATH})
38 $ENV{PKG_CONFIG_PATH} = "/usr/lib/pkgconfig:/usr/local/lib/pkgconfig:/usr/local/libdata/pkgconfig:/usr/X11R6/libdata/pkgconfig";
42 $ENV{PKG_CONFIG_PATH} .= ":/usr/local/lib/pkgconfig:/usr/local/libdata/pkgconfig:/usr/X11R6/libdata/pkgconfig";
46 sub pkgconfig_get_include_dirs($$$;$)
48 my ($packagename, $headername, $defaults, $module) = @_;
50 if (exists $config{$key})
52 my $key = "default_includedir_$packagename";
59 print "Locating include directory for package \033[1;32m$packagename\033[0m for module \033[1;32m$module\033[0m... ";
61 $ret = `pkg-config --cflags $packagename 2>/dev/null`;
62 if ((!defined $ret) || ($ret eq ""))
64 $foo = `locate "$headername" | head -n 1`;
65 $foo =~ /(.+)\Q$headername\E/;
76 if (($defaults ne "") && (($ret eq "") || (!defined $ret)))
78 $ret = "$foo " . $defaults;
81 if (($ret eq " ") || (!defined $ret))
83 my $key = "default_includedir_$packagename";
84 if (exists $config{$key})
90 $headername =~ s/^\///;
91 promptstring("path to the directory containing $headername", $key, "/usr/include");
97 print "\033[1;32m$ret\033[0m\n";
102 sub pkgconfig_get_lib_dirs($$$;$)
104 my ($packagename, $libname, $defaults, $module) = @_;
106 if (exists $config{$key})
108 my $key = "default_libdir_$packagename";
109 $ret = $config{$key};
115 print "Locating library directory for package \033[1;32m$packagename\033[0m for module \033[1;32m$module\033[0m... ";
117 $ret = `pkg-config --libs $packagename 2>/dev/null`;
118 if ((!defined $ret) || ($ret eq ""))
120 $foo = `locate "$libname" | head -n 1`;
121 $foo =~ /(.+)\Q$libname\E/;
133 if (($defaults ne "") && (($ret eq "") || (!defined $ret)))
135 $ret = "$foo " . $defaults;
138 if (($ret eq " ") || (!defined $ret))
140 my $key = "default_libdir_$packagename";
141 if (exists $config{$key})
143 $ret = $config{$key};
148 promptstring("path to the directory containing $libname", $key, "/usr/lib");
149 $ret = $config{$key};
154 print "\033[1;32m$ret\033[0m\n";
159 # Translate a $CompileFlags etc line and parse out function calls
160 # to functions within these modules at configure time.
161 sub translate_functions($$)
163 my ($line,$module) = @_;
167 $module =~ /modules*\/(.+?)$/;
170 # This is only a cursory check, just designed to catch casual accidental use of backticks.
171 # There are pleanty of ways around it, but its not supposed to be for security, just checking
172 # that people are using the new configuration api as theyre supposed to and not just using
173 # backticks instead of eval(), being as eval has accountability. People wanting to get around
174 # the accountability will do so anyway.
175 if (($line =~ /`/) && ($line !~ /eval\(.+?`.+?\)/))
177 die "Developers should no longer use backticks in configuration macros. Please use exec() and eval() macros instead. Offending line: $line (In module: $module)";
179 while ($line =~ /exec\("(.+?)"\)/)
181 print "Executing program for module \033[1;32m$module\033[0m ... \033[1;32m$1\033[0m\n";
184 $line =~ s/exec\("(.+?)"\)/$replace/;
186 while ($line =~ /eval\("(.+?)"\)/)
188 print "Evaluating perl code for module \033[1;32m$module\033[0m ... ";
193 } until sysopen(TF, $tmpfile, O_RDWR|O_CREAT|O_EXCL|O_NOFOLLOW, 0700);
194 print "(Created and executed \033[1;32m$tmpfile\033[0m)\n";
197 my $replace = `perl $tmpfile`;
199 $line =~ s/eval\("(.+?)"\)/$replace/;
201 while ($line =~ /pkgconflibs\("(.+?)","(.+?)","(.+?)"\)/)
203 my $replace = pkgconfig_get_lib_dirs($1, $2, $3, $module);
204 $line =~ s/pkgconflibs\("(.+?)","(.+?)","(.+?)"\)/$replace/;
206 while ($line =~ /pkgconflibs\("(.+?)","(.+?)",""\)/)
208 my $replace = pkgconfig_get_lib_dirs($1, $2, "", $module);
209 $line =~ s/pkgconflibs\("(.+?)","(.+?)",""\)/$replace/;
211 while ($line =~ /pkgconfincludes\("(.+?)","(.+?)",""\)/)
213 my $replace = pkgconfig_get_include_dirs($1, $2, "", $module);
214 $line =~ s/pkgconfincludes\("(.+?)","(.+?)",""\)/$replace/;
216 while ($line =~ /pkgconfincludes\("(.+?)","(.+?)","(.+?)"\)/)
218 my $replace = pkgconfig_get_include_dirs($1, $2, $3, $module);
219 $line =~ s/pkgconfincludes\("(.+?)","(.+?)","(.+?)"\)/$replace/;
221 while ($line =~ /rpath\("(.+?)"\)/)
223 my $replace = make_rpath($1,$module);
224 $line =~ s/rpath\("(.+?)"\)/$replace/;
229 print "\n\nConfiguration failed. The following error occured:\n\n$@\n";