我只需要能夠將pwdafter的值分配cd給一個變數,這樣我就可以在我的網路瀏覽器中打開一個檔案,但我似乎無法弄清楚如何在 Makefile 中做到這一點。有趣的是,在搜索有關想要在 cd 之后獲取當前正在運行的 Makefile 目錄的人的其他答案時(與我想要做的相反,即無論 Makefile 在哪里都獲取實際的當前作業目錄),做他們說的東西在 cd 之后不起作用似乎仍然指向 Makefile,而不是我 cd 進入的目錄。
我在 Mac OS 10.15 上運行 GNU Make 3.81,如果這很重要的話。
這是我的目錄設定:
whatever/
|
├── foo/
│ ├── Makefile
│ └── make_a_file.py
└── bar/
└── Makefile
make_a_file.py 生成 output.txt。make_a_file.py 可以通過 /foo/Makefile 運行make bizz,結果是:
whatever/
|
├── foo/
│ ├── Makefile
│ ├── make_a_file.py
│ └── output.txt
└── bar/
└── Makefile
如果我手動將 output.txt 復制到 bar 目錄并運行 /bar/Makefile's 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:///$(WHATEVER)/bar/output.htmlor的東西python -mwebbrowser file:///$(FULLPATH)/output.html。我不想硬編碼絕對路徑;我只能指望 bar/ 和 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 必須作業,并且make html必須在與 cd 相同的子 shell 中執行,因為make html如果不先 cd'ing into bar 就無法作業。然而,所有的回聲你都會相信你還在 foo 中。
這里發生了什么?有沒有辦法在不硬編碼絕對路徑的情況下解決這個問題?
uj5u.com熱心網友回復:
您嘗試過的方法都是make在執行 shell 命令之前評估的。您需要撰寫一個輸出當前目錄的 shell 命令,該命令在cd.
最簡單的選擇是pwd:
compile:
cp outputs/outfile.txt ../bar/output.txt
cd ../bar/; \
pwd; \
make html; \
python -mwebbrowser file:///$$(pwd)/_build/html/output.html
$PWD或者,您可以通過轉義來延遲評估,$以便外殼評估它而不是make:
compile:
cp outputs/outfile.txt ../bar/output.txt
cd ../bar/; \
echo $$PWD; \
make html; \
python -mwebbrowser file:///$$PWD/_build/html/output.html
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/474088.html
上一篇:總結'剩余'餅圖更具可讀性
