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/ ... ";
170 `rm -rf obj`; Fail "Cannot delete old obj directory" if $?;
172 -d "obj" or mkdir("obj", 0777) or Fail "Cannot create obj directory: $!";
173 -d "obj/ucw" or mkdir("obj/ucw", 0777) or Fail "Cannot create obj/ucw directory: $!";
176 Log "Generating autoconf.h ... ";
177 open X, ">obj/autoconf.h" or Fail $!;
178 print X "/* Generated automatically by $0, please don't touch manually. */\n";
179 foreach my $x (sort keys %vars) {
180 # Don't export variables which contain no underscores
181 next unless $x =~ /_/;
183 # Try to add quotes if necessary
184 $v = '"' . $v . '"' unless ($v =~ /^"/ || $v =~ /^\d*$/);
185 print X "#define $x $v\n";
190 Log "Generating config.mk ... ";
191 open X, ">obj/config.mk" or Fail $!;
192 print X "# Generated automatically by $0, please don't touch manually.\n";
193 foreach my $x (sort keys %vars) {
194 print X "$x=$vars{$x}\n";
196 print X "s=\${SRCDIR}\n";
205 defined $res or return;
207 return $res unless $?;
211 sub maybe_manually($) {
214 if (Get("$n")) { Log "YES (set manually)\n"; }
215 else { Log "NO (set manually)\n"; }
224 my $upper = $pkg; $upper =~ tr/a-z/A-Z/; $upper =~ s/[^0-9A-Z]+/_/g;
225 Log "Checking for package $pkg ... ";
226 maybe_manually("CONFIG_HAVE_$upper") and return Get("CONFIG_HAVE_$upper");
227 my $ver = TryCmd("pkg-config --modversion $pkg 2>/dev/null");
232 if (defined($opts{minversion})) {
233 my $min = $opts{minversion};
234 if (!defined TryCmd("pkg-config --atleast-version=$min $pkg")) {
235 Log("NO: version $ver is too old (need >= $min)\n");
239 Log("YES: version $ver\n");
240 Set("CONFIG_HAVE_$upper" => 1);
241 Set("CONFIG_VER_$upper" => $ver);
242 my $cf = TryCmd("pkg-config --cflags $pkg");
243 Set("${upper}_CFLAGS" => $cf) if defined $cf;
244 my $lf = TryCmd("pkg-config --libs $pkg");
245 Set("${upper}_LIBS" => $lf) if defined $lf;
251 return join(".", map { sprintf("%05s", $_) } split(/\./, $v));
257 my $upper = $pkg; $upper =~ tr/a-z/A-Z/; $upper =~ s/[^0-9A-Z]+/_/g;
258 Log "Checking for package $pkg ... ";
259 maybe_manually("CONFIG_HAVE_$upper") and return Get("CONFIG_HAVE_$upper");
260 my $pc = $opts{script};
261 my $ver = TryCmd("$pc --version 2>/dev/null");
266 if (defined($opts{minversion})) {
267 my $min = $opts{minversion};
268 if (ver_norm($ver) lt ver_norm($min)) {
269 Log("NO: version $ver is too old (need >= $min)\n");
273 Log("YES: version $ver\n");
274 Set("CONFIG_HAVE_$upper" => 1);
275 Set("CONFIG_VER_$upper" => $ver);
277 my $want = $opts{want};
278 defined $want or $want = ["cflags", "libs"];
280 my $uw = $w; $uw =~ tr/a-z-/A-Z_/;
281 my $cf = TryCmd("$pc --$w");
282 Set("${upper}_${uw}" => $cf) if defined $cf;