Skip to content

Commit

Permalink
builder: c/library: support passing --whole-archive on linking
Browse files Browse the repository at this point in the history
When linking internal libs set have the "library/link-whole" attribute set
to true, linking them will cause "-Wl,--whole-archive" to be passed.

That's only desired in a few cases, eg. if the objects contain some
sections that aren't referred at link time, but loaded at runtime,
(eg. linked-in glib resource)

Signed-off-by: Enrico Weigelt, metux IT consult <[email protected]>
  • Loading branch information
metux committed Dec 10, 2023
1 parent a57921d commit b06c39d
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
8 changes: 6 additions & 2 deletions engine/builder/c/library.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"github.com/metux/go-metabuild/engine/builder/base"
"github.com/metux/go-metabuild/spec"
"github.com/metux/go-metabuild/spec/target"
"github.com/metux/go-metabuild/util"
"github.com/metux/go-metabuild/util/compiler"
"github.com/metux/go-metabuild/util/jobs"
)
Expand All @@ -18,6 +19,7 @@ func (b *BuilderCLibrary) JobPrepare(id jobs.JobId) error {
libname := b.RequiredEntryStr(target.KeyLibraryName)
pkgname := b.RequiredEntryStr(target.KeyPkgName)
pkgid := b.RequiredEntryStr(target.KeyLibraryPkgId)
archive := "-l:" + b.RequiredEntryStr(target.KeyStaticLib)

cflags = []string{"-I."}

Expand All @@ -26,8 +28,10 @@ func (b *BuilderCLibrary) JobPrepare(id jobs.JobId) error {
PkgSpec: pkgname,
SharedLdflags: []string{"-L.", "-l" + libname},
SharedCflags: cflags,
StaticLdflags: []string{"-L.", "-l:" + b.RequiredEntryStr(target.KeyStaticLib)},
StaticCflags: cflags,
StaticLdflags: util.ValIf(b.EntryBoolDef(target.KeyLibraryLinkWhole, false),
[]string{"-L.", "-Wl,--whole-archive", archive, "-Wl,--no-whole-archive"},
[]string{"-L.", archive}),
StaticCflags: cflags,
}

return b.BuildConf.SetPkgConfig(b.ForBuild(), pkgid, pi)
Expand Down
3 changes: 3 additions & 0 deletions spec/target/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ const (
KeyLibraryName = Key("library/name")
KeyStaticLib = Key("static::file")

// static / archives
KeyLibraryLinkWhole = Key("library/link-whole")

// library devlink
KeyLinkTarget = Key("target")
)
Expand Down

0 comments on commit b06c39d

Please sign in to comment.