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 &debPrint);
29 # print "$_: $vars{$_}\n" foreach( keys %vars );
37 print @_ if $vars{"VERBOSE"};
41 print "WARNING: ", @_;
45 Log("ERROR: " . (shift @_) . "\n");
51 return exists $vars{$x};
56 return exists $overriden{$x};
66 $y=1 unless defined $y;
67 $vars{$x}=$y unless $overriden{$x};
72 delete $vars{$x} unless $overriden{$x};
77 Set($x, (IsSet($x) ? (Get($x) . " $y") : $y));
82 $y=1 unless defined $y;
88 my ($var,$msg,$sub) = @_;
100 } elsif ($f !~ /^\// && -f (Get("SRCDIR")."/$f")) {
101 return Get("SRCDIR")."/$f";
110 defined ($F = TryFindFile($f)) or Fail "Cannot find file $f";
115 my ($srcdir,$defconfig) = @_;
118 print STDERR "Usage: [<srcdir>/]configure " . (defined $dc ? "[" : "") . "<config-name>" . (defined $dc ? "]" : "") .
119 " [<option>[=<value>] | -<option>] ...\n";
122 Set('CONFIG' => $defconfig) if defined $defconfig;
124 usage($defconfig) if $ARGV[0] eq "--help";
125 if (!defined($defconfig) || $ARGV[0] !~ /^-?[A-Z][A-Z0-9_]*(=|$)/) {
126 # This does not look like an option, so read it as a file name
127 Set('CONFIG' => shift @ARGV);
130 Set("SRCDIR", $srcdir);
132 foreach my $x (@ARGV) {
133 if ($x =~ /^(\w+)=(.*)/) {
135 } elsif ($x =~ /^-(\w+)$/) {
138 } elsif ($x =~ /^(\w+)$/) {
141 print STDERR "Invalid option $x\n";
146 defined Get("CONFIG") or usage($defconfig);
147 if (!TryFindFile(Get("CONFIG"))) {
148 TryFindFile(Get("CONFIG")."/config") or Fail "Cannot find configuration " . Get("CONFIG");
149 Override("CONFIG" => Get("CONFIG")."/config");
156 Notice "Loading configuration $f\n";
163 if (Get("SRCDIR") ne ".") {
164 Log "Preparing for compilation from directory " . Get("SRCDIR") . " to obj/ ... ";
165 -l "src" and unlink "src";
166 symlink Get("SRCDIR"), "src" or Fail "Cannot link source directory to src: $!";
167 Override("SRCDIR" => "src");
168 -l "Makefile" and unlink "Makefile";
169 -f "Makefile" and Fail "Makefile already exists";
170 symlink "src/Makefile", "Makefile" or Fail "Cannot link Makefile: $!";
172 Log "Preparing for compilation from current directory to obj/ ... ";
175 `rm -rf obj`; Fail "Cannot delete old obj directory" if $?;
177 -d "obj" or mkdir("obj", 0777) or Fail "Cannot create obj directory: $!";
178 -d "obj/ucw" or mkdir("obj/ucw", 0777) or Fail "Cannot create obj/ucw directory: $!";
181 Log "Generating autoconf.h ... ";
182 open X, ">obj/autoconf.h" or Fail $!;
183 print X "/* Generated automatically by $0, please don't touch manually. */\n";
184 foreach my $x (sort keys %vars) {
185 # Don't export variables which contain no underscores
186 next unless $x =~ /_/;
188 # Try to add quotes if necessary
189 $v = '"' . $v . '"' unless ($v =~ /^"/ || $v =~ /^\d*$/);
190 print X "#define $x $v\n";
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";
201 print X "s=\${SRCDIR}\n";
210 defined $res or return;
212 return $res unless $?;
216 sub maybe_manually($) {
219 if (Get("$n")) { Log "YES (set manually)\n"; }
220 else { Log "NO (set manually)\n"; }
229 my $upper = $pkg; $upper =~ tr/a-z/A-Z/; $upper =~ s/[^0-9A-Z]+/_/g;
230 Log "Checking for package $pkg ... ";
231 maybe_manually("CONFIG_HAVE_$upper") and return Get("CONFIG_HAVE_$upper");
232 my $ver = TryCmd("pkg-config --modversion $pkg 2>/dev/null");
237 if (defined($opts{minversion})) {
238 my $min = $opts{minversion};
239 if (!defined TryCmd("pkg-config --atleast-version=$min $pkg")) {
240 Log("NO: version $ver is too old (need >= $min)\n");
244 Log("YES: version $ver\n");
245 Set("CONFIG_HAVE_$upper" => 1);
246 Set("CONFIG_VER_$upper" => $ver);
247 my $cf = TryCmd("pkg-config --cflags $pkg");
248 Set("${upper}_CFLAGS" => $cf) if defined $cf;
249 my $lf = TryCmd("pkg-config --libs $pkg");
250 Set("${upper}_LIBS" => $lf) if defined $lf;
256 return join(".", map { sprintf("%05s", $_) } split(/\./, $v));
262 my $upper = $pkg; $upper =~ tr/a-z/A-Z/; $upper =~ s/[^0-9A-Z]+/_/g;
263 Log "Checking for package $pkg ... ";
264 maybe_manually("CONFIG_HAVE_$upper") and return Get("CONFIG_HAVE_$upper");
265 my $pc = $opts{script};
266 my $ver = TryCmd("$pc --version 2>/dev/null");
271 if (defined($opts{minversion})) {
272 my $min = $opts{minversion};
273 if (ver_norm($ver) lt ver_norm($min)) {
274 Log("NO: version $ver is too old (need >= $min)\n");
278 Log("YES: version $ver\n");
279 Set("CONFIG_HAVE_$upper" => 1);
280 Set("CONFIG_VER_$upper" => $ver);
282 my $want = $opts{want};
283 defined $want or $want = ["cflags", "libs"];
285 my $uw = $w; $uw =~ tr/a-z-/A-Z_/;
286 my $cf = TryCmd("$pc --$w");
287 Set("${upper}_${uw}" => $cf) if defined $cf;