我已经覆盖了vs2012操作的onProject函数,该函数生成了一些cpp文件,然后尝试将它们包含在项目中
--cant override the generateProject directly
--so have to override at the action level
premake.override( premake.action._list.vs2012, 'onProject', function(base, prj)
if premake.project.iscpp(prj) then
--generate files
--print( "Generating extra files ...")
local extraFiles = mine.getExtraFiles(prj)
for _,file in ipairs( extraFiles ) do
p.generate( file, nil, mine.generateExtraFile )
mine.addFileToSources(file)
end
end
--Generate regular stuff
base(prj)
end)
function mine.getExtraFiles(prj)
local extraFiles = {}
--works out what files to generate and add relevant info to table
return extraFiles
end
--this function is passed as a callback to premake.generate
function mine.generateExtraFile(extraFile)
--write contents of file
end
这是尝试将每个生成的文件添加到项目中的函数
function mine.addFileToSources(extraFile)
local prj = extraFile.prj
local cfg = extraFile.cfg
local groups = premake.vstudio.vc2010.categorizeSources(prj)
local compiledFiles = groups.ClCompile or {}
--create a new file config for generated file
local filename = path.join(extraFile.location, extraFile.filename)
local fcfg = premake.fileconfig.new( filename, prj)
premake.fileconfig.addconfig(fcfg, cfg)
--add the config to the project's sources
table.insert(compiledFiles, fcfg)
compiledFiles[filename] = fcfg
--add to the projects source tree
--this bit is copied from premake.project.getsourcetree
-- The tree represents the logical source code tree to be displayed
-- in the IDE, not the physical organization of the file system. So
-- virtual paths are used when adding nodes.
-- If the project script specifies a virtual path for a file, disable
-- the logic that could trim out empty root nodes from that path. If
-- the script writer wants an empty root node they should get it.
local flags
if fcfg.vpath ~= fcfg.relpath then
flags = { trim = false }
end
-- Virtual paths can overlap, potentially putting files with the same
-- name in the same folder, even though they have different paths on
-- the underlying filesystem. The tree.add() call won't overwrite
-- existing nodes, so provide the extra logic here. Start by getting
-- the parent folder node, creating it if necessary.
local tr = premake.project.getsourcetree(prj)
local parent = premake.tree.add(tr, path.getdirectory(fcfg.vpath), flags)
local node = premake.tree.insert(parent, premake.tree.new(path.getname(fcfg.vpath)))
-- Pass through value fetches to the file configuration
setmetatable(node, { __index = fcfg })
end
在大多数情况下 - 这一切都有效: 正确生成文件并更正位置 这些文件也正确包含在vcxproj文件中
我的问题是没有生成vcxproj.filters文件。 当我运行premake时,我收到此错误:
Generating myproject.vcxproj.filters...Error: [string "src/actions/vstudio/vs2010_vcxproj_filters...."]:82: attempt to index field 'parent' (a nil value)
对应于函数premake.vstudio.vc2010.filterGroup(prj,groups,group) 我知道我创建的新fcfg需要有一个父母,但我无法确定我应该添加到哪里或者什么。
有人可以帮忙吗?
编辑1
我已经通过将这一行添加到函数mine.addFileToSources(extraFile)的末尾来完成工作
fcfg.parent = parent
这给文件配置一个父节点,所以一切正常,但我觉得有点脏,所以我会看看遵循Citron的建议
编辑2
覆盖烤饼更清洁,更整洁。它不像Citron的代码那么简单,因为我需要来自烘焙文件的信息才能执行我的文件生成,但我现在确信我的代码是正确的,并且可能与vstudio之外的其他出口商一起工作。
这是我的新代码:
premake.override( premake.oven, 'bakeFiles', function(base, prj)
--bake the files as normal
local bakedFiles = base(prj)
if premake.project.iscpp(prj) then
--gather information about what files to generate and how
local extraFiles = mine.getExtraFiles(prj, bakedFiles)
for _,file in ipairs( extraFiles ) do
--do the generation
premake.generate( file, file.extension, mine.generateExtraFile )
--add the new files
local filename = premake.filename(file, file.extension)
table.insert(file.cfg.files, filename)
-- This should be the first time I've seen this file, start a new
-- file configuration for it. Track both by key for quick lookups
-- and indexed for ordered iteration.
assert( bakedFiles[filename] == nil )
local fcfg = premake.fileconfig.new(filename, file.prj)
bakedFiles[filename] = fcfg
table.insert(bakedFiles, fcfg)
premake.fileconfig.addconfig( bakedFiles[filename], file.cfg)
end
--sort the baked files again - since we have added to them
table.sort(bakedFiles, function(a,b)
return a.vpath < b.vpath
end)
end
return bakedFiles
end)
答案 0 :(得分:1)
我不知道您的代码有什么问题(读取时间太多,时间不够:p)但如果您只是想将一些生成的文件添加到项目树中,我会建议您改为覆盖premake.oven.bakeFiles
。
这是我用来添加Qt在我的插件中生成的文件。请参阅https://github.com/dcourtois/premake-qt/blob/master/qt.lua
上的premake.extensions.qt.customBakeFiles
基本上在bakeFiles
覆盖中,您可以浏览项目,并轻松地在列表中插入文件。然后,如果这些添加的文件需要一些自定义配置,则可以覆盖premake.fileconfig.addconfig
。请参阅上述插件中的premake.extensions.qt.customAddFileConfig
。
在此addconfig
覆盖中,您将可以访问这些文件,并且您将能够修改其配置对象:您可以添加自定义构建规则,特殊选项等。
这不是您具体问题的直接答案,但我希望它能帮助您实现所需。