在Makefile中CD之后,如何获取工作目录?
我只需要能够在pwd
之后分配cd
之后的值在makefile中弄清楚如何做到这一点。有趣的是,当搜索有关想要在CD之后获得当前运行的Makefile目录的人的其他答案时(而不是我想做的事,这是获得实际的当前工作目录,无论Makefile在哪里)他们说的事情在CD之后不起作用的事情似乎仍然指向Makefile,而不是我使用的目录。
如果这很重要,我正在Mac OS 10.15上运行GNU 3.81。
这是我的目录设置:
whatever/
|
├── foo/
│ ├── Makefile
│ └── make_a_file.py
└── bar/
└── Makefile
make_a_file.py生成output.txt。可以通过make bizz
从/foo/makefile运行make_a_file.py,导致以下内容:
whatever/
|
├── foo/
│ ├── Makefile
│ ├── make_a_file.py
│ └── output.txt
└── bar/
└── Makefile
如果我手动将outper.txt复制到bar目录,并运行/bar/makefile的make html ,我最终在bar中获得了一个html文件,即
whatever/
|
├── foo/
│ ├── Makefile
│ ├── make_a_file.py
│ └── output.txt
└── bar/
├── Makefile
├── output.txt
└── output.html
我的问题是我想使用/foo/makefile在网络浏览器中打开output.html,但是看来我无法获得绝对/bar/output.html的路径,所以我不能称呼python -mwebbrowser file://// $(whything)/bar/output.html
或python-- mwebbrowser file:/// $(fullpath)/output.html
。我不想对绝对路径进行编码;我只能依靠bar/ and foo/不更改其名称,不一定在其上方。
这就是当前 /foo /makefile的样子。
bizz:
python make_a_file.py
compile:
cp outputs/outfile.txt ../bar/output.txt
cd ../bar/; \
echo "$(PWD)"; \
echo "$(shell pwd)"; \
echo "$(CURDIR)"; \
make html; \
python -mwebbrowser file:///$(CURDIR)/_build/html/output.html
当我从foo/:
(venv) foo user$ make compile
cp output.txt ../bar/output.txt
cd ../bar/; \
echo "/whatever/foo"; \
echo "/whatever/foo"; \
echo "/whatever/foo"; \
make html; \
python -mwebbrowser file:////whatever/foo/output.html
/whatever/foo
/whatever/foo
/whatever/foo
Running Sphinx v4.4.0
[and other miscellaneous output from the makefile in bar]
build succeeded.
0:85: execution error: File some object wasn’t found. (-43)
我知道CD必须有效时,这会导致这一点,并且制造html
必须与CD同一子壳执行,因为使HTML 如果不首先进入bar就无法使用。但是,所有的回声都会相信您仍然处于foo中。
这是怎么回事?在没有硬编码的绝对路径的情况下,有没有办法解决这个问题?
I just need to be able to assign the value of pwd
after cd
to a variable so I can open a file in my web browser, but I can't seem to figure out how to do that in a Makefile. Interestingly, when searching other answers about people who want to get the directory of the currently-running Makefile after a cd (as opposed to what I want to do, which is get the actual current working directory regardless of where the Makefile is), doing things that they said don't work after a cd still seem to point to the Makefile, not the directory I cd'd into.
I'm running GNU Make 3.81 on Mac OS 10.15, if that matters.
This is my directory setup:
whatever/
|
├── foo/
│ ├── Makefile
│ └── make_a_file.py
└── bar/
└── Makefile
make_a_file.py generates output.txt. make_a_file.py can be run from /foo/Makefile via make bizz
, resulting in:
whatever/
|
├── foo/
│ ├── Makefile
│ ├── make_a_file.py
│ └── output.txt
└── bar/
└── Makefile
If I manually copy output.txt to the bar directory, and run /bar/Makefile's make html
, I end up with an html file in bar, i.e.,
whatever/
|
├── foo/
│ ├── Makefile
│ ├── make_a_file.py
│ └── output.txt
└── bar/
├── Makefile
├── output.txt
└── output.html
My problem is that I want to open output.html in my web browser using /foo/Makefile, but it seems that I cannot get the absolute path of /bar/output.html, so I can't call something like python -mwebbrowser file:///$(WHATEVER)/bar/output.html
or python -mwebbrowser file:///$(FULLPATH)/output.html
. I do not want to hardcode the absolute path; I can only count on bar/ and foo/ not changing their names, not necessarily anything above them.
This is what /foo/Makefile currently looks like.
bizz:
python make_a_file.py
compile:
cp outputs/outfile.txt ../bar/output.txt
cd ../bar/; \
echo "$(PWD)"; \
echo "$(shell pwd)"; \
echo "$(CURDIR)"; \
make html; \
python -mwebbrowser file:///$(CURDIR)/_build/html/output.html
Which results this when I run it from foo/:
(venv) foo user$ make compile
cp output.txt ../bar/output.txt
cd ../bar/; \
echo "/whatever/foo"; \
echo "/whatever/foo"; \
echo "/whatever/foo"; \
make html; \
python -mwebbrowser file:////whatever/foo/output.html
/whatever/foo
/whatever/foo
/whatever/foo
Running Sphinx v4.4.0
[and other miscellaneous output from the makefile in bar]
build succeeded.
0:85: execution error: File some object wasn’t found. (-43)
I know that the cd must have worked, and that the make html
must be executed in the same subshell as the cd because make html
does not work without cd'ing into bar first. And yet, all of the echos would you have believe that you are still in foo.
What's going on here? Is there a way around this without hardcoding the absolute path?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在执行Shell命令之前,您尝试过的方法都通过
make
评估。您需要编写一个输出当前目录的shell命令,该目录在cd
之后执行。最简单的选项是
pwd
:另外,您可以通过逃脱
$
来延迟$ PWD
的评估,以便shell对其进行评估,而不是<<代码> make :The methods you've tried are all evaluated by
make
before the shell command is ever executed. You need to write a shell command that outputs the current directory, one that executes after thecd
.The simplest option is
pwd
:Alternatively, you could delay the evaluation of
$PWD
by escaping the$
so that the shell evaluates it rather thanmake
: