Search code examples
windowsvisual-studiovisual-c++premake

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


I would like to add two conditional properties to my project configuration to target a non-default vcpkg triplet: https://github.com/microsoft/vcpkg/blob/master/docs/users/integration.md#with-msbuild

My project files are generated by premake. How would I go about this?


Solution

  • you could try to override the global function from the visual studio generator some thing like this ... not tested ...

    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)
    

    UPDATE

    The code above doesn't seem to work for premake5.0.0-alpha14 so I tweaked it based on the docs and here you have a less general, but working version:

    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)
    

    Add this at the start of your main premake5.lua script, or find a way to include it from somewhere else (I don't know much about lua or premake I just needed to fix this and wanted to show it to the community)