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 );
40 print @_ if $vars{"VERBOSE"};
45 print "WARNING: ", @_;
50 Log("ERROR: " . (shift @_) . "\n");
56 return exists $vars{$x};
61 return exists $overriden{$x};
71 $y=1 unless defined $y;
72 $vars{$x}=$y unless $overriden{$x};
77 delete $vars{$x} unless $overriden{$x};
82 Set($x, (IsSet($x) ? (Get($x) . " $y") : $y));
87 $y=1 unless defined $y;
93 my ($var,$msg,$sub) = @_;
96 Log Get($var) . " (preset)\n";
105 my ($var,$msg,$sub) = @_;
107 if (IsSet($var) || IsGiven($var)) {
108 Log ((Get($var) ? "yes" : "no") . " (set)\n");
110 my ($val, $comment) = &$sub();
112 Log (($val ? "yes" : "no") . "\n");
119 return (-f $f) ? $f : undef;
122 } elsif (-f (Get("SRCDIR")."/$f")) {
123 return Get("SRCDIR")."/$f";
132 defined ($F = TryFindFile($f)) or Fail "Cannot find file $f";
137 my ($srcdir,$defconfig) = @_;
140 print STDERR "Usage: [<srcdir>/]configure " . (defined $dc ? "[" : "") . "<config-name>" . (defined $dc ? "]" : "") .
141 " [<option>[=<value>] | -<option>] ...\n";
144 Set('CONFIG' => $defconfig) if defined $defconfig;
146 usage($defconfig) if $ARGV[0] eq "--help";
147 if (!defined($defconfig) || $ARGV[0] !~ /^-?[A-Z][A-Z0-9_]*(=|$)/) {
148 # This does not look like an option, so read it as a file name
149 Set('CONFIG' => shift @ARGV);
152 Set("SRCDIR", $srcdir);
154 foreach my $x (@ARGV) {
155 if ($x =~ /^(\w+)=(.*)/) {
157 } elsif ($x =~ /^-(\w+)$/) {
160 } elsif ($x =~ /^(\w+)$/) {
163 print STDERR "Invalid option $x\n";
168 defined Get("CONFIG") or usage($defconfig);
169 if (!TryFindFile(Get("CONFIG"))) {
170 TryFindFile(Get("CONFIG")."/config") or Fail "Cannot find configuration " . Get("CONFIG");
171 Override("CONFIG" => Get("CONFIG")."/config");
178 Notice "Loading configuration $f\n";
183 unshift @postconfigs, $_[0];
187 unshift @atwrites, $_[0];
191 for my $post (@postconfigs) {
197 if (Get("SRCDIR") ne ".") {
198 Log "Preparing for compilation from directory " . Get("SRCDIR") . " to obj/ ... ";
199 -l "src" and unlink "src";
200 symlink Get("SRCDIR"), "src" or Fail "Cannot link source directory to src: $!";
201 Override("SRCDIR" => "src");
202 -l "Makefile" and unlink "Makefile";
203 -f "Makefile" and Fail "Makefile already exists";
204 symlink "src/Makefile", "Makefile" or Fail "Cannot link Makefile: $!";
206 Log "Preparing for compilation from current directory to obj/ ... ";
209 `rm -rf obj`; Fail "Cannot delete old obj directory" if $?;
211 -d "obj" or mkdir("obj", 0777) or Fail "Cannot create obj directory: $!";
212 -d "obj/ucw" or mkdir("obj/ucw", 0777) or Fail "Cannot create obj/ucw directory: $!";
215 Log "Generating config.mk ... ";
216 open X, ">obj/config.mk" or Fail $!;
217 print X "# Generated automatically by $0, please don't touch manually.\n";
218 foreach my $x (sort keys %vars) {
219 print X "$x=$vars{$x}\n";
221 print X "s=\${SRCDIR}\n";
226 for my $wr (@atwrites) {