]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - make/utilities.pm
5e0193361c88265fd2b4bb7b57943780b4ed9fc7
[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) = @_;
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 \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) = @_;
48         extend_pkg_path();
49
50         print "Locating include directory for package \033[1;32m$packagename\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) = @_;
86         extend_pkg_path();
87
88         print "Locating library directory for package \033[1;32m$packagename\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         eval
127         {
128                 my ($line,$module) = @_;
129
130                 # This is only a cursory check, just designed to catch casual accidental use of backticks.
131                 # There are pleanty of ways around it, but its not supposed to be for security, just checking
132                 # that people are using the new configuration api as theyre supposed to and not just using
133                 # backticks instead of eval(), being as eval has accountability. People wanting to get around
134                 # the accountability will do so anyway.
135                 if (($line =~ /`/) && ($line !~ /eval\(.+?`.+?\)/))
136                 {
137                         die "Developers should no longer use backticks in configuration macros. Please use exec() and eval() macros instead. Offending line: $line (In module: $module)";
138                 }
139                 while ($line =~ /exec\("(.+?)"\)/)
140                 {
141                         print "Executing program ... \033[1;32m$1\033[0m\n";
142                         my $replace = `$1`;
143                         chomp($replace);
144                         $line =~ s/exec\("(.+?)"\)/$replace/;
145                 }
146                 while ($line =~ /eval\("(.+?)"\)/)
147                 {
148                         print "Evaluating perl code ... ";
149                         my $tmpfile;
150                         do
151                         {
152                                 $tmpfile = tmpnam();
153                         } until sysopen(TF, $tmpfile, O_RDWR|O_CREAT|O_EXCL, 0700);
154                         print "(Created and executed \033[1;32m$tmpfile)\033[0m\n";
155                         print TF $1;
156                         close TF;
157                         my $replace = `perl $tmpfile`;
158                         chomp($replace);
159                         $line =~ s/eval\("(.+?)"\)/$replace/;
160                 }
161                 while ($line =~ /pkgconflibs\("(.+?)","(.+?)","(.+?)"\)/)
162                 {
163                         my $replace = pkgconfig_get_lib_dirs($1, $2, $3);
164                         $line =~ s/pkgconflibs\("(.+?)","(.+?)","(.+?)"\)/$replace/;
165                 }
166                 while ($line =~ /pkgconflibs\("(.+?)","(.+?)",""\)/)
167                 {
168                         my $replace = pkgconfig_get_lib_dirs($1, $2, "");
169                         $line =~ s/pkgconflibs\("(.+?)","(.+?)",""\)/$replace/;
170                 }
171                 while ($line =~ /pkgconfincludes\("(.+?)","(.+?)",""\)/)
172                 {
173                         my $replace = pkgconfig_get_include_dirs($1, $2, "");
174                         $line =~ s/pkgconfincludes\("(.+?)","(.+?)",""\)/$replace/;
175                 }
176                 while ($line =~ /pkgconfincludes\("(.+?)","(.+?)","(.+?)"\)/)
177                 {
178                         my $replace = pkgconfig_get_include_dirs($1, $2, $3);
179                         $line =~ s/pkgconfincludes\("(.+?)","(.+?)","(.+?)"\)/$replace/;
180                 }
181                 while ($line =~ /rpath\("(.+?)"\)/)
182                 {
183                         my $replace = make_rpath($1);
184                         $line =~ s/rpath\("(.+?)"\)/$replace/;
185                 }
186                 return $line;
187         };
188         if ($@)
189         {
190                 print "\n\nConfiguration failed. The following error occured:\n\n$@\n";
191                 exit;
192         }
193 }
194
195 1;
196