从另一个目录中的 makefile 调用 python 脚本

发布于 2024-09-12 20:07:56 字数 1368 浏览 2 评论 0原文

我有一个 makefile,它调用一个与 makefile 位于同一目录中的 python 脚本。它工作得很好。
在 makefile #1 中:

auto:
   ./myscript.py

现在,我在另一个目录中有另一个 makefile,并希望从中调用第一个 makefile。 在 makefile #2 中:

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

问题是,当脚本运行时,它的运行就像与 makefile #2 位于同一目录中一样。在 stdout 上,我在执行 make 之后和运行脚本之前看到“make[3]:离开目录”和 #1 的路径。

根据建议,我尝试将 makefile #2 修改为:

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

但这被解释为“cd IR; AKE auto”。当我替换 DIR 和 MAKE 周围的括号时,我得到与以前相同的行为。

我尝试修改 python 脚本,让它假设它位于 dir #2 中并为其指定 #1 的路径,但行为没有改变。

这是怎么回事,我该怎么办?

更新: 我下面的评论搞乱了代码格式,所以就在这里:

我尝试了这个,基本上得到了你所描述的内容。这可能与目标自动调用“makefile.rules”文件有关吗?

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

为了简单起见,我忽略了这个事实,但现在我想知道。

更新2: 我不明白为什么 makefile 不会导致 myscript.py 运行,就好像它在它所在的目录中一样,但我一直在尝试让脚本即使从不同的目录调用时也能正确运行。它打开几个子进程,在每个子进程中运行可执行文件,并将每个子进程的标准输出保存到文件中。 Python 的 subprocess.Popen 传入当前工作目录,默认情况下,该目录是调用脚本的位置,而不是脚本所在的位置。我添加了脚本以将住宅目录作为 cwd 传递到 Popen 调用中。不过,出于某种原因,当我在自己的目录中运行 myscript.py 时,它可以工作,但是当我从其他地方(从命令行)调用它时,它会挂在 proc.communicate() 中。我应该解决这个 python 问题,但我仍然想知道为什么外部 makefile 无法从其自己的目录调用此脚本。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不乱于心 2024-09-19 20:07:56

这很奇怪。首先是简单的部分:

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

括号在这里没有任何作用,Make 将 $DIR 解释为 $D 后跟字母“I”和字母“R”。由于变量 D 未定义,因此结果为“IR”。 $MAKE 相同。

现在来说说真正的问题。编写的 makefile 应该可以工作。你说它在脚本运行之前离开目录#1?我只能建议你先尝试一个更简单的问题。将其放入 Makefile #1 中:

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

然后使用 Makefile #2 来生成 target。这应该会产生

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

如果这就是它所说的,那么你的脚本有问题。如果它没有产生这样的结果,那么您根本无法到达 Makefile #1,并且可能您的 DIR 不正确。如果它有效但说它位于目录 #2 中,那么我最好的猜测是您有另一个引用 Makefile #1 的规则。尝试一下实验并告诉我们。

编辑:
好吧,这可能解释了它如何在脚本运行之前离开目录#1。我建议您注释掉这些行,看看问题是否仍然存在。现在介绍一下这个脚本:它有什么作用以及如何知道它在哪里运行?

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文