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);
29 # print "$_: $vars{$_}\n" foreach( keys %vars );
37 print @_ if $vars{"VERBOSE"};
41 print "WARNING: ", @_;
45 Log("ERROR: " . (shift @_) . "\n");
51 return exists $vars{$x};
56 return exists $overriden{$x};
66 $y=1 unless defined $y;
67 $vars{$x}=$y unless $overriden{$x};
72 delete $vars{$x} unless $overriden{$x};
77 Set($x, (IsSet($x) ? (Get($x) . " $y") : $y));
82 $y=1 unless defined $y;
88 my ($var,$msg,$sub) = @_;
100 } elsif ($f !~ /^\// && -f (Get("SRCDIR")."/$f")) {
101 return Get("SRCDIR")."/$f";
110 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";
164 if (Get("SRCDIR") ne ".") {
165 Log "Preparing for compilation from directory " . Get("SRCDIR") . " to obj/ ... ";
166 -l "src" and unlink "src";
167 symlink Get("SRCDIR"), "src" or Fail "Cannot link source directory to src: $!";
168 Override("SRCDIR" => "src");
169 -l "Makefile" and unlink "Makefile";
170 -f "Makefile" and Fail "Makefile already exists";
171 symlink "src/Makefile", "Makefile" or Fail "Cannot link Makefile: $!";
173 Log "Preparing for compilation from current directory to obj/ ... ";
176 `rm -rf obj`; Fail "Cannot delete old obj directory" if $?;
178 -d "obj" or mkdir("obj", 0777) or Fail "Cannot create obj directory: $!";
179 -d "obj/ucw" or mkdir("obj/ucw", 0777) or Fail "Cannot create obj/ucw directory: $!";
182 Log "Generating autoconf.h ... ";
183 open X, ">obj/autoconf.h" or Fail $!;
184 print X "/* Generated automatically by $0, please don't touch manually. */\n";
185 foreach my $x (sort keys %vars) {
186 # Don't export variables which contain no underscores
187 next unless $x =~ /_/;
189 # Try to add quotes if necessary
190 $v = '"' . $v . '"' unless ($v =~ /^"/ || $v =~ /^\d*$/);
191 print X "#define $x $v\n";
196 Log "Generating config.mk ... ";
197 open X, ">obj/config.mk" or Fail $!;
198 print X "# Generated automatically by $0, please don't touch manually.\n";
199 foreach my $x (sort keys %vars) {
200 print X "$x=$vars{$x}\n";
202 print X "s=\${SRCDIR}\n";
211 defined $res or return;
213 return $res unless $?;
217 sub maybe_manually($) {
220 if (Get("$n")) { Log "YES (set manually)\n"; }
221 else { Log "NO (set manually)\n"; }
230 my $upper = $pkg; $upper =~ tr/a-z/A-Z/; $upper =~ s/[^0-9A-Z]+/_/g;
231 Log "Checking for package $pkg ... ";
232 maybe_manually("CONFIG_HAVE_$upper") and return Get("CONFIG_HAVE_$upper");
233 my $ver = TryCmd("pkg-config --modversion $pkg 2>/dev/null");
238 if (defined($opts{minversion})) {
239 my $min = $opts{minversion};
240 if (!defined TryCmd("pkg-config --atleast-version=$min $pkg")) {
241 Log("NO: version $ver is too old (need >= $min)\n");
245 Log("YES: version $ver\n");
246 Set("CONFIG_HAVE_$upper" => 1);
247 Set("CONFIG_VER_$upper" => $ver);
248 my $cf = TryCmd("pkg-config --cflags $pkg");
249 Set("${upper}_CFLAGS" => $cf) if defined $cf;
250 my $lf = TryCmd("pkg-config --libs $pkg");
251 Set("${upper}_LIBS" => $lf) if defined $lf;
257 return join(".", map { sprintf("%05s", $_) } split(/\./, $v));
263 my $upper = $pkg; $upper =~ tr/a-z/A-Z/; $upper =~ s/[^0-9A-Z]+/_/g;
264 Log "Checking for package $pkg ... ";
265 maybe_manually("CONFIG_HAVE_$upper") and return Get("CONFIG_HAVE_$upper");
266 my $pc = $opts{script};
267 my $ver = TryCmd("$pc --version 2>/dev/null");
272 if (defined($opts{minversion})) {
273 my $min = $opts{minversion};
274 if (ver_norm($ver) lt ver_norm($min)) {
275 Log("NO: version $ver is too old (need >= $min)\n");
279 Log("YES: version $ver\n");
280 Set("CONFIG_HAVE_$upper" => 1);
281 Set("CONFIG_VER_$upper" => $ver);
283 my $want = $opts{want};
284 defined $want or $want = ["cflags", "libs"];
286 my $uw = $w; $uw =~ tr/a-z-/A-Z_/;
287 my $cf = TryCmd("$pc --$w");
288 Set("${upper}_${uw}" => $cf) if defined $cf;