premake-core icon indicating copy to clipboard operation
premake-core copied to clipboard

Links with `:static` suffix do not generate correct target dependencies

Open piotrdz opened this issue 4 years ago • 2 comments

What seems to be the problem? Given this sample project where links is used with :static suffix to enable putting the static library in static section of link libraries (-Wl,-Bstatic):

workspace "MyWorkspace"
    configurations { "Debug" }
    toolset "gcc"

    project "MyLib"
        kind "StaticLib"
        files { "lib.cpp" }

    project "MyApp"
        kind "ConsoleApp"
        libdirs { "bin/Debug" }
        links { "MyLib:static" }
        files { "app.cpp" }

When premake is called to generate gmake Makefiles, and make is invoked to build MyApp:

$ premake5 gmake
$ make MyApp

Then it fails to build with the compiler not finding the MyLib:

==== Building MyApp (debug) ====
Creating obj/Debug/MyApp
app.cpp
Creating bin/Debug
Linking MyApp
/usr/bin/ld: cannot find -lMyLib
collect2: error: ld returned 1 exit status
make[1]: *** [MyApp.make:54: bin/Debug/MyApp] Error 1
make: *** [Makefile:31: MyApp] Error 2

This is because MyApp in generated Makefiles does not include a dependency on MyLib target. Therefore, MyLib is never built and the linking phase of MyApp fails.

What did you expect to happen? Generated Makefiles should contain the correct dependency of MyApp on MyLib and make MyApp should build first MyLib, then MyApp successfully.

What have you tried so far? The only workaround is to manually build MyLib first, then MyApp.

How can we reproduce this? Run the example given above.

What version of Premake are you using? premake5 (Premake Build Script Generator) 5.0.0-alpha16

Anything else we should know? When not using the :static suffix, the dependencies are correct and the build works as expected:

However, that is not a valid workaround for the project I am working on. In this particular case, putting the library in -Wl,-Bstatic section is quite important for building the project correctly.

piotrdz avatar Aug 06 '21 11:08 piotrdz

Could you attach the makefile that premake generates?

nickclark2016 avatar Aug 10 '21 14:08 nickclark2016

Here is a zip archive of the Makefiles. makefiles.zip

piotrdz avatar Aug 15 '21 05:08 piotrdz

The lib project lookup doesn't take into account :static or :shared in the lib name.

https://gist.github.com/kevza/ec6a8c6f58019f367b50cc45f666d746

kevza avatar Sep 16 '22 01:09 kevza

@piotrdz Can you elaborate on why linking the sibling project normally is not an option? Having the linking decorators being handled properly for sibling projects is non-trivial but ignoring them is pretty simple - see the patch @kevza has linked.

samsinsane avatar Oct 03 '22 16:10 samsinsane

@samsinsane, @piotrdz and I work together on the same project.

Our build is really, really complicated and there are a bunch of targets that get assembled with different linking requirements (Plugins v/s Applications etc..) so we have to use the :shared :static annotations depending on what the target is.

We have added the patch I attached to our build of premake and everything is working as expected.

kevza avatar Oct 03 '22 20:10 kevza

@kevza Can you submit that as a PR? Otherwise, I'll create a PR after work.

samsinsane avatar Oct 04 '22 02:10 samsinsane

@samsinsane thank you for creating that PR. Apologies for not getting to it sooner.

kevza avatar Oct 05 '22 20:10 kevza