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

标签 windows visual-studio visual-c++ premake

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

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

最佳答案

您可以尝试从 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 不太了解我只需要修复它并想将它展示给社区)

关于windows - 如何通过 premake5 将全局属性添加到生成的 Visual Studio 项目和解决方案中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56620358/

相关文章:

c# - 内存溢出问题-OpenCV

c++ - XCode 上的 size_t

Windows 中的 Python-Can 或 Pyserial

c++ - DirectShow 和佳能 EOS 5D Mark II

visual-studio - .NET Framework 项目中的可空引用类型不能与 IntelliSense 一起使用

php - PHP 的 Visual Studio 替代方案

c++ - 将使用 VC++10 编译的代码链接到使用 VC++9 编译的代码

windows - SQLDeveloper 不会启动

c++ - Qt构建为没有dll的单个exe文件

windows - 具有重复键的 INI 文件