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 &Set &UnSet &Append &Override &Get &Test &Include &Finish &FindFile &TryFindFile);
32 print @_ if $vars{"VERBOSE"};
36 print "WARNING: ", @_;
40 Log("ERROR: " . (shift @_) . "\n");
46 return exists $vars{$x};
56 $y=1 unless defined $y;
57 $vars{$x}=$y unless $overriden{$x};
62 delete $vars{$x} unless $overriden{$x};
67 Set($x, (IsSet($x) ? (Get($x) . " $y") : $y));
72 $y=1 unless defined $y;
78 my ($var,$msg,$sub) = @_;
90 } elsif ($f !~ /^\// && -f (Get("SRCDIR")."/$f")) {
91 return Get("SRCDIR")."/$f";
100 defined ($F = TryFindFile($f)) or Fail "Cannot find file $f";
105 my ($srcdir,$defconfig) = @_;
106 if ((!defined $defconfig && !@ARGV) || @ARGV && $ARGV[0] eq "--help") {
107 print STDERR "Usage: [<srcdir>/]configure " . (defined $defconfig ? "[" : "") . "<config-name>" . (defined $defconfig ? "]" : "") .
108 " [<option>[=<value>] | -<option>] ...\n";
111 if (@ARGV && $ARGV[0] !~ /=/) {
112 Set('CONFIG' => shift @ARGV);
114 Set('CONFIG' => $defconfig);
116 Set("SRCDIR", $srcdir);
118 foreach my $x (@ARGV) {
119 if ($x =~ /^(\w+)=(.*)/) {
121 } elsif ($x =~ /^-(\w+)$/) {
124 } elsif ($x =~ /^(\w+)$/) {
127 print STDERR "Invalid option $x\n";
132 if (!TryFindFile(Get("CONFIG"))) {
133 TryFindFile(Get("CONFIG")."/config") or Fail "Cannot find configuration " . Get("CONFIG");
134 Override("CONFIG" => Get("CONFIG")."/config");
141 Notice "Loading configuration $f\n";
148 if (Get("SRCDIR") ne ".") {
149 Log "Preparing for compilation from directory " . Get("SRCDIR") . " to obj/ ... ";
150 -l "src" and unlink "src";
151 symlink Get("SRCDIR"), "src" or Fail "Cannot link source directory to src: $!";
152 Override("SRCDIR" => "src");
153 -l "Makefile" and unlink "Makefile";
154 -f "Makefile" and Fail "Makefile already exists";
155 symlink "src/Makefile", "Makefile" or Fail "Cannot link Makefile: $!";
157 Log "Preparing for compilation from current directory to obj/ ... ";
159 `rm -rf obj` if -d "obj"; Fail "Cannot delete old obj directory" if $?;
160 -d "obj" or mkdir("obj", 0777) or Fail "Cannot create obj directory: $!";
161 -d "obj/lib" or mkdir("obj/lib", 0777) or Fail "Cannot create obj/lib directory: $!";
164 Log "Generating autoconf.h ... ";
165 open X, ">obj/autoconf.h" or Fail $!;
166 print X "/* Generated automatically by $0, please don't touch manually. */\n";
167 foreach my $x (sort keys %vars) {
168 # Don't export variables which contain no underscores
169 next unless $x =~ /_/;
171 # Try to add quotes if necessary
172 $v = '"' . $v . '"' unless ($v =~ /^"/ || $v =~ /^\d*$/);
173 print X "#define $x $v\n";
178 Log "Generating config.mk ... ";
179 open X, ">obj/config.mk" or Fail $!;
180 print X "# Generated automatically by $0, please don't touch manually.\n";
181 foreach my $x (sort keys %vars) {
182 print X "$x=$vars{$x}\n";
184 print X "s=\${SRCDIR}\n";