最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【已解决】makefile中目标中实现命令行中的条件判断

makefile crifan 1513浏览 0评论
折腾:
【已解决】Makefile中目标中嵌套使用ifeq条件判断
期间,此处需要在makefile中的target目标中的命令行中实现条件判断。
去找找看
shell – How to write nested if in makefile? – Stack Overflow
shell if condition in makefile
sh – Shell conditional in makefile – Stack Overflow
    @if [ $(ENABLE_UPDATE_GITHUB_IO_README) == true ]; then \
    python $(UPDATE_GITHUB_IO_README_FILE) --localGithubIoPath $(GITHUB_IO_PATH);\
    fi;\
结果:
        # cd /Users/xxx/dev/crifan/gitbook/gitbook_template/books/mobile_network_evolution_history && \
        # pwd
/bin/sh: -c: line 0: syntax error near unexpected token `then'
试试:
    git pull && \
    @if [ $(ENABLE_UPDATE_GITHUB_IO_README) == true ]; then 
    python $(UPDATE_GITHUB_IO_README_FILE) --localGithubIoPath $(GITHUB_IO_PATH);
    fi;
    # git status && \
结果:
        @if [ true == true ]; then 
/bin/sh: -c: line 0: syntax error near unexpected token `then'
/bin/sh: -c: line 0: `cd /Users/xxx/dev/crifan/crifan.github.io && pwd && ls -la && git pull && @if [ true == true ]; then '
make: *** [commit] Error 2
试试:
    @if [ $(ENABLE_UPDATE_GITHUB_IO_README) == true ]; then \
        python $(UPDATE_GITHUB_IO_README_FILE) --localGithubIoPath $(GITHUB_IO_PATH); \
    fi; \
结果:
        git pull && \
        @if [ true == true ]; then \
                python /Users/xxx/dev/crifan/gitbook/gitbook_template/common/tools/update_crifan_github_io_readme.py --localGithubIoPath /Users/xxx/dev/crifan/crifan.github.io; \
        fi; \
        # git status && \
        # git add mobile_network_evolution_history/* && \
        # git status && \
        # git commit -m "1. update book mobile_network_evolution_history" && \
        # git status && \
        # git push && \
        # cd /Users/xxx/dev/crifan/gitbook/gitbook_template/books/mobile_network_evolution_history && \
        # pwd
/bin/sh: -c: line 0: syntax error near unexpected token `then'
还是会报,无法识别then
去掉空格:
    @if [ $(ENABLE_UPDATE_GITHUB_IO_README) == true ]; then\
        python $(UPDATE_GITHUB_IO_README_FILE) --localGithubIoPath $(GITHUB_IO_PATH);\
    fi;\
结果:错误依旧。
试试单行
    @if [ $(ENABLE_UPDATE_GITHUB_IO_README) == true ]; then python $(UPDATE_GITHUB_IO_README_FILE) --localGithubIoPath $(GITHUB_IO_PATH); fi;
结果:
错误依旧
试试:
@if [ $(ENABLE_UPDATE_GITHUB_IO_README) == true ] then python $(UPDATE_GITHUB_IO_README_FILE) --localGithubIoPath $(GITHUB_IO_PATH) fi
结果:
可以运行,但结果报错:
        ls -la && \
        git pull && \
        @if [ true == true ] then python /Users/xxx/dev/crifan/gitbook/gitbook_template/common/tools/update_crifan_github_io_readme.py --localGithubIoPath /Users/xxx/dev/crifan/crifan.github.io fi
