]> mj.ucw.cz Git - libucw.git/blob - ucw/perl/UCW/Configure.pm
Revert "Fail when O_DIRECT requested on Darwin"
[libucw.git] / ucw / perl / UCW / Configure.pm
1 #       Perl module for UCW Configure Scripts
2 #
3 #       (c) 2005--2008 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 &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                 Set $var, &$sub();
94         }
95         Log Get($var) . "\n";
96 }
97
98 sub TryFindFile($) {
99         my ($f) = @_;
100         if (-f $f) {
101                 return $f;
102         } elsif ($f !~ /^\// && -f (Get("SRCDIR")."/$f")) {
103                 return Get("SRCDIR")."/$f";
104         } else {
105                 return undef;
106         }
107 }
108
109 sub FindFile($) {
110         my ($f) = @_;
111         my $F;
112         defined ($F = TryFindFile($f)) or Fail "Cannot find file $f";
113         return $F;
114 }
115
116 sub Init($$) {
117         my ($srcdir,$defconfig) = @_;
118         sub usage($) {
119                 my ($dc) = @_;
120                 print STDERR "Usage: [<srcdir>/]configure " . (defined $dc ? "[" : "") . "<config-name>" . (defined $dc ? "]" : "") .
121                         " [<option>[=<value>] | -<option>] ...\n";
122                 exit 1;
123         }
124         Set('CONFIG' => $defconfig) if defined $defconfig;
125         if (@ARGV) {
126                 usage($defconfig) if $ARGV[0] eq "--help";
127                 if (!defined($defconfig) || $ARGV[0] !~ /^-?[A-Z][A-Z0-9_]*(=|$)/) {
128                         # This does not look like an option, so read it as a file name
129                         Set('CONFIG' => shift @ARGV);
130                 }
131         }
132         Set("SRCDIR", $srcdir);
133
134         foreach my $x (@ARGV) {
135                 if ($x =~ /^(\w+)=(.*)/) {
136                         Override($1 => $2);
137                 } elsif ($x =~ /^-(\w+)$/) {
138                         Override($1 => 0);
139                         delete $vars{$1};
140                 } elsif ($x =~ /^(\w+)$/) {
141                         Override($1 => 1);
142                 } else {
143                         print STDERR "Invalid option $x\n";
144                         exit 1;
145                 }
146         }
147
148         defined Get("CONFIG") or usage($defconfig);
149         if (!TryFindFile(Get("CONFIG"))) {
150                 TryFindFile(Get("CONFIG")."/config") or Fail "Cannot find configuration " . Get("CONFIG");
151                 Override("CONFIG" => Get("CONFIG")."/config");
152         }
153 }
154
155 sub Include($) {
156         my ($f) = @_;
157         $f = FindFile($f);
158         Notice "Loading configuration $f\n";
159         require $f;
160 }
161
162 sub PostConfig(&) {
163         unshift @postconfigs, $_[0];
164 }
165
166 sub AtWrite(&) {
167         unshift @atwrites, $_[0];
168 }
169
170 sub Finish() {
171         for my $post (@postconfigs) {
172                 &$post();
173         }
174
175         print "\n";
176
177         if (Get("SRCDIR") ne ".") {
178                 Log "Preparing for compilation from directory " . Get("SRCDIR") . " to obj/ ... ";
179                 -l "src" and unlink "src";
180                 symlink Get("SRCDIR"), "src" or Fail "Cannot link source directory to src: $!";
181                 Override("SRCDIR" => "src");
182                 -l "Makefile" and unlink "Makefile";
183                 -f "Makefile" and Fail "Makefile already exists";
184                 symlink "src/Makefile", "Makefile" or Fail "Cannot link Makefile: $!";
185         } else {
186                 Log "Preparing for compilation from current directory to obj/ ... ";
187         }
188         if (-d "obj") {
189                 `rm -rf obj`; Fail "Cannot delete old obj directory" if $?;
190         }
191         -d "obj" or mkdir("obj", 0777) or Fail "Cannot create obj directory: $!";
192         -d "obj/ucw" or mkdir("obj/ucw", 0777) or Fail "Cannot create obj/ucw directory: $!";
193         Log "done\n";
194
195         Log "Generating config.mk ... ";
196         open X, ">obj/config.mk" or Fail $!;
197         print X "# Generated automatically by $0, please don't touch manually.\n";
198         foreach my $x (sort keys %vars) {
199                 print X "$x=$vars{$x}\n";
200         }
201         print X "s=\${SRCDIR}\n";
202         print X "o=obj\n";
203         close X;
204         Log "done\n";
205
206         for my $wr (@atwrites) {
207                 &$wr();
208         }
209 }
210
211 1;  # OK