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();
113 Log (($val ? "yes" : "no") . "\n");
120 return (-f $f) ? $f : undef;
123 } elsif (-f (Get("SRCDIR")."/$f")) {
124 return Get("SRCDIR")."/$f";
133 defined ($F = TryFindFile($f)) or Fail "Cannot find file $f";
138 my ($srcdir,$defconfig) = @_;
141 print STDERR "Usage: [<srcdir>/]configure " . (defined $dc ? "[" : "") . "<config-name>" . (defined $dc ? "]" : "") .
142 " [<option>[=<value>] | -<option>] ...\n";
145 Set('CONFIG' => $defconfig) if defined $defconfig;
147 usage($defconfig) if $ARGV[0] eq "--help";
148 if (!defined($defconfig) || $ARGV[0] !~ /^-?[A-Z][A-Z0-9_]*(=|$)/) {
149 # This does not look like an option, so read it as a file name
150 Set('CONFIG' => shift @ARGV);
153 Set("SRCDIR", $srcdir);
155 foreach my $x (@ARGV) {
156 if ($x =~ /^(\w+)=(.*)/) {
158 } elsif ($x =~ /^-(\w+)$/) {
161 } elsif ($x =~ /^(\w+)$/) {
164 print STDERR "Invalid option $x\n";
169 defined Get("CONFIG") or usage($defconfig);
170 if (!TryFindFile(Get("CONFIG"))) {
171 TryFindFile(Get("CONFIG")."/config") or Fail "Cannot find configuration " . Get("CONFIG");
172 Override("CONFIG" => Get("CONFIG")."/config");
179 Notice "Loading configuration $f\n";
184 unshift @postconfigs, $_[0];
188 unshift @atwrites, $_[0];
192 for my $post (@postconfigs) {
198 if (Get("SRCDIR") ne ".") {
199 Log "Preparing for compilation from directory " . Get("SRCDIR") . " to obj/ ... ";
200 -l "src" and unlink "src";
201 symlink Get("SRCDIR"), "src" or Fail "Cannot link source directory to src: $!";
202 Override("SRCDIR" => "src");
203 -l "Makefile" and unlink "Makefile";
204 -f "Makefile" and Fail "Makefile already exists";
205 symlink "src/Makefile", "Makefile" or Fail "Cannot link Makefile: $!";
207 Log "Preparing for compilation from current directory to obj/ ... ";
210 `rm -rf obj`; Fail "Cannot delete old obj directory" if $?;
212 -d "obj" or mkdir("obj", 0777) or Fail "Cannot create obj directory: $!";
213 -d "obj/ucw" or mkdir("obj/ucw", 0777) or Fail "Cannot create obj/ucw directory: $!";
216 Log "Generating config.mk ... ";
217 open X, ">obj/config.mk" or Fail $!;
218 print X "# Generated automatically by $0, please don't touch manually.\n";
219 foreach my $x (sort keys %vars) {
220 print X "$x=$vars{$x}\n";
222 print X "s=\${SRCDIR}\n";
227 for my $wr (@atwrites) {