]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - make/utilities.pm
OMG SQLITE3 support, almost there now =)
[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);
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 make_rpath($;$)
15 {
16         my ($executable, $module) = @_;
17         chomp($data = `$executable`);
18         my $output = "";
19         while ($data =~ /-L(\S+)/)
20         {
21                 $libpath = $1;
22                 if (!exists $already_added{$libpath})
23                 {
24                         print "Adding extra library path to \033[1;32m$module\033[0m ... \033[1;32m$libpath\033[0m\n";
25                         $already_added{$libpath} = 1;
26                 }
27                 $output .= "-Wl,--rpath -Wl,$libpath -L$libpath ";
28                 $data =~ s/-L(\S+)//;
29         }
30         return $output;
31 }
32
33 sub extend_pkg_path()
34 {
35         if (!exists $ENV{PKG_CONFIG_PATH})
36         {
37                 $ENV{PKG_CONFIG_PATH} = "/usr/lib/pkgconfig:/usr/local/lib/pkgconfig:/usr/local/libdata/pkgconfig:/usr/X11R6/libdata/pkgconfig";
38         }
39         else
40         {
41                 $ENV{PKG_CONFIG_PATH} .= ":/usr/local/lib/pkgconfig:/usr/local/libdata/pkgconfig:/usr/X11R6/libdata/pkgconfig";
42         }
43 }
44
45 sub pkgconfig_get_include_dirs($$$;$)
46 {
47         my ($packagename, $headername, $defaults, $module) = @_;
48         extend_pkg_path();
49
50         print "Locating include directory for package \033[1;32m$packagename\033[0m for module \033[1;32m$module\033[0m... ";
51
52         $ret = `pkg-config --cflags $packagename 2>/dev/null`;
53         if ((!defined $ret) || ($ret eq ""))
54         {
55                 $foo = `locate "$headername" | head -n 1`;
56                 $foo =~ /(.+)\Q$headername\E/;
57                 if (defined $1)
58                 {
59                         $foo = "-I$1";
60                 }
61                 else
62                 {
63                         $foo = "";
64                 }
65                 $ret = "$foo";
66         }
67         if (($defaults ne "") && (($ret eq "") || (!defined $ret)))
68         {
69                 $ret = "$foo " . $defaults;
70         }
71         chomp($ret);
72         if (($ret eq " ") || (!defined $ret))
73         {
74                 print "\033[1;32mUsing defaults\033[0m\n";
75         }
76         else
77         {
78                 print "\033[1;32m$ret\033[0m\n";
79         }
80         return $ret;
81 }
82
83 sub pkgconfig_get_lib_dirs($$$;$)
84 {
85         my ($packagename, $libname, $defaults, $module) = @_;
86         extend_pkg_path();
87
88         print "Locating library directory for package \033[1;32m$packagename\033[0m for module \033[1;32m$module\033[0m... ";
89
90         $ret = `pkg-config --libs $packagename 2>/dev/null`;
91         if ((!defined $ret) || ($ret eq ""))
92         {
93                 $foo = `locate "$libname" | head -n 1`;
94                 $foo =~ /(.+)\Q$libname\E/;
95                 if (defined $1)
96                 {
97                         $foo = "-L$1";
98                 }
99                 else
100                 {
101                         $foo = "";
102                 }
103                 $ret = "$foo";
104         }
105
106         if (($defaults ne "") && (($ret eq "") || (!defined $ret)))
107         {
108                 $ret = "$foo " . $defaults;
109         }
110         chomp($ret);
111         if (($ret eq " ") || (!defined $ret))
112         {
113                 print "\033[1;32mUsing defaults\033[0m\n";
114         }
115         else
116         {
117                 print "\033[1;32m$ret\033[0m\n";
118         }
119         return $ret;
120 }
121
122 # Translate a $CompileFlags etc line and parse out function calls
123 # to functions within these modules at configure time.
124 sub translate_functions($$)
125 {
126         my ($line,$module) = @_;
127
128         eval
129         {
130                 $module =~ /modules*\/(.+?)$/;
131                 $module = $1;
132
133                 # This is only a cursory check, just designed to catch casual accidental use of backticks.
134                 # There are pleanty of ways around it, but its not supposed to be for security, just checking
135                 # that people are using the new configuration api as theyre supposed to and not just using
136                 # backticks instead of eval(), being as eval has accountability. People wanting to get around
137                 # the accountability will do so anyway.
138                 if (($line =~ /`/) && ($line !~ /eval\(.+?`.+?\)/))
139                 {
140                         die "Developers should no longer use backticks in configuration macros. Please use exec() and eval() macros instead. Offending line: $line (In module: $module)";
141                 }
142                 while ($line =~ /exec\("(.+?)"\)/)
143                 {
144                         print "Executing program for module \033[1;32m$module\033[0m ... \033[1;32m$1\033[0m\n";
145                         my $replace = `$1`;
146                         chomp($replace);
147                         $line =~ s/exec\("(.+?)"\)/$replace/;
148                 }
149                 while ($line =~ /eval\("(.+?)"\)/)
150                 {
151                         print "Evaluating perl code for module \033[1;32m$module\033[0m ... ";
152                         my $tmpfile;
153                         do
154                         {
155                                 $tmpfile = tmpnam();
156                         } until sysopen(TF, $tmpfile, O_RDWR|O_CREAT|O_EXCL, 0700);
157                         print "(Created and executed \033[1;32m$tmpfile\033[0m)\n";
158                         print TF $1;
159                         close TF;
160                         my $replace = `perl $tmpfile`;
161                         chomp($replace);
162                         $line =~ s/eval\("(.+?)"\)/$replace/;
163                 }
164                 while ($line =~ /pkgconflibs\("(.+?)","(.+?)","(.+?)"\)/)
165                 {
166                         my $replace = pkgconfig_get_lib_dirs($1, $2, $3, $module);
167                         $line =~ s/pkgconflibs\("(.+?)","(.+?)","(.+?)"\)/$replace/;
168                 }
169                 while ($line =~ /pkgconflibs\("(.+?)","(.+?)",""\)/)
170                 {
171                         my $replace = pkgconfig_get_lib_dirs($1, $2, "", $module);
172                         $line =~ s/pkgconflibs\("(.+?)","(.+?)",""\)/$replace/;
173                 }
174                 while ($line =~ /pkgconfincludes\("(.+?)","(.+?)",""\)/)
175                 {
176                         my $replace = pkgconfig_get_include_dirs($1, $2, "", $module);
177                         $line =~ s/pkgconfincludes\("(.+?)","(.+?)",""\)/$replace/;
178                 }
179                 while ($line =~ /pkgconfincludes\("(.+?)","(.+?)","(.+?)"\)/)
180                 {
181                         my $replace = pkgconfig_get_include_dirs($1, $2, $3, $module);
182                         $line =~ s/pkgconfincludes\("(.+?)","(.+?)","(.+?)"\)/$replace/;
183                 }
184                 while ($line =~ /rpath\("(.+?)"\)/)
185                 {
186                         my $replace = make_rpath($1,$module);
187                         $line =~ s/rpath\("(.+?)"\)/$replace/;
188                 }
189         };
190         if ($@)
191         {
192                 print "\n\nConfiguration failed. The following error occured:\n\n$@\n";
193                 exit;
194         }
195         else
196         {
197                 return $line;
198         }
199 }
200
201 1;
202