我有一个简单的构建系统,用于我使用SCons整理的项目。该项目有很多源文件,我认为随着构建的进展,显示某种进度信息对用户友好。 SCons提供了CXXCOMSTR
之类的构造变量,我可以覆盖它来控制在每个构建步骤中显示给终端的内容。例如,而不是看到像:
gcc file.c -o file.o
如果有这样的话会很好:
[1/10] Compiling file.c: `gcc file.c -o file.o`
[1/10]
指定这是此构建期间正在更新的十个目标中的第一个。有没有办法访问这些信息,以便我可以生成这样的消息?看起来我需要知道正在更新的目标总数(这是基于SCons所做的依赖性扫描)和一些枚举每个目标的方法。我知道类似的行为类似于其他构建系统,如CMake和waf,但在SCons(扩展)文档中没有遇到过任何内容。
答案 0 :(得分:5)
可以使用Progress() SCons功能完成此操作。
SCons man page中有更多示例,只搜索“进度”。
您可以提供要调用的函数,将定期显示的字符串,或者将以旋转方式显示的字符串列表。您可以访问正在构建的当前目标,但我认为不可能知道完成的百分比。
答案 1 :(得分:4)
我能够提出一个相当不错的解决方法,允许您以百分比显示构建进度。基本上下面的代码是如何工作的:
[??%]
.scons_progress
[50%]
)。 [??%]
并更新.scons_progress
interval
设置为5,以便它每5个节点仅更新一次缓存以下是代码:
custom_utils.py
下的site_scons/
:
PROGRESS = {
"env": None,
"nodes": 0,
"interval": 1,
"total": 0,
"cache": {},
"cache_key": "_",
"cache_name": ".scons_progress",
"cache_update": False
}
def get_progress_total():
return PROGRESS["cache"].get(PROGRESS["cache_key"], 0)
def update_progress_cache():
PROGRESS["cache"][PROGRESS["cache_key"]] = PROGRESS["nodes"]
with open(PROGRESS["cache_name"], "w") as f:
f.write(json.dumps(PROGRESS["cache"]))
def load_progress_cache():
try:
with open(PROGRESS["cache_name"]) as f:
PROGRESS["cache"] = json.load(f)
cache_key = "_".join(COMMAND_LINE_TARGETS)
if cache_key:
PROGRESS["cache_key"] = cache_key
except IOError as e:
pass
def progress_function(node):
PROGRESS["nodes"] += PROGRESS["interval"]
#Only update cached environment if it is not None
if node.env:
PROGRESS["env"] = node.env
PROGRESS["env"].Replace(PROGRESS = "[??%]")
#Update display message
if PROGRESS["nodes"] <= PROGRESS["total"]:
PROGRESS["env"].Replace(PROGRESS = "[%d%%]" % (100*PROGRESS["nodes"]/PROGRESS["total"]))
#If current node is more than total, we need to update the cache
elif not PROGRESS["cache_update"]:
PROGRESS["cache_update"] = True
PROGRESS["env"].Replace(PROGRESS = "[??%]")
#If cache flag is on, update cache file
else:
update_progress_cache()
def progress_settings(env, interval):
load_progress_cache()
PROGRESS["interval"] = interval
PROGRESS["env"] = env
PROGRESS["total"] = get_progress_total()
Progress(progress_function, interval=PROGRESS["interval"])
在SConstruct
:
if GetOption("progress"):
custom_utils.progress_settings(env, 5)
示例输出:
user@linuxbox:~/project1$ scons -j4 --progress
scons: Reading SConscript files ...
scons: done reading SConscript files.
scons: Building targets ...
[??%] Compiling (shared): src/ExpressRuntimeException.os
[??%] Compiling (shared): src/Application.os
[??%] Compiling (shared): src/Callback.os
[??%] Compiling (shared): src/Response.os
[??%] Compiling (shared): src/Router.os
[??%] Compiling (shared): src/Route.os
[??%] Compiling (shared): src/Regex.os
[??%] Compiling (shared): src/tools.os
[??%] Linking (shared): libexpresscpp.so
scons: done building targets.
user@linuxbox:~/project1$ scons -c
scons: Reading SConscript files ...
scons: done reading SConscript files.
scons: Cleaning targets ...
Removed src/ExpressRuntimeException.os
Removed src/Application.os
Removed src/Callback.os
Removed src/Response.os
Removed src/Router.os
Removed src/Route.os
Removed src/Regex.os
Removed src/tools.os
Removed libexpresscpp.so
scons: done cleaning targets.
user@linuxbox:~/project1$ scons -j4 --progress
scons: Reading SConscript files ...
scons: done reading SConscript files.
scons: Building targets ...
[14%] Compiling (shared): src/ExpressRuntimeException.os
[51%] Compiling (shared): src/Application.os
[59%] Compiling (shared): src/Callback.os
[66%] Compiling (shared): src/Response.os
[74%] Compiling (shared): src/Router.os
[81%] Compiling (shared): src/Route.os
[88%] Compiling (shared): src/Regex.os
[96%] Compiling (shared): src/tools.os
[100%] Linking (shared): libexpresscpp.so
scons: done building targets.