premake5 将生成的文件添加到vstudio项目中

premake5 add generated files to vstudio project

我已经覆盖了 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

覆盖 bakefile 更干净整洁。它不像 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 生成的文件的方法。请参阅 premake.extensions.qt.customBakeFiles https://github.com/dcourtois/premake-qt/blob/master/qt.lua

基本上在 bakeFiles 覆盖中,您可以只浏览您的项目,并轻松地将文件插入列表中。然后,如果这些添加的文件需要一些自定义配置,您可以覆盖 premake.fileconfig.addconfig。请参阅上述插件中的 premake.extensions.qt.customAddFileConfig

在此 addconfig 覆盖中,您将有权访问文件并能够修改它们的配置对象:您可以添加自定义构建规则、特殊选项等。

这不是您具体问题的直接答案,但我希望它能帮助您实现您的需求。