Makefile中Else命令执行异常问题求助
问题描述
编写了一个Makefile,用于检查指定文件夹中是否存在目标文件ms_auix:若不存在,则通过curl命令从JFrog Artifactory下载该文件;若存在则不执行任何操作。
原Makefile代码:
si_file := ms_auix extract_path := C:\si si_url := https://artifact.com/artifactory/aksi/SI/$(si_file).zip .PHONY: download-si download-si: @echo "Checking for $(si_file) in $(extract_path)..." @if not exist "$(extract_path)\$(si_file)" ( echo "Downloading $(si_file) from Artifactory..."; curl -u te14:cmVXluNkVJ -O "$(si_url)"; ) else ( echo "$(si_file) already exists in $(extract_path)."; )
运行脚本时,文件能成功从Artifactory下载,但仍会触发Else分支,终端报错:
) else ( process_begin: CreateProcess(NULL, ) else (, ...) failed. make (e=2): The system cannot find the file specified. make: *** [Makefile:11: download-si] Error 2
问题原因与修复方案
问题原因
在Windows环境下,Make会将每个缩进行视为独立的cmd命令执行。而Windows cmd的if/else语法要求整个条件结构要么是单行命令,要么用续行符^连接多行,确保整个逻辑是一个完整的cmd命令。原代码中) else (单独占一行,被Make当作独立命令执行,系统找不到这个命令,因此报错。
修复方案
有两种可行的修改方式:
方式1:将条件逻辑整合成单行
si_file := ms_auix extract_path := C:\si si_url := https://artifact.com/artifactory/aksi/SI/$(si_file).zip .PHONY: download-si download-si: @echo "Checking for $(si_file) in $(extract_path)..." @if not exist "$(extract_path)\$(si_file)" (echo "Downloading $(si_file) from Artifactory..."; curl -u te14:cmVXluNkVJ -O "$(si_url)") else (echo "$(si_file) already exists in $(extract_path).")
方式2:使用cmd续行符^连接多行
si_file := ms_auix extract_path := C:\si si_url := https://artifact.com/artifactory/aksi/SI/$(si_file).zip .PHONY: download-si download-si: @echo "Checking for $(si_file) in $(extract_path)..." @if not exist "$(extract_path)\$(si_file)" (^ echo "Downloading $(si_file) from Artifactory..."; ^ curl -u te14:cmVXluNkVJ -O "$(si_url)" ^ ) else (^ echo "$(si_file) already exists in $(extract_path)." ^ )
额外提示
原代码中curl的-O参数会将文件下载到当前工作目录,而非指定的extract_path。如果需要将文件直接下载到目标路径,可以将-O替换为-o "$(extract_path)\$(si_file).zip"。
内容的提问来源于stack exchange,提问作者PforPython
相关产品推荐
相关产品推荐

