]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - make/utilities.pm
2e0f7e67cc631b409263001ac763e372d371d182
[user/henk/code/inspircd.git] / make / utilities.pm
1 package make::utilities;
2 use Exporter 'import';
3 use POSIX;
4 @EXPORT = qw(make_rpath pkgconfig_get_include_dirs pkgconfig_get_lib_dirs translate_functions promptstring);
5
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.
9
10 # \033[1;32msrc/Makefile\033[0m
11
12 my %already_added = ();
13
14 sub promptstring($$$)
15 {
16         my ($prompt, $configitem, $default) = @_;
17         print "\nPlease enter the $prompt?\n";
18         print "[\033[1;32m$default\033[0m] -> ";
19         chomp($var = <STDIN>);
20         if ($var eq "")
21         {
22                 $var = $default;
23         }
24         $main::config{$configitem} = $var;
25 }
26
27 sub make_rpath($;$)
28 {
29         my ($executable, $module) = @_;
30         chomp($data = `$executable`);
31         my $output = "";
32         while ($data =~ /-L(\S+)/)
33         {
34                 $libpath = $1;
35                 if (!exists $already_added{$libpath})
36                 {
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;
39                 }
40                 $output .= "-Wl,--rpath -Wl,$libpath -L$libpath ";
41                 $data =~ s/-L(\S+)//;
42         }
43         return $output;
44 }
45
46 sub extend_pkg_path()
47 {
48         if (!exists $ENV{PKG_CONFIG_PATH})
49         {
50                 $ENV{PKG_CONFIG_PATH} = "/usr/lib/pkgconfig:/usr/local/lib/pkgconfig:/usr/local/libdata/pkgconfig:/usr/X11R6/libdata/pkgconfig";
51         }
52         else
53         {
54                 $ENV{PKG_CONFIG_PATH} .= ":/usr/local/lib/pkgconfig:/usr/local/libdata/pkgconfig:/usr/X11R6/libdata/pkgconfig";
55         }
56 }
57
58 sub pkgconfig_get_include_dirs($$$;$)
59 {
60         my ($packagename, $headername, $defaults, $module) = @_;
61
62         if (exists $main::config{$key})
63         {
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};
68                 return $ret;
69         }
70
71         extend_pkg_path();
72
73         print "Locating include directory for package \033[1;32m$packagename\033[0m for module \033[1;32m$module\033[0m... ";
74
75         $v = `pkg-config --modversion $packagename 2>/dev/null`;
76         $ret = `pkg-config --cflags $packagename 2>/dev/null`;
77
78         if ((!defined $v) || ($v eq ""))
79         {
80                 $foo = `locate "$headername" | head -n 1`;
81                 $foo =~ /(.+)\Q$headername\E/;
82                 if (defined $1)
83                 {
84                         $foo = "-I$1";
85                 }
86                 else
87                 {
88                         $foo = "";
89                 }
90                 $ret = "$foo";
91         }
92         if (($defaults ne "") && (($ret eq "") || (!defined $ret)))
93         {
94                 $ret = "$foo " . $defaults;
95         }
96         chomp($ret);
97         if ((($ret eq " ") || (!defined $ret)) && ((!defined $v) || ($v eq "")))
98         {
99                 my $key = "default_includedir_$packagename";
100                 if (exists $main::config{$key})
101                 {
102                         $ret = $main::config{$key};
103                 }
104                 else
105                 {
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};
110                         return $ret;
111                 }
112         }
113         else
114         {
115                 chomp($v);
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";
119         }
120         return $ret;
121 }
122
123 sub pkgconfig_get_lib_dirs($$$;$)
124 {
125         my ($packagename, $libname, $defaults, $module) = @_;
126
127         if (exists $main::config{$key})
128         {
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};
133                 return $ret;
134         }
135
136         extend_pkg_path();
137
138         print "Locating library directory for package \033[1;32m$packagename\033[0m for module \033[1;32m$module\033[0m... ";
139
140         $v = `pkg-config --modversion $packagename 2>/dev/null`;
141         $ret = `pkg-config --libs $packagename 2>/dev/null`;
142
143         if ((!defined $v) || ($v eq ""))
144         {
145                 $foo = `locate "$libname" | head -n 1`;
146                 $foo =~ /(.+)\Q$libname\E/;
147                 if (defined $1)
148                 {
149                         $foo = "-L$1";
150                 }
151                 else
152                 {
153                         $foo = "";
154                 }
155                 $ret = "$foo";
156         }
157
158         if (($defaults ne "") && (($ret eq "") || (!defined $ret)))
159         {
160                 $ret = "$foo " . $defaults;
161         }
162         chomp($ret);
163         if ((($ret eq " ") || (!defined $ret)) && ((!defined $v) || ($v eq "")))
164         {
165                 my $key = "default_libdir_$packagename";
166                 if (exists $main::config{$key})
167                 {
168                         $ret = $main::config{$key};
169                 }
170                 else
171                 {
172                         $libname =~ s/^\///;
173                         promptstring("path to the directory containing $libname", $key, "/usr/lib");
174                         chomp($v);
175                         $main::config{$key} = "-L$main::config{$key}" . " $defaults -DVERSION_$libname=\"$v\"";
176                         $ret = $main::config{$key};
177                         return $ret;
178                 }
179         }
180         else
181         {
182                 chomp($v);
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\"";
186         }
187         return "$ret -DVERSION_$libname=\"$v\"";
188 }
189
190 # Translate a $CompileFlags etc line and parse out function calls
191 # to functions within these modules at configure time.
192 sub translate_functions($$)
193 {
194         my ($line,$module) = @_;
195
196         eval
197         {
198                 $module =~ /modules*\/(.+?)$/;
199                 $module = $1;
200
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\(.+?`.+?\)/))
207                 {
208                         die "Developers should no longer use backticks in configuration macros. Please use exec() and eval() macros instead. Offending line: $line (In module: $module)";
209                 }
210                 while ($line =~ /exec\("(.+?)"\)/)
211                 {
212                         print "Executing program for module \033[1;32m$module\033[0m ... \033[1;32m$1\033[0m\n";
213                         my $replace = `$1`;
214                         chomp($replace);
215                         $line =~ s/exec\("(.+?)"\)/$replace/;
216                 }
217                 while ($line =~ /eval\("(.+?)"\)/)
218                 {
219                         print "Evaluating perl code for module \033[1;32m$module\033[0m ... ";
220                         my $tmpfile;
221                         do
222                         {
223                                 $tmpfile = tmpnam();
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";
226                         print TF $1;
227                         close TF;
228                         my $replace = `perl $tmpfile`;
229                         chomp($replace);
230                         $line =~ s/eval\("(.+?)"\)/$replace/;
231                 }
232                 while ($line =~ /pkgconflibs\("(.+?)","(.+?)","(.+?)"\)/)
233                 {
234                         my $replace = pkgconfig_get_lib_dirs($1, $2, $3, $module);
235                         $line =~ s/pkgconflibs\("(.+?)","(.+?)","(.+?)"\)/$replace/;
236                 }
237                 while ($line =~ /pkgconflibs\("(.+?)","(.+?)",""\)/)
238                 {
239                         my $replace = pkgconfig_get_lib_dirs($1, $2, "", $module);
240                         $line =~ s/pkgconflibs\("(.+?)","(.+?)",""\)/$replace/;
241                 }
242                 while ($line =~ /pkgconfincludes\("(.+?)","(.+?)",""\)/)
243                 {
244                         my $replace = pkgconfig_get_include_dirs($1, $2, "", $module);
245                         $line =~ s/pkgconfincludes\("(.+?)","(.+?)",""\)/$replace/;
246                 }
247                 while ($line =~ /pkgconfincludes\("(.+?)","(.+?)","(.+?)"\)/)
248                 {
249                         my $replace = pkgconfig_get_include_dirs($1, $2, $3, $module);
250                         $line =~ s/pkgconfincludes\("(.+?)","(.+?)","(.+?)"\)/$replace/;
251                 }
252                 while ($line =~ /rpath\("(.+?)"\)/)
253                 {
254                         my $replace = make_rpath($1,$module);
255                         $line =~ s/rpath\("(.+?)"\)/$replace/;
256                 }
257         };
258         if ($@)
259         {
260                 print "\n\nConfiguration failed. The following error occured:\n\n$@\n";
261                 exit;
262         }
263         else
264         {
265                 return $line;
266         }
267 }
268
269 1;
270