/Users/xxx/dev/crifan/crifan.github.io
total 16
drwxr-xr-x  59 xxx  CORP\Domain Users  1888  9 16 17:42 .
drwxr-xr-x  13 xxx  CORP\Domain Users   416  8 20 14:03 ..
drwxr-xr-x  15 xxx  CORP\Domain Users   480  9 18 14:29 .git
drwxr-xr-x   6 xxx  CORP\Domain Users   192  9 16 17:59 5g_message_rcs_tech_summary
。。。
Already up to date.
/bin/sh: @if: command not found
make: *** [commit] Error 127
很明显,不知此@if
改为:
if [ $(ENABLE_UPDATE_GITHUB_IO_README) == true ]; then python $(UPDATE_GITHUB_IO_README_FILE) --localGithubIoPath $(GITHUB_IO_PATH); fi;
结果:
可以运行了:
localGithubIoPath=/Users/xxx/dev/crifan/crifan.github.io
代码:
args = parser.parse_args()
print("args=%s" % args)
localGithubIoPath = args.localGithubIoPath
print("localGithubIoPath=%s" % localGithubIoPath)
输出:
args=Namespace(localGithubIoPath='/Users/xxx/dev/crifan/crifan.github.io')
localGithubIoPath=/Users/xxx/dev/crifan/crifan.github.io
再去优化缩进:
    git pull && \
    if [ $(ENABLE_UPDATE_GITHUB_IO_README) == true ]; then \
        python $(UPDATE_GITHUB_IO_README_FILE) --localGithubIoPath $(GITHUB_IO_PATH); \
    fi;
结果:
也是可以的。
且改为false:
ifeq ($(ENABLE_COMMIT_GITHUB_IO), true)
# GITHUB_IO_PATH=/Users/crifan/dev/dev_root/github/github.io/crifan.github.io
GITHUB_IO_PATH=/Users/xxx/dev/crifan/crifan.github.io


ENABLE_UPDATE_GITHUB_IO_README = false
# ENABLE_UPDATE_GITHUB_IO_README = true
endif
以及加上else
    if [ $(ENABLE_UPDATE_GITHUB_IO_README) == true ]; then \
        python $(UPDATE_GITHUB_IO_README_FILE) --localGithubIoPath $(GITHUB_IO_PATH); \
    else \
        echo Ignored update README.md before commit $(BOOK_NAME) to github.io
    fi;
结果:
        else \
                echo Ignored update README.md before commit mobile_network_evolution_history to github.io
/bin/sh: -c: line 1: syntax error: unexpected end of file
改为:
    if [ $(ENABLE_UPDATE_GITHUB_IO_README) == true ]; then \
        python $(UPDATE_GITHUB_IO_README_FILE) --localGithubIoPath $(GITHUB_IO_PATH); \
    else \
        echo "Ignored update README.md before commit $(BOOK_NAME) to github.io";
    fi;
结果:错误依旧。
加上:
    if [ $(ENABLE_UPDATE_GITHUB_IO_README) == true ]; then \
        python $(UPDATE_GITHUB_IO_README_FILE) --localGithubIoPath $(GITHUB_IO_PATH); \
    else \
        echo "Ignored update README.md before commit $(BOOK_NAME) to github.io"; \
    fi;
结果:
正常输出了:
Ignored update README.md before commit mobile_network_evolution_history to github.io
【总结】
此处在makefile中的target目标中,有:
commit:
。。。
    cd $(GITHUB_IO_PATH) && \
    pwd && \
    ls -la && \
    git pull && \
这种,其实是命令行的逻辑,其中想要实现 条件判断,且用到了makefile中的变量判断
最后是:
commit:
    @echo ================================================================================
ifeq ($(ENABLE_COMMIT_GITHUB_IO), true)
    @echo Commit for $(BOOK_NAME)
    # rsync $(RSYNC_PARAMS) $(RELEASE_PATH) $(GITHUB_IO_PATH)
    cd $(GITHUB_IO_PATH) && \
    pwd && \
    ls -la && \
    git pull && \
    if [ $(ENABLE_UPDATE_GITHUB_IO_README) == true ]; then \
        python $(UPDATE_GITHUB_IO_README_FILE) --localGithubIoPath $(GITHUB_IO_PATH); \
    else \
        echo "Ignored update README.md before commit $(BOOK_NAME) to github.io"; \
    fi;
else
    @echo Ignored commit $(BOOK_NAME) to github.io
endif
即:
在commit的target中,\反斜杠中,即其实是命令行环境,实现条件判断,则是:
if xxx; then \
    do something; \
else \
    do something else; \
fi;
即可。
其本身其实就是:普通命令行中的写法,和makefile没啥关系。
其中引用此处的makefile中的变量,则也是普通的:$(var_in_makefile),即可。

转载请注明:在路上 » 【已解决】makefile中目标中实现命令行中的条件判断

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
83 queries in 0.186 seconds, using 22.02MB memory