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) = @_;
108 print STDERR "Usage: [<srcdir>/]configure " . (defined $dc ? "[" : "") . "<config-name>" . (defined $dc ? "]" : "") .
109 " [<option>[=<value>] | -<option>] ...\n";
112 Set('CONFIG' => $defconfig) if defined $defconfig;
114 usage($defconfig) if $ARGV[0] eq "--help";
115 if (!defined($defconfig) || $ARGV[0] !~ /^-?[A-Z][A-Z0-9_]*(=|$)/) {
116 # This does not look like an option, so read it as a file name
117 Set('CONFIG' => shift @ARGV);
120 Set("SRCDIR", $srcdir);
122 foreach my $x (@ARGV) {
123 if ($x =~ /^(\w+)=(.*)/) {
125 } elsif ($x =~ /^-(\w+)$/) {
128 } elsif ($x =~ /^(\w+)$/) {
131 print STDERR "Invalid option $x\n";
136 defined Get("CONFIG") or usage($defconfig);
137 if (!TryFindFile(Get("CONFIG"))) {
138 TryFindFile(Get("CONFIG")."/config") or Fail "Cannot find configuration " . Get("CONFIG");
139 Override("CONFIG" => Get("CONFIG")."/config");
146 Notice "Loading configuration $f\n";
153 if (Get("SRCDIR") ne ".") {
154 Log "Preparing for compilation from directory " . Get("SRCDIR") . " to obj/ ... ";
155 -l "src" and unlink "src";
156 symlink Get("SRCDIR"), "src" or Fail "Cannot link source directory to src: $!";
157 Override("SRCDIR" => "src");
158 -l "Makefile" and unlink "Makefile";
159 -f "Makefile" and Fail "Makefile already exists";
160 symlink "src/Makefile", "Makefile" or Fail "Cannot link Makefile: $!";
162 Log "Preparing for compilation from current directory to obj/ ... ";
164 `rm -rf obj` if -d "obj"; Fail "Cannot delete old obj directory" if $?;
165 -d "obj" or mkdir("obj", 0777) or Fail "Cannot create obj directory: $!";
166 -d "obj/lib" or mkdir("obj/lib", 0777) or Fail "Cannot create obj/lib directory: $!";
169 Log "Generating autoconf.h ... ";
170 open X, ">obj/autoconf.h" or Fail $!;
171 print X "/* Generated automatically by $0, please don't touch manually. */\n";
172 foreach my $x (sort keys %vars) {
173 # Don't export variables which contain no underscores
174 next unless $x =~ /_/;
176 # Try to add quotes if necessary
177 $v = '"' . $v . '"' unless ($v =~ /^"/ || $v =~ /^\d*$/);
178 print X "#define $x $v\n";
183 Log "Generating config.mk ... ";
184 open X, ">obj/config.mk" or Fail $!;
185 print X "# Generated automatically by $0, please don't touch manually.\n";
186 foreach my $x (sort keys %vars) {
187 print X "$x=$vars{$x}\n";
189 print X "s=\${SRCDIR}\n";