简体   繁体   中英

Invoking a python script from a makefile in another directory

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.

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.


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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM