1 # Perl module for UCW Configure Scripts
3 # (c) 2005 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 &Fail &IsSet &Set &UnSet &Append &Override &Get &Test &Include &Finish &FindFile &TryFindFile);
32 print @_ if $vars{"VERBOSE"};
36 Log((shift @_) . "\n");
42 return exists $vars{$x};
52 $y=1 unless defined $y;
53 $vars{$x}=$y unless $overriden{$x};
58 delete $vars{$x} unless $overriden{$x};
63 Set($x, (IsSet($x) ? (Get($x) . " $y") : $y));
68 $y=1 unless defined $y;
74 my ($var,$msg,$sub) = @_;
86 } elsif ($f !~ /^\// && -f (Get("SRCDIR")."/$f")) {
87 return Get("SRCDIR")."/$f";
96 defined ($F = TryFindFile($f)) or Fail "Cannot find file $f";
101 my ($srcdir,$defconfig) = @_;
102 if ((!defined $defconfig && !@ARGV) || @ARGV && $ARGV[0] eq "--help") {
103 print STDERR "Usage: [<srcdir>/]configure " . (defined $defconfig ? "[" : "") . "<config-name>" . (defined $defconfig ? "]" : "") .
104 " [<option>[=<value>] | -<option>] ...\n";
107 if (@ARGV && $ARGV[0] !~ /=/) {
108 Set('CONFIG' => shift @ARGV);
110 Set('CONFIG' => $defconfig);
112 Set("SRCDIR", $srcdir);
114 foreach my $x (@ARGV) {
115 if ($x =~ /^(\w+)=(.*)/) {
117 } elsif ($x =~ /^-(\w+)$/) {
120 } elsif ($x =~ /^(\w+)$/) {
123 print STDERR "Invalid option $_\n";
128 if (!TryFindFile(Get("CONFIG"))) {
129 TryFindFile(Get("CONFIG")."/config") or Fail "Cannot find configuration " . Get("CONFIG");
130 Override("CONFIG" => Get("CONFIG")."/config");
137 Notice "Loading configuration $f\n";
144 if (Get("SRCDIR") ne ".") {
145 Log "Preparing for compilation from directory " . Get("SRCDIR") . " to obj/ ... ";
146 -l "src" and unlink "src";
147 symlink Get("SRCDIR"), "src" or Fail "Cannot link source directory to src: $!";
148 Override("SRCDIR" => "src");
149 -l "Makefile" and unlink "Makefile";
150 -f "Makefile" and Fail "Makefile already exists";
151 symlink "src/Makefile", "Makefile" or Fail "Cannot link Makefile: $!";
153 Log "Preparing for compilation from current directory to obj/ ... ";
155 `rm -rf obj` if -d "obj"; Fail "Cannot delete old obj directory" if $?;
156 -d "obj" or mkdir("obj", 0777) or Fail "Cannot create obj directory: $!";
157 -d "obj/lib" or mkdir("obj/lib", 0777) or Fail "Cannot create obj/lib directory: $!";
160 Log "Generating autoconf.h ... ";
161 open X, ">obj/lib/autoconf.h" or Fail $!;
162 print X "/* Generated automatically by $0, please don't touch manually. */\n";
163 foreach my $x (sort keys %vars) {
164 # Don't export variables which contain no underscores
165 next unless $x =~ /_/;
167 # Try to add quotes if necessary
168 $v = '"' . $v . '"' unless ($v =~ /^"/ || $v =~ /^\d*$/);
169 print X "#define $x $v\n";
174 Log "Generating config.mk ... ";
175 open X, ">obj/config.mk" or Fail $!;
176 print X "# Generated automatically by $0, please don't touch manually.\n";
177 foreach my $x (sort keys %vars) {
178 print X "$x=$vars{$x}\n";