ifndef OBJS
OBJS=$(CFILES:.c=.o) $(SFILES:.S=.o) $(XSFILES:.s=.o) $(SSFILES:.SS=.o)
endif

GENOBJS = $(GENSRC:.c=.o)

ifndef DEPENDS
DEPENDS=$(OBJS:.o=.d)
endif

GENERATED = $(top_builddir)/version.hh \
    $(top_builddir)/config.hh \
    $(top_builddir)/src/plugin/plugin_config.hh

ifneq ($(GENHDRS),)
ALL_CPPFLAGS += -iquote .
endif

LIB:=$(BINPATH)/lib/lib$(LIBNAME).a

AR=ar

lib: $(LIB)

$(BINPATH) $(BINPATH)/bin $(BINPATH)/commands $(BINPATH)/lib:
	mkdir -p $@

$(BINPATH)/bat: | $(BINPATH)
	$(LN_S) ../src/bindist/bat $@

$(top_builddir)/commands: | $(BINPATH)/commands $(BINPATH)/bat
	rm -rf $@
	mkdir $@
	mkdir $@/dosemu
	$(LN_S) -f ../src/bindist/fdppconf.sys $@/fdppconf.sys
	$(LN_S) -f ../src/bindist/fdppauto.bat $@/fdppauto.bat
	for i in `ls $(BINPATH)/bat/`; do $(LN_S) ../../$(THISVERSION)/bat/$$i $@/dosemu/$$i; done

$(top_builddir)/bin: | $(BINPATH)/bin
	rm -f $@
	$(LN_S) -f $(THISVERSION)/bin $@

$(top_builddir)/lib: | $(BINPATH)/lib
	rm -f $@
	$(LN_S) -f $(THISVERSION)/lib $@

GIT_REV := $(shell $(REALTOPDIR)/git-rev.sh $(REALTOPDIR) $(top_builddir))
.LOW_RESOLUTION_TIME: $(GIT_REV)
$(top_builddir)/version.hh: $(top_builddir)/Makefile.conf $(REALTOPDIR)/VERSION $(GIT_REV)
	echo "Updating version.h"; \
	echo "#ifndef	VERSION_HH" > $@; \
	echo "#define	VERSION_HH" >> $@; \
	echo "#define	VERSTR	\"$(PACKAGE_VERSION)\"" >> $@; \
	echo "#define	VERSION_NUM	$(VERSION)" >> $@; \
	echo "#define	SUBLEVEL	$(SUBLEVEL)" >> $@; \
	echo "#define	PATCHLEVEL1	\"$(PATCHLEVEL1)\"" >> $@; \
	echo "#define	REVISION	$(REVISION)" >> $@; \
	echo "#endif /* VERSION_H */" >> $@

$(LIB): $(OBJS) $(GENOBJS) | $(BINPATH)/lib
	rm -f $@
	$(AR) cr $@ $^
	$(RANLIB) $@

.PHONY:	clean realclean lib echo all install default

# Use wildcards instead of OBJS and DEPENDS because they may depend on
# a make target. The reason is that on clean we do not include Makefile.conf.
clean::
	-rm -f *.o $(LIB) $(GENSRC) *.d $(GENHDRS) configure *~

realclean:: clean
	rm -rf autom4te*.cache
	rm -f Makefile.conf config.log config.status configure aclocal.m4
	rm -rf plugin/include

$(GENOBJS): $(GENSRC)
ifneq ($(wildcard Makefile.conf.in),)
$(OBJS): Makefile.conf
endif

%.o: %.c
	$(CC) -c $(ALL_CPPFLAGS) $(ALL_CFLAGS) -o $@ $<

%.o: %.s
	$(CPP) $(ALL_CPPFLAGS) -x assembler-with-cpp $< | $(XAS) $(XASFLAGS) -o $@

# eg bios_S_START
VS = $(subst .,_,$(notdir $(1)))_START

%.o: %.S
	$(CPP) $(ALL_CPPFLAGS) -x assembler-with-cpp $< | $(XAS) $(XASFLAGS) -o _$@
	$(AS_LD) $(AS_LDFLAGS) -static $(if $($(call VS,$<)), \
		--section-start .text=$($(call VS,$<))) -o $@.elf _$@
	$(XOBJCOPY) -j .text -O binary $@.elf $@.bin
	nm -g -n -p -t x _$@ | gawk '\
		BEGIN {\
		  print "// Warning: autogenerated";\
	          print "";\
	          print "asm(";\
	          print "\t\".data\\n\"";\
	          print "\t\".globl _binary_$(@:.o=)_o_bin_start\\n\"";\
	          print "#if defined(__ELF__) || defined(__EMSCRIPTEN__)";\
	          print "\t\".size _binary_$(@:.o=)_o_bin_start,0\\n\"";\
	          print "#endif";\
	          print "\t\"_binary_$(@:.o=)_o_bin_start:\\n\"";\
	          print "\t\".incbin \\\"$@.bin\\\"\\n\"";\
	          print "\t\".globl _binary_$(@:.o=)_o_bin_end\\n\"";\
	          print "#if defined(__ELF__) || defined(__EMSCRIPTEN__)";\
	          print "\t\".size _binary_$(@:.o=)_o_bin_end,0\\n\"";\
	          print "#endif";\
	          print "\t\"_binary_$(@:.o=)_o_bin_end:\\n\"";\
	          print "#ifdef __ELF__";\
	          print "\t\".previous\\n\"";\
	          print "#endif";\
	          print ");";\
	          print "#include \"$(srcdir)/$(@:.o=)_offsets.h\"";\
		}\
		{\
		  if ($$1 == "") next;\
		  if (match($$1, /:$$/)) next;\
		  if (match($$1, /^0x/)) \
		    HEXSTR = $$1;\
		  else \
		    HEXSTR = "0x" $$1;\
		  ADDR = strtonum(HEXSTR) $(if $($(call VS,$<)),+ $($(call VS,$<)));\
		  if ($$3 != "_start") {\
		    printf "const unsigned %s = 0x%04x;\n", $$3, ADDR;\
		  }\
		}\
		' > $@_offsets.c
	$(CC) -c -o $@ $@_offsets.c

%.o: %.SS
	$(CPP) $(ALL_CPPFLAGS) -x assembler-with-cpp $< | $(AS) $(ASFLAGS) -o $@

ifndef CLEANING
ifneq "$(wildcard *.d)" ""
-include $(DEPENDS)
endif
endif

$(top_builddir)/Makefile.conf: $(REALTOPDIR)/configure
	cd $(top_builddir) && $(REALTOPDIR)/configure
Makefile.conf: configure
	./configure
configure: configure.ac
	autoreconf -v -I $(REALTOPDIR)/m4
.SECONDARY: configure
$(top_builddir)/src/plugin/plugin_config.hh:
	cd $(top_builddir) && $(REALTOPDIR)/scripts/mkpluginhooks

# this is mainly for debugging the makefile
echo::
	@echo REALTOPDIR=$(REALTOPDIR)
	@echo TOPDIR=$(TOPDIR)
	@echo LIB=$(LIB)
	@echo DEPENDS=$(DEPENDS)
	@echo OBJS=$(OBJS)
	@echo CFILES = $(wildcard *.c)
	@echo DEPENDS FOUND= $(wildcard *.d)
	@echo MAKEFLAGS=$(MAKEFLAGS)
