]> mj.ucw.cz Git - libucw.git/blobdiff - lib/perl/Configure.pm
First bits of the sorter infrastructure.
[libucw.git] / lib / perl / Configure.pm
index bbc6e82c268daeb749695e6892414cbbfb0dc52c..383639f9ea3fd1ffb61e47208b4b1184852a193a 100644 (file)
@@ -16,7 +16,7 @@ BEGIN {
        our ($VERSION, @ISA, @EXPORT, @EXPORT_OK, %EXPORT_TAGS);
        $VERSION = 1.0;
        @ISA = qw(Exporter);
-       @EXPORT = qw(&Init &Log &Fail &IsSet &Set &UnSet &Override &Get &Test &Include);
+       @EXPORT = qw(&Init &Log &Notice &Warn &Fail &IsSet &Set &UnSet &Append &Override &Get &Test &Include &Finish &FindFile &TryFindFile);
        @EXPORT_OK = qw();
        %EXPORT_TAGS = ();
 }
@@ -28,8 +28,16 @@ sub Log($) {
        print @_;
 }
 
+sub Notice($) {
+       print @_ if $vars{"VERBOSE"};
+}
+
+sub Warn($) {
+       print "WARNING: ", @_;
+}
+
 sub Fail($) {
-       Log((shift @_) . "\n");
+       Log("ERROR: " . (shift @_) . "\n");
        exit 1;
 }
 
@@ -54,6 +62,11 @@ sub UnSet($) {
        delete $vars{$x} unless $overriden{$x};
 }
 
+sub Append($$) {
+       my ($x,$y) = @_;
+       Set($x, (IsSet($x) ? (Get($x) . " $y") : $y));
+}
+
 sub Override($;$) {
        my ($x,$y) = @_;
        $y=1 unless defined $y;
@@ -63,7 +76,7 @@ sub Override($;$) {
 
 sub Test($$$) {
        my ($var,$msg,$sub) = @_;
-       Log "$msg... ";
+       Log "$msg ... ";
        if (!IsSet($var)) {
                Set $var, &$sub();
        }
@@ -106,12 +119,12 @@ sub Init($$) {
                if ($x =~ /^(\w+)=(.*)/) {
                        Override($1 => $2);
                } elsif ($x =~ /^-(\w+)$/) {
-                       Override($1 => 1);
+                       Override($1 => 0);
                        delete $vars{$1};
                } elsif ($x =~ /^(\w+)$/) {
                        Override($1 => 1);
                } else {
-                       print STDERR "Invalid option $_\n";
+                       print STDERR "Invalid option $x\n";
                        exit 1;
                }
        }
@@ -125,8 +138,51 @@ sub Init($$) {
 sub Include($) {
        my ($f) = @_;
        $f = FindFile($f);
-       Log "Loading configuration $f\n";
+       Notice "Loading configuration $f\n";
        require $f;
 }
 
+sub Finish() {
+       print "\n";
+
+       if (Get("SRCDIR") ne ".") {
+               Log "Preparing for compilation from directory " . Get("SRCDIR") . " to obj/ ... ";
+               -l "src" and unlink "src";
+               symlink Get("SRCDIR"), "src" or Fail "Cannot link source directory to src: $!";
+               Override("SRCDIR" => "src");
+               -l "Makefile" and unlink "Makefile";
+               -f "Makefile" and Fail "Makefile already exists";
+               symlink "src/Makefile", "Makefile" or Fail "Cannot link Makefile: $!";
+       } else {
+               Log "Preparing for compilation from current directory to obj/ ... ";
+       }
+       `rm -rf obj` if -d "obj"; Fail "Cannot delete old obj directory" if $?;
+       -d "obj" or mkdir("obj", 0777) or Fail "Cannot create obj directory: $!";
+       -d "obj/lib" or mkdir("obj/lib", 0777) or Fail "Cannot create obj/lib directory: $!";
+       Log "done\n";
+
+       Log "Generating autoconf.h ... ";
+       open X, ">obj/lib/autoconf.h" or Fail $!;
+       print X "/* Generated automatically by $0, please don't touch manually. */\n";
+       foreach my $x (sort keys %vars) {
+               # Don't export variables which contain no underscores
+               next unless $x =~ /_/;
+               my $v = $vars{$x};
+               # Try to add quotes if necessary
+               $v = '"' . $v . '"' unless ($v =~ /^"/ || $v =~ /^\d*$/);
+               print X "#define $x $v\n";
+       }
+       close X;
+       Log "done\n";
+
+       Log "Generating config.mk ... ";
+       open X, ">obj/config.mk" or Fail $!;
+       print X "# Generated automatically by $0, please don't touch manually.\n";
+       foreach my $x (sort keys %vars) {
+               print X "$x=$vars{$x}\n";
+       }
+       close X;
+       Log "done\n";
+}
+
 1;  # OK