# Build a shared libgcc library able to use embedded runpaths.

SHLIB_SOVERSION = 1.1
SHLIB_SO_MINVERSION = 1
SHLIB_VERSTRING = -compatibility_version $(SHLIB_SO_MINVERSION) \
                 -current_version $(SHLIB_SOVERSION)
SHLIB_EXT = .dylib
SHLIB_LC = -lSystem

SHLIB_MKMAP = $(srcdir)/mkmap-flat.awk
SHLIB_MKMAP_OPTS = -v leading_underscore=1

# Shorthand expressions for the LINK below, these are substituted in the
# link expression.
SHLIB_INSTALL_NAME = @shlib_base_name@.$(SHLIB_SOVERSION)$(SHLIB_EXT)
SHLIB_MAP = @shlib_map_file@
SHLIB_DIR = @multilib_dir@
SHLIB_SONAME = @shlib_base_name@$(SHLIB_EXT)

# Darwin only searches in shlib_slibdir for shared libraries, not in
# subdirectories.  The link builds one architecture slice in its designated
# subdir.  The code under MULTIBUILDTOP combines these into a single FAT
# library, that is what we eventually install.

# When enable_darwin_at_rpath is true, use @rpath instead of $(slibdir) for
# this and dylibs that depend on this.  So this def must come first and be
# overridden in a make fragment that depends on the rpath setting.
SHLIB_RPATH = $(slibdir)

SHLIB_LINK = $(CC) $(LIBGCC2_CFLAGS) $(LDFLAGS) -dynamiclib -nodefaultlibs \
       -install_name $(SHLIB_RPATH)/$(SHLIB_INSTALL_NAME) \
       -single_module -o $(SHLIB_DIR)/$(SHLIB_SONAME) \
       -Wl,-exported_symbols_list,$(SHLIB_MAP) \
       $(SHLIB_VERSTRING) -nodefaultrpaths \
       @multilib_flags@ @shlib_objs@ $(SHLIB_LC)

# we do our own thing
SHLIB_INSTALL =

LGCC_FILES = libgcc_s.$(SHLIB_SOVERSION)$(SHLIB_EXT)

ifeq ($(BUILD_LIBGCCS1),YES)

# We are going to build a libgcc_s.1.dylib so that distributions can
# install it for compatibility with binaries linked against the old
# libgcc_ext.10.x.dylib stubs.

# For systems after macOS 10.7 we can forward the correct unwinder symbols
# from libSystem.

# For older systems we have to forward an entire library (since they cannot
# selectively forward symbols, which means that we cannot forward the unwinder
# in /usr/lib/libgcc_s.1.dylib).  We also cannot forward the entire library
# since that creates a self-referencing loop when DYLD_LIBRARY_PATH is used.
# To provide the unwinder symbols in this case, we build the unwinder into a
# separate shared lib (libgcc_ehs) and forward that.

# These targets are local to this make fragment, which means that they do not
# get the substitutions seen in SHLIB_LINK.

ifneq ($(LIBEHSOBJS),)

EHS_INSTNAME = libgcc_ehs.$(SHLIB_SOVERSION)$(SHLIB_EXT)

# multilib build for a shared EH lib.

libgcc_ehs$(SHLIB_EXT): $(LIBEHSOBJS) $(extra-parts)
       mkdir -p $(MULTIDIR)
       $(CC) $(LIBGCC2_CFLAGS) $(LDFLAGS) -dynamiclib -nodefaultlibs \
       -install_name $(SHLIB_RPATH)/$(EHS_INSTNAME) \
       -o $(MULTIDIR)/libgcc_ehs$(SHLIB_EXT) $(SHLIB_VERSTRING) \
       -nodefaultrpaths $(LIBEHSOBJS) $(SHLIB_LC)

all: libgcc_ehs$(SHLIB_EXT)

LGCC_FILES += libgcc_ehs.$(SHLIB_SOVERSION)$(SHLIB_EXT)

endif
endif

INSTALL_FILES=$(LGCC_FILES)

ifeq ($(BUILD_LIBGCCS1),YES)
# Provide libgcc_s.1 for backwards compatibility.
INSTALL_FILES += libgcc_s.1.dylib
endif

# For the toplevel multilib, build FAT dylibs including all the multilibs.
ifeq ($(MULTIBUILDTOP),)

ifeq ($(enable_shared),yes)
all: $(INSTALL_FILES)
install-leaf: install-darwin-libgcc-stubs
endif

libgcc_s.$(SHLIB_SOVERSION)$(SHLIB_EXT): all-multi libgcc_s$(SHLIB_EXT)
       MLIBS=`$(CC) --print-multi-lib | sed -e 's/;.*$$//'` ; \
       for mlib in $$MLIBS ; do \
         cp ../$${mlib}/libgcc/$${mlib}/libgcc_s$(SHLIB_EXT)  \
           ./libgcc_s.$(SHLIB_SOVERSION)$(SHLIB_EXT)_T_$${mlib} || exit 1 ; \
       done
       $(LIPO) -output libgcc_s.$(SHLIB_SOVERSION)$(SHLIB_EXT) \
         -create libgcc_s.$(SHLIB_SOVERSION)$(SHLIB_EXT)_T*
       rm libgcc_s.$(SHLIB_SOVERSION)$(SHLIB_EXT)_T*

ifeq ($(BUILD_LIBGCCS1),YES)
ifneq ($(LIBEHSOBJS),)

