cons-discuss
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

BuildInTargetDir patch: cd to particular target's dir before running cmd


From: Gary Oberbrunner
Subject: BuildInTargetDir patch: cd to particular target's dir before running cmds
Date: Tue, 08 Jan 2002 11:59:24 -0500
User-agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:0.9.6) Gecko/20011120

I don't know if anyone's interested in something like this, but I seemed to need it today, or at least it seemed by far the easiest thing I could do, so here it is. If you like it, feel free to add it to cons. If not, well...

What it is is a new cons command, $env->BuildInTargetDir(<files>).
It makes cons cd to the directory of the target(s) before running the build commands for that target. It's a little like Conscript_chdir, but it's per-target instead of global. I don't think it would break any existing stuff.

--
. . . . . . . . . . . . . . . . . . . . . . . . .
Gary Oberbrunner                address@hidden
GenArts, Inc.                   Tel: 617-492-2888
8 Clinton Street                Fax: 617-492-2852
Cambridge, MA 02139 USA         http://web.genarts.com

Index: cons
===================================================================
RCS file: /cvsroot/sapphire/cons,v
retrieving revision 1.10
retrieving revision 1.11
diff -u -r1.10 -r1.11
--- cons        2001/12/18 23:28:07     1.10
+++ cons        2002/01/08 16:48:55     1.11
@@ -15,7 +15,7 @@
 
 use vars qw( $CVS_id $CVS_ver $ver_num $ver_rev $version );
 
-$CVS_id = '$Id: cons,v 1.10 2001/12/18 23:28:07 garyo Exp $ ';
+$CVS_id = '$Id: cons,v 1.11 2002/01/08 16:48:55 garyo Exp $ ';
 $CVS_ver = (split(/\s+/, $CVS_id))[2];
 
 $ver_num = "2.3";
@@ -1164,6 +1164,14 @@
     }
 }
 
+# Tell cons to cd to these targets' dirs before running their build
+# commands.
+sub BuildInTargetDir {
+    my $env = shift;
+    map($_->{build_in_target_dir} = 1,
+       map($dir::cwd->lookup($env->_subst($_)), @_));
+}
+
 sub Install {
     my($env) = shift;
     my($tgtdir) = $dir::cwd->lookupdir($env->_subst(shift));
@@ -1996,8 +2004,18 @@
        }
        next if ! $param::build;
 
+       # Handle BuildInTargetDir:
+       # must cd back to $cwd before returning in any way.
+       my $cwd = Cwd::cwd();
+       if ($tgt->{build_in_target_dir}) {
+           my $tgtdir = $tgt->{dir}->path;
+           print "cd ".$tgtdir."\n";
+           chdir($tgtdir) ||
+               die("$0: couldn't change to directory $tgtdir ($!)\n");
+       }
+
        if ($com =~ /^\[perl\]\s*/) {
-           my $perlcmd = $';
+           my $perlcmd = $';   # '
            my $status;
            {
                # Restore the script package variables that were defined
@@ -2010,6 +2028,8 @@
                NameSpace::restore($package, $pkgvars) if $pkgvars;
                $status = eval "package $package; $perlcmd";
                NameSpace::remove($package, keys %$pkgvars) if $pkgvars;
+               print "cd ".$cwd."\n", chdir $cwd
+                   if $tgt->{build_in_target_dir};
            }
            if (!defined($status)) {
                warn "$0: *** Error during perl command eval: address@hidden";
@@ -2021,9 +2041,14 @@
            }
            next;
        }
+
        if (! $self->do_command($com, $tgt->path)) {
-               return undef;
+           print "cd ".$cwd."\n", chdir $cwd
+               if $tgt->{build_in_target_dir};
+           return undef;
        }
+       print "cd ".$cwd."\n", chdir $cwd
+           if $tgt->{build_in_target_dir};
     }
 
     # success.
@@ -2085,8 +2110,9 @@
     if ($?) {
        my ($b0, $b1) = ($? & 0xFF, $? >> 8);
        my $err = $b1 || $?;
-       my $warn = qq($0: *** [$path] Error $err);
+       my $warn = qq($0: *** [$path] Error $err ($b1.$b0));
        $warn .= " (executable not found in path?)" if $b1 == 0xFF;
+       $warn .= " ... PATH is $ENV{PATH}" if $b1 == 0xFF;
        warn "$warn\n";
        return undef;
     }
@@ -6074,6 +6100,17 @@
 This is especially useful for allowing incremental updates to libraries
 or debug information files which are updated rather than rebuilt anew each
 time.  Cons will still delete the files when the C<-r> flag is specified.
+
+=head2 The C<BuildInTargetDir> method
+
+The C<BuildInTargetDir> method tells cons to change to the directory of
+the target or targets before executing the build commands for each
+listed target.  It is invoked as:
+
+  BuildInTargetDir $env <files>;
+
+This is useful if the commands don't have a way to specify the output
+dir or filename.
 
 =head2 The C<AfterBuild> method
 

reply via email to

[Prev in Thread] Current Thread [Next in Thread]