1 # Perl module for UCW Configure Scripts
3 # (c) 2005--2008 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 &DebugDump &PostConfig &AtWrite);
31 print "$_: $vars{$_}\n" foreach( keys %vars );
39 print @_ if $vars{"VERBOSE"};
43 print "WARNING: ", @_;
47 Log("ERROR: " . (shift @_) . "\n");
53 return exists $vars{$x};
58 return exists $overriden{$x};
68 $y=1 unless defined $y;
69 $vars{$x}=$y unless $overriden{$x};
74 delete $vars{$x} unless $overriden{$x};
79 Set($x, (IsSet($x) ? (Get($x) . " $y") : $y));
84 $y=1 unless defined $y;
90 my ($var,$msg,$sub) = @_;
102 } elsif ($f !~ /^\// && -f (Get("SRCDIR")."/$f")) {
103 return Get("SRCDIR")."/$f";
112 defined ($F = TryFindFile($f)) or Fail "Cannot find file $f";
117 my ($srcdir,$defconfig) = @_;
120 print STDERR "Usage: [<srcdir>/]configure " . (defined $dc ? "[" : "") . "<config-name>" . (defined $dc ? "]" : "") .
121 " [<option>[=<value>] | -<option>] ...\n";
124 Set('CONFIG' => $defconfig) if defined $defconfig;
126 usage($defconfig) if $ARGV[0] eq "--help";
127 if (!defined($defconfig) || $ARGV[0] !~ /^-?[A-Z][A-Z0-9_]*(=|$)/) {
128 # This does not look like an option, so read it as a file name
129 Set('CONFIG' => shift @ARGV);
132 Set("SRCDIR", $srcdir);
134 foreach my $x (@ARGV) {
135 if ($x =~ /^(\w+)=(.*)/) {
137 } elsif ($x =~ /^-(\w+)$/) {
140 } elsif ($x =~ /^(\w+)$/) {
143 print STDERR "Invalid option $x\n";
148 defined Get("CONFIG") or usage($defconfig);
149 if (!TryFindFile(Get("CONFIG"))) {
150 TryFindFile(Get("CONFIG")."/config") or Fail "Cannot find configuration " . Get("CONFIG");
151 Override("CONFIG" => Get("CONFIG")."/config");
158 Notice "Loading configuration $f\n";
163 unshift @postconfigs, $_[0];
167 unshift @atwrites, $_[0];
171 for my $post (@postconfigs) {
177 if (Get("SRCDIR") ne ".") {
178 Log "Preparing for compilation from directory " . Get("SRCDIR") . " to obj/ ... ";
179 -l "src" and unlink "src";
180 symlink Get("SRCDIR"), "src" or Fail "Cannot link source directory to src: $!";
181 Override("SRCDIR" => "src");
182 -l "Makefile" and unlink "Makefile";
183 -f "Makefile" and Fail "Makefile already exists";
184 symlink "src/Makefile", "Makefile" or Fail "Cannot link Makefile: $!";
186 Log "Preparing for compilation from current directory to obj/ ... ";
189 `rm -rf obj`; Fail "Cannot delete old obj directory" if $?;
191 -d "obj" or mkdir("obj", 0777) or Fail "Cannot create obj directory: $!";
192 -d "obj/ucw" or mkdir("obj/ucw", 0777) or Fail "Cannot create obj/ucw directory: $!";
195 Log "Generating config.mk ... ";
196 open X, ">obj/config.mk" or Fail $!;
197 print X "# Generated automatically by $0, please don't touch manually.\n";
198 foreach my $x (sort keys %vars) {
199 print X "$x=$vars{$x}\n";
201 print X "s=\${SRCDIR}\n";
206 for my $wr (@atwrites) {