我們正在評估scons作爲一個構建系統,並且我的問題適應了我們的舊系統。在我們的一些源代碼子目錄中,我們有一個「sources.lib」文件,它列出了爲編譯該目錄的目標庫而需要編譯的C++文件的名稱。但是,在同一個目錄中還有其他C++文件,所以我不能只用Glob()來找到合適的文件。scons是否知道SConscript文件位於哪個目錄?
如何找出SConscript文件所在的目錄? os.getcwd()總是返回構建目錄。即使文檔聲明SConscript中的路徑與源目錄相關(否則Glob('* .cpp')將不起作用),只是執行open('sources.lib')失敗,因爲它尋找文件在構建目錄中。最後,該SConscript文件中的構建環境不包含實際的當前源目錄。
編輯從this reply它看起來像
File('sources.lib').srcnode().abspath
返回正確的文件名和目錄,但,如果它存在,它不會告訴你(必須使用os.path.isfile爲)。它也出現
Dir('.').srcnode().abspath
會告訴你在哪裏SConstruct文件駐留。
例當定義它的源文件編譯爲一個庫,我不希望使用
lib = env.SharedLibrary('mylib', Glob('*.cpp'))
而是寧願構造函數,對於「sources.lib的存在首先檢查「如果它不存在,請使用globbing。所以我定義我的圖書館,像這樣
lib = env.SharedLibrary('mylib', env.getSources('*.cpp'))
,使一個功能讀取該文件,如果它存在
def getSources(self, pattern): # list of source files to assign to a target sources = [] # srcFile = 'sources.lib' # failed # srcFile = os.path.join(os.getcwd(), 'sources.lib') # failed srcFile = File('sources.lib').srcnode().abspath # works # look for sources.lib try: infile = open(srcFile,'r') except IOError: #print "Globbing to get sources" sources = Glob(pattern, strings=True) else: #print "Reading sources.lib" for line in infile.readlines(): line = line.rstrip('\n\r') if line != '': sources.append(line) return sources buildEnv.AddMethod(getSources)
這似乎是工作。我不知道File.srcnode()。abspath直到今天。
要確定文件是否存在,你可以使用os.path.exists() – Brady 2012-03-21 14:46:47
我不明白你的問題,建立小例子說明吧。 – Torsten 2012-03-21 14:50:38
你的問題救了我:)'Dir('。')。srcnode()。abspath'爲我工作。我使用這個選項'env.SConscriptChdir(0)'來避免目錄「幻燈片」。謝謝! – Destroyica 2013-10-29 16:23:09