Premake5添加生成的文件到vstudio项目



我已经覆盖了生成一些cpp文件的vs2012动作的onProject函数,然后尝试将它们包含在项目

--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。没有生成过滤器文件。当我运行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

重写bakefiles更加干净整洁。它不像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)

我不知道你的代码有什么问题(有点太多的阅读,没有足够的时间: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覆盖中,你将有权访问文件,你将能够修改它们的配置对象:你可以添加自定义构建规则,特殊选项等。

这不是对你具体问题的直接回答,但我希望它能帮助你实现你所需要的。

相关内容

最新更新