Search code examples
visual-studio-2013premake

Premake directive to embed a static library into a static library


First, I'm using premake5 and VisualStudio 2013. Please don't jump on me for that one (since premake5 is still alpha). I have already searched and can't find an answer, not one that actually works.

I need to embed a static library into a custom static library using premake. I'm converting some older code after updating the libraries and only want to link the custom library with the application. I don't want to have to link the custom library and all the other libraries it deals with to the application configuration. Everything else with premake5 is working swimmingly.

I can get it to link to the application fine using the link directive, example: links { "SDL2", "SDL2main", "SDL2_image" }

This works fine for normal inclusion of static libraries but I need a way to embed say these example libraries into a custom static library.

I can take the resulting project files that premake creates into visual studio and manually modify the project to add the libraries the way I need to, but I don't want to have to do that every time I need to regenerate the project.

The result I need is a way for premake to generate the following XML into the project xml files:

    <ItemGroup>
    <Library Include="..\deps\SDL2\lib\x86\SDL2.lib">
      <ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug Win64|x64'">true</ExcludedFromBuild>
    </Library>
    <Library Include="..\deps\SDL2\lib\x86\SDL2main.lib">
        <ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug Win64|x64'">true</ExcludedFromBuild>
    </Library>
    <Library Include="..\deps\SDL2_image\lib\x86\SDL2_image.lib" />
        <ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug Win64|x64'">true</ExcludedFromBuild>
    </Library>
  </ItemGroup>

This is the result of manually editing the project in visual studio. I added the exclusion of the libraries from an x64 build because ultimately, I have to use x64 versions of these libraries as well. It would be great if I have seriously overlooked something but I need to be able to get this to work.

Did I overlook something really simple here?


Solution

  • Premake doesn't support linking static libraries together out of the box, as that behavior isn't portable to other toolsets. However, you can install a small extension in your project script to make it work:

    ---
    -- Allow static library projects to link in their dependencies. Visual Studio
    -- doesn't support this out of the box, so I need to manually add items to
    -- the list of additional dependencies.
    ---
    
    local p = premake
    
    function myAdditionalStaticDependencies(cfg)
        local links = p.config.getlinks(cfg, "siblings", "fullpath")
        if #links > 0 then
            links = path.translate(table.concat(links, ";"))
            p.x('<AdditionalDependencies>%s;%%(AdditionalDependencies)</AdditionalDependencies>', links)
        end
    end
    
    p.override(p.vstudio.vc2010.elements, "lib", function(base, cfg, explicit)
        local calls = base(cfg, explicit)
        if cfg.kind == p.STATICLIB then
            table.insert(calls, myAdditionalStaticDependencies)
        end
        return calls
    end)