]> mj.ucw.cz Git - leo.git/blob - perl/UCW/Configure.pm
5d458adbb63c15e96d1011682eb1286877a0bafd
[leo.git] / perl / UCW / Configure.pm
1 #       Perl module for UCW Configure Scripts
2 #
3 #       (c) 2005--2010 Martin Mares <mj@ucw.cz>
4 #
5 #       This software may be freely distributed and used according to the terms
6 #       of the GNU Lesser General Public License.
7
8 package UCW::Configure;
9
10 use strict;
11 use warnings;
12
13 BEGIN {
14         # The somewhat hairy Perl export mechanism
15         use Exporter();
16         our ($VERSION, @ISA, @EXPORT, @EXPORT_OK, %EXPORT_TAGS);
17         $VERSION = 1.0;
18         @ISA = qw(Exporter);
19         @EXPORT = qw(&Init &Log &Notice &Warn &Fail &IsSet &IsGiven &Set &UnSet &Append &Override &Get &Test &TestBool &Include &Finish &FindFile &TryFindFile &DebugDump &PostConfig &AtWrite);
20         @EXPORT_OK = qw();
21         %EXPORT_TAGS = ();
22 }
23
24 our %vars;
25 our %overriden;
26 our @postconfigs;
27 our @atwrites;
28
29 sub DebugDump() {
30         print "VARS:\n";
31         print "$_: $vars{$_}\n" foreach( keys %vars );
32 }
33
34 sub Log($) {
35         print @_;
36 }
37
38 sub Notice($) {
39         print @_ if $vars{"VERBOSE"};
40 }
41
42 sub Warn($) {
43         print "WARNING: ", @_;
44 }
45
46 sub Fail($) {
47         Log("ERROR: " . (shift @_) . "\n");
48         exit 1;
49 }
50
51 sub IsSet($) {
52         my ($x) = @_;
53         return exists $vars{$x};
54 }
55
56 sub IsGiven($) {
57         my ($x) = @_;
58         return exists $overriden{$x};
59 }
60
61 sub Get($) {
62         my ($x) = @_;
63         return $vars{$x};
64 }
65
66 sub Set($;$) {
67         my ($x,$y) = @_;
68         $y=1 unless defined $y;
69         $vars{$x}=$y unless $overriden{$x};
70 }
71
72 sub UnSet($) {
73         my ($x) = @_;
74         delete $vars{$x} unless $overriden{$x};
75 }
76
77 sub Append($$) {
78         my ($x,$y) = @_;
79         Set($x, (IsSet($x) ? (Get($x) . " $y") : $y));
80 }
81
82 sub Override($;$) {
83         my ($x,$y) = @_;
84         $y=1 unless defined $y;
85         $vars{$x}=$y;
86         $overriden{$x} = 1;
87 }
88
89 sub Test($$$) {
90         my ($var,$msg,$sub) = @_;
91         Log "$msg ... ";
92         if (IsSet($var)) {
93                 Log Get($var) . " (preset)\n";
94         } else {
95                 my $val = &$sub();
96                 Set($var, $val);
97                 Log "$val\n";
98         }
99 }
100
101 sub TestBool($$$) {
102         my ($var,$msg,$sub) = @_;
103         Log "$msg ... ";
104         if (IsSet($var) || IsGiven($var)) {
105                 Log ((Get($var) ? "yes" : "no") . " (set)\n");
106         } else {
107                 my ($val, $comment) = &$sub();
108                 Set($var, $val);
109                 Log (($val ? "yes" : "no") . "\n");
110         }
111 }
112
113 sub TryFindFile($) {
114         my ($f) = @_;
115         if (-f $f) {
116                 return $f;
117         } elsif ($f !~ /^\// && -f (Get("SRCDIR")."/$f")) {
118                 return Get("SRCDIR")."/$f";
119         } else {
120                 return undef;
121         }
122 }
123
124 sub FindFile($) {
125         my ($f) = @_;
126         my $F;
127         defined ($F = TryFindFile($f)) or Fail "Cannot find file $f";
128         return $F;
129 }
130
131 sub Init($$) {
132         my ($srcdir,$defconfig) = @_;
133         sub usage($) {
134                 my ($dc) = @_;
135                 print STDERR "Usage: [<srcdir>/]configure " . (defined $dc ? "[" : "") . "<config-name>" . (defined $dc ? "]" : "") .
136                         " [<option>[=<value>] | -<option>] ...\n";
137                 exit 1;
138         }
139         Set('CONFIG' => $defconfig) if defined $defconfig;
140         if (@ARGV) {
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);
145                 }
146         }
147         Set("SRCDIR", $srcdir);
148
149         foreach my $x (@ARGV) {
150                 if ($x =~ /^(\w+)=(.*)/) {
151                         Override($1 => $2);
152                 } elsif ($x =~ /^-(\w+)$/) {
153                         Override($1 => 0);
154                         delete $vars{$1};
155                 } elsif ($x =~ /^(\w+)$/) {
156                         Override($1 => 1);
157                 } else {
158                         print STDERR "Invalid option $x\n";
159                         exit 1;
160                 }
161         }
162
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");
167         }
168 }
169
170 sub Include($) {
171         my ($f) = @_;
172         $f = FindFile($f);
173         Notice "Loading configuration $f\n";
174         require $f;
175 }
176
177 sub PostConfig(&) {
178         unshift @postconfigs, $_[0];
179 }
180
181 sub AtWrite(&) {
182         unshift @atwrites, $_[0];
183 }
184
185 sub Finish() {
186         for my $post (@postconfigs) {
187                 &$post();
188         }
189
190         print "\n";
191
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: $!";
200         } else {
201                 Log "Preparing for compilation from current directory to obj/ ... ";
202         }
203         if (-d "obj") {
204                 `rm -rf obj`; Fail "Cannot delete old obj directory" if $?;
205         }
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: $!";
208         Log "done\n";
209
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";
215         }
216         print X "s=\${SRCDIR}\n";
217         print X "o=obj\n";
218         close X;
219         Log "done\n";
220
221         for my $wr (@atwrites) {
222                 &$wr();
223         }
224 }
225
226 1;  # OK