折腾:
【基本解决】Makefile中从独立文件比如json中读取配置变量
期间,后来想到当前做法:
include ../DeployServerInfo.mk
…
upload:
@echo ================================================================================
ifeq ($(SHOULD_IGNORE), true)
@echo Ignore upload $(BOOK_NAME) to book.crifan.com
else
@echo Upload for $(BOOK_NAME)
sshpass -p $(DEPLOY_SERVER_PASSWORD) rsync -avzh –progress –stats –delete –force $(OUTPUT_PATH) $(DEPLOY_SERVER_USER)@$(DEPLOY_SERVER_IP):$(DEPLOY_SERVER_PATH)
endif
有个小缺点:
当使用此makefile时,就一定要额外导入额外makefile
而有时候,比如有人不需要此处upload,按道理可以不需要导入才对
所以希望实现:
把include放到upload的target中
去研究如何实现
makefile include within target
makefile – include files depended on target – Stack Overflow
gnu make – Can a Makefile ‘include’ be dynamically set based on the target? – Stack Overflow
gnu – Obtaining Target-Specific Variables in included Makefiles – Stack Overflow
gnu make – How to use the include directive in a makefile for a specific target – Stack Overflow
Auto-Dependency Generation | GNU make
Why Makefile include should be after all: target? – Stack Overflow
contiki/Makefile.include at master · contiki-os/contiki
干脆试试直接放include到target中是否可行
upload: @echo ================================================================================ ifeq ($(SHOULD_IGNORE), true)
@echo Ignore upload $(BOOK_NAME) to book.crifan.com
else
@echo Upload for $(BOOK_NAME)
include ../DeployServerInfo.mk
sshpass -p $(DEPLOY_SERVER_PASSWORD) rsync -avzh –progress –stats –delete –force $(OUTPUT_PATH) $(DEPLOY_SERVER_USER)@$(DEPLOY_SERVER_IP):$(DEPLOY_SERVER_PATH)
endif
结果:
include ../DeployServerInfo.mk make: include: No such file or directory make: *** [upload] Error 1
说是找不到。
难道是路径不对?
那加上绝对路径试试
include /Users/crifan/dev/dev_root/gitbook/gitbook_src_root/selenium_summary/../DeployServerInfo.mk make: include: No such file or directory
不行,感觉target中就不支持include
makefile include not working inside target
makefile – GNU make: clean target depends on includes – Stack Overflow
去试了试-减号
如果是放在target中:
upload: -include ../DeployServerInfo.mk sshpass -p $(DEPLOY_SERVER_PASSWORD) rsync -avzh --progress --stats --delete --force $(OUTPUT_PATH) $(DEPLOY_SERVER_USER)@$(DEPLOY_SERVER_IP):$(DEPLOY_SERVER_PATH)
endif
是可以的,但是由于include出错,rsync没了参数还是失败
突然想到,可以利用减号对于不存在的makefile不报错,可以放到最开始:
-include ../DeployServerInfo.mk upload: sshpass -p $(DEPLOY_SERVER_PASSWORD) rsync -avzh --progress --stats --delete --force $(OUTPUT_PATH) $(DEPLOY_SERVER_USER)@$(DEPLOY_SERVER_IP):$(DEPLOY_SERVER_PATH) endif
当故意改名DeployServerInfo.mk导致DeployServerInfo.mk不存在,希望也不报错
结果还是会报错:
➜ selenium_summary git:(master) ✗ make upload Makefile:1: ../GitbookCommon.mk: No such file or directory make: *** No rule to make target `../GitbookCommon.mk'. Stop.
makefile minus include still error
makefile “-include” No such file or directory
c – Error “no such file or directory” when compiling with Makefile – Stack Overflow
makefile minus include still No such file or directory
makefile -include still error
makefile -include
makefile include 短横线
Makefile中include、-include、sinclude的区别 – huangshanchun的专栏 – CSDN博客
结果:
sinclude ../DeployServerInfo.mk
错误依旧:
➜ selenium_summary git:(master) ✗ make upload Makefile:1: ../GitbookCommon.mk: No such file or directory make: *** No rule to make target `../GitbookCommon.mk'. Stop.
此处是mac,感觉和标准make不兼容啊
按道理说:
-include not_exist_file
或
sinclude not_exist_file
不会报错才对
此处mac中make报错了。
去看看版本
是不是太旧导致的?
结果发现最新版也不支持:
➜ selenium_summary git:(master) ✗ gmake upload Makefile:1: ../GitbookCommon.mk: No such file or directory gmake: *** No rule to make target '../GitbookCommon.mk'. Stop.
结果也是不支持的。
【总结】
Mac中make 3.81版,不支持-include和sinclude
而brew安装了最新版本的4.2.1的gmake,也不支持。
那暂时只能是算了。
或者是,保留-include的写法:
-include ../DeployServerInfo.mk
然后万一别人的linux中make支持这个写法,就可以正常工作的:
当include的文件不存在,也不会报错,而会继续运行。