# Override targets
LANGUAGES = c gcov$(exeext) c++ f77 objc

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

# Override linker flags
LDFLAGS = -Zexe -Zcrtdll

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

# Provide an 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 _gcc_bcmp

# Eliminate unneeded functions from libgcc2, along with those
# implemented in the alternative $(LIB1ASMSRC)
NO_LIB2FUNCS = __gcc_bcmp _trampoline _ctors $(LIB1ASMFUNCS)

# Extra functions to add to libgcc
LIB2FUNCS_EXTRA = $(srcdir)/config/i386/emx-ctordtor.c

# EMX include directory
SYSTEM_HEADER_DIR = /emx/include

# 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/iso646.h \
    $(srcdir)/ginclude/proto.h $(EXTRA_HEADERS) \
    $(LANG_EXTRA_HEADERS)

# Build name appendix for shared libgcc
libgcc_ver=`echo $(version)|sed -e "s|.*-||" -e "s|\.||g"`

# Pass libgcc_ver to tm-emx.h included from xgcc
TARGET_DRIVER_DEFINES=-DLIBGCC_VER=\"$(libgcc_ver)\"

# Additional dependencies to build gcc.a and gcc_p.a
# plus dynamicaly-linked version of libgcc*
$(ALL): gcc.a
gcc.a: $(LIBGCC)
	mv $(LIBGCC) $@ && \
	dllar.cmd -ordinal -nocrtdll -ex "__main __do_global_* _GLOBAL* \
	   _exit_dummy_ref __ctordtor*" -o gcc$(libgcc_ver) $@ \
	   -d "GNU C runtime shared library version $(version)" \
	   -libf "INITINSTANCE TERMGLOBAL" -lc_static -lc_import && \
	ar x $@ __main.o emx-ctordtor.o && \
	ar r gcc$(libgcc_ver).a __main.o emx-ctordtor.o && \
	rm -f __main.o emx-ctordtor.o libgcc*.a && \
	echo "*** Building profiling version of libgcc ***" && \
	$(MAKE) $(LIBGCC) SHELL="$(SHELL)" CFLAGS="$(CFLAGS) -fno-omit-frame-pointer -pg" && \
	cp $(LIBGCC) gcc_p.a && \
	touch $@ -r $(LIBGCC)

stage1 stage2 stage3 stage4:
	mv -f *.exe $@
	mv -f *.a $@
	mv -f *.dll $@
