2016-01-22 69 views
0

我正在研究一個C++項目,它有一些手工編碼的源文件,以及一些由命令行工具生成的源文件和頭文件。 生成的實際源文件和頭文件由工具讀取的JSON文件的內容決定,因此無法硬編碼到scons腳本中。 我想設置scons,這樣如果我清理項目,然後創建它,它會知道運行命令行工具來生成生成的源文件和頭文件作爲第一步,然後編譯我的手編碼的文件和生成的源文件並將它們鏈接起來以製作二進制文件。 這可能嗎?我不知道如何實現這一點,所以任何幫助將不勝感激。如何設置scons來構建生成源文件的項目?

回答

1

是的,這是可能的。根據您使用哪種工具創建標題/源文件,您需要查看我們的工具索引https://bitbucket.org/scons/scons/wiki/ToolsIndex,或閱讀我們的指南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會發現創建的文件,即使他們沒有物理硬盤驅動器上還存在,並將它們添加到整體的依賴關係。 如果您還有其他問題或疑問,請考慮在[email protected]上訂閱我們的用戶郵件列表(另請參閱http://scons.org/lists.html)。

+0

這是我自己的定製工具,可以生成源文件。我可以修改它以輸出將要生成的文件列表。你是說我應該爲YourBuilder創建一個Emitter,它調用我的工具來返回輸出文件的列表,然後你的例子將正常工作? –

0

感謝德克Baechle我得到了這個工作 - 對於這裏感興趣的任何人是我使用的代碼。

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')) 
相關問題