python - 使用 SCons 动态重新创建包含文件
问题描述
我需要修复一个 SCons 项目,其中包含文件由 SCons 动态生成。我创建了一个简单的例子来说明这个问题。SConstruct
看起来像这样:
current_time = Command("current_time.h",
None,
"echo '#define CURRENT_TIME' `date +%H%M` > $TARGET")
test = Program("test", "test.cpp", current_time)
# AlwaysBuild(current_time)
与test.cpp
:
include <iostream>
#include "current_time.h"
int main() {
std::cout << "the time is: " << CURRENT_TIME << std::endl;
}
当时间改变时,SCons 不会重建项目,因为它不是魔法。解决它的一种方法是添加AlwaysBuild(current_time)
到 SCons 文件中。
在实际项目中,重建包含文件AlwaysBuild
是相当昂贵的,而且它只需要每天重建一次,因为没有时间而是日期会改变。那么,我怎样才能实现文件每天只重新生成一次呢?
解决方案:我创建了一个返回生成的包含文件内容的函数:
def include_file_contents():
...
return file_contents # str
然后我在依赖项中替换None
为:Value(include_file_contents())
current_time = Command("current_time.h",
Value(include_file_contents()),
"echo '#define CURRENT_TIME' `date +%H%M` > $TARGET")
解决方案
像这样的东西应该工作:
import time
now=time.strftime("%H:%M",time.localtime())
current_time = Command("current_time.h",
Value(now),
"echo '#define CURRENT_TIME' %s > $TARGET"%now)
test = Program("test", "test.cpp")
您不必将 current_time.h 作为来源。SCons 将扫描 test.cpp 并找到包含的文件。
推荐阅读
- twig - Timber twig 函数输出在不同的服务器上是不同的
- puppeteer - puppeteer 是否有任何 Windows 容器指南?
- java - 如何使用存储在 String 中的方法创建 java.lang.reflect.Method
- bash - 无法从 WSL bash 中的批处理文件运行 Homebrew 应用程序(找不到命令)
- javascript - 登录系统与谷歌电子表格中的数据库连接
- pine-script - 这些 pine 脚本行的 AFL 版本将是什么
- javascript - PM2 - 部署后需要重新启动
- android - 如何以编程方式使用 android 中的媒体记录器设置屏幕录制的自定义分辨率?
- timestamp - bigtable 反向时间戳相对于常规时间戳的优势
- node.js - express router matching wrong routes with nested router.use