如何通过 premake5 将全局属性添加到生成的 Visual Studio 项目和解决方案?

How do I add global properties to generated Visual Studio projects and solutions via premake5?

我想在我的项目配置中添加两个条件属性以针对非默认 vcpkg 三元组:https://github.com/microsoft/vcpkg/blob/master/docs/users/integration.md#with-msbuild

我的项目文件是由premake生成的。我该怎么做?

您尝试过使用 CMake 吗?这是一个复杂得多的构建系统,应该可以轻松处理这个问题。

您可以尝试覆盖 visual studio 生成器中的全局函数,类似这样...未测试...

local vs2010 = premake.vstudio.vs2010


function vcPkgOverride(prj)
        -- go trough the configs and platforms and figure out which conditions to put   
        for _, cfg in pairs(prj.cfgs) do
           local condition = vs2010.condition(cfg)
           if cfg.platform == "Win32" then 
                vs2010.vc2010.element("VcpkgTriplet ", condition, "x86-windows-static")
          else if cfg.platform == "x64" then
                vs2010.vc2010.element("VcpkgTriplet ", condition, "x64-windows-static")
          end
        end
end

premake.override(vc2010.elements, "globals", function (oldfn, prj)
        local elements = oldfn(prj)

            elements = table.join(elements, {
                vcPkgOverride
            })
        end

        return elements
    end)

更新

上面的代码似乎不适用于 premake5.0.0-alpha14,所以我根据 the docs 对其进行了调整,这里有一个不太通用但可以工作的版本:

require('vstudio')

local vs = premake.vstudio.vc2010

local function premakeVersionComment(prj)
    premake.w('<!-- Generated by Premake ' .. _PREMAKE_VERSION .. ' -->')
end

local function vcpkg(prj)
    premake.w('<VcpkgTriplet>x64-windows-static</VcpkgTriplet>')
    premake.w('<VcpkgEnabled>true</VcpkgEnabled>')
end

premake.override(premake.vstudio.vc2010.elements, "project", function(base, prj)
    local calls = base(prj)
    table.insertafter(calls, vs.xmlDeclaration, premakeVersionComment)
    return calls
end)

premake.override(premake.vstudio.vc2010.elements, "globals", function(base, prj)
    local calls = base(prj)
    table.insertafter(calls, vs.globals, vcpkg)
    return calls
end)

在主 premake5.lua 脚本的开头添加它,或者找到一种从其他地方包含它的方法(我对 lua 或 premake 了解不多,我只需要修复这个并想向社区展示它)