1 # Perl module for UCW Configure Scripts
3 # (c) 2005 Martin Mares <mj@ucw.cz>
5 # This software may be freely distributed and used according to the terms
6 # of the GNU Lesser General Public License.
8 package UCW::Configure;
14 # The somewhat hairy Perl export mechanism
16 our ($VERSION, @ISA, @EXPORT, @EXPORT_OK, %EXPORT_TAGS);
19 @EXPORT = qw(&Init &Log &Notice &Warn &Fail &IsSet &IsGiven &Set &UnSet &Append &Override &Get &Test &Include &Finish &FindFile &TryFindFile &TryCmd &PkgConfig &TrivConfig &debPrint);
30 # print "$_: $vars{$_}\n" foreach( keys %vars );
38 print @_ if $vars{"VERBOSE"};
42 print "WARNING: ", @_;
46 Log("ERROR: " . (shift @_) . "\n");
52 return exists $vars{$x};
57 return exists $overriden{$x};
67 $y=1 unless defined $y;
68 $vars{$x}=$y unless $overriden{$x};
73 delete $vars{$x} unless $overriden{$x};
78 Set($x, (IsSet($x) ? (Get($x) . " $y") : $y));
83 $y=1 unless defined $y;
89 my ($var,$msg,$sub) = @_;
101 } elsif ($f !~ /^\// && -f (Get("SRCDIR")."/$f")) {
102 return Get("SRCDIR")."/$f";
111 defined ($F = TryFindFile($f)) or Fail "Cannot find file $f";
116 my ($srcdir,$defconfig) = @_;
119 print STDERR "Usage: [<srcdir>/]configure " . (defined $dc ? "[" : "") . "<config-name>" . (defined $dc ? "]" : "") .
120 " [<option>[=<value>] | -<option>] ...\n";
123 Set('CONFIG' => $defconfig) if defined $defconfig;
125 usage($defconfig) if $ARGV[0] eq "--help";
126 if (!defined($defconfig) || $ARGV[0] !~ /^-?[A-Z][A-Z0-9_]*(=|$)/) {
127 # This does not look like an option, so read it as a file name
128 Set('CONFIG' => shift @ARGV);
131 Set("SRCDIR", $srcdir);
133 foreach my $x (@ARGV) {
134 if ($x =~ /^(\w+)=(.*)/) {
136 } elsif ($x =~ /^-(\w+)$/) {
139 } elsif ($x =~ /^(\w+)$/) {
142 print STDERR "Invalid option $x\n";
147 defined Get("CONFIG") or usage($defconfig);
148 if (!TryFindFile(Get("CONFIG"))) {
149 TryFindFile(Get("CONFIG")."/config") or Fail "Cannot find configuration " . Get("CONFIG");
150 Override("CONFIG" => Get("CONFIG")."/config");
157 Notice "Loading configuration $f\n";
162 unshift @postconfigs, $_[0];
166 for my $post (@postconfigs) {
172 if (Get("SRCDIR") ne ".") {
173 Log "Preparing for compilation from directory " . Get("SRCDIR") . " to obj/ ... ";
174 -l "src" and unlink "src";
175 symlink Get("SRCDIR"), "src" or Fail "Cannot link source directory to src: $!";
176 Override("SRCDIR" => "src");
177 -l "Makefile" and unlink "Makefile";
178 -f "Makefile" and Fail "Makefile already exists";
179 symlink "src/Makefile", "Makefile" or Fail "Cannot link Makefile: $!";
181 Log "Preparing for compilation from current directory to obj/ ... ";
184 `rm -rf obj`; Fail "Cannot delete old obj directory" if $?;
186 -d "obj" or mkdir("obj", 0777) or Fail "Cannot create obj directory: $!";
187 -d "obj/ucw" or mkdir("obj/ucw", 0777) or Fail "Cannot create obj/ucw directory: $!";
190 Log "Generating autoconf.h ... ";
191 open X, ">obj/autoconf.h" or Fail $!;
192 print X "/* Generated automatically by $0, please don't touch manually. */\n";
193 foreach my $x (sort keys %vars) {
194 # Don't export variables which contain no underscores
195 next unless $x =~ /_/;
197 # Try to add quotes if necessary
198 $v = '"' . $v . '"' unless ($v =~ /^"/ || $v =~ /^\d*$/);
199 print X "#define $x $v\n";
204 Log "Generating config.mk ... ";
205 open X, ">obj/config.mk" or Fail $!;
206 print X "# Generated automatically by $0, please don't touch manually.\n";
207 foreach my $x (sort keys %vars) {
208 print X "$x=$vars{$x}\n";
210 print X "s=\${SRCDIR}\n";
219 defined $res or return;
221 return $res unless $?;
225 sub maybe_manually($) {
228 if (Get("$n")) { Log "YES (set manually)\n"; }
229 else { Log "NO (set manually)\n"; }
238 my $upper = $pkg; $upper =~ tr/a-z/A-Z/; $upper =~ s/[^0-9A-Z]+/_/g;
239 Log "Checking for package $pkg ... ";
240 maybe_manually("CONFIG_HAVE_$upper") and return Get("CONFIG_HAVE_$upper");
241 my $ver = TryCmd("pkg-config --modversion $pkg 2>/dev/null");
246 if (defined($opts{minversion})) {
247 my $min = $opts{minversion};
248 if (!defined TryCmd("pkg-config --atleast-version=$min $pkg")) {
249 Log("NO: version $ver is too old (need >= $min)\n");
253 Log("YES: version $ver\n");
254 Set("CONFIG_HAVE_$upper" => 1);
255 Set("CONFIG_VER_$upper" => $ver);
256 my $cf = TryCmd("pkg-config --cflags $pkg");
257 Set("${upper}_CFLAGS" => $cf) if defined $cf;
258 my $lf = TryCmd("pkg-config --libs $pkg");
259 Set("${upper}_LIBS" => $lf) if defined $lf;
265 return join(".", map { sprintf("%05s", $_) } split(/\./, $v));
271 my $upper = $pkg; $upper =~ tr/a-z/A-Z/; $upper =~ s/[^0-9A-Z]+/_/g;
272 Log "Checking for package $pkg ... ";
273 maybe_manually("CONFIG_HAVE_$upper") and return Get("CONFIG_HAVE_$upper");
274 my $pc = $opts{script};
275 my $ver = TryCmd("$pc --version 2>/dev/null");
280 if (defined($opts{minversion})) {
281 my $min = $opts{minversion};
282 if (ver_norm($ver) lt ver_norm($min)) {
283 Log("NO: version $ver is too old (need >= $min)\n");
287 Log("YES: version $ver\n");
288 Set("CONFIG_HAVE_$upper" => 1);
289 Set("CONFIG_VER_$upper" => $ver);
291 my $want = $opts{want};
292 defined $want or $want = ["cflags", "libs"];
294 my $uw = $w; $uw =~ tr/a-z-/A-Z_/;
295 my $cf = TryCmd("$pc --$w");
296 Set("${upper}_${uw}" => $cf) if defined $cf;