【问题标题】:How do I set up scons to build a project that has generated source files?如何设置 scons 来构建已生成源文件的项目?
【发布时间】:2016-01-22 09:51:21
【问题描述】:

我正在开发一个 C++ 项目,该项目有一些手动编码的源文件,以及一些由命令行工具生成的源文件和头文件。 生成的实际源文件和头文件由工具读取的 JSON 文件的内容决定,因此不能硬编码到 scons 脚本中。 我想设置scons,这样如果我清理项目,然后制作它,它就会知道运行命令行工具来生成生成的源文件和头文件作为第一步,然后编译我的手工编码文件和生成的源文件并链接它们以生成我的二进制文件。 这可能吗?我不知道如何实现这一点,所以任何帮助将不胜感激。

【问题讨论】:

    标签: c++ scons generated


    【解决方案1】:

    是的,这是可能的。根据您用于创建头文件/源文件的工具,您需要在 https://bitbucket.org/scons/scons/wiki/ToolsIndex 查看我们的 ToolIndex,或阅读我们的指南 https://bitbucket.org/scons/scons/wiki/ToolsForFools 以编写您自己的 Builder。 根据您的描述,您可能必须编写自己的 Emitter,它解析 JSON 输入文件并返回最终将由调用产生的文件名。然后,您需要做的就是:

    # creates foo.h/cpp and bar.h/cpp
    env.YourBuilder('input.json') 
    
    env.Program(Glob('*.cpp'))
    

    Glob 将找到创建的文件,即使它们实际上不存在于硬盘驱动器上,并将它们添加到整体依赖项中。 如果您还有其他疑问或问题,请考虑通过 scons-users@scons.org 订阅我们的用户邮件列表(另请参阅 http://scons.org/lists.html)。

    【讨论】:

    • 生成源文件是我自己的自定义工具。我可以修改它以输出它将生成的文件列表。你是说我应该为 YourBuilder 创建一个 Emitter 来调用我的工具以返回输出文件列表,然后你的示例才能正常工作?
    【解决方案2】:

    感谢 Dirk Ba​​echle,我得到了这个工作 - 对于其他感兴趣的人,这里是我使用的代码。

    import subprocess
    
    env = Environment( MSVC_USE_SCRIPT = "c:\\Program Files (x86)\\Microsoft Visual Studio 11.0\\VC\\bin\\vcvars32.bat")
    
    def modify_targets(target, source, env):
        #Call the code generator to generate the list of file names that will be generated.
        subprocess.call(["d:/nk/temp/sconstest/codegenerator/CodeGenerator.exe", "-filelist"])
        #Read the file name list and add a target for each file.
        with open("GeneratedFileList.txt") as f:
            content = f.readlines()
            content = [x.strip('\n') for x in content]
            for newTarget in content:
                target.append(newTarget)
        return target, source
    
    bld = Builder(action = 'd:/nk/temp/sconstest/codegenerator/CodeGenerator.exe', emitter = modify_targets)
    env.Append(BUILDERS = {'GenerateCode' : bld})
    
    env.GenerateCode('input.txt')
    
    # Main.exe depends on all the CPP files in the folder. Note that this
    # will include the generated files, even though they may not currently
    # exist in the folder.
    env.Program('main.exe', Glob('*.cpp'))
    

    【讨论】:

      【解决方案3】:

      有一个例子:

      https://github.com/SCons/scons/wiki/UsingCodeGenerators

      我也会响应 Dirk 加入用户邮件列表的建议。

      【讨论】:

      • 链接不再有效。如果不点击链接,答案不包含任何有用的信息:这是“link-only”答案的直接标志。在 Stack Overflow 上,我们避免使用此类答案。
      • 更新了链接。你宁愿我在这里复制整个内容吗?该链接指向保持最新的信息(大部分)
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-06
      • 1970-01-01
      • 1970-01-01
      • 2014-05-19
      • 1970-01-01
      • 2010-12-25
      相关资源
      最近更新 更多