折腾:
【已解决】给crifan的gitbook的template添加部署时更新github.io的README
期间,涉及到在makefile中使用嵌套的ifeq
ifeq的嵌套
## Commit generated files to github io commit: all @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 && \ ifeq ($(ENABLE_UPDATE_GITHUB_IO_README), true) @python $(UPDATE_GITHUB_IO_README_FILE) --localGithubIoPath $(GITHUB_IO_PATH) endif # git status && \ # git add $(BOOK_NAME)/* && \ # git status && \ # git commit -m $(COMMIT_COMMENT) && \ # git status && \ # git push && \ # cd $(CURRENT_DIR) && \ # pwd else @echo Ignored commit $(BOOK_NAME) to github.io endif
语法报错
../../common/gitbook_makefile.mk:397: *** extraneous `else'. Stop.
看来不支持ifeq的嵌套
makefile ifeq embedding
7.2 Syntax of Conditionals GNU make
makefile ifeq nested
ifeq ($(VAR1),some-string) ifneq ($(VAR2),some-other-string) <do something> endif endif
去试试
不过此处很特殊:
属于在target中,即commit的target中的
commit: all @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 && \ ifeq ($(ENABLE_UPDATE_GITHUB_IO_README), true) @python $(UPDATE_GITHUB_IO_README_FILE) --localGithubIoPath $(GITHUB_IO_PATH) endif
结果:
至少make help没语法报错了。
继续试试逻辑上对不对
结果报错:
cd /Users/xxx/dev/crifan/crifan.github.io && \ pwd && \ ls -la && \ git pull && \ ifeq (true, true) /bin/sh: -c: line 0: syntax error near unexpected token `true,' /bin/sh: -c: line 0: `cd /Users/xxx/dev/crifan/crifan.github.io && pwd && ls -la && git pull && ifeq (true, true)' make: *** [commit] Error 2
去试试:
ifeq ($(ENABLE_UPDATE_GITHUB_IO_README), true) @python $(UPDATE_GITHUB_IO_README_FILE) --localGithubIoPath $(GITHUB_IO_PATH) endif
结果:错误依旧
发现了
pwd && ls -la && git pull && ifeq (true, true)
这种是 命令行中的写法,不支持ifeq
看看如何改造成别的方式,即命令行中,如何实现条件判断,且整合到makefile中能正常运行:
【已解决】makefile中目标中实现命令行中的条件判断
【总结】
此处,如果本身是makefile中,实现嵌套的ifqe,则是普通的写法:
ifeq ($(xxx),TRUE) ifeq ($(xxx),FALSE) # do something endif endif
而此处特殊在于:
(1)处于target中
则需要缩进
some_target: ... ifeq ($(var), true) ... ifeq($(var2), true) ... endif ... endif
即可。
(2)此处底层ifeq,其实是属于 反斜杠\ 之中的逻辑,本身属于 命令行中的语法,而不是make中的ifeq的语法。
详见:
【已解决】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
即可。
转载请注明:在路上 » 【已解决】Makefile中目标中嵌套使用ifeq条件判断