- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在尝试实现在 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 循环在 shell 中执行,因此不会更新 Makefile 的变量(或者至少在 Make 通过评估变量引用构建命令字符串时不会更新一次)里面)。
我能看到的唯一可行的解决方案是将循环结构移到 Makefile 中。试试这个:
## PRINT_PROGRESS is initially undefined
ifndef PRINT_PROGRESS
# T estimates how many targets we are building by replacing PRINT_PROGRESS with a special string
T := $(shell $(MAKE) $(MAKECMDGOALS) --no-print-directory \
-rRf $(firstword $(MAKEFILE_LIST)) \
PRINT_PROGRESS="echo COUNTTHIS" BUILD="test x ||" | grep -c "COUNTTHIS")
N := 1
## PRINT_PROGRESS is now defined to show the progress and update N
PRINT_PROGRESS = echo "`expr " [\`expr $N '*' 100 / $T\`" : '.*\(....\)$$'`%]"$(eval N := $(shell expr $N + 1))
endif
ifndef BUILD
BUILD := #blank
endif
MODULE_LIST = module1
MODULE_LIST += module2
MODULE_LIST += module3
MODULE_LIST += module4
MODULE_LIST += module5
simple:
# T=5 and C increases for every access
@$(PRINT_PROGRESS) "Cleaning Module \"module1\""
@$(BUILD) { sleep 0.1 ; echo "doing some work" ; }
@$(PRINT_PROGRESS) "Cleaning Module \"module2\""
@$(BUILD) { sleep 0.1 ; echo "doing some work" ; }
@$(PRINT_PROGRESS) "Cleaning Module \"module3\""
@$(BUILD) { sleep 0.1 ; echo "doing some work" ; }
@$(PRINT_PROGRESS) "Cleaning Module \"module4\""
@$(BUILD) { sleep 0.1 ; echo "doing some work" ; }
@$(PRINT_PROGRESS) "Cleaning Module \"module5\""
@$(BUILD) { sleep 0.1 ; echo "doing some work" ; }
for:
@$(foreach MODULE,$(MODULE_LIST),\
$(PRINT_PROGRESS) "Cleaning Module \"$(MODULE)\"" ; \
$(BUILD) { \
sleep 0.1 ; \
echo "doing some work" ; \
} ; \
)
这还包括一些其他更改。
乍一看似乎合理的解决方案是从 makefile 中导出 N
并设计 PRINT_PROGRESS
命令,以便始终更新 N
作为环境变量。不过,我找不到使这项工作正常进行的方法,因为它需要某种方法将更新的 N 值返回到 Make after命令已写入,因此单独的命令仍然有效。
编辑:运行上述脚本时的输出(使用更正的制表符缩进),加上使用的版本:
jpab@oberon : /memtmp
$ make --version
GNU Make 3.81
Copyright (C) 2006 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.
There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE.
This program built for x86_64-pc-linux-gnu
jpab@oberon : /memtmp
$ bash --version
GNU bash, version 4.1.5(1)-release (x86_64-pc-linux-gnu)
Copyright (C) 2009 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
jpab@oberon : /memtmp
$ make
# T=5 and C increases for every access
[20%] Cleaning Module "module1"
doing some work
[40%] Cleaning Module "module2"
doing some work
[60%] Cleaning Module "module3"
doing some work
[80%] Cleaning Module "module4"
doing some work
[100%] Cleaning Module "module5"
doing some work
jpab@oberon : /memtmp
$ make for
[20%] Cleaning Module "module1"
doing some work
[40%] Cleaning Module "module2"
doing some work
[60%] Cleaning Module "module3"
doing some work
[80%] Cleaning Module "module4"
doing some work
[100%] Cleaning Module "module5"
doing some work
关于linux - makefile 中的循环问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4979426/
我有一个 Makefile,它针对特定目标调用另一个 Makefile。假设主 Makefile 包含 some_dir/some_target: cd some_dir && make so
这两个文件大多出现在开源项目中。 它们的用途是什么?它们如何工作? 最佳答案 Makefile.am 是程序员定义的文件,由 automake 使用来生成 Makefile.in 文件( .am 代表
我的源代码位于一堆子目录中,例如: src/widgets/apple.cpp src/widgets/knob.cpp src/tests/blend.cpp src/ui/flash.cpp 在项
这就是我所拥有的: SUBDIRS = src/lib/ResourceManager all: $(SUBDIRS) $(SUBDIRS): make install -C $@ 我正在尝试
我想写一个 Makefile 来执行来自两个不同数组的两个输入的命令例如 a = A B C b = 1 2 3 ./run A 1 ./run B 2 ./run C 3 我不知道怎么写,因为在Ma
在 GNU make 手册的早期部分之一,Section 3.7 , 有一个 makefile 配方的大纲 immediate : immediate ; deferred defer
是否存在将 gmake 的 GNU Makefile 转换为可用于 make (FreeBSD-make) 的 Makefile 的实用程序? 最佳答案 该实用程序称为开发人员(程序员,制作大师,..
所以我前段时间了解了什么是 Makefile,创建了一个模板 Makefile,我所做的就是为我正在执行的每个程序复制和更改相同的文件。我改了几次,但它仍然是一个非常粗糙的Makefile。我应该如何
我正在做一些 Makefile 重构,并试图找出最简洁的方法来实现一个 Makefile,它执行以下操作: 有一个变量列出了所有源文件(可以是 C 和 C++ 文件) 所有目标文件都在 OBJ_DIR
我正在尝试创建一个 Makefile,它将通过 tic 编译位于目录中的 terminfo 文件。 tic 还将它自动创建的 termcap 文件复制到系统或用户特定的目标文件夹。对于普通用户,如果
我想要类似的东西 BROKEN_THINGS = \ thing1 \ # thing1 is completely broken thing2 \ # thing2 is broken to
如果我的程序必须为不同的结果(主要是错误)返回不同的值(例如 0、1、2、3 等),则调用该程序的 makefile 将不得不停止执行其余的 makefile 命令。即使该命令产生错误(返回非零值),
我正在学习使用漂亮的 Linux 工具:make。还有一点我想了解的: 让我们看一下这个简单的例子: JADE = $(shell find pages/*.jade) HTML = $(JADE:.
假设您有一个包含两个伪目标“all”和“debug”的 Makefile。 'debug' 目标旨在构建与 'all' 相同的项目,除了一些不同的编译开关(例如 -ggdb)。由于目标使用不同的编译开
我有一个调用多个其他生成文件的生成文件。 我想将 -j 参数传递给其他 makefile 调用。 类似(make -j8): all: make -f libpng_linux.mk -j
我处理过的 Makefile 大部分都很复杂,并且隐藏了很多关系。我自己从来没有写过一个,想知道是否有人有一些关于编写易于阅读和可重用的 Makefile 的提示? 最佳答案 我通常使用这样的东西,在
嘿,我有一个简单的“主” Makefile,它只是调用其他 makefile。我正在尝试执行以下操作,以便以正确的顺序构建组件: LIB_A = folder_a LIB_B = folder_b L
生成文件: #there is a whitespace after "/my/path/to" FOO = "/my/path/to" BAR = "dir" INCLUDE_DIRS = $(FO
我正在学习 makefile,我知道如何创建一个简单的 makefile。我正在继续使用嵌套的 makefile。这是我的目录结构 /src ...makefile ...main.cpp ...fo
什么TEMP0_FILES下面计算到? SOURCE_FILES可以等于多个源文件。请告诉我以下语法 :.cpp=.o 的用途 SOURCE_FILES = main.cpp TEMP0_FILES
我是一名优秀的程序员,十分优秀!