【发布时间】:2011-02-12 17:20:58
【问题描述】:
我正在尝试实现逻辑以在 makefile 中显示构建进度。
我可以在级联的makefile中成功地为目标“simple”打印它。但是,当涉及到 makefile 中的另一个目标“for”时,出现了问题,我无法弄清楚它是什么。
任何帮助将不胜感激。
## BUILD is initially undefined
ifndef BUILD
# T estimates how many targets we are building by replacing BUILD with a special string
T := $(shell $(MAKE) progress --no-print-directory \
-nrRf $(firstword $(MAKEFILE_LIST)) \
BUILD="COUNTTHIS" | grep -c "COUNTTHIS")
## N is the target number
N := x
## incrementing counter
C = $(words $N)$(eval N := x $N)$(shell export $N)
## BUILD is now defined to show the progress, this also avoids redefining T in loop
BUILD = echo "`expr " [\`expr $C '*' 100 / $T\`" : '.*\(....\)$$'`%]"
endif
MODULE_LIST = module1
MODULE_LIST := $(MODULE_LIST) module2
MODULE_LIST := $(MODULE_LIST) module3
MODULE_LIST := $(MODULE_LIST) module4
MODULE_LIST := $(MODULE_LIST) module5
progress:
@$(BUILD)
@$(BUILD)
@$(BUILD)
@$(BUILD)
@$(BUILD)
simple:
# T=5 and C increases for every access
@$(BUILD) "Cleaning Module \"module1\""
@sleep 0.1
@$(BUILD) "Cleaning Module \"module2\""
@sleep 0.1
@$(BUILD) "Cleaning Module \"module3\""
@sleep 0.1
@$(BUILD) "Cleaning Module \"module4\""
@sleep 0.1
@$(BUILD) "Cleaning Module \"module5\""
@sleep 0.1
for:
# T=1 and C increases for every access but not inside the for loop
@for MODULE in $(MODULE_LIST); do \
$(BUILD) "Cleaning Module \"$$MODULE\"" ; \
sleep 0.1 ; \
done
【问题讨论】:
-
for MODULE...命令是否可以在您的操作系统的命令行中工作? -
我怀疑这是通过改进
make更好地实现的,但让我看看我是否理解。您使用-n运行make 来计算进度标记的数量,然后重新定义BUILD以输出分数进度?好的,这很聪明。但看起来您必须手动干预每条规则才能调用BUILD。如果是这样,那不就是让知道如何在不被告知的情况下执行大多数构建的许多步骤的目的吗? -
@dmckee 是的,计算用于特定构建目标的命令总数是正确的。然而,它只是知道目标必须调用多少命令。对于目标“simple”,它是 5。但是,对于目标“for”,它是 2,这就是为什么我必须创建另一个虚拟目标“progress”,当在 makefile 中使用循环时,它实际上给出了总命令。我的问题是当循环运行时,计数器没有增加,我什至没有丝毫线索知道出了什么问题! :(
-
当你
make for时会发生什么? -
这些是输出: :~/Desktop$ make simple # T=5 并且每次访问都会增加 C [20%] 清理模块 "module1" [40%] 清理模块 "module2" [60 %] 清理模块 "module3" [80%] 清理模块 "module4" [100%] 清理模块 "module5" ~/Desktop$ make for # T=1 并且 C 每次访问都会增加,但不在 for 循环内 [20% ] 清洁模块“module1” [20%] 清洁模块“module2” [20%] 清洁模块“module3” [20%] 清洁模块“module4” [20%] 清洁模块“module5”