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 &Fail &IsSet &Set &UnSet &Override &Get &Test &Include &Finish &FindFile &TryFindFile);
32 Log((shift @_) . "\n");
38 return exists $vars{$x};
48 $y=1 unless defined $y;
49 $vars{$x}=$y unless $overriden{$x};
54 delete $vars{$x} unless $overriden{$x};
59 $y=1 unless defined $y;
65 my ($var,$msg,$sub) = @_;
77 } elsif ($f !~ /^\// && -f (Get("SRCDIR")."/$f")) {
78 return Get("SRCDIR")."/$f";
87 defined ($F = TryFindFile($f)) or Fail "Cannot find file $f";
92 my ($srcdir,$defconfig) = @_;
93 if ((!defined $defconfig && !@ARGV) || @ARGV && $ARGV[0] eq "--help") {
94 print STDERR "Usage: [<srcdir>/]configure " . (defined $defconfig ? "[" : "") . "<config-name>" . (defined $defconfig ? "]" : "") .
95 " [<option>[=<value>] | -<option>] ...\n";
98 if (@ARGV && $ARGV[0] !~ /=/) {
99 Set('CONFIG' => shift @ARGV);
101 Set('CONFIG' => $defconfig);
103 Set("SRCDIR", $srcdir);
105 foreach my $x (@ARGV) {
106 if ($x =~ /^(\w+)=(.*)/) {
108 } elsif ($x =~ /^-(\w+)$/) {
111 } elsif ($x =~ /^(\w+)$/) {
114 print STDERR "Invalid option $_\n";
119 if (!TryFindFile(Get("CONFIG"))) {
120 TryFindFile(Get("CONFIG")."/config") or Fail "Cannot find configuration " . Get("CONFIG");
121 Override("CONFIG" => Get("CONFIG")."/config");
128 Log "Loading configuration $f\n";
135 if (Get("SRCDIR") ne ".") {
136 Log "Preparing for compilation from directory " . Get("SRCDIR") . " to obj/ ... ";
137 -l "src" and unlink "src";
138 symlink Get("SRCDIR"), "src" or Fail "Cannot link source directory to src: $!";
139 Override("SRCDIR" => "src");
140 -l "Makefile" and unlink "Makefile";
141 -f "Makefile" and Fail "Makefile already exists";
142 symlink "src/Makefile", "Makefile" or Fail "Cannot link Makefile: $!";
144 Log "Preparing for compilation from current directory to obj/ ... ";
146 `rm -rf obj` if -d "obj"; Fail "Cannot delete old obj directory" if $?;
147 -d "obj" or mkdir("obj", 0777) or Fail "Cannot create obj directory: $!";
148 -d "obj/lib" or mkdir("obj/lib", 0777) or Fail "Cannot create obj/lib directory: $!";
151 Log "Generating autoconf.h... ";
152 open X, ">obj/lib/autoconf.h" or Fail $!;
153 print X "/* Generated automatically by $0, please don't touch manually. */\n";
154 foreach my $x (sort keys %vars) {
155 # Don't export variables which contain no underscores
156 next unless $x =~ /_/;
158 # Try to add quotes if necessary
159 $v = '"' . $v . '"' unless ($v =~ /^"/ || $v =~ /^\d*$/);
160 print X "#define $x $v\n";
165 Log "Generating config.mk... ";
166 open X, ">obj/config.mk" or Fail $!;
167 print X "# Generated automatically by $0, please don't touch manually.\n";
168 foreach my $x (sort keys %vars) {
169 print X "$x=$vars{$x}\n";