libgcc_ehs.$(SHLIB_SOVERSION)$(SHLIB_EXT): all-multi libgcc_ehs$(SHLIB_EXT)
       MLIBS=`$(CC) --print-multi-lib | sed -e 's/;.*$$//'` ; \
       for mlib in $$MLIBS ; do \
         cp ../$${mlib}/libgcc/$${mlib}/libgcc_ehs$(SHLIB_EXT)  \
           ./libgcc_ehs.$(SHLIB_SOVERSION)$(SHLIB_EXT)_T_$${mlib} || exit 1 ; \
       done
       $(LIPO) -output libgcc_ehs.$(SHLIB_SOVERSION)$(SHLIB_EXT) \
         -create libgcc_ehs.$(SHLIB_SOVERSION)$(SHLIB_EXT)_T*
       rm libgcc_ehs.$(SHLIB_SOVERSION)$(SHLIB_EXT)_T*


libgcc_s.1.dylib: all-multi libgcc_s.$(SHLIB_SOVERSION)$(SHLIB_EXT) \
                 libgcc_ehs.$(SHLIB_SOVERSION)$(SHLIB_EXT)
       MLIBS=`$(CC) --print-multi-lib | sed -e 's/;.*$$//'` ; \
       for mlib in $$MLIBS ; do \
         cp ../$${mlib}/libgcc/$${mlib}/libgcc_s$(SHLIB_EXT)  \
           ./libgcc_s.$(SHLIB_SOVERSION)$(SHLIB_EXT)_T_$${mlib} || exit 1 ; \
         cp ../$${mlib}/libgcc/$${mlib}/libgcc_ehs$(SHLIB_EXT)  \
           ./libgcc_ehs.$(SHLIB_SOVERSION)$(SHLIB_EXT)_T_$${mlib} || exit 1 ; \
         arch=`$(LIPO) -info libgcc_s.$(SHLIB_SOVERSION)$(SHLIB_EXT)_T_$${mlib} | sed -e 's/.*:\ //'` ; \
         $(CC) -arch $${arch} -nodefaultlibs -dynamiclib -nodefaultrpaths \
           -o libgcc_s.1$(SHLIB_EXT)_T_$${mlib} \
           -Wl,-reexport_library,libgcc_s.$(SHLIB_SOVERSION)$(SHLIB_EXT)_T_$${mlib} \
           -Wl,-reexport_library,libgcc_ehs.$(SHLIB_SOVERSION)$(SHLIB_EXT)_T_$${mlib} \
           -install_name $(SHLIB_RPATH)/libgcc_s.1.dylib \
           -compatibility_version 1 -current_version 1.1 ; \
       done
       $(LIPO) -output libgcc_s.1$(SHLIB_EXT) -create libgcc_s.1$(SHLIB_EXT)_T*
       rm libgcc_s.$(SHLIB_SOVERSION)$(SHLIB_EXT)_T*
       rm libgcc_ehs.$(SHLIB_SOVERSION)$(SHLIB_EXT)_T*

else

libgcc_s.1.dylib: all-multi libgcc_s.$(SHLIB_SOVERSION)$(SHLIB_EXT)
       MLIBS=`$(CC) --print-multi-lib | sed -e 's/;.*$$//'` ; \
       for mlib in $$MLIBS ; do \
         cp ../$${mlib}/libgcc/$${mlib}/libgcc_s$(SHLIB_EXT)  \
           ./libgcc_s.$(SHLIB_SOVERSION)$(SHLIB_EXT)_T_$${mlib} || exit 1 ; \
         arch=`$(LIPO) -info libgcc_s.$(SHLIB_SOVERSION)$(SHLIB_EXT)_T_$${mlib} | sed -e 's/.*:\ //'` ; \
         $(CC) -arch $${arch} -nodefaultlibs -dynamiclib -nodefaultrpaths \
           -o libgcc_s.1$(SHLIB_EXT)_T_$${mlib} \
           -Wl,-reexport_library,libgcc_s.$(SHLIB_SOVERSION)$(SHLIB_EXT)_T_$${mlib} \
           -lSystem \
           -Wl,-reexported_symbols_list,$(srcdir)/config/darwin-unwind.ver \
           -install_name $(SHLIB_RPATH)/libgcc_s.1.dylib \
           -compatibility_version 1 -current_version 1.1 ; \
       done
       $(LIPO) -output libgcc_s.1$(SHLIB_EXT) -create libgcc_s.1$(SHLIB_EXT)_T*
       rm libgcc_s.$(SHLIB_SOVERSION)$(SHLIB_EXT)_T*

endif
endif

# Install the shared libraries.

install-darwin-libgcc-stubs :
       $(mkinstalldirs) $(DESTDIR)$(slibdir)
       if test x$(slibdir) = x; then \
         for d in $(LGCC_FILES) ; do \
           $(INSTALL_DATA) $$d $(DESTDIR)$(slibdir)/$$d || exit 1 ; \
         done; \
       else \
         for d in $(INSTALL_FILES) ; do \
           $(INSTALL_DATA) $$d $(DESTDIR)$(slibdir)/$$d || exit 1 ; \
         done; \
       fi

else

# Do not install shared libraries for multilibs.  Unless we are putting them
# in the gcc directory during a build, for compatibility with the pre-top-
# level layout.  In that case we provide symlinks to the FAT lib from the
# multilib sub-directories.

ifeq ($(enable_shared),yes)
all: install-darwin-libgcc-links
endif

install-darwin-libgcc-links:
       $(mkinstalldirs) $(gcc_objdir)$(MULTISUBDIR)
       for file in $(INSTALL_FILES); do                        \
         rm -f $(gcc_objdir)$(MULTISUBDIR)/$$file;             \
         $(LN_S) ../$$file $(gcc_objdir)$(MULTISUBDIR)/;       \
       done

endif