Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 31 additions & 1 deletion mk/libraries.mk
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ endif
# target andwill not be listed in the make help output. This is
# useful for libraries built solely for testing, for example.
#
# - $(1)_WHOLE_ARCHIVE: if defined, the whole archive of the static
# library will be used. This is necessary when using global
# constructors such as setting up a RegisterStoreImplementation.
# Only applies when BUILD_SHARED_LIBS=0.
#
# - BUILD_SHARED_LIBS: if equal to ‘1’, a dynamic library will be
# built, otherwise a static library.
define build-library
Expand Down Expand Up @@ -127,7 +132,32 @@ define build-library
$$($(1)_PATH): $$($(1)_OBJS) | $$(_d)/
$(trace-ar) $(AR) crs $$@ $$?

$(1)_LDFLAGS_USE += $$($(1)_PATH) $$($(1)_LDFLAGS)
# Special care is needed to support C++ global initializers, used
# in RegisterStoreImplementation. The .init section needs to be in
# the linked executable for it find the registered stores. On
# Linux systems systems, wrap the linked object archive with whole
# archive flags. Note that --whole-archive is only supported with
# the bfd, gold, and lld linkers. The macOS ld64 linker has a
# -force_load that seems to work similarly, but takes one argument
# instead of a group of arguments.

ifdef $(1)_WHOLE_ARCHIVE
ifeq ($(OS), Linux)
$(1)_LDFLAGS_USE += -Wl,--whole-archive
else ifeq ($(OS), Darwin)
$(1)_LDFLAGS_USE += -Wl,-force_load
endif
endif

$(1)_LDFLAGS_USE += $$($(1)_PATH)

ifdef $(1)_WHOLE_ARCHIVE
ifeq ($(OS), Linux)
$(1)_LDFLAGS_USE += -Wl,--no-whole-archive
endif
endif

$(1)_LDFLAGS_USE += $$($(1)_LDFLAGS)

$(1)_INSTALL_PATH := $$(libdir)/$$($(1)_NAME).a

Expand Down
2 changes: 2 additions & 0 deletions src/libexpr/local.mk
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ ifneq ($(OS), FreeBSD)
libexpr_LDFLAGS += -ldl
endif

libexpr_WHOLE_ARCHIVE = 1

# The dependency on libgc must be propagated (i.e. meaning that
# programs/libraries that use libexpr must explicitly pass -lgc),
# because inline functions in libexpr's header files call libgc.
Expand Down
3 changes: 3 additions & 0 deletions src/libfetchers/local.mk
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,6 @@ libfetchers_SOURCES := $(wildcard $(d)/*.cc)
libfetchers_CXXFLAGS += -I src/libutil -I src/libstore

libfetchers_LIBS = libutil libstore libnixrust

libfetchers_WHOLE_ARCHIVE = 1

2 changes: 2 additions & 0 deletions src/libstore/local.mk
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ ifneq ($(OS), FreeBSD)
libstore_LDFLAGS += -ldl
endif

libstore_WHOLE_ARCHIVE = 1

ifeq ($(OS), Darwin)
libstore_FILES = sandbox-defaults.sb sandbox-minimal.sb sandbox-network.sb
endif
Expand Down