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;
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 &TestBool &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) = @_;
93 Log Get($var) . " (preset)\n";
102 my ($var,$msg,$sub) = @_;
104 if (IsSet($var) || IsGiven($var)) {
105 Log ((Get($var) ? "yes" : "no") . " (set)\n");
107 my ($val, $comment) = &$sub();
109 Log (($val ? "yes" : "no") . "\n");
117 } elsif ($f !~ /^\// && -f (Get("SRCDIR")."/$f")) {
118 return Get("SRCDIR")."/$f";
127 defined ($F = TryFindFile($f)) or Fail "Cannot find file $f";
132 my ($srcdir,$defconfig) = @_;
135 print STDERR "Usage: [<srcdir>/]configure " . (defined $dc ? "[" : "") . "<config-name>" . (defined $dc ? "]" : "") .
136 " [<option>[=<value>] | -<option>] ...\n";
139 Set('CONFIG' => $defconfig) if defined $defconfig;
141 usage($defconfig) if $ARGV[0] eq "--help";
142 if (!defined($defconfig) || $ARGV[0] !~ /^-?[A-Z][A-Z0-9_]*(=|$)/) {
143 # This does not look like an option, so read it as a file name
144 Set('CONFIG' => shift @ARGV);
147 Set("SRCDIR", $srcdir);
149 foreach my $x (@ARGV) {
150 if ($x =~ /^(\w+)=(.*)/) {
152 } elsif ($x =~ /^-(\w+)$/) {
155 } elsif ($x =~ /^(\w+)$/) {
158 print STDERR "Invalid option $x\n";
163 defined Get("CONFIG") or usage($defconfig);
164 if (!TryFindFile(Get("CONFIG"))) {
165 TryFindFile(Get("CONFIG")."/config") or Fail "Cannot find configuration " . Get("CONFIG");
166 Override("CONFIG" => Get("CONFIG")."/config");
173 Notice "Loading configuration $f\n";
178 unshift @postconfigs, $_[0];
182 unshift @atwrites, $_[0];
186 for my $post (@postconfigs) {
192 if (Get("SRCDIR") ne ".") {
193 Log "Preparing for compilation from directory " . Get("SRCDIR") . " to obj/ ... ";
194 -l "src" and unlink "src";
195 symlink Get("SRCDIR"), "src" or Fail "Cannot link source directory to src: $!";
196 Override("SRCDIR" => "src");
197 -l "Makefile" and unlink "Makefile";
198 -f "Makefile" and Fail "Makefile already exists";
199 symlink "src/Makefile", "Makefile" or Fail "Cannot link Makefile: $!";
201 Log "Preparing for compilation from current directory to obj/ ... ";
204 `rm -rf obj`; Fail "Cannot delete old obj directory" if $?;
206 -d "obj" or mkdir("obj", 0777) or Fail "Cannot create obj directory: $!";
207 -d "obj/ucw" or mkdir("obj/ucw", 0777) or Fail "Cannot create obj/ucw directory: $!";
210 Log "Generating config.mk ... ";
211 open X, ">obj/config.mk" or Fail $!;
212 print X "# Generated automatically by $0, please don't touch manually.\n";
213 foreach my $x (sort keys %vars) {
214 print X "$x=$vars{$x}\n";
216 print X "s=\${SRCDIR}\n";
221 for my $wr (@atwrites) {