include ../Makefile.conf

MODULES=Main.o ProcessFile.o
SUBDIRS=kmap mys uni hwd
CPPFLAGS:=$(CPPFLAGS) -DHAVE_STRTOL

ifeq ($(SPLATFORM),WINDOWS)
OBJS=$(subst .o,.obj,$(MODULES))
SLIBS=/libpath:../stoolkit stoolkit.lib
SWINLIBS=/libpath:../swindow 
MYLIBS=/link /subsystem:CONSOLE $(SWINLIBS) $(SLIBS) $(ALL_LIBS)
else
OBJS=$(MODULES)
SLIBS=-L../stoolkit -lstoolkit
SWINLIBS=-L../swindow -lswindow 
MYLIBS=$(SWINLIBS) $(SLIBS) $(ALL_LIBS)
endif

SUBDIRS_all:=$(foreach dir, $(SUBDIRS), $(dir)_all)
SUBDIRS_clean:=$(foreach dir, $(SUBDIRS), $(dir)_clean)

.PHONY:all clean config  $(SUBDIRS_all) $(SUBDIRS_clean)

all:  mytool $(SUBDIRS_all)

mytool: $(OBJS)
	$(LD) -o mytool $(OBJS) $(MYLIBS) 

%.o:%.cpp
	$(CXX) -c $(CPPFLAGS) $(subst .o,.cpp,$@)

%.obj:%.cpp
	$(CXX) -c $(CPPFLAGS) $(subst .obj,.cpp,$@)

$(SUBDIRS_all): mytool
	$(MAKE) -C $(patsubst %_all,%,$@) all

clean: $(SUBDIRS_clean)
	rm -f *.o *.obj mytool mytool.exe

depend:
	$(CXX) -M  $(CPPFLAGS)  $(patsubst %.o,%.cpp,$(OBJS)) > .depend

install:
	rm -f $(DESTDIR)/$(bindir)/mytool
	@for i in $(SUBDIRS); do \
		cd $$i; $(MAKE) install; cd ..; \
	done
	$(INSTALL_PROGRAM) mytool $(DESTDIR)/$(bindir)
	$(INSTALL_DATA) mytool.1 $(DESTDIR)/$(mandir)/man1

$(SUBDIRS_clean):
	$(MAKE) -C $(patsubst %_clean,%,$@) clean

ifeq (.depend, $(wildcard .depend))
include .depend
endif
