使用SCON动态重新创建包含文件

2024-10-01 04:44:02 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要修复一个由SCON动态生成包含文件的SCons项目。我创建了一个简单的例子来说明这个问题。{}看起来像这样:

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;
}

当时间改变时,SCON不会重建项目,因为它不是魔术。解决这个问题的一种方法是将AlwaysBuild(current_time)添加到SCons文件中

在实际项目中,使用AlwaysBuild重建include文件是相当昂贵的,而且它每天只需要重建一次,因为没有时间,只有日期会改变。那么,如何才能实现每天只重新生成一次文件

解决方案:我创建了一个函数,返回生成的包含文件的内容:

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")

Tags: 文件项目testnoneincludetimecontentscurrent
1条回答
网友
1楼 · 发布于 2024-10-01 04:44:02

像这样的方法应该会奏效:

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") 

您不应该将当前_time.h作为源。SCON将扫描test.cpp并查找包含的文件

相关问题 更多 >