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 &Warn &Fail &IsSet &IsGiven &Set &UnSet &Append &Override &Get &Test &Include &Finish &FindFile &TryFindFile &TryCmd &PkgConfig &TrivConfig);
32 print @_ if $vars{"VERBOSE"};
36 print "WARNING: ", @_;
40 Log("ERROR: " . (shift @_) . "\n");
46 return exists $vars{$x};
51 return exists $overriden{$x};
61 $y=1 unless defined $y;
62 $vars{$x}=$y unless $overriden{$x};
67 delete $vars{$x} unless $overriden{$x};
72 Set($x, (IsSet($x) ? (Get($x) . " $y") : $y));
77 $y=1 unless defined $y;
83 my ($var,$msg,$sub) = @_;
95 } elsif ($f !~ /^\// && -f (Get("SRCDIR")."/$f")) {
96 return Get("SRCDIR")."/$f";
105 defined ($F = TryFindFile($f)) or Fail "Cannot find file $f";
110 my ($srcdir,$defconfig) = @_;
113 print STDERR "Usage: [<srcdir>/]configure " . (defined $dc ? "[" : "") . "<config-name>" . (defined $dc ? "]" : "") .
114 " [<option>[=<value>] | -<option>] ...\n";
117 Set('CONFIG' => $defconfig) if defined $defconfig;
119 usage($defconfig) if $ARGV[0] eq "--help";
120 if (!defined($defconfig) || $ARGV[0] !~ /^-?[A-Z][A-Z0-9_]*(=|$)/) {
121 # This does not look like an option, so read it as a file name
122 Set('CONFIG' => shift @ARGV);
125 Set("SRCDIR", $srcdir);
127 foreach my $x (@ARGV) {
128 if ($x =~ /^(\w+)=(.*)/) {
130 } elsif ($x =~ /^-(\w+)$/) {
133 } elsif ($x =~ /^(\w+)$/) {
136 print STDERR "Invalid option $x\n";
141 defined Get("CONFIG") or usage($defconfig);
142 if (!TryFindFile(Get("CONFIG"))) {
143 TryFindFile(Get("CONFIG")."/config") or Fail "Cannot find configuration " . Get("CONFIG");
144 Override("CONFIG" => Get("CONFIG")."/config");
151 Notice "Loading configuration $f\n";
158 if (Get("SRCDIR") ne ".") {
159 Log "Preparing for compilation from directory " . Get("SRCDIR") . " to obj/ ... ";
160 -l "src" and unlink "src";
161 symlink Get("SRCDIR"), "src" or Fail "Cannot link source directory to src: $!";
162 Override("SRCDIR" => "src");
163 -l "Makefile" and unlink "Makefile";
164 -f "Makefile" and Fail "Makefile already exists";
165 symlink "src/Makefile", "Makefile" or Fail "Cannot link Makefile: $!";
167 Log "Preparing for compilation from current directory to obj/ ... ";
169 `rm -rf obj` if -d "obj"; Fail "Cannot delete old obj directory" if $?;
170 -d "obj" or mkdir("obj", 0777) or Fail "Cannot create obj directory: $!";
171 -d "obj/lib" or mkdir("obj/lib", 0777) or Fail "Cannot create obj/lib directory: $!";
174 Log "Generating autoconf.h ... ";
175 open X, ">obj/autoconf.h" or Fail $!;
176 print X "/* Generated automatically by $0, please don't touch manually. */\n";
177 foreach my $x (sort keys %vars) {
178 # Don't export variables which contain no underscores
179 next unless $x =~ /_/;
181 # Try to add quotes if necessary
182 $v = '"' . $v . '"' unless ($v =~ /^"/ || $v =~ /^\d*$/);
183 print X "#define $x $v\n";
188 Log "Generating config.mk ... ";
189 open X, ">obj/config.mk" or Fail $!;
190 print X "# Generated automatically by $0, please don't touch manually.\n";
191 foreach my $x (sort keys %vars) {
192 print X "$x=$vars{$x}\n";
194 print X "s=\${SRCDIR}\n";
203 defined $res or return;
205 return $res unless $?;
209 sub maybe_manually($) {
212 if (Get("$n")) { Log "YES (set manually)\n"; }
213 else { Log "NO (set manually)\n"; }
222 my $upper = $pkg; $upper =~ tr/a-z/A-Z/; $upper =~ s/[^0-9A-Z]+/_/g;
223 Log "Checking for package $pkg ... ";
224 maybe_manually("CONFIG_HAVE_$upper") and return Get("CONFIG_HAVE_$upper");
225 my $ver = TryCmd("pkg-config --modversion $pkg 2>/dev/null");
230 if (defined($opts{minversion})) {
231 my $min = $opts{minversion};
232 if (!defined TryCmd("pkg-config --atleast-version=$min $pkg")) {
233 Log("NO: version $ver is too old (need >= $min)\n");
237 Log("YES: version $ver\n");
238 Set("CONFIG_HAVE_$upper" => 1);
239 Set("CONFIG_VER_$upper" => $ver);
240 my $cf = TryCmd("pkg-config --cflags $pkg");
241 Set("${upper}_CFLAGS" => $cf) if defined $cf;
242 my $lf = TryCmd("pkg-config --libs $pkg");
243 Set("${upper}_LIBS" => $lf) if defined $lf;
249 return join(".", map { sprintf("%05s", $_) } split(/\./, $v));
255 my $upper = $pkg; $upper =~ tr/a-z/A-Z/; $upper =~ s/[^0-9A-Z]+/_/g;
256 Log "Checking for package $pkg ... ";
257 maybe_manually("CONFIG_HAVE_$upper") and return Get("CONFIG_HAVE_$upper");
258 my $pc = $opts{script};
259 my $ver = TryCmd("$pc --version 2>/dev/null");
264 if (defined($opts{minversion})) {
265 my $min = $opts{minversion};
266 if (ver_norm($ver) lt ver_norm($min)) {
267 Log("NO: version $ver is too old (need >= $min)\n");
271 Log("YES: version $ver\n");
272 Set("CONFIG_HAVE_$upper" => 1);
273 Set("CONFIG_VER_$upper" => $ver);
275 my $want = $opts{want};
276 defined $want or $want = ["cflags", "libs"];
278 my $uw = $w; $uw =~ tr/a-z/A-Z/;
279 my $cf = TryCmd("$pc --$w");
280 Set("${upper}_${uw}" => $cf) if defined $cf;