views:

34

answers:

1

I have a makefile that invokes a python script that lives in the same directory as the makefile. It works just fine.
In makefile #1:

auto:
   ./myscript.py

Now, I have another makefile, in another directory, and wish to call the first makefile from it. In makefile #2:

target:
    cd $(DIR); $(MAKE) auto;

The problem is, when the script runs, it runs as though it's in the same dir as makefile #2. On stdout, I see "make[3]: Leaving directory" and the path to #1, just after the make is executed and before the script is run.

On a suggestion I tried modifying makefile #2 to:

target:
    ( cd $DIR; $MAKE auto; )

but that's interpreted as "cd IR; AKE auto". When I replace the parentheses around DIR and MAKE, I get the same behavior as before.

I've tried modifying the python script by having it assume it's in dir #2 and giving it a path to #1, but the behavior doesn't change.

What's going on, and what should I do?

Update: My comment below messed up code formatting so it's here:

I tried this out and got essentially what you describe. Might it have anything to do with the fact that target auto invokes a "makefile.rules" file?

auto: 
    @echo this is makefile \#1 making $@ in $(PWD)
    FLAG=1 $(MAKE) -f makefile.rules rulestargetA
    FLAG=2 $(MAKE) -f makefile.rules rulestargetB
    ./myscript.py

I omitted that fact for simplicity, but now I wonder.

Update 2: I don't understand why the makefiles aren't causing myscript.py to be run as though it's in the directory in which it resides, but I have been trying to get the script to operate correctly even when invoked from a different directory. It opens a couple of subprocesses, runs an executable in each subprocess, and saves the stdout from each subprocess to files. Python's subprocess.Popen passes in the current working directory, which would be where the script is invoked from, not where it resides, by default. I've added script to pass in the residential directory as cwd into the Popen call. For some reason, though, when I run myscript.py in its own directory it works, but when I invoke it from elsewhere (from the command line) it hangs in proc.communicate(). I should solve this python issue, but I'd still like to know why the external makefile can't invoke this script as from its own directory.

A: 

This is very strange. First the easy part:

target:
  ( cd $DIR; $MAKE auto; )

The parentheses do nothing here, and Make interprets $DIR as $D followed by the letter 'I' and the letter 'R'. Since the variable D is not defined, this works out to 'IR'. Same for $MAKE.

Now for the real problem. The makefiles as written should work. And you say it leaves directory #1 before the script runs? All I can suggest is that you try a simpler problem first. Put this in Makefile #1:

auto:
  @echo this is makefile \#1 making $@ in $(PWD)

And then use Makefile #2 to make target. This should produce

make[1]: Entering directory 'path-to-one'
this is makefile #1 making auto in path-to-one
make[1]: Leaving directory 'path-to-one'

If this is what it says, then there's something screwy about your script. If it produces nothing like this, you're failing to reach Makefile #1 at all, and maybe your DIR isn't right. If it works but says it's in directory #2, then my best guess is that you have another rule referring to Makefile #1. Try the experiment and let us know.

EDIT:
Well, that probably explains how it can leave directory #1 before the script runs. I suggest you comment out those lines and see if the problem is still there. Now about this script: what does it do and how do you know where it's running?

Beta
I tried this out and got essentially what you describe. Might it have anything to do with the fact that target auto invokes a "makefile.rules" file? auto: @echo this is makefile \#1 making $@ in $(PWD) FLAG=1 $(MAKE) -f makefile.rules rulestargetA FLAG=2 $(MAKE) -f makefile.rules rulestargetB ./myscript.pyI omitted that fact for simplicity, but now I wonder.
jasper77
How can the makefile in dir#1 invoking makefile.rules (which is essentially another makefile) within the same directory, on code that is in that directory, cause that directory to be exited before the script is run?
jasper77
@jasper77: I don't have enough information to be sure, but I have two hypotheses: 1) the "make[3] Leaving directory dir_one" message is from a `$(MAKE)` command in the `auto` rule (the entering and leaving messages can be misleading, since the directory can be the same as the one where the last process was running) 2) the script may launch a process and then terminate, allowing Make to terminate (and "Leave") before the output of the script's process appears. Anyway, it looks as if you've solved the problem.
Beta