# some commands and settings
SLASH		:=	\\
RM		:=	del /Q
CP		:=	copy

# the project
PROJECT		:=	qwe

MODS		:=	gui file net

SRC_gui		:=	gui.c window.c

SRC_file	:=	file.c stat.c

SRC_net		:=	net.c socks.c


# in case the user did not define it
ifndef PROJECT
PROJECT		:=	stupid
endif

CC		:=	gcc
LD		:=	gcc


# some names of the subdirs of this project
D_SRC		:=	src
D_DEP		:=	dep
D_OBJ		:=	obj
D_PERL		:=	perl


# some names for the files of this project
F_ELF		:=	$(PROJECT).elf


# some extensions to the file names
E_O			:=	o
E_D			:=	d
E_C			:=	c


all: $(F_ELF)

$(F_ELF): $(OBJ) $(DEP)
	$(LD) -o $@ $(OBJ)

# don't do any automatic rules
.SUFFIXES:

# the targets
.PHONY: dep all clean mrproper


# to let "make" find all the sources
VPATH =	$(patsubst %,$(D_SRC)/%/SRC,$(MODS))

SRC	=	$(foreach mod,$(MODS),$(foreach src,$(SRC_$(mod)),$(mod)/SRC/$(src)))

DEP =	$(patsubst %.c,$(D_DEP)/%.$(E_D), $(foreach src,$(SRC),$(notdir $(src))))
OBJ =	$(patsubst %.c,$(D_OBJ)/%.$(E_O), $(foreach src,$(SRC),$(notdir $(src))))

MODPATH = $(patsubst %,$(D_SRC)/%/SRC,$(MODS))
IMODPATH = $(patsubst %,-i %,$(MODPATH))

dep: $(DEP)

#obj: $(OBJ)

# the rule to create the dependencies
$(DEP): $(D_DEP)/%.$(E_D) : %.$(E_C)
	perl perl/autodep.pl	\
		-s $< \
		-o $@ \
		$(IMODPATH) \
		-objdir $(D_OBJ) \
		-depdir $(D_DEP) \
		-eo $(E_O) \
		-ed $(E_D)

# include the dependencies.  This has to be written AFTER the rule to
# generate them
include $(DEP)

$(OBJ): $(D_OBJ)/%.$(E_O) : %.$(E_C)
	$(CC) -o $@ -c $<

clean:
	-$(RM) $(subst /,$(SLASH),$(DEP))
	-$(RM) $(subst /,$(SLASH),$(OBJ))

show:
	@echo "SRC"
	@echo $(SRC)
	@ECHO "DEP"
	@echo $(DEP)
	@echo "VPATH"
	@echo $(VPATH)
	@echo "MODPATH"
	@echo $(MODPATH)
	@echo "OS"
	@echo $(OS)
	@echo "HOST"
	@echo $(HOST)
	@echo "OBJ"
	@echo $(OBJ)

