# Build a shared libgcc library.
SHLIB_EXT =
SHLIB_LINK = touch $$@ && export DLLAR_CMDLINE="@shlib_objs@" && \
	cmd.exe /c dllar.cmd -o `echo $$@ | sed -e "s,\(.\)\(.*\)libgcc_s_.*,\1\2gcc$(gccdll_version)\1.dll,"` \
	-ordinal @multilib_flags@ -nocrtdll \
	-ex "__main __do_global_* _GLOBAL* _exit_dummy_ref __ctordtor* __eh* _DLL_InitTerm" \
	-d "GNU C runtime shared library version $(gcc_version)" \
	-libf "INITINSTANCE TERMGLOBAL" -lc_static -lc_import
SHLIB_INSTALL =

# Allow multi-threaded/single-threaded libraries
MULTILIB_OPTIONS    = Zmt
MULTILIB_DIRNAMES   = mt st
MULTILIB_MATCHES    = Zmt=Zmts Zmt=Zmtd
MULTILIB_EXCEPTIONS =
MULTILIB_EXTRA_OPTS =

# Dont use collect2
USE_COLLECT2 =
# Don't run fixproto
STMP_FIXPROTO =
# Don't fix includes
STMP_FIXINC =

# Override linker flags
LDFLAGS = -Zexe -Zcrtdll -B/emx/lib

# Add the 's' flag to $AR so that we don't need ranlib
AR_FLAGS = rcs

# We don`t want debug information in libgcc
LIBGCC2_DEBUG_CFLAGS = -s
# We also want epilogues for each function in libgcc
TARGET_LIBGCC2_CFLAGS = -O2 -fomit-frame-pointer -Wall -mprobe -mepilogue -DNDEBUG

# Provide alternative source code for libgcc1
LIBGCC1 = libgcc1-asm.a
CROSS_LIBGCC1 = libgcc1-asm.a
LIB1ASMSRC = i386/emx-libgcc1.asm
LIB1ASMFUNCS = _alloca _udivdi3 _umoddi3 _divdi3 _moddi3

# Extra functions to add to libgcc
LIB2FUNCS_EXTRA = config/i386/emx-ctordtor.c
LIB2ADDEH = config/i386/emx-eh.c config/i386/emx-dllinit.c \
  unwind-dw2.c unwind-dw2-fde.c unwind-sjlj.c
#LIB2ADDEHDEP

# EMX include directory
SYSTEM_HEADER_DIR = /emx/include
# When compiling libgcc with the fresh xgcc, we still need the system headers
LIBGCC2_INCLUDES = -I$(SYSTEM_HEADER_DIR)
# Same when compiling stage 2, 3 etc
T_CFLAGS = -I$(SYSTEM_HEADER_DIR)

# Copy these files to include/ subdirectory
# $(srcdir)/ginclude/stddef.h does not contain emx`s definition of _threadid
USER_H = $(srcdir)/ginclude/stdarg.h $(srcdir)/ginclude/varargs.h \
    $(srcdir)/ginclude/stdbool.h $(srcdir)/ginclude/iso646.h \
    $(EXTRA_HEADERS)

# Additional dependencies to build gcc.a and gcc_p.a
# plus dynamicaly-linked version of libgcc*
gccdll_version = $(subst .,,$(gcc_version))

stage1 stage2 stage3 stage4:
	-mv -f *.exe *.ready st mt s-libgcc $@
	-rm -f $@/$(LIBGCC)

# Some final polishing of libgcc ...
$(ALL): s-libgcc
s-libgcc: $(LIBGCC)
	mv -f st/libgcc.a st/gcc.a
	mv -f st/libgcc_eh.a st/gcc_eh.a
	if [ -f st/gcc$(gccdll_version)s.a ]; then \
	  mv -f st/gcc$(gccdll_version)s.a st/gcc$(gccdll_version).a; \
	fi
	mv -f mt/libgcc.a mt/gcc.a
	mv -f mt/libgcc_eh.a mt/gcc_eh.a
	if [ -f mt/gcc$(gccdll_version)m.a ]; then \
	  mv -f mt/gcc$(gccdll_version)m.a mt/gcc$(gccdll_version).a; \
	fi
	ar x st/gcc.a __main.o emx-ctordtor.o
	ar x st/gcc_eh.a emx-eh.o emx-dllinit.o
	ar rs st/gcc$(gccdll_version).a __main.o emx-ctordtor.o emx-eh.o emx-dllinit.o
	ar rs mt/gcc$(gccdll_version).a __main.o emx-ctordtor.o emx-eh.o emx-dllinit.o
	rm -f __main.o emx-ctordtor.o emx-eh.o emx-dllinit.o
	$(STAMP) s-libgcc

emx.o: $(srcdir)/config/i386/emx.c $(RTL_H) $(TREE_H) $(CONFIG_H)
	$(CC) -c $(ALL_CFLAGS) $(ALL_CPPFLAGS) $(INCLUDES) $(srcdir)/config/i386/emx.c
