# bird: dropping multilibs. Old hacks - START
## EMX target have share libgcc, but the names are release specific and constrained by 8.3 limits.
## The link command depends heavily on the directory structure.
#SHLIB_EXT      =
#SHLIB_BASENAME = gcc$(gccdll_version)
#SHLIB_MULTINAME= $(SHLIB_BASENAME)`echo @shlib_so_name@ | cut -b10`
#SHLIB_DLLNAME  = $(SHLIB_MULTINAME).dll
#SHLIB_LINK     = export DLLAR_CMDLINE="@shlib_objs@" && \
#	kRx.exe dllar.cmd -o $$(@D)/$(SHLIB_DLLNAME) \
#	-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 \
#    && mv -f $$(@D)/$(SHLIB_MULTINAME).a $$(@D)/$(SHLIB_BASENAME).a \
#	&& ar rs $$(@D)/$(SHLIB_BASENAME).a libgcc/$$(@D)/__main.o libgcc/$$(@D)/emx-ctordtor.o libgcc/$$(@D)/emx-eh.o libgcc/$$(@D)/emx-dllinit.o \
#    && touch $$@
#SHLIB_SUBDIR  = `echo @shlib_base_name@ | sed -e "s/^libgcc_s_//" -e "s,_,/,g"`
#SHLIB_INSTALL = $$(INSTALL_DATA) $(SHLIB_SUBDIR)/$(SHLIB_DLLNAME) $$(DESTDIR)$$(slibdir)/ \
#    && $$(INSTALL_DATA) $(SHLIB_SUBDIR)/$(SHLIB_BASENAME).a $$(DESTDIR)$$(libsubdir)/$(SHLIB_SUBDIR)/
#
## EMX Multi-threaded/Single-threaded libraries.
#MULTILIB_OPTIONS    = Zmt
#MULTILIB_DIRNAMES   = mt st
#MULTILIB_MATCHES    = Zmt=Zmts Zmt=Zmtd
#MULTILIB_EXCEPTIONS =
#MULTILIB_EXTRA_OPTS =
# bird: dropping multilibs. Old hacks - END

# EMX target have share libgcc, but the names are release specific and constrained by 8.3 limits.
SHLIB_BASENAME = gcc$(gccdll_version)
SHLIB_MULTINAME= $(SHLIB_BASENAME)
SHLIB_DLLNAME  = $(SHLIB_MULTINAME).dll
SHLIB_LINK     = export DLLAR_CMDLINE="@shlib_objs@" && \
	kRx.exe dllar.cmd -o $$(@D)/$(SHLIB_DLLNAME) \
	-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 \
	&& ar rs $$(@D)/$(SHLIB_BASENAME).a libgcc/$$(@D)/__main.o libgcc/$$(@D)/emx-ctordtor.o libgcc/$$(@D)/emx-eh.o libgcc/$$(@D)/emx-dllinit.o \
    && touch $$@
SHLIB_SUBDIR  = .
SHLIB_INSTALL = $$(INSTALL_DATA) $(SHLIB_SUBDIR)/$(SHLIB_DLLNAME) $$(DESTDIR)$$(slibdir)/ \
    && $$(INSTALL_DATA) $(SHLIB_SUBDIR)/$(SHLIB_BASENAME).a $$(DESTDIR)$$(libsubdir)/$(SHLIB_SUBDIR)/

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

# Override linker flags
# bird: the -B specification doesn't work too well here...
#LDFLAGS = -Zexe -Zcrtdll -B/emx/lib
#LDFLAGS = -Zexe -Zcrtdll -B$(PATH_EMXPGCC)/lib
LDFLAGS = -Zexe -Zcrtdll

# 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. (bird: don't forget $(srcdir) prefixing!)
LIB2FUNCS_EXTRA = $(srcdir)/config/i386/emx-ctordtor.c
LIB2ADDEH = $(srcdir)/config/i386/emx-eh.c $(srcdir)/config/i386/emx-dllinit.c \
  $(srcdir)/unwind-dw2.c $(srcdir)/unwind-dw2-fde.c $(srcdir)/unwind-sjlj.c
#LIB2ADDEHDEP


# EMX include directory
# /emx/ is not emx here.
#SYSTEM_HEADER_DIR = /emx/include
NATIVE_SYSTEM_HEADER_DIR = $(PATH_EMXPGCC)/include

# When compiling libgcc with the fresh xgcc, we still need the system headers
# bird: this doesn't work for me... What am I doing wrong?
#LIBGCC2_INCLUDES = -I$(SYSTEM_HEADER_DIR)

# Same when compiling stage 2, 3 etc
# bird: this doesn't work for me... What am I doing wrong?
#T_CFLAGS = -I$(SYSTEM_HEADER_DIR)
# bird: flags for GCC bootstrapping (stage2+)

# 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))

# bird (#424): dropping multilibs. Old hacks - START
#stage1 stage2 stage3 stage4:
#	echo "t-emx hacks: $@ starting"
#	-mv -f *.exe *.ready st mt s-libgcc $@
#	-rm -f $@/$(LIBGCC)
#	echo "t-emx hacks: $@ done"

## Some final polishing of libgcc ...
## r=bird: Moved parts of this up to the SHLIB_LINK command.
##   Use cp as mv causes recreation of libversions during make install.
##   Is there ANY chance we could juse leave them as libgcc*.a? That would've
##   been much more convenient... 8.3 isn't an argument.
#$(ALL): s-libgcc
#s-libgcc: $(LIBGCC)
#	echo "t-emx hacks: $@ starting"
#	cp -f st/libgcc.a st/gcc.a
#	cp -f st/libgcc_eh.a st/gcc_eh.a
#	cp -f mt/libgcc.a mt/gcc.a
#	cp -f mt/libgcc_eh.a mt/gcc_eh.a
#	$(STAMP) s-libgcc
#	echo "t-emx hacks: $@ done"
# bird (#424): dropping multilibs. Old hacks - END

# bird (#424): New gcc*dll hacks
stage1 stage2 stage3 stage4:
	echo "t-emx hacks: $@ starting"
	-mv -f gcc3*.dll gcc3*.a gcc3*.def $@
	echo "t-emx hacks: $@ done"

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

