OBJS+=$(CSRCS:%.c=$(OBJDIR)$/%$(OBJEXT))
OBJS:=$(OBJS:%.cc=$(OBJDIR)$/%$(OBJEXT))

RESOBJS:=$(RESOURCES:%.rc=$(OBJDIR)$/%.res)

ifdef SUBDIRS
load_makefile $(SUBDIRS:%=%\makefile MAKESERVER=$(MAKESERVER) DEBUG=$(DEBUG);)
all: $(SUBDIRS:%=%\all)
endif


### Static library stuff ###
ifdef LIBRARY
LIBRARY_DIR := $(LIBRARY:%=$(OBJDIR)\%.lib)
PDB := $(LIBRARY_DIR:%.lib=%.pdb)

all: $(LIBRARY_DIR)

$(LIBRARY_DIR) : $(OBJS)
	$(AR) /OUT:$(relpath $@) $(OBJS)

endif # End static library stuff

### WINAPP/TTYAPP stuff ###
ifeq (1,$(call OR, $(call NE,$(WINAPP)_,_) $(call NE,$(TTYAPP)_,_)))

ifdef WINAPP

load_makefile $(MHMAKECONF)\libwinmain\makefile MAKESERVER=$(MAKESERVER) DEBUG=$(DEBUG)
EXTRALIB := $(MHMAKECONF)\libwinmain\$(OBJDIR)\libwinmain.lib

EXE := $(WINAPP:%=$(OBJDIR)\%.exe)
LINKFLAGS += /SUBSYSTEM:WINDOWS
else
EXE := $(TTYAPP:%=$(OBJDIR)\%.exe)
LINKFLAGS += /SUBSYSTEM:CONSOLE
endif

PDB := $(EXE:%.exe=%.pdb)

MANIFESTFILE:=$(OBJDIR)\runtime.manifest

all: $(EXE)

$(MANIFESTFILE):
	$(MHMAKECONF)/tools/genruntimemanifest $@ $(DEBUG)

$(EXE) :  $(OBJS) $(INCLUDELIBFILES) $(RESOBJS) $(MANIFESTFILE) $(EXTRALIB)
	$(LINK) $(LINKFLAGS) /MANIFEST:NO /OUT:$(relpath $@) $(INCLUDELIBFILES) $(SYSTEMLIBS) $(LINKLIBS) $(EXTRALIB) $(OBJS) $(RESOBJS)
	mt -nologo -manifest $(MANIFESTFILE) -outputresource:$(relpath $@);\#1

endif  # End WINAPP or TTYAPP stuff

ifeq ($(DEBUG),1)
COMMONCFLAGS += $(DEFINES:%=-D%) $(INCLUDES:%=-I%) -Fo$(relpath $@) -Fd"$(PDB)" $<
else
COMMONCFLAGS += $(DEFINES:%=-D%) $(INCLUDES:%=-I%) -Fo$(relpath $@) -Fd"$(PDB)" $<
#PDB=   # There is no PDB file generated in a release build
endif

.PHONY: all cleanall clean clean$(OBJDIRPREFIX)

### Implicit rules ###
CREATEDIR=if not exist $@ mkdir $@

$(OBJDIR) :
	$(CREATEDIR)

ifneq ($(OBJDIRPREFIX),)
clean: clean$(OBJDIRPREFIX)
endif

clean$(OBJDIRPREFIX):
	del -e $(OBJDIR)

$(OBJDIR)\%$(OBJEXT) : %.c
	$(CC) $(CCFLAGS) $(COMMONCFLAGS)

$(OBJDIR)\%$(OBJEXT) : %.cc
	$(CC) $(CCFLAGS) /EHsc $(COMMONCFLAGS)

$(OBJDIR)\%.res : %.rc
	$(RC) $(RCFLAGS) $(RCDEFINES:%=-d "%") $(RCINCLUDES:%=-i %) -Fo$(relpath $@) $<

ifdef INC_BDF_RULES
load_makefile $(MHMAKECONF)\bdftopcf\makefile MAKESERVER=0 DEBUG=$(DEBUG)
load_makefile $(MHMAKECONF)\mkfontscale\makefile MAKESERVER=0 DEBUG=$(DEBUG)

$(DESTDIR)\%.pcf.gz: %.bdf
	@del -e $@
	$(BDFTOPCF) -t $< | gzip > $@

$(DESTDIR)\%.enc.gz: %.enc
	gzip -c < $< > $@
endif

ifdef x11thislocaledir

$(x11thislocaledir)\%: %.pre
	cl /nologo /EP $< -DXCOMM\#\# > $@

$(x11thislocaledir):
	$(CREATEDIR)

all: $(x11thislocaledir)
endif

ifdef DESTDIR
$(DESTDIR):
	$(CREATEDIR)
endif