1 # Perl module for UCW Configure Scripts
3 # (c) 2005--2010 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;
15 # The somewhat hairy Perl export mechanism
17 our ($VERSION, @ISA, @EXPORT, @EXPORT_OK, %EXPORT_TAGS);
20 @EXPORT = qw(&Init &Log &Notice &Warn &Fail &IsSet &IsGiven &Set &UnSet &Append &Override &Get &Test &TestBool &Include &Finish &FindFile &TryFindFile &DebugDump &PostConfig &AtWrite);
32 print "$_: $vars{$_}\n" foreach( keys %vars );
41 print @_ if $vars{"VERBOSE"};
46 print "WARNING: ", @_;
51 Log("ERROR: " . (shift @_) . "\n");
57 return exists $vars{$x};
62 return exists $overriden{$x};
72 $y=1 unless defined $y;
73 $vars{$x}=$y unless $overriden{$x};
78 delete $vars{$x} unless $overriden{$x};
83 Set($x, (IsSet($x) ? (Get($x) . " $y") : $y));
88 $y=1 unless defined $y;
94 my ($var,$msg,$sub) = @_;
97 Log Get($var) . " (preset)\n";
106 my ($var,$msg,$sub) = @_;
108 if (IsSet($var) || IsGiven($var)) {
109 Log ((Get($var) ? "yes" : "no") . " (set)\n");
111 my ($val, $comment) = &$sub();
117 Log (($val ? "yes" : "no") . "\n");
124 return (-f $f) ? $f : undef;
127 } elsif (-f (Get("SRCDIR")."/$f")) {
128 return Get("SRCDIR")."/$f";
137 defined ($F = TryFindFile($f)) or Fail "Cannot find file $f";
142 my ($srcdir,$defconfig) = @_;
145 print STDERR "Usage: [<srcdir>/]configure " . (defined $dc ? "[" : "") . "<config-name>" . (defined $dc ? "]" : "") .
146 " [<option>[=<value>] | -<option>] ...\n";
149 Set('CONFIG' => $defconfig) if defined $defconfig;
151 usage($defconfig) if $ARGV[0] eq "--help";
152 if (!defined($defconfig) || $ARGV[0] !~ /^-?[A-Z][A-Z0-9_]*(=|$)/) {
153 # This does not look like an option, so read it as a file name
154 Set('CONFIG' => shift @ARGV);
157 Set("SRCDIR", $srcdir);
159 foreach my $x (@ARGV) {
160 if ($x =~ /^(\w+)=(.*)/) {
162 } elsif ($x =~ /^-(\w+)$/) {
165 } elsif ($x =~ /^(\w+)$/) {
168 print STDERR "Invalid option $x\n";
173 defined Get("CONFIG") or usage($defconfig);
174 if (!TryFindFile(Get("CONFIG"))) {
175 TryFindFile(Get("CONFIG")."/config") or Fail "Cannot find configuration " . Get("CONFIG");
176 Override("CONFIG" => Get("CONFIG")."/config");
183 Notice "Loading configuration $f\n";
188 unshift @postconfigs, $_[0];
192 unshift @atwrites, $_[0];
196 for my $post (@postconfigs) {
202 if (Get("SRCDIR") ne ".") {
203 Log "Preparing for compilation from directory " . Get("SRCDIR") . " to obj/ ... ";
204 -l "src" and unlink "src";
205 symlink Get("SRCDIR"), "src" or Fail "Cannot link source directory to src: $!";
206 Override("SRCDIR" => "src");
207 -l "Makefile" and unlink "Makefile";
208 -f "Makefile" and Fail "Makefile already exists";
209 symlink "src/Makefile", "Makefile" or Fail "Cannot link Makefile: $!";
211 Log "Preparing for compilation from current directory to obj/ ... ";
214 `rm -rf obj`; Fail "Cannot delete old obj directory" if $?;
216 -d "obj" or mkdir("obj", 0777) or Fail "Cannot create obj directory: $!";
217 -d "obj/ucw" or mkdir("obj/ucw", 0777) or Fail "Cannot create obj/ucw directory: $!";
220 Log "Generating config.mk ... ";
221 open X, ">obj/config.mk" or Fail $!;
222 print X "# Generated automatically by $0, please don't touch manually.\n";
223 foreach my $x (sort keys %vars) {
224 print X "$x=$vars{$x}\n";
226 print X "s=\${SRCDIR}\n";
231 for my $wr (@atwrites) {